From e3dba82cf3cdbdf5c0aad3d308013695a2074ecf Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sat, 27 Mar 2021 21:57:16 +0530 Subject: Code Refactoring * Adds Config for commands * Moved Casual Messages to language.json --- src/commands/jokes.js | 28 +++++ src/commands/loadout.js | 75 ++++++++++++ src/commands/play.js | 44 +++++++ src/commands/stop.js | 17 +++ src/commons/confusion.js | 22 ++++ src/commons/jokes.js | 7 -- src/configs/commands.config.json | 24 ++++ src/language/language.json | 15 +++ src/main.js | 250 ++++++--------------------------------- 9 files changed, 259 insertions(+), 223 deletions(-) create mode 100644 src/commands/jokes.js create mode 100644 src/commands/loadout.js create mode 100644 src/commands/play.js create mode 100644 src/commands/stop.js create mode 100644 src/commons/confusion.js delete mode 100644 src/commons/jokes.js create mode 100644 src/configs/commands.config.json create mode 100644 src/language/language.json (limited to 'src') diff --git a/src/commands/jokes.js b/src/commands/jokes.js new file mode 100644 index 0000000..578e51b --- /dev/null +++ b/src/commands/jokes.js @@ -0,0 +1,28 @@ +const fetch = require('node-fetch'); + +function randomJokes(message) { + message.channel.startTyping(); + getJokes().then(response => { + message.channel.stopTyping(); + if (response) { + message.channel.startTyping(); + message.reply(response.setup); + setTimeout(() => { + message.channel.stopTyping(); + message.reply(`||${response.punchline}||`); + }, 5000); + } + }); +} + +async function getJokes() { + return fetch(`https://official-joke-api.appspot.com/jokes/random`) + .then(res => res.json()) +} + + +module.exports = { + execute(client,message, args) { + randomJokes(message); + } +} \ No newline at end of file diff --git a/src/commands/loadout.js b/src/commands/loadout.js new file mode 100644 index 0000000..30ca276 --- /dev/null +++ b/src/commands/loadout.js @@ -0,0 +1,75 @@ +const Airtable = require('airtable'); +const {MessageEmbed} = require('discord.js'); +Airtable.configure({ + endpointUrl: 'https://api.airtable.com', + apiKey: process.env.AIRTABLE_KEY +}); +const base = Airtable.base('appppieGLc8loZp5H'); +const AirTableFields = { + WEAPON_TYPE: 'cod_weapon_type', + WEAPON_NAME: 'cod_weapon_name', + MATCH_TYPE: 'cod_match_type', + ATTACHMENTS: 'cod_weapon_attachments' +}; + +async function getCodMLoadOut(message, args) { + const codUserName = args[0]; + if (!codUserName) return; + base('cod_loadout').select({ + maxRecords: 2, + view: "Grid view", + filterByFormula: `({cod_username} = '${args[0]}')` + }).eachPage((records, fetchNextPage) => { + if (records && records.length > 0) { + records.forEach(async (record) => { + const weaponType = await getWeaponType(record.get(AirTableFields.WEAPON_TYPE)); + const weaponName = await getWeaponName(record.get(AirTableFields.WEAPON_NAME)); + const loadOutMessage = new MessageEmbed() + .setTitle(`Loadout of ${codUserName} : ${record.get(AirTableFields.MATCH_TYPE)}`) + .addField('Weapon Name', weaponName, true) + .addField('Weapon Type', weaponType, true) + .addField('Attachments', record.get(AirTableFields.ATTACHMENTS), true) + .setColor("RANDOM"); + message.channel.send(loadOutMessage); + }); + fetchNextPage(); + } else { + message.channel.send(`No Loadout found for ***${codUserName}***`); + } + + }, function done(err) { + if (err) { console.error(err); return; } + }); +} + +async function getWeaponType(weaponType) { + return new Promise((resolve, reject) => { + base('Weapon Types').find(weaponType, (error, record) => { + if (error) { + console.log(error); + reject(); + } else { + resolve(record.get(AirTableFields.WEAPON_TYPE)); + } + }); + }) +} + +async function getWeaponName(weaponName) { + return new Promise((resolve, reject) => { + base('Weapons').find(weaponName, (error, record) => { + if (error) { + console.log(error); + reject(); + } else { + resolve(record.get(AirTableFields.WEAPON_NAME)); + } + }); + }) +} + +module.exports = { + execute(client,message, args) { + getCodMLoadOut(message,args); + } +} \ No newline at end of file diff --git a/src/commands/play.js b/src/commands/play.js new file mode 100644 index 0000000..24e4fcd --- /dev/null +++ b/src/commands/play.js @@ -0,0 +1,44 @@ +const ytdl = require('ytdl-core'); +const ytSearch = require('yt-search'); +async function playMusik(client, message, args) { + const voiceChannel = message.member.voice.channel; + if (!voiceChannel) { + return message.channel.send("Join a voice channel to Play Music"); + } + const permission = voiceChannel.permissionsFor(message.client.user); + if (!permission.has('CONNECT') || !permission.has('SPEAK')) { + return message.channel.send("You don't have the permission to play music. ๐Ÿ˜ฅ"); + } + + if (!args.length) { + return message.channel.send("Please pass something to play as second argument"); + } + + const connection = await voiceChannel.join(); + const video = await searchVideo(args.join(' ')); + + if (video) { + const youtubeStream = ytdl(video.url, { filter: 'audioonly' }); + client.user.setPresence({ activity: { name: `${video.title}`, type: 'LISTENING' } }); + connection.play(youtubeStream, { seek: 0, volume: 1 }) + .on('finish', () => { + voiceChannel.leave(); + client.user.setPresence({ activity: { name: ` ` } }); + }); + await message.reply(`Now Playing ${video.title}...`); + } else { + message.channel.send("No music found to play for you mate. Try again! ๐Ÿ‘"); + } +} + +async function searchVideo(query) { + const searchResult = await ytSearch(query); + return (searchResult.videos.length > 1) ? searchResult.videos[0] : null; +} + + +module.exports = { + execute(client,message, args) { + playMusik(client,message,args); + } +} \ No newline at end of file diff --git a/src/commands/stop.js b/src/commands/stop.js new file mode 100644 index 0000000..0c5e3df --- /dev/null +++ b/src/commands/stop.js @@ -0,0 +1,17 @@ +async function stopMusik(client,message) { + const voiceChannel = message.member.voice.channel; + + if (!voiceChannel) { + return message.channel.send("Join a voice channel to Execute this command"); + } + + await voiceChannel.leave(); + await message.channel.send("Stoping Music... Baye Baye.... ๐Ÿ˜…"); + client.user.setPresence({ activity: { name: `COMMANDS`, type: 'LISTENING' } }); +} + +module.exports = { + execute(client,message, args) { + stopMusik(client, message); + } +} \ No newline at end of file diff --git a/src/commons/confusion.js b/src/commons/confusion.js new file mode 100644 index 0000000..7a5d9b8 --- /dev/null +++ b/src/commons/confusion.js @@ -0,0 +1,22 @@ +const {MessageEmbed} = require('discord.js'); +const fetch = require('node-fetch'); +function generateConfusionGif(message) { + message.channel.startTyping(); + const embed = new MessageEmbed() + .setDescription(`I dont understand what you are saying <@${process.env.KLIAS_TAG}> do you know what this guy is asking?`) + .setColor("RANDOM"); + const randomIndex = Math.floor(Math.random() * 49); + fetch(`https://api.tenor.com/v1/random?key=${process.env.TENOR_TOKEN}&q=I%20dont%20understand&limit=50`) + .then(res => res.json()) + .then(response => { + embed.setImage(response.results[randomIndex].media[0].gif.url); + message.channel.stopTyping(); + message.channel.send(embed); + }); +} + +module.exports = { + execute(message) { + generateConfusionGif(message); + } +} \ No newline at end of file diff --git a/src/commons/jokes.js b/src/commons/jokes.js deleted file mode 100644 index 261cacb..0000000 --- a/src/commons/jokes.js +++ /dev/null @@ -1,7 +0,0 @@ -module.exports = { - name: "jokes", - description: "Only bad jokes here", - execute() { - - } -} \ No newline at end of file diff --git a/src/configs/commands.config.json b/src/configs/commands.config.json new file mode 100644 index 0000000..1865374 --- /dev/null +++ b/src/configs/commands.config.json @@ -0,0 +1,24 @@ +{ + "commands" :[ + { + "name": "jokes", + "description": "Bad Jokes Command", + "file": "./commands/jokes.js" + }, + { + "name": "play", + "description": "Play Music", + "file": "./commands/play.js" + }, + { + "name": "stop", + "description": "Stop Music", + "file": "./commands/stop.js" + }, + { + "name": "loadout", + "description": "Get CoD Mobile User Loadout", + "file": "./commands/loadout.js" + } + ] +} \ No newline at end of file diff --git a/src/language/language.json b/src/language/language.json new file mode 100644 index 0000000..5912b78 --- /dev/null +++ b/src/language/language.json @@ -0,0 +1,15 @@ +{ + "hi": "Hey ๐Ÿ™‹โ€โ™‚๏ธ. How Are You?", + "hello": "Hey ๐Ÿ™‹โ€โ™‚๏ธ. How Are You?", + "เดนเดฒเต‹": "เดนเดฒเตเดฒเดพ.... เด‡เดคเดพเดฐเดพ.. เดธเตเด–เด‚ เดคเดจเตเดจเต†?", + "เดนเดฟ": "เดนเดฒเตเดฒเดพ.... เด‡เดคเดพเดฐเดพ.. เดธเตเด–เด‚ เดคเดจเตเดจเต†?", + "เดŽเดจเตเดคเตŠเด•เตเด•เต† เด‰เดฃเตเดŸเต เดตเดฟเดถเต‡เดทเด‚": "เด…เด™เตเด™เดฟเดจเต† เดชเต‹เดฃเต...๐Ÿคทโ€โ™‚๏ธ เดธเตเด–เด™เตเด™เดณเตŠเด•เตเด•เต† เดคเดจเตเดจเต†?", + "enthokke und vishesham": "เด…เด™เตเด™เดฟเดจเต† เดชเต‹เดฃเต...๐Ÿคทโ€โ™‚๏ธ เดธเตเด–เด™เตเด™เดณเตŠเด•เตเด•เต† เดคเดจเตเดจเต†?", + "เดฎเดจเตเดธเตเดธเดจเดฒเตเดฒเต‡ เดชเตเดณเตเดณเต†": "เด‡เดฑเดšเตเดšเดฟเดฏเตเด‚ เดฌเดฑเต‹เดŸเตเดŸเดฏเตเด‚ เดตเต‡เดฃเดพเดฏเดฟเดฐเดฟเด•เตเด•เตเด‚... ๐Ÿคฃ๐Ÿคฃ๐Ÿคฃ", + "manusanalle pulle": "เด‡เดฑเดšเตเดšเดฟเดฏเตเด‚ เดฌเดฑเต‹เดŸเตเดŸเดฏเตเด‚ เดตเต‡เดฃเดพเดฏเดฟเดฐเดฟเด•เตเด•เตเด‚... ๐Ÿคฃ๐Ÿคฃ๐Ÿคฃ", + "เดจเต€ เด†เดฐเดพ": "เดคเดพเตป เด†เดฐเดพเดฃเต†เดจเตเดจเต เดคเดจเดฟเด•เตเด•เต เด…เดฑเดฟเดฏเดพเดจเตเดฎเต‡เดฒเต†เด™เตเด•เดฟเตฝ เดคเดพเตป เดŽเดจเตเดจเต‹เดŸเต เดšเต‹เดฏเตเด•เตเด•เต เดคเดพเตป เด†เดฐเดพเดฃเต†เดจเตเดจเต??? เดคเดจเดฟเด•เตเด•เต เดžเดพเตป เดชเดฑเดžเตเดžเต เดคเดฐเดพเด‚ เดคเดพเตป เด†เดฐเดพเดฃเต†เดจเตเดจเต... ๐Ÿคช๐Ÿคช", + "nee araa": "เดคเดพเตป เด†เดฐเดพเดฃเต†เดจเตเดจเต เดคเดจเดฟเด•เตเด•เต เด…เดฑเดฟเดฏเดพเดจเตเดฎเต‡เดฒเต†เด™เตเด•เดฟเตฝ เดคเดพเตป เดŽเดจเตเดจเต‹เดŸเต เดšเต‹เดฏเตเด•เตเด•เต เดคเดพเตป เด†เดฐเดพเดฃเต†เดจเตเดจเต??? เดคเดจเดฟเด•เตเด•เต เดžเดพเตป เดชเดฑเดžเตเดžเต เดคเดฐเดพเด‚ เดคเดพเตป เด†เดฐเดพเดฃเต†เดจเตเดจเต... ๐Ÿคช๐Ÿคช", + "kya banana hei": "Thumarah banana kaha hei...๐Ÿคฃ๐Ÿคฃ", + "เด•เตเดฏเดพ เดฌเดจเดพเดจ เดนเต‡เดฏเต": "เดคเตเดฎเดฐเดนเต เดฌเดจเดพเดจ เด•เดนเดพ เดนเต‡เดฏเต...๐Ÿคฃ๐Ÿคฃ", + "good night": "Good Night ๐ŸŒ‰. See you tomorrow. ๐Ÿ‘‹๐Ÿ‘‹" +} \ No newline at end of file diff --git a/src/main.js b/src/main.js index f99bd5a..f53941b 100644 --- a/src/main.js +++ b/src/main.js @@ -1,85 +1,60 @@ require('dotenv').config(); +const fs = require('fs'); const { Client, MessageEmbed, Collection } = require('discord.js'); const fetch = require('node-fetch'); -const ytdl = require('ytdl-core'); -const ytSearch = require('yt-search'); -const Airtable = require('airtable'); -Airtable.configure({ - endpointUrl: 'https://api.airtable.com', - apiKey: process.env.AIRTABLE_KEY -}); -const base = Airtable.base('appppieGLc8loZp5H'); - +const confusion = require('./commons/confusion'); const client = new Client(); +client.commands = new Collection(); +let casualMessages = {}; const CMD_PREFIX = "!"; const dayToMilliS = 60 * 60 * 24 * 1000; -const AirTableFields = { - WEAPON_TYPE: 'cod_weapon_type', - WEAPON_NAME: 'cod_weapon_name', - MATCH_TYPE: 'cod_match_type', - ATTACHMENTS: 'cod_weapon_attachments' -}; client.on('message', (message) => { if (message.author.bot) return; if (message.content.startsWith(CMD_PREFIX)) { const commandReply = parseCMD(message); - if (commandReply && (typeof commandReply != "object")) { - message.reply(commandReply); - } else if (!commandReply) { - generateConfusionGif(message); + if (!commandReply) { + confusion.execute(message); } - } else { const replyMessage = parseCasualMessage(message); if (replyMessage) { message.reply(replyMessage); - } else { - } } }); client.on('ready', () => { console.log("BOT is now LIVE"); + initConfig(); + initLanguages(); }) -function parseCasualMessage(message) { - const { content } = message; - switch (content.trim().toLowerCase()) { - case 'hello': - case 'hi': - return `Hey ${message.author} ๐Ÿ™‹โ€โ™‚๏ธ. How Are You?`; - case 'เดนเดฒเต‹': - case 'เดนเดฟ': - return `เดนเดฒเตเดฒเดพ.... เด‡เดคเดพเดฐเดพ ${message.author}เดฏเต‹... เดธเตเด–เด‚ เดคเดจเตเดจเต†?`; - case 'เดŽเดจเตเดคเตŠเด•เตเด•เต† เด‰เดฃเตเดŸเต เดตเดฟเดถเต‡เดทเด‚': - case 'enthokke und vishesham': - case 'enthokkeyund vishesham': return `เด…เด™เตเด™เดฟเดจเต† เดชเต‹เดฃเต...๐Ÿคทโ€โ™‚๏ธ เดธเตเด–เด™เตเด™เดณเตŠเด•เตเด•เต† เดคเดจเตเดจเต†?`; - case 'เดฎเดจเตเดธเตเดธเดจเดฒเตเดฒเต‡ เดชเตเดณเตเดณเต†': - case 'manusanalle pulle': return `เด‡เดฑเดšเตเดšเดฟเดฏเตเด‚ เดฌเดฑเต‹เดŸเตเดŸเดฏเตเด‚ เดตเต‡เดฃเดพเดฏเดฟเดฐเดฟเด•เตเด•เตเด‚... ๐Ÿคฃ๐Ÿคฃ๐Ÿคฃ`; - case 'เดจเต€ เด†เดฐเดพ': - case 'nee araa': return `เดคเดพเตป เด†เดฐเดพเดฃเต†เดจเตเดจเต เดคเดจเดฟเด•เตเด•เต เด…เดฑเดฟเดฏเดพเดจเตเดฎเต‡เดฒเต†เด™เตเด•เดฟเตฝ เดคเดพเตป เดŽเดจเตเดจเต‹เดŸเต เดšเต‹เดฏเตเด•เตเด•เต เดคเดพเตป เด†เดฐเดพเดฃเต†เดจเตเดจเต??? เดคเดจเดฟเด•เตเด•เต เดžเดพเตป เดชเดฑเดžเตเดžเต เดคเดฐเดพเด‚ เดคเดพเตป เด†เดฐเดพเดฃเต†เดจเตเดจเต... ๐Ÿคช๐Ÿคช`; - case 'kya banana hei': return `Thumarah banana kaha hei...๐Ÿคฃ๐Ÿคฃ`; - case 'เด•เตเดฏเดพ เดฌเดจเดพเดจ เดนเต‡เดฏเต': return 'เดคเตเดฎเดฐเดนเต เดฌเดจเดพเดจ เด•เดนเดพ เดนเต‡เดฏเต...๐Ÿคฃ๐Ÿคฃ'; - case 'good night': return null; - default: return null; - } +function initConfig() { + fs.readFile('src/configs/commands.config.json', 'utf8', (error, data) => { + if (error) throw error; + const { commands } = JSON.parse(data); + if (commands) { + commands.forEach(command => { + const commandFile = require(command.file); + client.commands.set(command.name, commandFile); + }); + } + }) +} + +function initLanguages() { + fs.readFile('src/language/language.json', 'utf8', (error, data) => { + if (error) throw error; + casualMessages = JSON.parse(data); + }); } -function generateConfusionGif(message) { - message.channel.startTyping(); - const embed = new MessageEmbed() - .setDescription(`I dont understand what you are saying <@${process.env.KLIAS_TAG}> do you know what this guy is asking?`) - .setColor("RANDOM"); - const randomIndex = Math.floor(Math.random() * 49); - fetch(`https://api.tenor.com/v1/random?key=${process.env.TENOR_TOKEN}&q=I%20dont%20understand&limit=50`) - .then(res => res.json()) - .then(response => { - embed.setImage(response.results[randomIndex].media[0].gif.url); - message.channel.stopTyping(); - message.channel.send(embed); - }); +function parseCasualMessage(message) { + const { content } = message; + const parsedMessage = content.trim().toLowerCase(); + if (!casualMessages.hasOwnProperty(parsedMessage)) return null; + return casualMessages[parsedMessage]; } function parseCMD(message) { @@ -87,172 +62,15 @@ function parseCMD(message) { .trim() .substring(CMD_PREFIX.length) .split(/\s+/); - switch (CMD_NAME) { - case 'jokes': randomJokes(message); return {}; - case 'play': playMusik(message, args); return {}; - case 'stop': stopMusik(message); return {}; - case 'loadout': getCodMLoadOut(message, args); return {}; - default: return null; - } -} - -function randomJokes(message) { - message.channel.startTyping(); - getJokes().then(response => { - message.channel.stopTyping(); - if (response) { - message.channel.startTyping(); - message.reply(response.setup); - setTimeout(() => { - message.channel.stopTyping(); - message.reply(`||${response.punchline}||`); - }, 5000); - } - }); -} - -async function getJokes() { - return fetch(`https://official-joke-api.appspot.com/jokes/random`) - .then(res => res.json()) -} - -function intervalJokes() { - getJokesChannel().then(channel => { - if (channel) { - // channel.bulkDelete(100); - setInterval(jokeOfTheDay.bind(this, channel), dayToMilliS); - } - }); -} - -function jokeOfTheDay(channel) { - getJokes().then(response => { - if (response) { - const joke = new MessageEmbed() - .setTitle(`**${response.setup}**`) - .setDescription(`||${response.punchline}||`) - .setFooter('https://github.com/15Dkatz/official_joke_api') - .setColor("RANDOM"); - channel.send(joke); - } - }); -} + if (!client.commands.has(CMD_NAME)) return false; + client.commands.get(CMD_NAME).execute(client, message, args); -async function getJokesChannel() { - const channel = await client.channels.fetch(process.env.JOKES_CHANNEL); - return channel; -} - -async function playMusik(message, args) { - const voiceChannel = message.member.voice.channel; - if (!voiceChannel) { - return message.channel.send("Join a voice channel to Play Music"); - } - const permission = voiceChannel.permissionsFor(message.client.user); - if (!permission.has('CONNECT') || !permission.has('SPEAK')) { - return message.channel.send("You don't have the permission to play music. ๐Ÿ˜ฅ"); - } - - if (!args.length) { - return message.channel.send("Please pass something to play as second argument"); - } - - const connection = await voiceChannel.join(); - const video = await searchVideo(args.join(' ')); - - if (video) { - const youtubeStream = ytdl(video.url, { filter: 'audioonly' }); - client.user.setPresence({ activity: { name: `${video.title}`, type: 'LISTENING' } }); - connection.play(youtubeStream, { seek: 0, volume: 1 }) - .on('finish', () => { - voiceChannel.leave(); - client.user.setPresence({ activity: { name: ` ` } }); - }); - await message.reply(`Now Playing ${video.title}...`); - } else { - message.channel.send("No music found to play for you mate. Try again! ๐Ÿ‘"); - } -} - -async function stopMusik(message) { - const voiceChannel = message.member.voice.channel; - - if (!voiceChannel) { - return message.channel.send("Join a voice channel to Execute this command"); - } - - await voiceChannel.leave(); - await message.channel.send("Stoping Music... Baye Baye.... ๐Ÿ˜…"); - client.user.setPresence({ activity: { name: `COMMANDS`, type: 'LISTENING' } }); -} - -async function searchVideo(query) { - const searchResult = await ytSearch(query); - return (searchResult.videos.length > 1) ? searchResult.videos[0] : null; -} - -async function getCodMLoadOut(message, args) { - const codUserName = args[0]; - if (!codUserName) return; - base('cod_loadout').select({ - maxRecords: 2, - view: "Grid view", - filterByFormula: `({cod_username} = '${args[0]}')` - }).eachPage((records, fetchNextPage) => { - if (records && records.length > 0) { - records.forEach(async (record) => { - const weaponType = await getWeaponType(record.get(AirTableFields.WEAPON_TYPE)); - const weaponName = await getWeaponName(record.get(AirTableFields.WEAPON_NAME)); - const loadOutMessage = new MessageEmbed() - .setTitle(`Loadout of ${codUserName} : ${record.get(AirTableFields.MATCH_TYPE)}`) - .addField('Weapon Name', weaponName, true) - .addField('Weapon Type', weaponType, true) - .addField('Attachments', record.get(AirTableFields.ATTACHMENTS), true) - .setColor("RANDOM"); - message.channel.send(loadOutMessage); - }); - fetchNextPage(); - } else { - message.channel.send(`No Loadout found for ***${codUserName}***`); - } - - }, function done(err) { - if (err) { console.error(err); return; } - }); -} - -async function getWeaponType(weaponType) { - return new Promise((resolve, reject) => { - base('Weapon Types').find(weaponType, (error, record) => { - if (error) { - console.log(error); - reject(); - } else { - resolve(record.get(AirTableFields.WEAPON_TYPE)); - } - }); - }) -} - -async function getWeaponName(weaponName) { - return new Promise((resolve, reject) => { - base('Weapons').find(weaponName, (error, record) => { - if (error) { - console.log(error); - reject(); - } else { - resolve(record.get(AirTableFields.WEAPON_NAME)); - } - }); - }) + return true; } client.login(process.env.LUL_BOT_TKN) - .then(() => { - intervalJokes(); - }) .catch((error) => { console.error("BOT Login Failed ", error); }); -- cgit v1.2.3