From 2e71b114f2851bb7029bb0488242bc19c8f06e05 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 19 Jul 2021 16:01:57 -0400 Subject: [PATCH] ability to cancel savegif --- commands/savegif.js | 49 ++++++++++++++++++++++++++++++++++++++++----- gifs/test.js | 4 ++++ src/strings.json | 9 ++++++++- 3 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 gifs/test.js diff --git a/commands/savegif.js b/commands/savegif.js index 3647138..202f46d 100644 --- a/commands/savegif.js +++ b/commands/savegif.js @@ -6,7 +6,7 @@ const tenor = require('tenorjs').client({ "DateFormat": "D/MM/YYYY - H:mm:ss A" // Change this accordingly }); const functions = require('../functions'); -const { emoji } = require('../config.json'); +const { emoji } = require('../src/strings.json'); module.exports = { name: 'savegif', @@ -32,15 +32,17 @@ module.exports = { // Send the first GIF result as an Embed channel.send(embed) .then(selfMessage => { - // Add reactions to go back, forward, and confirm GIF choice. + // Add reactions to go back, forward, cancel and confirm GIF choice. // React order is important so these are done in a chain selfMessage.react(emoji.previous).then(() => { selfMessage.react(emoji.confirm).then(() => { - selfMessage.react(emoji.next); + selfMessage.react(emoji.next).then(() => { + selfMessage.react(emoji.cancel); + }); }); }); const filter = (reaction, user) => { - return ((reaction.emoji.name == emoji.next) || (reaction.emoji.name == emoji.confirm) || (reaction.emoji.name == emoji.previous)) && user.id == message.author.id; + return ((reaction.emoji.name == emoji.next) || (reaction.emoji.name == emoji.confirm) || (reaction.emoji.name == emoji.previous) || (reaction.emoji.name == emoji.cancel)) && user.id == message.author.id; } const collector = selfMessage.createReactionCollector(filter, { time: 120000 }); @@ -60,8 +62,19 @@ module.exports = { } break; case emoji.confirm: - channel.send('GIF Selected. What should I save the GIF as? (don\'t include the `.gif`)') + channel.send('GIF Selected. What should I save the GIF as? (don\'t include the `.gif`)\nReact with ' + emoji.cancel + ' to cancel.') .then(nameQueryMessage => { + nameQueryMessage.react(emoji.cancel); + const cancelReactFilter = (reaction, user) => { + return (reaction.emoji.name == emoji.cancel) && (user.id == message.author.id); + } + const cancelReactCollector = nameQueryMessage.createReactionCollector(cancelReactFilter, { time: 20000, max: 1 }); + + cancelReactCollector.on('collect', (reaction, user) => { + nameCollector.stop('cancel'); + if (selfMessage.deletable) selfMessage.delete(); + if (nameQueryMessage.deletable) nameQueryMessage.delete(); + }) const nameCollectorFilter = nameMessage => nameMessage.author == message.author; const nameCollector = nameQueryMessage.channel.createMessageCollector(nameCollectorFilter, { time: 30000, max: 1 }); @@ -69,7 +82,17 @@ module.exports = { channel.send('The GIF has been saved as: ' + nameMessage.content + '.gif'); functions.saveGif(message, nameMessage.content, data.embed_url); }); + nameCollector.on('end', (collected, reason) => { + switch (reason) { + case 'cancel': + channel.send('The action has been canceled.'); + break; + default: + break; + } + }); }); + collector.stop("confirm"); break; case emoji.previous: if (i > 0) { @@ -84,10 +107,26 @@ module.exports = { selfMessage.edit(embed); } break; + case emoji.cancel: + collector.stop('cancel'); + break; default: channel.send('There was an error, sorry.'); break; } + }); + + collector.on('end', (collected, reason) => { + switch (reason) { + case 'cancel': + selfMessage.delete(); + channel.send('The action has been canceled.'); + break; + case 'messageDelete': + break; + default: + break; + } }) }).catch(err => console.error(err)); }) diff --git a/gifs/test.js b/gifs/test.js new file mode 100644 index 0000000..47b14d8 --- /dev/null +++ b/gifs/test.js @@ -0,0 +1,4 @@ +module.exports = { + name: 'test', + embed_url: 'https://media.tenor.com/images/0f097ed319d498c2bda3d87ba4f6ff10/tenor.gif' +} \ No newline at end of file diff --git a/src/strings.json b/src/strings.json index b09423d..38fb002 100644 --- a/src/strings.json +++ b/src/strings.json @@ -9,5 +9,12 @@ "You're gonna be doing a lot of doobie rollin' when you're living in a ran down by the river!", "You'll have plenty of time to live in a van down by the river... when you're living in a van down by the river!", "Roll, roll, roll my joint, pick out the seeds and stems" - ] + ], + "emoji": { + "joint": "<:joint:862082955902976000>", + "next": "⏭️", + "previous": "⏮️", + "confirm": "☑️", + "cancel": "❌" + } } \ No newline at end of file