various stuff

This commit is contained in:
Gus Caplan 2020-12-11 17:14:00 -06:00
parent 2b8a6294af
commit 53412f6b9f
No known key found for this signature in database
GPG key ID: F00BD11880E82F0E
4 changed files with 44 additions and 35 deletions

View file

@ -108,18 +108,7 @@ class Client extends BaseClient {
* The interaction client. * The interaction client.
* @type {InteractionClient} * @type {InteractionClient}
*/ */
this.interactionClient = new InteractionClient( this.interactionClient = new InteractionClient(options, this);
options,
interaction => {
/**
* Emitted when an interaction is created.
* @event Client#interactionCreate
* @param {Interaction} interaction The interaction which was created.
*/
this.emit(Events.INTERACTION_CREATE, interaction);
},
this,
);
/** /**
* All of the {@link User} objects that have been cached at any point, mapped by their IDs * All of the {@link User} objects that have been cached at any point, mapped by their IDs

View file

@ -3,27 +3,29 @@
const BaseClient = require('./BaseClient'); const BaseClient = require('./BaseClient');
const ApplicationCommand = require('../structures/ApplicationCommand'); const ApplicationCommand = require('../structures/ApplicationCommand');
const Interaction = require('../structures/Interaction'); const Interaction = require('../structures/Interaction');
const { ApplicationCommandOptionType, InteractionType, InteractionResponseType } = require('../util/Constants'); const { Events, ApplicationCommandOptionType, InteractionType, InteractionResponseType } = require('../util/Constants');
let sodium; let sodium;
/** /**
* Interaction client is used for interactions. * Interaction client is used for interactions.
* *
* ```js * @example
* const client = new InteractionClient({ * const client = new InteractionClient({
* token: ABC, * token: ABC,
* publicKey: XYZ, * publicKey: XYZ,
* }, async (interaction) => { * });
*
* client.on('interactionCreate', () => {
* // automatically handles long responses * // automatically handles long responses
* if (will take a long time) { * if (will take a long time) {
* await doSomethingLong.then((d) => { * doSomethingLong.then((d) => {
* interaction.reply({ * interaction.reply({
* content: 'wow that took long', * content: 'wow that took long',
* }); * });
* }); * });
* } else { * } else {
* await interaction.reply('hi!'); * interaction.reply('hi!');
* } * }
* }); * });
* ``` * ```
@ -31,16 +33,25 @@ let sodium;
class InteractionClient extends BaseClient { class InteractionClient extends BaseClient {
/** /**
* @param {Options} options Options for the client. * @param {Options} options Options for the client.
* @param {Handler} handler Handler to handle things.
* @param {undefined} client For internal use. * @param {undefined} client For internal use.
*/ */
constructor(options, handler, client) { constructor(options, client) {
super(options); super(options);
this.handler = handler; Object.defineProperty(this, 'token', {
this.token = options.token; value: options.token,
this.publicKey = options.publicKey ? Buffer.from(options.publicKey, 'hex') : undefined; writable: true,
this.clientID = options.clientID; });
Object.defineProperty(this, 'clientID', {
value: options.clientID,
writable: true,
});
Object.defineProperty(this, 'publicKey', {
value: options.publicKey ? Buffer.from(options.publicKey, 'hex') : undefined,
writable: true,
});
// Compat for direct usage // Compat for direct usage
this.client = client || this; this.client = client || this;
@ -49,7 +60,7 @@ class InteractionClient extends BaseClient {
/** /**
* Get registered slash commands. * Get registered slash commands.
* @param {Snowflake?} guildID Optional guild ID. * @param {Snowflake} [guildID] Optional guild ID.
* @returns {Command[]} * @returns {Command[]}
*/ */
async getCommands(guildID) { async getCommands(guildID) {
@ -92,7 +103,7 @@ class InteractionClient extends BaseClient {
return new ApplicationCommand(this, c, guildID); return new ApplicationCommand(this, c, guildID);
} }
async handle(data) { handle(data) {
switch (data.type) { switch (data.type) {
case InteractionType.PING: case InteractionType.PING:
return { return {
@ -101,7 +112,7 @@ class InteractionClient extends BaseClient {
case InteractionType.APPLICATION_COMMAND: { case InteractionType.APPLICATION_COMMAND: {
let timedOut = false; let timedOut = false;
let resolve; let resolve;
const p0 = new Promise(r => { const directPromise = new Promise(r => {
resolve = r; resolve = r;
this.client.setTimeout(() => { this.client.setTimeout(() => {
timedOut = true; timedOut = true;
@ -133,13 +144,14 @@ class InteractionClient extends BaseClient {
const interaction = new Interaction(this.client, data, syncHandle); const interaction = new Interaction(this.client, data, syncHandle);
Promise.resolve(this.handler(interaction)).catch(e => { /**
this.client.emit('error', e); * Emitted when an interaction is created.
}); * @event Client#interactionCreate
* @param {Interaction} interaction The interaction which was created.
*/
this.client.emit(Events.INTERACTION_CREATE, interaction);
const result = await p0; return directPromise;
return result;
} }
default: default:
throw new RangeError('Invalid interaction data'); throw new RangeError('Invalid interaction data');

View file

@ -17,7 +17,7 @@ class ApplicationCommand extends Base {
* @type {Snowflake?} * @type {Snowflake?}
* @readonly * @readonly
*/ */
this.guildID = guildID; this.guildID = guildID || null;
this._patch(data); this._patch(data);
} }

View file

@ -19,48 +19,56 @@ class Interaction extends Base {
/** /**
* The ID of this interaction. * The ID of this interaction.
* @type {Snowflake} * @type {Snowflake}
* @readonly
*/ */
this.id = data.id; this.id = data.id;
/** /**
* The token of this interaction. * The token of this interaction.
* @type {string} * @type {string}
* @readonly
*/ */
this.token = data.token; this.token = data.token;
/** /**
* The ID of the invoked command. * The ID of the invoked command.
* @type {Snowflake} * @type {Snowflake}
* @readonly
*/ */
this.commandID = data.data.id; this.commandID = data.data.id;
/** /**
* The name of the invoked command. * The name of the invoked command.
* @type {string} * @type {string}
* @readonly
*/ */
this.commandName = data.data.name; this.commandName = data.data.name;
/** /**
* The options passed to the command. * The options passed to the command.
* @type {Object} * @type {Object}
* @readonly
*/ */
this.options = data.data.options; this.options = data.data.options;
/** /**
* The channel this interaction was sent in. * The channel this interaction was sent in.
* @type {Channel} * @type {?Channel}
* @readonly
*/ */
this.channel = this.client.channels?.cache.get(data.channel_id); this.channel = this.client.channels?.cache.get(data.channel_id) || null;
/** /**
* The guild this interaction was sent in, if any. * The guild this interaction was sent in, if any.
* @type {?Guild} * @type {?Guild}
* @readonly
*/ */
this.guild = data.guild_id ? this.client.guilds?.cache.get(data.guild_id) : null; this.guild = data.guild_id ? this.client.guilds?.cache.get(data.guild_id) : null;
/** /**
* If this interaction was sent in a guild, the member which sent it. * If this interaction was sent in a guild, the member which sent it.
* @type {?Member} * @type {?Member}
* @readonly
*/ */
this.member = data.member ? this.guild?.members.add(data.member, false) : null; this.member = data.member ? this.guild?.members.add(data.member, false) : null;
} }