-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDiscordClient.ts
227 lines (210 loc) · 6.37 KB
/
DiscordClient.ts
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { config as dotenv } from "dotenv";
import db from "./database";
import { readdirSync } from "fs";
import { join } from "path";
import {
BaseInteraction,
ChannelType,
Client,
Collection,
CommandInteraction,
DMChannel,
GatewayIntentBits,
GuildChannel,
HTTPError,
Message,
PermissionFlagsBits,
PermissionResolvable,
TextBasedChannelTypes,
TextChannel,
} from "discord.js";
dotenv();
const intCommands: Collection<
string,
{
name: string;
channels: TextBasedChannelTypes[];
permissions: PermissionResolvable[];
exec(interaction: CommandInteraction): Promise<void>;
}
> = new Collection();
const intFiles = readdirSync(join(__dirname, "commands")).filter((f) =>
f.endsWith(".js"),
);
for (const file of intFiles) {
const intCommand = require(`./commands/${file}`);
intCommands.set(intCommand.name, intCommand);
}
db.connect().catch((e) => {
console.error(e);
process.exit();
});
const bot = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
presence: {
activities: [{ type: 5, name: "message eating contest." }],
},
});
bot.login().catch((e) => {
console.error(e);
process.exit();
});
bot.once("ready", function (): void {
console.log(
`Shard ${bot.shard?.ids[0]} ready with ${bot.guilds.cache.size} guilds.`,
);
});
if (process.env.ENABLEDEBUG)
bot.on("debug", function (info: string): void {
console.log(info);
});
bot.on("messageCreate", async function (message: Message): Promise<void> {
if (message.channel.type === ChannelType.DM || !message.deletable) return;
const searchedChannel = await db
.query("SELECT is_insta FROM channels WHERE channel = $1;", [
message.channel.id,
])
.catch((e) => console.error(e));
if (!searchedChannel?.rowCount || !searchedChannel.rows[0].is_insta) return;
await message.delete().catch((e) => console.error(e));
});
bot.on(
"channelDelete",
async function (channel: DMChannel | GuildChannel): Promise<void> {
if (channel.type !== ChannelType.GuildText) return;
const foundChannel = await db
.query("SELECT channel FROM channels WHERE channel = $1;", [channel.id])
.catch((e) => console.error(e));
if (!foundChannel?.rowCount) return; // Return if rowCount is zero or query failed
await db
.query("DELETE FROM channels WHERE channel = $1;", [channel.id])
.catch((e) => console.error(e));
},
);
bot.on(
"interactionCreate",
async function (int: BaseInteraction): Promise<void> {
if (!int.isChatInputCommand() || !intCommands.has(int.commandName)) return;
try {
const command = intCommands.get(int.commandName); // Hacky but whatever, require() is sort of a mess with ts
if (!command)
throw Error(
"Command does not exist but <Collection>.has() returned true",
);
if (!int.member?.user?.id)
throw Error("Interaction has no associated member");
if (
!int.channel?.type ||
(command?.channels.length &&
!command.channels.includes(int.channel?.type))
)
return;
const commandUser = await int.guild?.members.fetch(int.member.user.id);
if (!commandUser)
throw Error(
"Interaction has a member but member did not exist in guild",
);
if (
command.permissions?.length &&
!commandUser.permissions.has(command.permissions)
) {
await int.reply("You cannot run this command.");
return;
}
await command.exec(int);
} catch (e) {
console.error(e);
await int
.reply(
`Oops! An error occured while running this command;\n\nIf you contact the developer, give them this information:\n${e}`,
)
.catch((e) => console.error(e));
}
},
);
setInterval(async function (): Promise<void> {
const staleChannels = await db
.query("SELECT * FROM channels WHERE is_insta = false AND last_ran < $1;", [
Date.now() - 1800000,
])
.catch((e) => console.error(e));
if (typeof staleChannels === "undefined") return;
for (let i = 0; i < (staleChannels.rowCount ?? 0); i++) {
if (
parseInt(staleChannels.rows[i].last_ran) +
parseInt(staleChannels.rows[i].interval) * 60000 >
Date.now()
)
continue;
const guild = await bot.guilds
.fetch(staleChannels.rows[i].guild)
.catch((e) => console.error(e));
if (typeof guild === "undefined") continue;
const untypedChannel: any = await guild.channels
.fetch(staleChannels.rows[i].channel)
.catch((e: HTTPError) => e);
if (untypedChannel instanceof HTTPError && untypedChannel.status === 404) {
await db.query("DELETE FROM channels WHERE guild = $1;", [guild]);
continue;
}
if (typeof untypedChannel === "undefined") continue;
const user = bot.application?.id;
if (!user) return;
const channel: TextChannel = untypedChannel;
if (
!channel
.permissionsFor(user)
?.has([
PermissionFlagsBits.ManageMessages,
PermissionFlagsBits.ReadMessageHistory,
])
)
continue;
if (channel.messages.cache.size === 0) {
try {
await channel.messages.fetch();
} catch (e) {
console.error(e);
}
}
while (
channel.messages.cache.filter((msg) => !msg.pinned).size > 0 &&
typeof channel.lastMessage?.createdTimestamp !== "undefined" &&
channel.lastMessage.createdTimestamp > Date.now() - 1209600000
) {
const fetchedMsgs = await channel.messages.fetch({ limit: 100 });
const ids: string[] = [];
fetchedMsgs.forEach((msg) => {
if (!msg.pinned && msg.createdTimestamp > Date.now() - 1209600000)
ids.push(msg.id);
});
channel.messages.cache.forEach((msg) => {
if (
!ids.includes(msg.id) &&
!msg.pinned &&
msg.createdTimestamp > Date.now() - 1209600000
)
ids.push(msg.id);
});
await channel.bulkDelete(ids).catch((e) => console.error(e));
}
await db
.query("UPDATE channels SET last_ran = $1 WHERE channel = $2;", [
Date.now(),
channel.id,
])
.catch((e) => console.error(e));
}
}, 60000);
process.on("SIGHUP", function (): void {
bot.destroy();
process.exit();
});
process.on("SIGINT", function (): void {
bot.destroy();
process.exit();
});
process.on("SIGTERM", function () {
bot.destroy();
process.exit();
});