Initial testing for interactive gif search and save
This commit is contained in:
parent
4f16f0394d
commit
f5e7918957
@ -1,20 +1,73 @@
|
|||||||
|
const giphy = require('giphy-api')(process.env.giphyAPIKey);
|
||||||
|
const functions = require('../functions');
|
||||||
|
const { emoji } = require('../config.json');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'savegif',
|
name: 'savegif',
|
||||||
description: 'Adds a given gif to the hardcoded list.',
|
description: 'Adds a given gif to the hardcoded list.',
|
||||||
usage: '<https://link.to.gif> <gif_name>',
|
usage: '<search query>',
|
||||||
execute(message, file) {
|
execute(message, file) {
|
||||||
const tempArray = file.name.split(' ');
|
message.author.createDM()
|
||||||
const embedURL = tempArray.shift();
|
.then(channel => {
|
||||||
const gifName = tempArray.join(' ');
|
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);
|
||||||
|
|
||||||
const fs = require('fs');
|
channel.send(embed)
|
||||||
fs.appendFile(`./gifs/${gifName}.js`, `module.exports = {\n\tname: '${gifName}',\n\tembed_url: '${embedURL}'\n}`, function(err) {
|
.then(selfMessage => {
|
||||||
if (err) throw err;
|
selfMessage.react(emoji.previous).then(() => {
|
||||||
console.log('Saved file!');
|
selfMessage.react(emoji.confirm).then(() => {
|
||||||
const gif = require(`../gifs/${gifName}.js`);
|
selfMessage.react(emoji.next);
|
||||||
message.client.gifs.set(gif.name, gif);
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
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 });
|
||||||
|
|
||||||
message.reply('GIF saved as: ' + gifName + '.gif!');
|
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;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}).catch(err => console.error(err));
|
||||||
|
})
|
||||||
|
.catch(err => console.error(err));
|
||||||
|
})
|
||||||
|
.catch(err => console.error(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user