Switching to Tenor for GIFs

This commit is contained in:
= 2021-07-18 15:47:31 -04:00
parent b66c545500
commit c99662723b
6 changed files with 70 additions and 24 deletions

View File

@ -1,5 +1,11 @@
const functions = require('../functions'); const functions = require('../functions');
const giphy = require('giphy-api')(process.env.giphyAPIKey); 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
});
module.exports = { module.exports = {
name: 'gif', name: 'gif',
@ -7,17 +13,13 @@ module.exports = {
execute(message, file) { execute(message, file) {
const client = message.client; const client = message.client;
if (!client.gifs.has(file.name)) { if (!client.gifs.has(file.name)) {
giphy.search(file.name).then(function (res) { tenor.Search.Query(file.name, 1).then(res => {
if (res.data[0] != undefined) { if (res[0] == undefined) return;
// message.channel.send(file.name + ' requested by ' + message.author.username + '\n' + res.data[0].embed_url).then().catch(console.error); const gifInfo = {
const gifInfo = { 'name': file.name,
'name': file.name, 'embed_url': res[0].media[0].gif.url
'embed_url': res.data[0].images.original.url };
}; message.channel.send(functions.createGifEmbed(gifInfo, message.author, `${file.name}.${file.extension}`));
message.channel.send(functions.createGifEmbed(gifInfo, message.author, `${file.name}.${file.extension}`));
} else {
message.channel.send('I was unable to find a gif of ' + file.name);
}
}) })
.catch(err => console.error(err)); .catch(err => console.error(err));
} else { } else {

View File

@ -1,4 +1,10 @@
const giphy = require('giphy-api')(process.env.giphyAPIKey); 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
});
const functions = require('../functions'); const functions = require('../functions');
const { emoji } = require('../config.json'); const { emoji } = require('../config.json');
@ -9,17 +15,16 @@ module.exports = {
execute(message, file) { execute(message, file) {
const channel = message.channel; const channel = message.channel;
const query = file.name; const query = file.name;
giphy.search(query) tenor.Search.Query(query, 20)
.then(res => { .then(res => {
if (res.data == undefined) return; if (res[0] == undefined) {
if (res.data[0] == undefined) {
channel.send('Sorry, I wasn\'t able to find a GIF of ' + file.name); channel.send('Sorry, I wasn\'t able to find a GIF of ' + file.name);
return; return;
} }
let i = 0; let i = 0;
const data = { const data = {
"name": file.name, "name": file.name,
"embed_url": res.data[i].images.original.url, "embed_url": res[0].media[0].gif.url,
"author": message.author "author": message.author
}; };
let embed = functions.createGifEmbed(data, message.author, `${Object.values(file).join('.')}`); let embed = functions.createGifEmbed(data, message.author, `${Object.values(file).join('.')}`);
@ -42,8 +47,13 @@ module.exports = {
collector.on('collect', (reaction, user) => { collector.on('collect', (reaction, user) => {
switch (reaction.emoji.name) { switch (reaction.emoji.name) {
case emoji.next: case emoji.next:
i++; if (i < res.length) {
data.embed_url = res.data[i].images.original.url; i++;
} else {
selfMessage.channel.send('That\'s the last GIF, sorry!');
break;
}
data.embed_url = res[i].media[0].gif.url;
embed = functions.createGifEmbed(data, message.author, `${file.name}.${file.extension}`); embed = functions.createGifEmbed(data, message.author, `${file.name}.${file.extension}`);
if (selfMessage.editable) { if (selfMessage.editable) {
selfMessage.edit(embed); selfMessage.edit(embed);
@ -60,11 +70,15 @@ module.exports = {
functions.saveGif(message, nameMessage.content, data.embed_url); functions.saveGif(message, nameMessage.content, data.embed_url);
}); });
}); });
break; break;
case emoji.previous: case emoji.previous:
i--; if (i > 0) {
data.embed_url = res.data[i].images.original.url; 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;
embed = functions.createGifEmbed(data, message.author, `${file.name}.${file.extension}`); embed = functions.createGifEmbed(data, message.author, `${file.name}.${file.extension}`);
if (selfMessage.editable) { if (selfMessage.editable) {
selfMessage.edit(embed); selfMessage.edit(embed);

4
gifs/psh.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
name: 'psh',
embed_url: 'https://media.tenor.com/images/c88d8cdde582c01cb1396db7f01023eb/tenor.gif'
}

View File

@ -1,4 +1,4 @@
module.exports = { module.exports = {
name: 'unreal', name: 'unreal',
embed_url: 'https://media1.giphy.com/media/Q8hDlkzBrcrdz200GC/giphy.gif' embed_url: 'https://media.tenor.com/images/5ff1d1ef874429a81db93a15d49f208c/tenor.gif'
} }

25
package-lock.json generated
View File

@ -440,6 +440,11 @@
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="
}, },
"colors": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz",
"integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
},
"combined-stream": { "combined-stream": {
"version": "1.0.8", "version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
@ -1263,6 +1268,11 @@
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
}, },
"moment": {
"version": "2.29.1",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz",
"integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ=="
},
"ms": { "ms": {
"version": "2.1.3", "version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
@ -1505,6 +1515,11 @@
"glob": "^7.1.3" "glob": "^7.1.3"
} }
}, },
"roi": {
"version": "0.18.0",
"resolved": "https://registry.npmjs.org/roi/-/roi-0.18.0.tgz",
"integrity": "sha512-nRUZ5fJxnU9SUAc30Pul10O4LlWqdpXh1xunb3zsR7FA/NJ4yPNzL2ViTgAB7H70YqLsfGQRghvaff8ZcV/7Ug=="
},
"semver": { "semver": {
"version": "5.7.1", "version": "5.7.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
@ -1680,6 +1695,16 @@
} }
} }
}, },
"tenorjs": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/tenorjs/-/tenorjs-1.0.8.tgz",
"integrity": "sha512-rPN3AVJYK7M3fRrELV4YC5Iw+FyEy+BAgsj9iRg9KujATLlT0lPtnE3btC2fuWUA7evSwqFc1t17g/vPhL7EyA==",
"requires": {
"colors": "^1.3.3",
"moment": "^2.24.0",
"roi": "^0.18.0"
}
},
"term-size": { "term-size": {
"version": "2.2.1", "version": "2.2.1",
"resolved": "https://registry.npmjs.org/term-size/-/term-size-2.2.1.tgz", "resolved": "https://registry.npmjs.org/term-size/-/term-size-2.2.1.tgz",

View File

@ -8,7 +8,8 @@
"discord.js": "^12.5.3", "discord.js": "^12.5.3",
"dotenv": "^10.0.0", "dotenv": "^10.0.0",
"giphy-api": "^2.0.2", "giphy-api": "^2.0.2",
"nodemon": "^2.0.9" "nodemon": "^2.0.9",
"tenorjs": "^1.0.8"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^7.29.0" "eslint": "^7.29.0"