Skip to content

Commit 7bc6e64

Browse files
committed
Clean up code, and listeners
1 parent cc43565 commit 7bc6e64

File tree

6 files changed

+94
-72
lines changed

6 files changed

+94
-72
lines changed

readme.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,46 @@ It's about the same for listeners, just add the Javascript file into a ``listene
1212
## Commands to add
1313

1414
### Moderation
15-
-[x] Mutes
15+
- [x] Mutes
1616

17-
-[x] Temp Mutes
17+
- [x] Temp Mutes
1818

19-
-[x] Unmute
19+
- [x] Unmute
2020

21-
-[x] Ban (Done)
21+
- [x] Ban (Done)
2222

23-
-[x] Unban
23+
- [x] Unban
2424

25-
-[ ] Warns
25+
- [ ] Warns
2626

27-
-[ ] Lock channel
27+
- [ ] Lock channel
2828

29-
-[ ] Unlock channel
29+
- [ ] Unlock channel
3030

31-
-[ ] Help command
31+
- [ ] Help command
3232

3333
### Admin
3434

35-
-[x] Welcome messages
35+
- [x] Welcome messages
3636

37-
-[ ] Ticket System
37+
- [ ] Ticket System
3838

39-
-[ ] Verification System
39+
- [ ] Verification System
4040

41-
-[ ] Polls
41+
- [ ] Polls
4242

43-
-[ ] Giveaways
43+
- [ ] Giveaways
4444

4545
### Music
4646

47-
-[ ] Play song
47+
- [ ] Play song
4848

49-
-[ ] View Queue
49+
- [ ] View Queue
5050

51-
-[ ] Skip current song
51+
- [ ] Skip current song
5252

53-
-[ ] Shuffle
53+
- [ ] Shuffle
5454

55-
-[ ] Clear Queue
55+
- [ ] Clear Queue
5656

57-
-[ ] Remove
57+
- [ ] Remove song

src/baseBot.ts

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { AllyClient } from "./interfaces/AllyClient.js";
22
import { config } from "./config.js"; // Not ready to do this yet
33

44
import {
5-
GatewayIntentBits,
6-
Partials,
7-
Collection,
8-
Interaction,
9-
CacheType,
5+
GatewayIntentBits,
6+
Partials,
7+
Collection,
8+
Interaction
109
} from "discord.js";
1110
import * as fs from "fs";
1211
import { Sequelize } from "sequelize";
12+
import {Listeners} from "./interfaces/Listeners";
13+
import path from "path";
1314

1415
const client = new AllyClient({
1516
intents: config.intents as GatewayIntentBits[],
@@ -19,70 +20,80 @@ const client = new AllyClient({
1920
client.commands = new Collection();
2021
client.DB = new Sequelize("sqlite::memory");
2122

22-
fs.readdir(
23-
"./dist/commands",
24-
async (err: NodeJS.ErrnoException | null, items: string[]) => {
25-
if (err) throw err;
26-
var baseFiles = items.filter((files) => files.split(".").pop() === "js");
27-
var folders = items.filter((files) => files.split(".").pop() != "js");
23+
const commandsDirectory = path.join(__dirname, "commands");
2824

29-
// Load all commands from the base commands folder
30-
baseFiles.forEach((fileName) => {
31-
import(`./commands/${fileName}`).then((properties) => {
32-
let commandName = properties.SlashCommand.name.toLowerCase();
25+
fs.readdir(commandsDirectory, async (err: NodeJS.ErrnoException | null, items: string[]) => {
26+
if (err) throw err;
27+
const baseFiles = filterNonJsFiles(items);
28+
const folders = items.filter((files) => files.split(".").pop() != "js");
3329

34-
client.commands.set(commandName, properties.SlashCommand);
30+
loadCommands(baseFiles, commandsDirectory);
3531

36-
console.log(`${fileName} command loaded`);
37-
});
38-
});
39-
40-
// Load all commands from the subfolders
41-
folders.forEach((folderName) => {
42-
fs.readdir(
43-
`./dist/commands/${folderName}`,
44-
async (err: NodeJS.ErrnoException | null, subfolderItems: string[]) => {
32+
folders.forEach((folderName) => {
33+
fs.readdir(path.join(commandsDirectory, folderName), async (err: NodeJS.ErrnoException | null, subFolderItems: string[]) => {
4534
if (err) throw err;
46-
var Files = subfolderItems.filter(
47-
(files) => files.split(".").pop() === "js"
48-
);
49-
50-
Files.forEach((fileName) => {
51-
import(`./commands/${folderName}/${fileName}`).then(
52-
(properties) => {
53-
let commandName: String =
54-
properties.SlashCommand.name.toLowerCase();
55-
56-
client.commands.set(commandName, properties.SlashCommand);
57-
58-
console.log(`${fileName} command loaded`);
59-
}
60-
);
61-
});
35+
const Files = filterNonJsFiles(subFolderItems);
36+
37+
loadCommands(Files, path.join(commandsDirectory, folderName));
6238
}
6339
);
6440
});
6541
}
6642
);
6743

44+
const listenersDirectory = path.join(__dirname, "listeners");
45+
46+
fs.readdir(listenersDirectory, async (err: NodeJS.ErrnoException | null, items: string[]) => {
47+
if (err) throw err;
48+
const listeners = items.filter((files) => files.split(".").pop() === "js");
49+
50+
listeners.forEach((fileName: string) => {
51+
import(path.join(listenersDirectory, fileName)).then((properties) => {
52+
let listener:Listeners<any> = properties.Listener;
53+
54+
client.on(listener.event, (args) => {
55+
listener.run(client, args);
56+
});
57+
console.log(`${listener.purpose} listener was loaded`);
58+
});
59+
});
60+
});
61+
6862
client.on("ready", () => {
6963
console.log(`Logged in as ${client.user?.tag}!`);
7064
});
7165

72-
client.on("interactionCreate", (interaction: Interaction<CacheType>) => {
66+
client.on("interactionCreate", (interaction: Interaction) => {
7367
if (!interaction.isCommand()) return;
7468

7569
let commandFile = client.commands.get(interaction.commandName);
7670

7771
if (commandFile) commandFile.run(client, interaction);
7872
});
7973

80-
client.on("interactionCreate", (interaction: Interaction<CacheType>) => {
74+
client.on("interactionCreate", (interaction: Interaction) => {
8175
if (!interaction.isButton()) return;
8276

8377
let buttonFollowUp = client.commands.get(interaction.customId.split(".")[0]);
8478

8579
if (buttonFollowUp) buttonFollowUp.followup(client, interaction);
8680
});
8781

82+
8883
client.login(config.token);
84+
85+
const loadCommands = (files: string[], location: string) => {
86+
files.forEach((fileName) => {
87+
import(path.join(location, fileName)).then((properties) => {
88+
let commandName = properties.SlashCommand.name.toLowerCase();
89+
90+
client.commands.set(commandName, properties.SlashCommand);
91+
92+
console.log(`${fileName} command loaded`);
93+
});
94+
});
95+
};
96+
97+
const filterNonJsFiles = (items: string[]) => {
98+
return items.filter((files) => files.split(".").pop() === "js");
99+
};

src/interfaces/Listeners.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {AllyClient} from "./AllyClient";
2+
import {Awaitable, ClientEvents} from "discord.js";
23

3-
export interface Listeners {
4-
event: String;
4+
export interface Listeners<Event extends keyof ClientEvents> {
5+
event: Event;
56
purpose: String;
6-
run: (client: AllyClient, listener: any) => void;
7+
run: (client: AllyClient, ...args: ClientEvents[Event]) => Awaitable<void>;
78
}
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import {Listeners} from "../../../interfaces/Listeners";
22
import {AllyClient} from "../../../interfaces/AllyClient";
3+
import {Events, GuildMember, TextBasedChannel} from "discord.js";
34

4-
export const Listener: Listeners = {
5-
event: "guildMemberAdd",
5+
export const Listener: Listeners<Events.GuildMemberAdd> = {
6+
event: Events.GuildMemberAdd,
67
purpose: "Welcome message",
7-
run(client: AllyClient, listener: any): void {
8+
run(client: AllyClient, listener: GuildMember): void {
9+
const welcomeChannelId = listener.guild.systemChannelId; //Add ability to change channel to send to
10+
listener.guild.channels.fetch(welcomeChannelId).then((channel) => {
11+
if (channel.isTextBased) {
12+
let welcomeChannel = channel as TextBasedChannel;
13+
let messageContent = `Hello ${listener.displayName}!`;
814

15+
welcomeChannel.send(messageContent);
16+
}
17+
});
918
}
1019
};

src/modules/moderation/commands/ban.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const SlashCommand: Command = {
4444
option
4545
.setName('reason')
4646
.setDescription('Ban reason')
47+
.setRequired(true)
4748
)
4849
.setDefaultMemberPermissions(
4950
PermissionFlagsBits.BanMembers

src/modules/moderation/commands/mute.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export const SlashCommand: Command = {
1616
run: (client: AllyClient, interaction: CommandInteraction) => {
1717
const embed = new EmbedBuilder();
1818

19-
var mentionedUser = interaction.options.getUser("user");
20-
var time = interaction.options.get("time", true).value as number;
21-
var guild = interaction.guild;
19+
const mentionedUser = interaction.options.getUser("user");
20+
const time = interaction.options.get("time", true).value as number;
21+
const guild = interaction.guild;
2222

2323
if (time >= MAX_TIME) {
2424
embed

0 commit comments

Comments
 (0)