-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
100 lines (88 loc) · 3.75 KB
/
bot.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
require("dotenv").config();
const { Telegraf, Format } = require('telegraf');
const fs = require('fs');
const path = require('path');
const {loadEvents} = require('./utilities/events.js');
const {sendError} = require("./utilities/send_error.js");
//let events_pending = require(`./events.js`);
//const { message } = require('telegraf/filters');
const bot = new Telegraf(process.env.BOT_TOKEN);
let commands = new Map();
const foldersPathCommands = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPathCommands);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPathCommands, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));//This next step is how you'll dynamically retrieve all your newly created command files. Add this below your client.commands line:
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
let automations = new Map();
const foldersPathAutomations = path.join(__dirname, 'automations');
const automationsFolder = fs.readdirSync(foldersPathAutomations);
for (const folder of automationsFolder) {
const automationsPath = path.join(foldersPathAutomations, folder);
const automationFiles = fs.readdirSync(automationsPath).filter(file => file.endsWith('.js'));//This next step is how you'll dynamically retrieve all your newly created command files. Add this below your client.commands line:
for (const file of automationFiles) {
const filePath = path.join(automationsPath, file);
const automation = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in automation && 'execute' in automation) {
automations.set(automation.data.name, automation);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
loadEvents();
//check if it's a command or not
bot.use(async (ctx, next) => {
console.log('use')
await next();
// const command = commands.get('todo');
// if(command) {
// try{
// await command.execute(ctx);
// }catch(e){
// console.error(e);
// await ctx.telegram.sendMessage(process.env.DEBUG_ADMIN, e);
// }
// }else{
// await next();
// }
// try {
// await command.execute(interaction);
// } catch (error) {
// console.error(error);
// if (interaction.replied || interaction.deferred) {
// await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
// } else {
// await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
// }
// }
});
bot.on("channel_post", async (ctx) => {
console.log('channel post')
await automations.get('summary_on_update').execute(ctx);
})
commands.forEach((command, trigger) => {
bot.command(trigger, async (ctx) => {
console.log("command: "+trigger);
try{
await command.execute(ctx);
}catch(e){
sendError(ctx, e);
}
});
})
bot.launch().then(() => {
console.log('Il bot è online!');//idk perché non va
});
console.log('cia')