-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (91 loc) · 3.32 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
110
111
112
113
var db = require('./db.js');
const util = require('util');
// node native promisify
const query = util.promisify(db.query).bind(db);
const fs = require('node:fs');
const path = require('node:path');
const { ActivityType, Client, Collection, GatewayIntentBits, ActionRowBuilder, ButtonBuilder, ButtonStyle, Events } = require('discord.js');
const { token } = require('./config.json');
const of = require("./preset/otherfunctions.js");
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
}
client.once(Events.ClientReady, () => {
console.log('Bot is ready on Discord!');
client.user.setActivity("for Coc's", { type: ActivityType.Watching });
const scheduled = require('./scheduled.js');
scheduled.start(client);
});
/*client.on('guildMemberAdd', member => {
member.guild.channels.get('channelID').send("CoCBot joined the Server, try /commands from this bot to see the commands");
deploy();
});*/
client.on('disconnect', (event) => {
console.error(`Disconnected: ${event.reason} (${event.code})`);
// Attempt to reconnect after delay
setTimeout(() => {
console.log(`Trying to connect again...`);
client.login(token);
}, 5000);
});
client.on('interactionCreate', async interaction => {
if (interaction.isChatInputCommand()) {
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
//await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
} else if (interaction.isAutocomplete()) {
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.autocomplete(interaction);
} catch (error) {
console.error(error);
}
}
else if (interaction.isButton()) {
//i use the underscore here to get variables through the button and execute the correct thing
if (interaction.customId.includes('_')) {
const customcommand = interaction.customId.split('_')[0];
if (customcommand == "warlog") {
const date = interaction.customId.split('_')[1];
of.warLog(interaction, date);
}
else if (customcommand == "cwllog") {
const warId = interaction.customId.split('_')[1];
if (warId == "all") {
const season = interaction.customId.split('_')[2];
of.cwlLog(interaction, season);
}
else {
of.cwlLogDay(interaction, warId);
}
}
//here possible other ones with underscore
}
else {
// Code to handle buttons without underscore at some point if there should come more buttons ^^
}
}
});
console.log(`Trying to connect`);
client.login(token);
async function keepDatabaseAlive() {
const rows = await query('SELECT 1;');
console.log("Kept Database alive");
}
//required for my own database, if you dont need this, you can delete it.should have no impact on performance
setInterval(keepDatabaseAlive, 1 * 60 * 60 * 1000);