basic working

This commit is contained in:
Gus Caplan 2020-12-11 14:18:26 -06:00
parent db6d1b3ba9
commit 5be32161b9
No known key found for this signature in database
GPG key ID: F00BD11880E82F0E
3 changed files with 41 additions and 47 deletions

View file

@ -116,8 +116,7 @@ class Client extends BaseClient {
* @event Client#interactionCreate * @event Client#interactionCreate
* @param {Interaction} interaction The interaction which was created. * @param {Interaction} interaction The interaction which was created.
*/ */
this.client.emit(Events.INTERACTION_CREATE, interaction); this.emit(Events.INTERACTION_CREATE, interaction);
return null;
}, },
this, this,
); );

View file

@ -1,7 +1,6 @@
'use strict'; 'use strict';
const BaseClient = require('./BaseClient'); const BaseClient = require('./BaseClient');
const APIMessage = require('../structures/APIMessage');
const Interaction = require('../structures/Interaction'); const Interaction = require('../structures/Interaction');
const { ApplicationCommandOptionType, InteractionType, InteractionResponseType } = require('../util/Constants'); const { ApplicationCommandOptionType, InteractionType, InteractionResponseType } = require('../util/Constants');
@ -15,16 +14,16 @@ let sodium;
* token: ABC, * token: ABC,
* publicKey: XYZ, * publicKey: XYZ,
* }, async (interaction) => { * }, async (interaction) => {
* // automatically handles long responses
* if (will take a long time) { * if (will take a long time) {
* doSomethingLong.then((d) => { * await doSomethingLong.then((d) => {
* interaction.reply({ * interaction.reply({
* content: 'wow that took long', * content: 'wow that took long',
* }); * });
* }); * });
* // return null to signal that we will be replying via `interaction.reply`. * } else {
* return null; * await interaction.reply('hi!');
* } * }
* return { content: 'hi!' };
* }); * });
* ``` * ```
*/ */
@ -40,7 +39,7 @@ class InteractionClient extends BaseClient {
this.handler = handler; this.handler = handler;
this.token = options.token; this.token = options.token;
this.publicKey = options.publicKey ? Buffer.from(options.publicKey, 'hex') : undefined; this.publicKey = options.publicKey ? Buffer.from(options.publicKey, 'hex') : undefined;
this.clientId = options.clientId; this.clientID = options.clientID;
// Compat for direct usage // Compat for direct usage
this.client = client || this; this.client = client || this;
@ -94,44 +93,34 @@ class InteractionClient extends BaseClient {
type: InteractionResponseType.PONG, type: InteractionResponseType.PONG,
}; };
case InteractionType.APPLICATION_COMMAND: { case InteractionType.APPLICATION_COMMAND: {
const interaction = new Interaction(this.client, data); let timedOut = false;
let resolve;
let done = false; const p0 = new Promise(r => {
const r0 = new Promise(resolve => { resolve = r;
this.client.setTimeout(() => { this.client.setTimeout(() => {
done = true; timedOut = true;
resolve({ r({
type: InteractionResponseType.ACKNOWLEDGE_WITH_SOURCE, type: InteractionResponseType.ACKNOWLEDGE_WITH_SOURCE,
}); });
}, 500); }, 500);
}); });
const r1 = this.handler(interaction).then(async r => {
if (done) { const interaction = new Interaction(this.client, data, resolved => {
interaction.reply(r).catch(e => { if (timedOut) {
this.client.emit('error', e); return false;
});
return undefined;
} }
resolve({
let apiMessage;
if (r instanceof APIMessage) {
apiMessage = r.resolveData();
} else {
apiMessage = APIMessage.create(interaction, r).resolveData();
if (Array.isArray(apiMessage.data.content)) {
throw new Error();
}
}
const resolved = await apiMessage.resolveFiles();
return {
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE, type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: resolved.data, data: resolved.data,
}; });
return true;
}); });
const result = await Promise.race([r0, r1]); Promise.resolve(this.handler(interaction)).catch(e => {
this.client.emit('error', e);
});
const result = await p0;
return result; return result;
} }
@ -175,8 +164,11 @@ class InteractionClient extends BaseClient {
} }
async handleFromGateway(data) { async handleFromGateway(data) {
const interaction = new Interaction(this.client, data); const result = await this.handle(data);
await this.handler(interaction);
await this.client.api.interactions(data.id, data.token).callback.post({
data: result,
});
} }
} }

View file

@ -10,8 +10,9 @@ const Snowflake = require('../util/Snowflake');
* @extends {Base} * @extends {Base}
*/ */
class Interaction extends Base { class Interaction extends Base {
constructor(client, data) { constructor(client, data, handler) {
super(client); super(client);
this.handler = handler;
this._patch(data); this._patch(data);
} }
@ -97,16 +98,18 @@ class Interaction extends Base {
} }
} }
const { data, files } = await apiMessage.resolveFiles(); const resolved = await apiMessage.resolveFiles();
const clientId = this.client.interactionClient.clientId if (!this.handler(resolved)) {
|| (await this.client.api.oauth2.applications('@me').get()).id; const clientID = this.client.interactionClient.clientID
|| (await this.client.api.oauth2.applications('@me').get()).id;
return this.client.api.webhooks(clientId, this.token).post({ await this.client.api.webhooks(clientID, this.token).post({
auth: false, auth: false,
data, data: resolved.data,
files, files: resolved.files,
}); });
}
} }
} }