From c712fe793debed3d5153c477a379cd5062fde5bd Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Sat, 15 Jan 2022 02:21:45 +0200 Subject: [PATCH] reply & prompt stuff --- .../client/wrappers/InteractionWrapper.js | 62 ++++++++++++++++--- 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/src/structure/client/wrappers/InteractionWrapper.js b/src/structure/client/wrappers/InteractionWrapper.js index ddf0527..0cf3bbe 100644 --- a/src/structure/client/wrappers/InteractionWrapper.js +++ b/src/structure/client/wrappers/InteractionWrapper.js @@ -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; \ No newline at end of file