forked from Sayrix/Ticket-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
170 lines (143 loc) · 5.84 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
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
/*
Copyright 2023 Sayrix (github.com/Sayrix)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const fs = require("fs-extra");
const path = require("node:path");
const { Client, Collection, GatewayIntentBits } = require("discord.js");
// eslint-disable-next-line node/no-missing-require, node/no-unpublished-require
const { token } = require("./config/token.json");
const { QuickDB, MySQLDriver } = require("quick.db");
const jsonc = require("jsonc");
process.on("unhandledRejection", (reason, promise, a) => {
console.log(reason, promise, a);
});
process.stdout.write(`
\x1b[38;2;143;110;250m████████╗██╗ ██████╗██╗ ██╗███████╗████████╗ ██████╗ ██████╗ ████████╗
\x1b[38;2;157;101;254m╚══██╔══╝██║██╔════╝██║ ██╔╝██╔════╝╚══██╔══╝ ██╔══██╗██╔═══██╗╚══██╔══╝
\x1b[38;2;172;90;255m ██║ ██║██║ █████╔╝ █████╗ ██║ ██████╔╝██║ ██║ ██║
\x1b[38;2;188;76;255m ██║ ██║██║ ██╔═██╗ ██╔══╝ ██║ ██╔══██╗██║ ██║ ██║
\x1b[38;2;205;54;255m ██║ ██║╚██████╗██║ ██╗███████╗ ██║ ██████╔╝╚██████╔╝ ██║
\x1b[38;2;222;0;255m ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝\x1b[0m
https://github.com/Sayrix/ticket-bot
Connecting to Discord...
`);
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
// All variables stored in the client object
client.discord = require("discord.js");
client.config = jsonc.parse(
fs.readFileSync(path.join(__dirname, "config/config.jsonc"), "utf8")
);
let db = null;
if (client.config.mysql?.enabled) {
(async () => {
try {
require.resolve("mysql2");
} catch(e) {
console.error("mysql2 is not installed!\n\nPlease run \"npm i mysql2\" in the console!");
throw e.code;
}
const mysql = new MySQLDriver({
host: client.config.mysql?.host,
user: client.config.mysql?.user,
password: client.config.mysql?.password,
database: client.config.mysql?.database,
charset: "utf8mb4"
});
await mysql.connect();
db = new QuickDB({
driver: mysql,
table: client.config.mysql?.table ?? "json"
});
client.db = db;
})();
} else {
db = new QuickDB();
client.db = db;
};
client.locales = require(`./locales/${client.config.lang}.json`);
client.embeds = client.locales.embeds;
client.log = require("./utils/logs.js").log;
client.msToHm = function dhm(ms) {
const days = Math.floor(ms / (24 * 60 * 60 * 1000));
const daysms = ms % (24 * 60 * 60 * 1000);
const hours = Math.floor(daysms / (60 * 60 * 1000));
const hoursms = ms % (60 * 60 * 1000);
const minutes = Math.floor(hoursms / (60 * 1000));
const minutesms = ms % (60 * 1000);
const sec = Math.floor(minutesms / 1000);
if (days > 0) return `${days}d ${hours}h ${minutes}m ${sec}s`;
if (hours > 0) return `${hours}h ${minutes}m ${sec}s`;
if (minutes > 0) return `${minutes}m ${sec}s`;
if (sec > 0) return `${sec}s`;
return "0s";
};
// Command handler
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);
}
// Execute commands
client.on("interactionCreate", async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, client);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});
// Event handler
const eventsPath = path.join(__dirname, "events");
const eventFiles = fs
.readdirSync(eventsPath)
.filter((file) => file.endsWith(".js"));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args, client));
}
}
// Login the bot
client.login(token);
/*
Copyright 2023 Sayrix (github.com/Sayrix)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/