mirror of
https://github.com/danbulant/discord.js
synced 2026-06-17 21:51:20 +00:00
basic working
This commit is contained in:
parent
db6d1b3ba9
commit
5be32161b9
3 changed files with 41 additions and 47 deletions
|
|
@ -116,8 +116,7 @@ class Client extends BaseClient {
|
|||
* @event Client#interactionCreate
|
||||
* @param {Interaction} interaction The interaction which was created.
|
||||
*/
|
||||
this.client.emit(Events.INTERACTION_CREATE, interaction);
|
||||
return null;
|
||||
this.emit(Events.INTERACTION_CREATE, interaction);
|
||||
},
|
||||
this,
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
const BaseClient = require('./BaseClient');
|
||||
const APIMessage = require('../structures/APIMessage');
|
||||
const Interaction = require('../structures/Interaction');
|
||||
const { ApplicationCommandOptionType, InteractionType, InteractionResponseType } = require('../util/Constants');
|
||||
|
||||
|
|
@ -15,16 +14,16 @@ let sodium;
|
|||
* token: ABC,
|
||||
* publicKey: XYZ,
|
||||
* }, async (interaction) => {
|
||||
* // automatically handles long responses
|
||||
* if (will take a long time) {
|
||||
* doSomethingLong.then((d) => {
|
||||
* await doSomethingLong.then((d) => {
|
||||
* interaction.reply({
|
||||
* content: 'wow that took long',
|
||||
* });
|
||||
* });
|
||||
* // return null to signal that we will be replying via `interaction.reply`.
|
||||
* return null;
|
||||
* } else {
|
||||
* await interaction.reply('hi!');
|
||||
* }
|
||||
* return { content: 'hi!' };
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
|
|
@ -40,7 +39,7 @@ class InteractionClient extends BaseClient {
|
|||
this.handler = handler;
|
||||
this.token = options.token;
|
||||
this.publicKey = options.publicKey ? Buffer.from(options.publicKey, 'hex') : undefined;
|
||||
this.clientId = options.clientId;
|
||||
this.clientID = options.clientID;
|
||||
|
||||
// Compat for direct usage
|
||||
this.client = client || this;
|
||||
|
|
@ -94,44 +93,34 @@ class InteractionClient extends BaseClient {
|
|||
type: InteractionResponseType.PONG,
|
||||
};
|
||||
case InteractionType.APPLICATION_COMMAND: {
|
||||
const interaction = new Interaction(this.client, data);
|
||||
|
||||
let done = false;
|
||||
const r0 = new Promise(resolve => {
|
||||
let timedOut = false;
|
||||
let resolve;
|
||||
const p0 = new Promise(r => {
|
||||
resolve = r;
|
||||
this.client.setTimeout(() => {
|
||||
done = true;
|
||||
resolve({
|
||||
timedOut = true;
|
||||
r({
|
||||
type: InteractionResponseType.ACKNOWLEDGE_WITH_SOURCE,
|
||||
});
|
||||
}, 500);
|
||||
});
|
||||
const r1 = this.handler(interaction).then(async r => {
|
||||
if (done) {
|
||||
interaction.reply(r).catch(e => {
|
||||
this.client.emit('error', e);
|
||||
});
|
||||
return undefined;
|
||||
|
||||
const interaction = new Interaction(this.client, data, resolved => {
|
||||
if (timedOut) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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 {
|
||||
resolve({
|
||||
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||
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;
|
||||
}
|
||||
|
|
@ -175,8 +164,11 @@ class InteractionClient extends BaseClient {
|
|||
}
|
||||
|
||||
async handleFromGateway(data) {
|
||||
const interaction = new Interaction(this.client, data);
|
||||
await this.handler(interaction);
|
||||
const result = await this.handle(data);
|
||||
|
||||
await this.client.api.interactions(data.id, data.token).callback.post({
|
||||
data: result,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,8 +10,9 @@ const Snowflake = require('../util/Snowflake');
|
|||
* @extends {Base}
|
||||
*/
|
||||
class Interaction extends Base {
|
||||
constructor(client, data) {
|
||||
constructor(client, data, handler) {
|
||||
super(client);
|
||||
this.handler = handler;
|
||||
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
|
||||
|| (await this.client.api.oauth2.applications('@me').get()).id;
|
||||
if (!this.handler(resolved)) {
|
||||
const clientID = this.client.interactionClient.clientID
|
||||
|| (await this.client.api.oauth2.applications('@me').get()).id;
|
||||
|
||||
return this.client.api.webhooks(clientId, this.token).post({
|
||||
auth: false,
|
||||
data,
|
||||
files,
|
||||
});
|
||||
await this.client.api.webhooks(clientID, this.token).post({
|
||||
auth: false,
|
||||
data: resolved.data,
|
||||
files: resolved.files,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue