-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (92 loc) · 3.62 KB
/
index.js
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
'use strict';
require('dotenv').config();
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.commands = new Discord.Collection();
const botCommands = require('./commands');
const { DiscordUtils, CronJobUtils, LeagueOfLegendsUtils } = require('./utils');
Object.keys(botCommands).forEach(key => {
const botCommand = { ...botCommands[key] };
if (botCommand.availableCommands) {
const subCommands = new Discord.Collection();
Object.keys(botCommand.availableCommands).forEach(subKey => {
subCommands.set(botCommand.availableCommands[subKey].name, botCommand.availableCommands[subKey]);
});
botCommand.availableCommands = subCommands;
}
bot.commands.set(botCommand.name, botCommand);
});
const TOKEN = process.env.TOKEN_DISCORD;
bot.login(TOKEN).catch(err => {
console.log('Token ' + TOKEN + ' invalid.');
});
bot.on('ready', () => {
console.info(`Logged in as ${bot.user.tag}!`);
const launchAlmanax = () => botCommands.Dofus.availableCommands.Almanax.execute(DiscordUtils.getChannel(bot, 'dofus-almanax'));
CronJobUtils.scheduleCronEveryDayAt(0, 0, 0, launchAlmanax);
const launchTracking = async () => {
try {
const trackedPlayers = await LeagueOfLegendsUtils.getTrackedPlayer();
await Promise.all(trackedPlayers.map(async (summonerId) => {
const lastGame = await LeagueOfLegendsUtils.getLastGameBySummonerId(summonerId);
const lastGameLocalId = await LeagueOfLegendsUtils.getLastGameLocal(summonerId);
if (lastGame.gameId !== lastGameLocalId)
botCommands.LeagueOfLegends.availableCommands.LastGame.execute(DiscordUtils.getChannel(bot, 'league-of-legends'), [lastGame.summonerName]);
}));
}
catch (e) {
console.error(e);
}
};
CronJobUtils.scheduleCronEveryXSeconds(30, launchTracking);
});
function processInput(msg, args, availableCommands, parentCommand = null) {
const commandName = args.length > 0 ? args.shift().toLowerCase() : null;
if (!availableCommands.has(commandName) && !parentCommand) return;
if (!commandName || !availableCommands.has(commandName)) {
const embed = new Discord.MessageEmbed()
.setTitle(`Commandes disponible avec \`${parentCommand}\``)
.setDescription('Voici les commandes proposées :')
.addFields(
Array.from(availableCommands.keys()).map(key => {
const command = availableCommands.get(key);
let args = command.arguments;
if (args && Array.isArray(args)) {
args = ` <${args[0]}_1> <${args[0]}_2> <${args[0]}_3> ...`;
}
else if (args) {
args = ` <${args}>`;
}
return {
name: `${key} \`.pdb ${parentCommand} ${key}${args ?? ''}\``,
value: command.description,
inline: true
};
})
)
.setTimestamp()
.setFooter('Pandisbot');
msg.channel.send(embed);
}
else {
const command = availableCommands.get(commandName);
if (command.execute) {
try {
command.execute(msg, args);
} catch (error) {
console.error(`Error: ${error}`);
msg.reply('il y a une erreur pendant l\'exécution de la commande.');
}
}
else {
return processInput(msg, args, command.availableCommands, commandName);
}
}
msg.delete();
}
bot.on('message', msg => {
const msgSplitted = msg.content.split(/ +/);
if (msgSplitted.shift() !== '.pdb') return;
if (msgSplitted.length === 0) msgSplitted.push('help');
return processInput(msg, msgSplitted, bot.commands);
});