Skip to content

Commit 0025e61

Browse files
committed
Review comments
1 parent ca3aff1 commit 0025e61

File tree

7 files changed

+61
-54
lines changed

7 files changed

+61
-54
lines changed

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
pretty-quick --staged

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ We also have a docker-compose.yml for development, along with a .env.example.
1616

1717
## Thanks!
1818

19-
- [ckie](https://github.com/ckiee) for writing the base for the bot.
19+
- [ckie](https://github.com/ckiee) for writing the base for the bot!
2020
- [Python Discord](https://github.com/python-discord) for heavily influencing our help channel system.

package.json

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,18 @@
2020
"undici": "^5.12.0"
2121
},
2222
"devDependencies": {
23-
"@types/dotenv-safe": "^8.1.2",
24-
"@types/lz-string": "^1.3.34",
25-
"@types/node": "^16",
26-
"@types/npm-registry-fetch": "^8.0.4",
27-
"@types/prettier": "^2.7.1",
28-
"@types/tar": "^6.1.3",
29-
"@types/ws": "^8.5.3",
23+
"@types/dotenv-safe": "8.1.2",
24+
"@types/lz-string": "1.3.34",
25+
"@types/node": "16.18.3",
26+
"@types/npm-registry-fetch": "8.0.4",
27+
"@types/prettier": "2.7.1",
28+
"@types/tar": "6.1.3",
29+
"@types/ws": "8.5.3",
3030
"husky": "^8.0.2",
3131
"pretty-quick": "^3.1.3",
3232
"ts-node-dev": "^2.0.0",
3333
"typescript": "^4.9.3"
3434
},
35-
"husky": {
36-
"hooks": {
37-
"pre-commit": "pretty-quick --staged"
38-
}
39-
},
4035
"scripts": {
4136
"start": "ts-node-dev --respawn src",
4237
"build": "tsc",

src/bot.ts

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ export interface CommandRegistration {
77
listener: (msg: Message, content: string) => Promise<void>;
88
}
99

10+
interface Command {
11+
admin: boolean;
12+
aliases: string[];
13+
description?: string;
14+
listener: (msg: Message, content: string) => Promise<void>;
15+
}
16+
1017
export class Bot {
11-
commands: CommandRegistration[] = [];
12-
adminCommands: CommandRegistration[] = [];
18+
commands = new Map<string, Command>();
1319

1420
constructor(public client: Client<true>) {
1521
client.on('messageCreate', msg => {
@@ -26,11 +32,7 @@ export class Bot {
2632
triggerWithPrefix.substring(matchingPrefix.length),
2733
);
2834

29-
if (
30-
!command ||
31-
(this.adminCommands.includes(command) &&
32-
!this.isAdmin(msg.author))
33-
) {
35+
if (!command || (command.admin && !this.isAdmin(msg.author))) {
3436
return;
3537
}
3638
command.listener(msg, content).catch(err => {
@@ -40,17 +42,28 @@ export class Bot {
4042
});
4143
}
4244

43-
registerCommand(command: CommandRegistration) {
44-
this.commands.push(command);
45+
registerCommand(registration: CommandRegistration) {
46+
const command: Command = {
47+
...registration,
48+
admin: false,
49+
};
50+
for (const a of command.aliases) {
51+
this.commands.set(a, command);
52+
}
4553
}
4654

47-
registerAdminCommand(command: CommandRegistration) {
48-
this.adminCommands.push(command);
55+
registerAdminCommand(registration: CommandRegistration) {
56+
const command: Command = {
57+
...registration,
58+
admin: true,
59+
};
60+
for (const a of command.aliases) {
61+
this.commands.set(a, command);
62+
}
4963
}
5064

51-
getByTrigger(trigger: string): CommandRegistration | undefined {
52-
const match = (c: CommandRegistration) => c.aliases.includes(trigger);
53-
return this.commands.find(match) || this.adminCommands.find(match);
65+
getByTrigger(trigger: string): Command | undefined {
66+
return this.commands.get(trigger);
5467
}
5568

5669
isMod(member: GuildMember | null) {
@@ -80,18 +93,18 @@ export class Bot {
8093
const mentioned = msg.mentions.members?.first()?.user;
8194
if (mentioned) return mentioned;
8295

83-
if (query) {
84-
// Search by ID
85-
const queriedUser = await this.client.users
86-
.fetch(query)
87-
.catch(() => undefined);
88-
if (queriedUser) return queriedUser;
89-
90-
// Search by name, likely a better way to do this...
91-
for (const user of this.client.users.cache.values()) {
92-
if (user.tag === query || user.username === query) {
93-
return user;
94-
}
96+
if (!query) return;
97+
98+
// Search by ID
99+
const queriedUser = await this.client.users
100+
.fetch(query)
101+
.catch(() => undefined);
102+
if (queriedUser) return queriedUser;
103+
104+
// Search by name, likely a better way to do this...
105+
for (const user of this.client.users.cache.values()) {
106+
if (user.tag === query || user.username === query) {
107+
return user;
95108
}
96109
}
97110
}

src/modules/autorole.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { TextBasedChannel } from 'discord.js';
21
import { Bot } from '../bot';
32
import { autorole, rolesChannelId } from '../env';
43

@@ -19,10 +18,6 @@ export async function autoroleModule({ client }: Bot) {
1918
await msg?.react(ar.emoji);
2019
}
2120

22-
client.channels.fetch(rolesChannelId).then(channel => {
23-
(channel as TextBasedChannel).messages.fetch();
24-
});
25-
2621
client.on('messageReactionAdd', async (reaction, user) => {
2722
if (user.id == client.user.id) return;
2823
if (reaction.partial) await reaction.fetch();

src/modules/help.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ export function helpModule(bot: Bot) {
5454
`Hello ${msg.author.username}! Here is a list of all commands in me! To get detailed description on any specific command, do \`help <command>\``,
5555
);
5656

57-
for (const cat of getCommandCategories(bot.commands)) {
57+
for (const cat of getCommandCategories(bot.commands.values())) {
5858
embed.addFields({
5959
name: `**${cat} Commands:**`,
60-
value: getCategoryHelp(cat, bot.commands),
60+
value: getCategoryHelp(cat, bot.commands.values()),
6161
});
6262
}
6363

@@ -72,7 +72,7 @@ export function helpModule(bot: Bot) {
7272
}
7373

7474
let cmd: { description?: string; aliases?: string[] } =
75-
bot.commands.find(c => c.aliases.includes(cmdTrigger)) || {};
75+
bot.getByTrigger(cmdTrigger) || {};
7676

7777
if (!cmd.description && cmdTrigger.includes(':')) {
7878
const snippet = await Snippet.findOne({

yarn.lock

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,14 @@
227227
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.3.tgz#472eaab5f15c1ffdd7f8628bd4c4f753995ec79e"
228228
integrity sha512-yOlFc+7UtL/89t2ZhjPvvB/DeAr3r+Dq58IgzsFkOAvVC6NMJXmCGjbptdXdR9qsX7pKcTL+s87FtYREi2dEEQ==
229229

230-
"@types/dotenv-safe@^8.1.2":
230+
231231
version "8.1.2"
232232
resolved "https://registry.yarnpkg.com/@types/dotenv-safe/-/dotenv-safe-8.1.2.tgz#72f126969f445af5654efd3167deabc2cbc6c24e"
233233
integrity sha512-R/B/wIMda6lRE2P1H0vwSoJsV78IOkhccE4vIvmKZQNXOIjiU0QyJsUXwSotBxOPZFZ/oOnjCa3+kK5kVJwGyw==
234234
dependencies:
235235
dotenv "^8.2.0"
236236

237-
"@types/lz-string@^1.3.34":
237+
238238
version "1.3.34"
239239
resolved "https://registry.yarnpkg.com/@types/lz-string/-/lz-string-1.3.34.tgz#69bfadde419314b4a374bf2c8e58659c035ed0a5"
240240
integrity sha512-j6G1e8DULJx3ONf6NdR5JiR2ZY3K3PaaqiEuKYkLQO0Czfi1AzrtjfnfCROyWGeDd5IVMKCwsgSmMip9OWijow==
@@ -257,7 +257,7 @@
257257
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.9.tgz#02d013de7058cea16d36168ef2fc653464cfbad4"
258258
integrity sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==
259259

260-
"@types/node@^16":
260+
"@types/node@16.18.3":
261261
version "16.18.3"
262262
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.18.3.tgz#d7f7ba828ad9e540270f01ce00d391c54e6e0abc"
263263
integrity sha512-jh6m0QUhIRcZpNv7Z/rpN+ZWXOicUUQbSoWks7Htkbb9IjFQj4kzcX/xFCkjstCj5flMsN8FiSvt+q+Tcs4Llg==
@@ -267,7 +267,7 @@
267267
resolved "https://registry.yarnpkg.com/@types/npm-package-arg/-/npm-package-arg-6.1.1.tgz#9e2d8adc04d39824a3d9f36f738010a3f7da3c1a"
268268
integrity sha512-452/1Kp9IdM/oR10AyqAgZOxUt7eLbm+EMJ194L6oarMYdZNiFIFAOJ7IIr0OrZXTySgfHjJezh2oiyk2kc3ag==
269269

270-
"@types/npm-registry-fetch@^8.0.4":
270+
271271
version "8.0.4"
272272
resolved "https://registry.yarnpkg.com/@types/npm-registry-fetch/-/npm-registry-fetch-8.0.4.tgz#77b2737cde22314ccda1dfdb9568fd7769e95b90"
273273
integrity sha512-R9yEj6+NDmXLpKNS19cIaMyaHfV0aHjy/1qbo8K9jiHyjyaYg0CEmuOV/L0Q91DZDi3SuxlYY+2XYwh9TbB+eQ==
@@ -283,7 +283,7 @@
283283
resolved "https://registry.yarnpkg.com/@types/npmlog/-/npmlog-4.1.4.tgz#30eb872153c7ead3e8688c476054ddca004115f6"
284284
integrity sha512-WKG4gTr8przEZBiJ5r3s8ZIAoMXNbOgQ+j/d5O4X3x6kZJRLNvyUJuUK/KoG3+8BaOHPhp2m7WC6JKKeovDSzQ==
285285

286-
"@types/prettier@^2.7.1":
286+
287287
version "2.7.1"
288288
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.7.1.tgz#dfd20e2dc35f027cdd6c1908e80a5ddc7499670e"
289289
integrity sha512-ri0UmynRRvZiiUJdiz38MmIblKK+oH30MztdBVR95dv/Ubw6neWSb8u1XpRb72L4qsZOhz+L+z9JD40SJmfWow==
@@ -305,15 +305,15 @@
305305
resolved "https://registry.yarnpkg.com/@types/strip-json-comments/-/strip-json-comments-0.0.30.tgz#9aa30c04db212a9a0649d6ae6fd50accc40748a1"
306306
integrity sha512-7NQmHra/JILCd1QqpSzl8+mJRc8ZHz3uDm8YV1Ks9IhK0epEiTw8aIErbvH9PI+6XbqhyIQy3462nEsn7UVzjQ==
307307

308-
"@types/tar@^6.1.3":
308+
309309
version "6.1.3"
310310
resolved "https://registry.yarnpkg.com/@types/tar/-/tar-6.1.3.tgz#46a2ce7617950c4852dfd7e9cd41aa8161b9d750"
311311
integrity sha512-YzDOr5kdAeqS8dcO6NTTHTMJ44MUCBDoLEIyPtwEn7PssKqUYL49R1iCVJPeiPzPlKi6DbH33eZkpeJ27e4vHg==
312312
dependencies:
313313
"@types/node" "*"
314314
minipass "^3.3.5"
315315

316-
"@types/ws@^8.5.3":
316+
"@types/ws@8.5.3", "@types/ws@^8.5.3":
317317
version "8.5.3"
318318
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.3.tgz#7d25a1ffbecd3c4f2d35068d0b283c037003274d"
319319
integrity sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w==

0 commit comments

Comments
 (0)