Slash Command and Context Menu
- Doc. comming soon
This commit is contained in:
@@ -4,6 +4,7 @@ const Base = require('./Base');
|
||||
const ApplicationCommandPermissionsManager = require('../managers/ApplicationCommandPermissionsManager');
|
||||
const { ApplicationCommandOptionTypes, ApplicationCommandTypes, ChannelTypes } = require('../util/Constants');
|
||||
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
||||
const { Message } = require('..');
|
||||
|
||||
/**
|
||||
* Represents an application command.
|
||||
@@ -42,7 +43,7 @@ class ApplicationCommand extends Base {
|
||||
* The manager for permissions of this command on its guild or arbitrary guilds when the command is global
|
||||
* @type {ApplicationCommandPermissionsManager}
|
||||
*/
|
||||
this.permissions = new ApplicationCommandPermissionsManager(this);
|
||||
this.permissions = new ApplicationCommandPermissionsManager(this, user);
|
||||
|
||||
/**
|
||||
* The type of this application command
|
||||
@@ -50,6 +51,8 @@ class ApplicationCommand extends Base {
|
||||
*/
|
||||
this.type = ApplicationCommandTypes[data.type];
|
||||
|
||||
this.user = client.users.cache.get(this.applicationId);
|
||||
|
||||
this._patch(data);
|
||||
}
|
||||
|
||||
@@ -392,6 +395,87 @@ class ApplicationCommand extends Base {
|
||||
[maxValueKey]: option.maxValue ?? option.max_value,
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Send Slash command to channel
|
||||
* @param {Message} message Discord Message
|
||||
* @param {Array<string>} options The options to Slash Command
|
||||
* @returns {Promise<Boolean>}
|
||||
* @example
|
||||
* const botID = '12345678987654321'
|
||||
* const user = await client.users.fetch(botID);
|
||||
* const application = await user.applications.fetch();
|
||||
* const command = application.commands.first();
|
||||
* await command.sendSlashCommand(messsage, ['option1', 'option2']);
|
||||
*/
|
||||
async sendSlashCommand(message, options = []) {
|
||||
// Check Options
|
||||
if (!message instanceof Message) throw new TypeError('The message must be a Discord.Message');
|
||||
if (!Array.isArray(options)) throw new TypeError('The options must be an array of strings');
|
||||
if (this.type !== 'CHAT_INPUT') return false;
|
||||
const optionFormat = [];
|
||||
let i = 0;
|
||||
for (i; i < options.length ; i++) {
|
||||
const value = options[i];
|
||||
if (typeof value !== 'string') {
|
||||
throw new TypeError(`Expected option to be a String, got ${typeof value}`);
|
||||
}
|
||||
if (!this.options[i]) continue;
|
||||
const data = {
|
||||
type: ApplicationCommandOptionTypes[this.options[i].type],
|
||||
name: this.options[i].name,
|
||||
value: value,
|
||||
}
|
||||
optionFormat.push(data);
|
||||
}
|
||||
if (this.options[i]?.required) throw new Error('Value required missing');
|
||||
await this.client.api.interactions.post({ body: {
|
||||
type: 2, // ???
|
||||
application_id: this.applicationId,
|
||||
guild_id: message.guildId,
|
||||
channel_id: message.channelId,
|
||||
session_id: this.client.session_id,
|
||||
data: {
|
||||
// ApplicationCommandData
|
||||
version: this.version,
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
type: ApplicationCommandTypes[this.type],
|
||||
options: optionFormat,
|
||||
},
|
||||
}})
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Message Context Menu
|
||||
* @param {Message} message Discord Message
|
||||
* @returns {Promise<Boolean>}
|
||||
* @example
|
||||
* const botID = '12345678987654321'
|
||||
* const user = await client.users.fetch(botID);
|
||||
* const application = await user.applications.fetch();
|
||||
* const command = application.commands.first();
|
||||
* await command.sendContextMenu(messsage);
|
||||
*/
|
||||
async sendContextMenu(message) {
|
||||
if (!message instanceof Message) throw new TypeError('The message must be a Discord.Message');
|
||||
if (this.type == 'CHAT_INPUT') return false;
|
||||
await this.client.api.interactions.post({ body: {
|
||||
type: 2, // ???
|
||||
application_id: this.applicationId,
|
||||
guild_id: message.guildId,
|
||||
channel_id: message.channelId,
|
||||
session_id: this.client.session_id,
|
||||
data: {
|
||||
// ApplicationCommandData
|
||||
version: this.version,
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
type: ApplicationCommandTypes[this.type],
|
||||
target_id: ApplicationCommandTypes[this.type] == 1 ? message.author.id : message.id,
|
||||
},
|
||||
}})
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ApplicationCommand;
|
||||
|
@@ -18,7 +18,7 @@ class ClientApplication extends Application {
|
||||
* The application command manager for this application
|
||||
* @type {ApplicationCommandManager}
|
||||
*/
|
||||
this.commands = new ApplicationCommandManager(this.client);
|
||||
this.commands = null // Selfbot
|
||||
}
|
||||
|
||||
_patch(data) {
|
||||
@@ -97,7 +97,7 @@ class ClientApplication extends Application {
|
||||
* @returns {Promise<ClientApplication>}
|
||||
*/
|
||||
async fetch() {
|
||||
if(!this.client.bot) throw new Error("INVALID_USER_METHOD");
|
||||
if(!this.client.user.bot) throw new Error("INVALID_USER_METHOD");
|
||||
const app = await this.client.api.oauth2.applications('@me').get();
|
||||
this._patch(app);
|
||||
return this;
|
||||
|
@@ -57,12 +57,6 @@ class Guild extends AnonymousGuild {
|
||||
constructor(client, data) {
|
||||
super(client, data, false);
|
||||
|
||||
/**
|
||||
* A manager of the application commands belonging to this guild
|
||||
* @type {GuildApplicationCommandManager}
|
||||
*/
|
||||
this.commands = new GuildApplicationCommandManager(this);
|
||||
|
||||
/**
|
||||
* A manager of the members belonging to this guild
|
||||
* @type {GuildMemberManager}
|
||||
|
@@ -6,6 +6,7 @@ const { Error } = require('../errors');
|
||||
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
||||
const UserFlags = require('../util/UserFlags');
|
||||
const { default: Collection } = require('@discordjs/collection');
|
||||
const ApplicationCommandManager = require('../managers/ApplicationCommandManager');
|
||||
|
||||
/**
|
||||
* Represents a user on Discord.
|
||||
@@ -37,7 +38,7 @@ class User extends Base {
|
||||
this.premiumSince = null;
|
||||
this.premiumGuildSince = null;
|
||||
this.mutualGuilds = new Collection();
|
||||
|
||||
this.applications = null;
|
||||
this._patch(data);
|
||||
}
|
||||
|
||||
@@ -58,6 +59,9 @@ class User extends Base {
|
||||
* @type {?boolean}
|
||||
*/
|
||||
this.bot = Boolean(data.bot);
|
||||
if (this.bot == true) {
|
||||
this.applications = new ApplicationCommandManager(this.client, undefined, this);
|
||||
}
|
||||
} else if (!this.partial && typeof this.bot !== 'boolean') {
|
||||
this.bot = false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user