Skip to content

Commit

Permalink
feat: add UserContextMenuInteraction and `MessageContextMenuInterac…
Browse files Browse the repository at this point in the history
…tion` (discordjs#7003)

Co-authored-by: Rodry <38259440+ImRodry@users.noreply.github.com>
Co-authored-by: Suneet Tipirneni <77477100+suneettipirneni@users.noreply.github.com>
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
Co-authored-by: GrapeColor <grapecolor@users.noreply.github.com>
  • Loading branch information
5 people authored Dec 1, 2021
1 parent a39d8c4 commit 4fe063f
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/client/actions/InteractionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const Action = require('./Action');
const AutocompleteInteraction = require('../../structures/AutocompleteInteraction');
const ButtonInteraction = require('../../structures/ButtonInteraction');
const CommandInteraction = require('../../structures/CommandInteraction');
const ContextMenuInteraction = require('../../structures/ContextMenuInteraction');
const MessageContextMenuInteraction = require('../../structures/MessageContextMenuInteraction');
const SelectMenuInteraction = require('../../structures/SelectMenuInteraction');
const UserContextMenuInteraction = require('../../structures/UserContextMenuInteraction');
const { Events, InteractionTypes, MessageComponentTypes, ApplicationCommandTypes } = require('../../util/Constants');

let deprecationEmitted = false;
Expand All @@ -25,8 +26,10 @@ class InteractionCreateAction extends Action {
InteractionType = CommandInteraction;
break;
case ApplicationCommandTypes.USER:
InteractionType = UserContextMenuInteraction;
break;
case ApplicationCommandTypes.MESSAGE:
InteractionType = ContextMenuInteraction;
InteractionType = MessageContextMenuInteraction;
break;
default:
client.emit(
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ exports.MessageAttachment = require('./structures/MessageAttachment');
exports.MessageButton = require('./structures/MessageButton');
exports.MessageCollector = require('./structures/MessageCollector');
exports.MessageComponentInteraction = require('./structures/MessageComponentInteraction');
exports.MessageContextMenuInteraction = require('./structures/MessageContextMenuInteraction');
exports.MessageEmbed = require('./structures/MessageEmbed');
exports.MessageMentions = require('./structures/MessageMentions');
exports.MessagePayload = require('./structures/MessagePayload');
Expand Down Expand Up @@ -140,6 +141,7 @@ exports.ThreadChannel = require('./structures/ThreadChannel');
exports.ThreadMember = require('./structures/ThreadMember');
exports.Typing = require('./structures/Typing');
exports.User = require('./structures/User');
exports.UserContextMenuInteraction = require('./structures/UserContextMenuInteraction');
exports.VoiceChannel = require('./structures/VoiceChannel');
exports.VoiceRegion = require('./structures/VoiceRegion');
exports.VoiceState = require('./structures/VoiceState');
Expand Down
18 changes: 17 additions & 1 deletion src/structures/Interaction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Base = require('./Base');
const { InteractionTypes, MessageComponentTypes } = require('../util/Constants');
const { InteractionTypes, MessageComponentTypes, ApplicationCommandTypes } = require('../util/Constants');
const Permissions = require('../util/Permissions');
const SnowflakeUtil = require('../util/SnowflakeUtil');

Expand Down Expand Up @@ -160,6 +160,22 @@ class Interaction extends Base {
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND && typeof this.targetId !== 'undefined';
}

/**
* Indicates whether this interaction is a {@link UserContextMenuInteraction}
* @returns {boolean}
*/
isUserContextMenu() {
return this.isContextMenu() && ApplicationCommandTypes[this.targetType] === ApplicationCommandTypes.USER;
}

/**
* Indicates whether this interaction is a {@link MessageContextMenuInteraction}
* @returns {boolean}
*/
isMessageContextMenu() {
return this.isContextMenu() && ApplicationCommandTypes[this.targetType] === ApplicationCommandTypes.MESSAGE;
}

/**
* Indicates whether this interaction is an {@link AutocompleteInteraction}
* @returns {boolean}
Expand Down
20 changes: 20 additions & 0 deletions src/structures/MessageContextMenuInteraction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const ContextMenuInteraction = require('./ContextMenuInteraction');

/**
* Represents a message context menu interaction.
* @extends {ContextMenuInteraction}
*/
class MessageContextMenuInteraction extends ContextMenuInteraction {
/**
* The message this interaction was sent from
* @type {Message|APIMessage}
* @readonly
*/
get targetMessage() {
return this.options.getMessage('message');
}
}

module.exports = MessageContextMenuInteraction;
29 changes: 29 additions & 0 deletions src/structures/UserContextMenuInteraction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const ContextMenuInteraction = require('./ContextMenuInteraction');

/**
* Represents a user context menu interaction.
* @extends {ContextMenuInteraction}
*/
class UserContextMenuInteraction extends ContextMenuInteraction {
/**
* The user this interaction was sent from
* @type {User}
* @readonly
*/
get targetUser() {
return this.options.getUser('user');
}

/**
* The member this interaction was sent from
* @type {?(GuildMember|APIGuildMember)}
* @readonly
*/
get targetMember() {
return this.options.getMember('user');
}
}

module.exports = UserContextMenuInteraction;
17 changes: 17 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,8 @@ export class Interaction<Cached extends CacheType = CacheType> extends Base {
public isCommand(): this is CommandInteraction<Cached>;
public isAutocomplete(): this is AutocompleteInteraction<Cached>;
public isContextMenu(): this is ContextMenuInteraction<Cached>;
public isUserContextMenu(): this is UserContextMenuInteraction<Cached>;
public isMessageContextMenu(): this is MessageContextMenuInteraction<Cached>;
public isMessageComponent(): this is MessageComponentInteraction<Cached>;
public isSelectMenu(): this is SelectMenuInteraction<Cached>;
}
Expand Down Expand Up @@ -1536,6 +1538,13 @@ export class MessageComponentInteraction<Cached extends CacheType = CacheType> e
public static resolveType(type: MessageComponentTypeResolvable): MessageComponentType;
}

export class MessageContextMenuInteraction<Cached extends CacheType = CacheType> extends ContextMenuInteraction<Cached> {
public readonly targetMessage: CacheTypeReducer<Cached, Message, APIMessage>;
public inGuild(): this is MessageContextMenuInteraction<'present'>;
public inCachedGuild(): this is MessageContextMenuInteraction<'cached'>;
public inRawGuild(): this is MessageContextMenuInteraction<'raw'>;
}

export class MessageEmbed {
private _fieldEquals(field: EmbedField, other: EmbedField): boolean;

Expand Down Expand Up @@ -2188,6 +2197,14 @@ export class User extends PartialTextBasedChannel(Base) {
public toString(): UserMention;
}

export class UserContextMenuInteraction<Cached extends CacheType = CacheType> extends ContextMenuInteraction<Cached> {
public readonly targetUser: User;
public readonly targetMember: CacheTypeReducer<Cached, GuildMember, APIInteractionGuildMember>;
public inGuild(): this is UserContextMenuInteraction<'present'>;
public inCachedGuild(): this is UserContextMenuInteraction<'cached'>;
public inRawGuild(): this is UserContextMenuInteraction<'raw'>;
}

export class UserFlags extends BitField<UserFlagsString> {
public static FLAGS: Record<UserFlagsString, number>;
public static resolve(bit?: BitFieldResolvable<UserFlagsString, number>): number;
Expand Down

0 comments on commit 4fe063f

Please sign in to comment.