From b34700fd857c9b563dd8cab1ac2d6a077edd90cf Mon Sep 17 00:00:00 2001 From: = Date: Thu, 15 Jul 2021 21:41:21 -0400 Subject: [PATCH] Add query for filename --- commands/savegif.js | 123 ++++++++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 57 deletions(-) diff --git a/commands/savegif.js b/commands/savegif.js index 2b0a778..7ee27bd 100644 --- a/commands/savegif.js +++ b/commands/savegif.js @@ -7,66 +7,75 @@ module.exports = { description: 'Adds a given gif to the hardcoded list.', usage: '', execute(message, file) { - message.author.createDM() - .then(channel => { - const query = file.name; - giphy.search(query) - .then(res => { - if (res.data == undefined) return; - if (res.data[0] == undefined) { - channel.send('Sorry, I wasn\'t able to find a GIF of ' + file.name); - return; + const channel = message.channel; + const query = file.name; + giphy.search(query) + .then(res => { + if (res.data == undefined) return; + if (res.data[0] == undefined) { + channel.send('Sorry, I wasn\'t able to find a GIF of ' + file.name); + return; + } + let i = 0; + const data = { + "name": file.name, + "embed_url": res.data[i].images.original.url, + "author": message.author + }; + let embed = functions.createGifEmbed(data, message.author, `${Object.values(file).join('.')}`); + + // Send the first GIF result as an Embed + channel.send(embed) + .then(selfMessage => { + // Add reactions to go back, forward, 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); + }); + }); + 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; } - let i = 0; - const data = { - "name": file.name, - "embed_url": res.data[i].images.original.url, - "author": message.author - }; - let embed = functions.createGifEmbed(data); + const collector = selfMessage.createReactionCollector(filter, { time: 120000 }); - channel.send(embed) - .then(selfMessage => { - selfMessage.react(emoji.previous).then(() => { - selfMessage.react(emoji.confirm).then(() => { - selfMessage.react(emoji.next); - }); - }); - 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; - } - const collector = selfMessage.createReactionCollector(filter, { time: 120000 }); - - collector.on('collect', (reaction, user) => { - switch (reaction.emoji.name) { - case emoji.next: - i++; - data.embed_url = res.data[i].images.original.url; - embed = functions.createGifEmbed(data); - if (selfMessage.editable) { - selfMessage.edit(embed); - } - break; - case emoji.confirm: - channel.send('Saving GIF: ' + res.data[i].images.original.url); - functions.saveGif(message, data.name, data.embed_url); - break; - case emoji.previous: - i--; - data.embed_url = res.data[i].images.original.url; - embed = functions.createGifEmbed(data); - if (selfMessage.editable) { - selfMessage.edit(embed); - } - break; - default: - channel.send('There was an error, sorry.'); - break; + collector.on('collect', (reaction, user) => { + switch (reaction.emoji.name) { + case emoji.next: + i++; + data.embed_url = res.data[i].images.original.url; + embed = functions.createGifEmbed(data, message.author, `${file.name}.${file.extension}`); + if (selfMessage.editable) { + selfMessage.edit(embed); } - }) - }).catch(err => console.error(err)); - }) - .catch(err => console.error(err)); + break; + case emoji.confirm: + channel.send('GIF Selected. What should I save the GIF as? (don\'t include the `.gif`)') + .then(nameQueryMessage => { + const nameCollectorFilter = nameMessage => nameMessage.author == message.author; + const nameCollector = nameQueryMessage.channel.createMessageCollector(nameCollectorFilter, { time: 30000, max: 1 }); + + nameCollector.on('collect', nameMessage => { + channel.send('The GIF has been saved as: ' + nameMessage.content + '.gif'); + functions.saveGif(message, nameMessage.content, data.embed_url); + }); + }); + + break; + case emoji.previous: + i--; + data.embed_url = res.data[i].images.original.url; + embed = functions.createGifEmbed(data, message.author, `${file.name}.${file.extension}`); + if (selfMessage.editable) { + selfMessage.edit(embed); + } + break; + default: + channel.send('There was an error, sorry.'); + break; + } + }) + }).catch(err => console.error(err)); }) .catch(err => console.error(err)); }