This repository was archived by the owner on Apr 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
160 lines (138 loc) · 5.29 KB
/
Copy pathindex.js
File metadata and controls
160 lines (138 loc) · 5.29 KB
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
const express = require("express");
const app = express();
const Discord = require("discord.js");
const {REST} = require("@discordjs/rest");
const {Routes} = require("discord-api-types/v9");
const {AuditLogEvent} = require("discord.js");
const fs = require("fs");
const path = require("path");
const {updateMessages} = require("./components/updateMessages");
const {updateAllEmojis} = require("./components/emoji/updateAllEmojis");
const {unwatchDeletedRoles} = require("./components/unwatchDeletedRoles");
const desiredRoles = require("./components/desiredRoles");
const {isWatchedRole} = require("./components/isWatchedRole");
const {addEmoji} = require("./components/emoji/addEmoji");
const {isLinkedRole} = require("./components/linked/isLinkedRole");
const {updateUserLinkedroles} = require("./components/linked/updateUserLinkedRoles");
const {isMasterLinkedRole} = require("./components/linked/isMasterLinkedRole");
const color = "\x1b[35m";
const colorReset = "\x1b[0m";
const client = new Discord.Client({
allowedMentions: {parse: []},
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.GuildModeration,
],
});
require("dotenv").config();
const token = process.env.token;
const clientId = process.env.clientId;
// Console log
app.listen(1000, () => {
console.log(`Running bot!`);
});
// List of all commands
const commands = [];
client.commands = new Discord.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);
commands.push(command.data.toJSON());
}
client.on("ready", () => {
app.get("/", (req, res) => {
const guild_ids = client.guilds.cache.map((guild) => guild.id);
res.send(guild_ids);
});
});
// Deploy
client.on("ready", () => {
const guilds = client.guilds.cache;
const rest = new REST({version: "9"}).setToken(token);
for (const guild of guilds) {
rest.put(Routes.applicationGuildCommands(clientId, guild[1].id), {body: commands})
.then(() => console.log(`${color}[${guild[1].name}]${colorReset} Successfully updated commands.`))
// Automatically update after down-time
// .then(() => updateAllEmojis({client, interactionGuildId: guild[1].id, compileUpdates: true}))
.then(() => updateMessages({client, interactionGuildId: guild[1].id}))
.catch(console.error);
}
for (const guild of guilds) {
}
});
// Handle Commands
client.on("interactionCreate", async (interaction) => {
const rest = new REST({version: "9"}).setToken(token);
if (!interaction.isCommand()) {
return;
}
const command = client.commands.get(interaction.commandName);
if (!command) {
return;
}
try {
await command.execute({client, interaction});
} catch (error) {
console.error(error);
rest.put;
await interaction.channel.send({
content: "Sorry little one. Something screwed up. Tag <@512664079496642575> so he can fix it.",
});
}
});
// Error Handler
process.on("uncaughtException", (error) => {
console.error(error);
});
process.on("unhandledRejection", (reason, promise) => {
console.error("Unhandled Rejection at:", promise, "reason:", reason);
});
// Listen for role member changes
client.on("guildMemberUpdate", async (oldMember, newMember) => {
const oldRoles = oldMember.roles.cache;
const newRoles = newMember.roles.cache;
const addedRole = [...newRoles.filter((role) => !oldRoles.has(role.id))][0]?.[1].id;
const removedRole = [...oldRoles.filter((role) => !newRoles.has(role.id))][0]?.[1].id;
// get the id of user who edited the role to filter out edits done by the bot.
let executorId = await newMember.guild.fetchAuditLogs({
limit: 1,
type: AuditLogEvent.MemberRoleUpdate,
});
executorId = executorId.entries.map((entry) => entry.executorId)[0];
// update role count list messages
if (executorId != "1223751197131804742" && ((await isWatchedRole(addedRole)) || (await isWatchedRole(removedRole)))) {
updateMessages({client, interactionGuildId: newMember.guild.id});
}
// listen for linked roles changes
if (
(await isLinkedRole(addedRole)) ||
(await isLinkedRole(removedRole)) ||
(await isMasterLinkedRole(addedRole)) ||
(await isMasterLinkedRole(removedRole))
) {
await updateUserLinkedroles({client, interactionGuildId: newMember.guild.id, user: newMember});
}
});
// Listen for removed roles
client.on("roleDelete", async (deletedRole) => {
console.log(`${color}[${deletedRole.guild.name}]${colorReset} The role ${deletedRole.name} has been removed from the server.`);
unwatchDeletedRoles({client, interactionGuildId: deletedRole.guild.id, deletedRoleId: deletedRole.id});
updateMessages({client, interactionGuildId});
});
// Listen for updated role icons
client.on("roleUpdate", async (oldRole, newRole) => {
if (await isWatchedRole(newRole.id)) {
if (oldRole.iconURL() !== newRole.iconURL()) {
console.log(`${color}[${newRole.guild.name}]${colorReset} The icon of the ${newRole.name} role was updated.`);
await addEmoji({client, interactionGuildId: newRole.guild.id, roleId: newRole.id});
updateMessages({client, interactionGuildId: newRole.guild.id});
}
}
});
// Login
client.login(token);