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 +++++++++++
 4 files changed, 164 insertions(+)
 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

(limited to 'src/commands')

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
-- 
cgit v1.2.3