aboutsummaryrefslogtreecommitdiff
path: root/src/main.js
blob: f99bd5a2b91045733d0ebc83cf4c34ebe67b5044 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
require('dotenv').config();
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 client = new Client();
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);
        }

    } else {
        const replyMessage = parseCasualMessage(message);
        if (replyMessage) {
            message.reply(replyMessage);
        } else {

        }
    }
});

client.on('ready', () => {
    console.log("BOT is now LIVE");
})

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 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 parseCMD(message) {
    const [CMD_NAME, ...args] = message.content
        .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);
        }
    });
}



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

client.login(process.env.LUL_BOT_TKN)
    .then(() => {
        intervalJokes();
    })
    .catch((error) => {
        console.error("BOT Login Failed ", error);
    });