2021-07-18 19:47:31 +00:00
|
|
|
const tenor = require('tenorjs').client({
|
|
|
|
"Key": process.env.tenorAPIKey, // https://tenor.com/developer/keyregistration
|
|
|
|
"Filter": "off", // "off", "low", "medium", "high", not case sensitive
|
|
|
|
"Locale": "en_US", // Your locale here, case-sensitivity depends on input
|
|
|
|
"MediaFilter": "minimal", // either minimal or basic, not case sensitive
|
|
|
|
"DateFormat": "D/MM/YYYY - H:mm:ss A" // Change this accordingly
|
|
|
|
});
|
2021-07-10 03:24:58 +00:00
|
|
|
const functions = require('../functions');
|
2021-07-19 20:01:57 +00:00
|
|
|
const { emoji } = require('../src/strings.json');
|
2021-07-10 03:24:58 +00:00
|
|
|
|
2021-07-01 22:12:21 +00:00
|
|
|
module.exports = {
|
|
|
|
name: 'savegif',
|
2021-07-19 02:23:40 +00:00
|
|
|
description: 'Saves a gif selected from a search to a given filename.',
|
2021-07-10 03:24:58 +00:00
|
|
|
usage: '<search query>',
|
2021-07-01 22:12:21 +00:00
|
|
|
execute(message, file) {
|
2021-07-16 01:41:21 +00:00
|
|
|
const query = file.name;
|
2021-07-19 01:17:38 +00:00
|
|
|
message.author.createDM().then(channel => {
|
|
|
|
tenor.Search.Query(query, 20)
|
2021-07-16 01:41:21 +00:00
|
|
|
.then(res => {
|
2021-07-18 19:47:31 +00:00
|
|
|
if (res[0] == undefined) {
|
2021-07-16 01:41:21 +00:00
|
|
|
channel.send('Sorry, I wasn\'t able to find a GIF of ' + file.name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let i = 0;
|
|
|
|
const data = {
|
|
|
|
"name": file.name,
|
2021-07-18 19:47:31 +00:00
|
|
|
"embed_url": res[0].media[0].gif.url,
|
2021-07-16 01:41:21 +00:00
|
|
|
"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 => {
|
2021-07-19 20:01:57 +00:00
|
|
|
// Add reactions to go back, forward, cancel and confirm GIF choice.
|
2021-07-16 01:41:21 +00:00
|
|
|
// React order is important so these are done in a chain
|
|
|
|
selfMessage.react(emoji.previous).then(() => {
|
|
|
|
selfMessage.react(emoji.confirm).then(() => {
|
2021-07-19 20:01:57 +00:00
|
|
|
selfMessage.react(emoji.next).then(() => {
|
|
|
|
selfMessage.react(emoji.cancel);
|
|
|
|
});
|
2021-07-16 01:41:21 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
const filter = (reaction, user) => {
|
2021-07-19 20:01:57 +00:00
|
|
|
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;
|
2021-07-10 03:24:58 +00:00
|
|
|
}
|
2021-07-16 01:41:21 +00:00
|
|
|
const collector = selfMessage.createReactionCollector(filter, { time: 120000 });
|
2021-07-01 22:12:21 +00:00
|
|
|
|
2021-07-16 01:41:21 +00:00
|
|
|
collector.on('collect', (reaction, user) => {
|
|
|
|
switch (reaction.emoji.name) {
|
|
|
|
case emoji.next:
|
2021-07-18 19:47:31 +00:00
|
|
|
if (i < res.length) {
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
selfMessage.channel.send('That\'s the last GIF, sorry!');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data.embed_url = res[i].media[0].gif.url;
|
2021-07-16 01:41:21 +00:00
|
|
|
embed = functions.createGifEmbed(data, message.author, `${file.name}.${file.extension}`);
|
|
|
|
if (selfMessage.editable) {
|
|
|
|
selfMessage.edit(embed);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case emoji.confirm:
|
2021-07-19 20:01:57 +00:00
|
|
|
channel.send('GIF Selected. What should I save the GIF as? (don\'t include the `.gif`)\nReact with ' + emoji.cancel + ' to cancel.')
|
2021-07-16 01:41:21 +00:00
|
|
|
.then(nameQueryMessage => {
|
2021-07-19 20:01:57 +00:00
|
|
|
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();
|
|
|
|
})
|
2021-07-16 01:41:21 +00:00
|
|
|
const nameCollectorFilter = nameMessage => nameMessage.author == message.author;
|
|
|
|
const nameCollector = nameQueryMessage.channel.createMessageCollector(nameCollectorFilter, { time: 30000, max: 1 });
|
2021-07-01 22:12:21 +00:00
|
|
|
|
2021-07-16 01:41:21 +00:00
|
|
|
nameCollector.on('collect', nameMessage => {
|
|
|
|
channel.send('The GIF has been saved as: ' + nameMessage.content + '.gif');
|
2021-07-24 20:09:22 +00:00
|
|
|
functions.saveGif(message, nameMessage.content.toLowerCase(), data.embed_url);
|
2021-07-16 01:41:21 +00:00
|
|
|
});
|
2021-07-19 20:01:57 +00:00
|
|
|
nameCollector.on('end', (collected, reason) => {
|
|
|
|
switch (reason) {
|
|
|
|
case 'cancel':
|
|
|
|
channel.send('The action has been canceled.');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2021-07-16 01:41:21 +00:00
|
|
|
});
|
2021-07-19 20:01:57 +00:00
|
|
|
collector.stop("confirm");
|
2021-07-16 01:41:21 +00:00
|
|
|
break;
|
|
|
|
case emoji.previous:
|
2021-07-18 19:47:31 +00:00
|
|
|
if (i > 0) {
|
|
|
|
i--;
|
|
|
|
} else {
|
|
|
|
selfMessage.channel.send('That\'s the first GIF, can\'t go back any further!');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data.embed_url = res[i].media[0].gif.url;
|
2021-07-16 01:41:21 +00:00
|
|
|
embed = functions.createGifEmbed(data, message.author, `${file.name}.${file.extension}`);
|
|
|
|
if (selfMessage.editable) {
|
|
|
|
selfMessage.edit(embed);
|
2021-07-10 03:24:58 +00:00
|
|
|
}
|
2021-07-16 01:41:21 +00:00
|
|
|
break;
|
2021-07-19 20:01:57 +00:00
|
|
|
case emoji.cancel:
|
|
|
|
collector.stop('cancel');
|
|
|
|
break;
|
2021-07-16 01:41:21 +00:00
|
|
|
default:
|
|
|
|
channel.send('There was an error, sorry.');
|
|
|
|
break;
|
|
|
|
}
|
2021-07-19 20:01:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
collector.on('end', (collected, reason) => {
|
|
|
|
switch (reason) {
|
|
|
|
case 'cancel':
|
|
|
|
selfMessage.delete();
|
|
|
|
channel.send('The action has been canceled.');
|
|
|
|
break;
|
|
|
|
case 'messageDelete':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-07-16 01:41:21 +00:00
|
|
|
})
|
|
|
|
}).catch(err => console.error(err));
|
2021-07-10 03:24:58 +00:00
|
|
|
})
|
|
|
|
.catch(err => console.error(err));
|
2021-07-19 01:17:38 +00:00
|
|
|
})
|
2021-07-01 22:12:21 +00:00
|
|
|
}
|
|
|
|
}
|