add ack api

This commit is contained in:
Gus Caplan 2020-12-11 15:05:25 -06:00
parent 4599ef954f
commit 8c65961a07
No known key found for this signature in database
GPG key ID: F00BD11880E82F0E
2 changed files with 39 additions and 18 deletions

View file

@ -108,19 +108,30 @@ class InteractionClient extends BaseClient {
r({
type: InteractionResponseType.ACKNOWLEDGE_WITH_SOURCE,
});
}, 500);
}, 250);
});
const interaction = new Interaction(this.client, data, resolved => {
if (timedOut) {
return false;
}
resolve({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: resolved.data,
});
return true;
});
const syncHandle = {
acknowledge() {
if (!timedOut) {
resolve({
type: InteractionResponseType.ACKNOWLEDGE_WITH_SOURCE,
});
}
},
reply(resolved) {
if (timedOut) {
return false;
}
resolve({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: resolved.data,
});
return true;
},
};
const interaction = new Interaction(this.client, data, syncHandle);
Promise.resolve(this.handler(interaction)).catch(e => {
this.client.emit('error', e);
@ -141,7 +152,7 @@ class InteractionClient extends BaseClient {
* @returns {Function} The middleware function.
*/
middleware() {
return async (req, res, next) => {
return async (req, res) => {
const timestamp = req.get('x-signature-timestamp');
const signature = req.get('x-signature-ed25519');
@ -169,8 +180,6 @@ class InteractionClient extends BaseClient {
const result = await this.handle(data);
res.status(200).end(JSON.stringify(result));
next();
};
}

View file

@ -9,9 +9,9 @@ const Snowflake = require('../util/Snowflake');
* @extends {Base}
*/
class Interaction extends Base {
constructor(client, data, handler) {
constructor(client, data, syncHandle) {
super(client);
this.handler = handler;
this.syncHandle = syncHandle;
this._patch(data);
}
@ -83,6 +83,18 @@ class Interaction extends Base {
return new Date(this.createdTimestamp);
}
/**
* Acknowledge this interaction without content.
*/
async acknowledge() {
await this.syncHandle.acknowledge();
}
/**
* Reply to this interaction.
* @param {(StringResolvable | APIMessage)?} content The content for the message.
* @param {(MessageOptions | MessageAdditions)?} options The options to provide.
*/
async reply(content, options) {
let apiMessage;
@ -91,13 +103,13 @@ class Interaction extends Base {
} else {
apiMessage = APIMessage.create(this, content, options).resolveData();
if (Array.isArray(apiMessage.data.content)) {
throw new Error();
throw new Error('Message is too long');
}
}
const resolved = await apiMessage.resolveFiles();
if (!this.handler(resolved)) {
if (!this.syncHandle.reply(resolved)) {
const clientID =
this.client.interactionClient.clientID || (await this.client.api.oauth2.applications('@me').get()).id;