forked from Galactic/galactic-bot
reply & prompt stuff
This commit is contained in:
parent
e7b7c582e1
commit
c712fe793d
@ -27,28 +27,70 @@ class InteractionWrapper {
|
||||
options.content = `${Emojis[options.emoji]} ${options.content}`;
|
||||
}
|
||||
|
||||
// Automatically edits a deferred interaction, idk if it should in the final version
|
||||
async reply(options = {}) {
|
||||
|
||||
if (typeof options === 'string') options = { content: options };
|
||||
if (options.template) {
|
||||
const { template } = options;
|
||||
options.content = this.format(template.index, template.params, template.opts);
|
||||
}
|
||||
if (options.emoji) this.emojify(options);
|
||||
this._pending = await this.interaction.reply(options);
|
||||
|
||||
if (this.deferred && !this.replied) options._edit = true;
|
||||
|
||||
if (options._edit) this._pending = await this._editReply(options);
|
||||
else this._pending = await this.interaction.reply(options);
|
||||
return this._pending;
|
||||
|
||||
}
|
||||
|
||||
async editReply(options = {}) {
|
||||
if (typeof options === 'string') options = { content: options };
|
||||
if (!options.components) options.components = [];
|
||||
options._edit = true;
|
||||
return this.reply(options);
|
||||
}
|
||||
|
||||
if (options.template) {
|
||||
const { template } = options;
|
||||
options.content = this.format(template.index, template.params, template.opts);
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
async _editReply(options) {
|
||||
return this.interaction.editReply(options);
|
||||
}
|
||||
|
||||
async promptInteraction(opts = {}, time = 30) {
|
||||
if (!opts.components) throw new Error('Missing components. Use promptMessage');
|
||||
let message = null;
|
||||
if (this.replied || this.deferred) message = await this.editReply(opts);
|
||||
else message = await this.reply(opts);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
message.createMessageComponentCollector({
|
||||
time: time*1000,
|
||||
componentType: 'BUTTON',
|
||||
max: 1,
|
||||
filter: (i) => i.user.id === this.user.id
|
||||
}).on('collect', resolve)
|
||||
.on('end', (collected) => {
|
||||
if (!collected.size) resolve(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async promptMessage(str, opts = {}) {
|
||||
if (typeof str === 'string') {
|
||||
if (opts.emoji) str = `${Emojis[opts.emoji]} ${str}`;
|
||||
if (opts.reply) str = `<@!${this.author.id}> ${str}`;
|
||||
}
|
||||
if (options.emoji) this.emojify(options);
|
||||
this._pending = await this.interaction.editReply(options);
|
||||
return this._pending;
|
||||
|
||||
await this.channel.send(str, { files: opts.files, embeds: [opts.embed], disableMentions: opts.disableMentions });
|
||||
return this.channel.awaitMessages({ filter: (m) => m.author.id === this.user.id, max: 1, time: opts.time || 30000, errors: ['time'] })
|
||||
.then((collected) => {
|
||||
return collected.first();
|
||||
})
|
||||
.catch((error) => { //eslint-disable-line no-unused-vars, handle-callback-err
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
replyEmbed(embed) {
|
||||
@ -109,6 +151,10 @@ class InteractionWrapper {
|
||||
return this.interaction.replied;
|
||||
}
|
||||
|
||||
get deferred() {
|
||||
return this.interaction.deferred;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = InteractionWrapper;
|
Loading…
Reference in New Issue
Block a user