@sodacore/discord
Version:
Sodacore Discord is a plugin that offers Discord SSO/OAuth2 support and the ability to create bots in a similar controller pattern.
58 lines (57 loc) • 1.76 kB
JavaScript
import { InteractionCallbackType } from 'lilybird';
export default class InteractionContext {
constructor(client, payload) {
this.client = client;
this.payload = payload;
}
getUser() {
if ('guild_id' in this.payload) {
return this.payload.member.user ?? null;
}
else if ('user' in this.payload) {
return this.payload.user;
}
return null;
}
getUserId() {
const user = this.getUser();
return user ? user.id : null;
}
getGuildId() {
if ('guild_id' in this.payload) {
return this.payload.guild_id;
}
return null;
}
getData() {
return this.payload.data;
}
getChannel() {
if ('channel' in this.payload) {
return this.payload.channel;
}
return null;
}
async deferMessage() {
await this.client.rest.createInteractionResponse(this.payload.id, this.payload.token, {
type: InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE,
});
}
async replyWithMessage(message, defer = false) {
await this.client.rest.createInteractionResponse(this.payload.id, this.payload.token, {
type: defer ? InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE : InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: message,
},
});
}
async updateDeferredWithMessage(message) {
await this.client.rest.createInteractionResponse(this.payload.id, this.payload.token, {
type: InteractionCallbackType.UPDATE_MESSAGE,
data: {
content: message,
},
});
}
}
;