Code Refactoring

* Adds Config for commands
* Moved Casual Messages to language.json
This commit is contained in:
Indrajith K L
2021-03-27 21:57:16 +05:30
parent 6048206733
commit e3dba82cf3
9 changed files with 259 additions and 223 deletions

28
src/commands/jokes.js Normal file
View File

@@ -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);
}
}

75
src/commands/loadout.js Normal file
View File

@@ -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);
}
}

44
src/commands/play.js Normal file
View File

@@ -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);
}
}

17
src/commands/stop.js Normal file
View File

@@ -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);
}
}