mirror of
https://github.com/danbulant/discord.js
synced 2026-07-06 19:50:55 +00:00
more
This commit is contained in:
parent
c6f87aa32d
commit
a626dc8c41
3 changed files with 57 additions and 28 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
'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');
|
||||||
|
|
||||||
|
|
@ -37,6 +38,7 @@ class InteractionClient extends BaseClient {
|
||||||
super(options);
|
super(options);
|
||||||
this.client = client || this;
|
this.client = client || this;
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
|
this.token = options.token;
|
||||||
this.publicKey = options.publicKey ? Buffer.from(options.publicKey, 'hex') : undefined;
|
this.publicKey = options.publicKey ? Buffer.from(options.publicKey, 'hex') : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,44 +89,72 @@ class InteractionClient extends BaseClient {
|
||||||
type: InteractionResponseType.PONG,
|
type: InteractionResponseType.PONG,
|
||||||
};
|
};
|
||||||
case InteractionType.APPLICATION_COMMAND: {
|
case InteractionType.APPLICATION_COMMAND: {
|
||||||
try {
|
const interaction = new Interaction(this.client, data);
|
||||||
const interaction = new Interaction(this.client, data);
|
|
||||||
const result = await this.handler(interaction);
|
let done = false;
|
||||||
if (result === null) {
|
const r0 = new Promise(resolve => {
|
||||||
return {
|
this.client.setTimeout(() => {
|
||||||
type: InteractionResponseType.ACKNOWLEDGE,
|
done = true;
|
||||||
};
|
resolve({
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
// handle result as message resolvable here, probably DRY this branch with code in
|
|
||||||
// `Interaction#reply`, except `Interaction#reply` obviously does a POST and this just
|
let apiMessage;
|
||||||
// returns the data.
|
|
||||||
throw new Error('fucc');
|
if (r instanceof APIMessage) {
|
||||||
} catch (e) {
|
apiMessage = r.resolveData();
|
||||||
this.client.emit('error', e);
|
} else {
|
||||||
|
apiMessage = APIMessage.create(interaction, r).resolveData();
|
||||||
|
if (Array.isArray(apiMessage.data.content)) {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolved = await apiMessage.resolveFiles();
|
||||||
return {
|
return {
|
||||||
type: InteractionResponseType.ACKNOWLEDGE,
|
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||||
|
data: resolved.data,
|
||||||
};
|
};
|
||||||
}
|
});
|
||||||
|
|
||||||
|
const result = await Promise.race([r0, r1]);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
throw new RangeError('Invalid interaction data');
|
throw new RangeError('Invalid interaction data');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleFromHTTP(body, signature) {
|
async handleFromHTTP(body, signature, timestamp) {
|
||||||
if (sodium === undefined) {
|
if (sodium === undefined) {
|
||||||
sodium = require('../util/Sodium');
|
sodium = require('../util/Sodium');
|
||||||
}
|
}
|
||||||
if (!sodium.methods.verify(Buffer.from(signature, 'hex'), Buffer.from(body), this.publicKey)) {
|
if (!sodium.methods.verify(Buffer.from(signature, 'hex'), Buffer.from(timestamp + body), this.publicKey)) {
|
||||||
throw new Error('Invalid signature');
|
return { status: 400, body: '' };
|
||||||
}
|
}
|
||||||
const data = JSON.parse(body);
|
const data = JSON.parse(body);
|
||||||
|
|
||||||
const result = await this.handle(data);
|
const result = await this.handle(data);
|
||||||
return JSON.stringify(result);
|
|
||||||
|
return {
|
||||||
|
status: 200,
|
||||||
|
body: JSON.stringify(result),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleFromGateway(data) {
|
async handleFromGateway(data) {
|
||||||
await this.handle(data);
|
const interaction = new Interaction(this.client, data);
|
||||||
|
await this.handler(interaction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,19 +50,19 @@ class Interaction extends Base {
|
||||||
* The channel this interaction was sent in.
|
* The channel this interaction was sent in.
|
||||||
* @type {Channel}
|
* @type {Channel}
|
||||||
*/
|
*/
|
||||||
this.channel = this.client.channels.cache.get(data.channel_id);
|
this.channel = this.client.channels?.cache.get(data.channel_id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The guild this interaction was sent in, if any.
|
* The guild this interaction was sent in, if any.
|
||||||
* @type {?Guild}
|
* @type {?Guild}
|
||||||
*/
|
*/
|
||||||
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}
|
||||||
*/
|
*/
|
||||||
this.member = data.member ? this.guild.members.add(data.member, false) : null;
|
this.member = data.member ? this.guild?.members.add(data.member, false) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -99,11 +99,9 @@ class Interaction extends Base {
|
||||||
|
|
||||||
const { data, files } = await apiMessage.resolveFiles();
|
const { data, files } = await apiMessage.resolveFiles();
|
||||||
|
|
||||||
return this.client.api.interactions(this.id, this.token).callback.post({
|
return this.client.api.webhooks(this.id, this.token).post({
|
||||||
data: {
|
auth: false,
|
||||||
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
data,
|
||||||
data,
|
|
||||||
},
|
|
||||||
files,
|
files,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -700,6 +700,7 @@ exports.InteractionResponseType = {
|
||||||
ACKNOWLEDGE: 2,
|
ACKNOWLEDGE: 2,
|
||||||
CHANNEL_MESSAGE: 3,
|
CHANNEL_MESSAGE: 3,
|
||||||
CHANNEL_MESSAGE_WITH_SOURCE: 4,
|
CHANNEL_MESSAGE_WITH_SOURCE: 4,
|
||||||
|
ACKNOWLEDGE_WITH_SOURCE: 5,
|
||||||
};
|
};
|
||||||
|
|
||||||
function keyMirror(arr) {
|
function keyMirror(arr) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue