setting up gif Collection and getting bot functional

This commit is contained in:
= 2021-06-27 20:24:58 -04:00
parent abe1097c0c
commit b64d687802
8 changed files with 76 additions and 15 deletions

7
commands/reload-gifs.js Normal file
View File

@ -0,0 +1,7 @@
module.exports = {
name: "reload-gifs",
description: "Refresh the hardcoded gif library.",
execute(message, args) {
getGifFiles();
}
}

4
gifs/dab.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
name: "dab",
embed_url: "https://giphy.com/embed/lae7QSMFxEkkE"
}

4
gifs/nod.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
name: "nod",
embed_url: "https://tenor.com/view/smile-nod-yes-robert-redford-beard-gif-10489927"
}

4
gifs/psh.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
name: "psh",
embed_url: "https://tenor.com/view/ed-bassmaster-youtuber-youtube-influencer-psh-gif-10556767"
}

4
gifs/shedsalive.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
name: "shedsalive",
embed_url: "https://giphy.com/embed/glL1yvxJ4KvYc"
}

4
gifs/shedsdead.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
name: "shedsdead",
embed_url: "http://voidf1sh.me/shedsdead.gif"
}

3
gifs/template Normal file
View File

@ -0,0 +1,3 @@
module.exports = {
}

View File

@ -6,11 +6,14 @@ dotenv.config();
const Discord = require('discord.js'); const Discord = require('discord.js');
const client = new Discord.Client(); const client = new Discord.Client();
client.commands = new Discord.Collection(); client.commands = new Discord.Collection();
client.gifs = new Discord.Collection();
const debug = true; const debug = true;
const config = require('./config.json'); // const config = require('./config.json');
const { prefix, serverID } = require('./config.json'); const { prefix } = require('./config.json');
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
const gifFiles = fs.readdirSync('./gifs').filter(file => file.endsWith('.js'));
// const owner = process.env.ownerID; // const owner = process.env.ownerID;
const giphy = require('giphy-api')(process.env.giphyAPIKey);
function getCommandFiles() { function getCommandFiles() {
for (const file of commandFiles) { for (const file of commandFiles) {
@ -20,6 +23,14 @@ function getCommandFiles() {
if (debug) console.log(client.commands); if (debug) console.log(client.commands);
} }
function getGifFiles() {
for (const file of gifFiles) {
const gif = require(`./gifs/${file}`);
client.gifs.set(gif.name, gif);
}
if (debug) console.log(client.gifs);
}
function extCheck(content) { function extCheck(content) {
const lastFour = content.slice(-4); const lastFour = content.slice(-4);
switch (lastFour) { switch (lastFour) {
@ -34,18 +45,19 @@ function extCheck(content) {
client.once('ready', () => { client.once('ready', () => {
console.log('Ready'); console.log('Ready');
client.guilds.fetch(serverID) client.user.setActivity('Nod Simulator 2021', { type: 'PLAYING' }).then().catch(console.error);
.then(guild => client.user.setActivity('Nod Simulator 2021', {type: "PLAYING"}))
.catch(console.error);
getCommandFiles(); getCommandFiles();
getGifFiles();
}); });
client.login(process.env.TOKEN); client.login(process.env.TOKEN);
client.on('message', message => { client.on('message', message => {
if (debug) console.log(extCheck(message.content)); const ext = extCheck(message.content);
if (!message.content.startsWith(prefix) || message.author.bot || !extCheck(message.content)) return; if (debug) console.log(ext);
if ((!message.content.startsWith(prefix) && ext == false) || message.author.bot) return;
if (message.content.startsWith(prefix)) {
const args = message.content.slice(prefix.length).trim().split(/ +/); const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase(); const command = args.shift().toLowerCase();
@ -55,6 +67,25 @@ client.on('message', message => {
client.commands.get(command).execute(message, args); client.commands.get(command).execute(message, args);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
message.reply('There was an error trying to execute that command.'); message.channel.send('There was an error trying to execute that command.');
}
}
if (ext == 'gif') {
const query = message.content.slice(0, -4);
if (debug) console.log(query);
if (!client.gifs.has(query)) {
giphy.search(query, (err, res) => {
if (res.data[0] != undefined) {
message.channel.send(res.data[0].embed_url).then().catch(console.error);
} else {
message.channel.send('I was unable to find a gif of ' + query);
}
if (err) console.error(err);
});
} else {
message.channel.send(client.gifs.get(query).embed_url);
}
} }
}); });