Skip to content

Commit

Permalink
basic working
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Dec 11, 2020
1 parent 7f405bd commit c942f86
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 46 deletions.
3 changes: 1 addition & 2 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,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,
);
Expand Down
62 changes: 27 additions & 35 deletions src/client/InteractionClient.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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!' };
* });
* ```
*/
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

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 interaction = new Interaction(this.client, data, resolved => {
if (timedOut) {
return false;
}

const resolved = await apiMessage.resolveFiles();
return {
resolve({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: resolved.data,
};
});
return true;
});

Promise.resolve(this.handler(interaction)).catch(e => {
this.client.emit('error', e);
});

const result = await Promise.race([r0, r1]);
const result = await p0;

return result;
}
Expand Down Expand Up @@ -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,
});
}
}

Expand Down
21 changes: 12 additions & 9 deletions src/structures/Interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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,
});
}
}
}

Expand Down

0 comments on commit c942f86

Please sign in to comment.