-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
197 lines (179 loc) · 5.34 KB
/
app.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
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
const fs = require("fs");
const { Client, Collection, Intents } = require("discord.js");
const http = require("http");
http
.createServer(function (req, res) {
res.writeHead(200, { "Content-Type": "text/plain" });
res.write("Server is up!");
res.end();
})
.listen(8080);
const Sentry = require("@sentry/node");
// or use es6 import statements
// import * as Sentry from '@sentry/node';
const Tracing = require("@sentry/tracing");
// or use es6 import statements
// import * as Tracing from '@sentry/tracing';
Sentry.init({
dsn: "https://[email protected]/6474117",
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MEMBERS,
],
});
client.commands = new Map();
const approvedGuilds = process.env.SERVER_ID.split(",");
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}
const getApp = (guildId) => {
const app = client.api.applications(client.user.id);
if (guildId) {
app.guilds(guildId);
}
return app;
};
client.once("ready", () => {
//to delete specific command from guilds
/*approvedGuilds.forEach( async guild => {
const command = await client.guilds.cache.get(guild)?.commands.fetch();
command.forEach(async cmd => {
if (cmd.name == "zeus") {
await client.guilds.cache.get(guild)?.commands.delete(cmd.id);
}
})
})*/
//pushing commands to the discord API
client.commands.forEach(async (cmd) => {
let command = undefined;
if (cmd.isGlobal) {
try {
command = await client.application?.commands.create(cmd.data);
if ("permissions" in cmd.data) {
await command.permissions.set({ permissions: cmd.data.permissions });
}
console.log(
'[1/1] - "' + cmd.data.name + '" is ready! (globally: all guilds)'
);
} catch (error) {
console.error(error);
}
} else {
let j = 1;
await approvedGuilds.forEach(async (guild) => {
if ("guilds" in cmd && cmd.guilds.indexOf(guild) === -1) {
return;
}
try {
command = await client.guilds.cache
.get(guild)
?.commands.create(cmd.data);
if ("permissions" in cmd.data && command) {
await command.permissions.set({
permissions: cmd.data.permissions,
});
}
console.log(
"[" +
j++ +
"/" +
approvedGuilds.length +
'] - "' +
cmd.data.name +
'" is ready! (locally: ' +
guild +
")"
);
} catch (error) {
//console.error(error);
console.error("An error has been sent to Sentry");
Sentry.captureException(error);
}
});
}
if (typeof cmd.init === "function") {
cmd.init({ client: client });
}
});
//caching AGENDA channel for reaction listener & MEMBERS
approvedGuilds.forEach((guild) => {
const server = client.guilds.cache.get(guild);
if (server) {
server.members.fetch();
let agenda = server.channels.cache.find(
(chan) => chan.name === "📅-agenda"
);
if (agenda) {
agenda.messages.fetch();
} else {
console.warn('No channel named "📅-agenda" found in guild ' + guild);
}
}
});
console.log("Ready!");
});
client.on("interactionCreate", async (interaction) => {
const cmdName = interaction.isButton()
? interaction.message.interaction.commandName
: interaction.commandName;
if (
!interaction.isCommand() &&
!interaction.isButton() &&
!interaction.isContextMenu()
)
return;
if (
!client.commands.has(cmdName) ||
(interaction.isButton() &&
!(typeof client.commands.get(cmdName).callback === "function"))
)
return;
try {
if (interaction.isButton()) {
await client.commands.get(cmdName).callback({
user: interaction.member,
interaction: interaction,
channel: interaction.channel,
});
} else {
await client.commands.get(cmdName).execute({
options: interaction.options,
channel: interaction.channel,
user: interaction.user,
client: client,
interaction: interaction,
});
}
} catch (error) {
//console.error(error);
console.error("An error has been sent to Sentry");
Sentry.captureException(error);
await interaction.reply({
content:
"Une erreur est survenue lors de l'exécution de cette commande !",
ephemeral: true,
});
}
});
client.on("messageCreate", async (message) => {
if (
(message.mentions.users.has(client.user.id) ||
message.content.indexOf("<@" + client.user.id + ">") > -1 ||
message.content.indexOf("<@!" + client.user.id + ">") > -1) &&
message.author.id != client.user.id
)
message.react("👀");
});
client.login(process.env.TOKEN);