Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract Commands/Interactions and cleanup codebase #59

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions src/commands/afk.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {
ApplicationCommandOptionType,
CommandInteraction,
Message,
} from "discord.js";
import { ApplicationCommandOptionType } from "discord.js";
import { Command } from "../interfaces/command.interface";

// TODO Move this to database
import {
getImpetusId,
getImpetusOption,
replyToImpetus,
} from "../utilities/Impetus";

export const afkStatuses: { [userId: string]: string } = {};

export default {
Expand All @@ -20,23 +21,14 @@ export default {
required: false,
},
],
async execute(
interaction?: CommandInteraction,
message?: Message,
args?: string[]
) {
const reason =
(interaction?.options.get("reason")?.value as string) ||
args?.join(" ") ||
"No reason given";

const userId = interaction?.user.id || message?.author.id;
async execute(impetus, args = []) {
const userId = getImpetusId(impetus);
if (!userId) return;

afkStatuses[userId] = reason;
const reason = getImpetusOption(impetus, args, "reason", true);

afkStatuses[userId] = reason!;

await interaction?.reply(`You are now AFK: ${reason}`);
const reply = await message?.reply(`You are now AFK: ${reason}`);
setTimeout(() => reply?.delete(), 5000);
replyToImpetus(impetus, `You are now AFK: ${reason}`, undefined, true);
},
} as Command;
65 changes: 29 additions & 36 deletions src/commands/banUser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
ApplicationCommandOptionType,
CommandInteraction,
GuildMember,
Message,
} from "discord.js";
import { Command } from "../interfaces/command.interface";
import { filterUserId } from "../utilities/filterUserId";
import { sendMessage } from "../utilities/sendMessage";

import { ApplicationCommandOptionType, GuildMember } from "discord.js";

import {
getImpetusOption,
getImpetusTarget,
replyToImpetus,
} from "../utilities/Impetus";

export default {
name: "banuser",
Expand All @@ -27,52 +27,45 @@ export default {
required: false,
},
],
async execute(
interaction?: CommandInteraction,
message?: Message,
args?: string[]
) {
if (!args) args = [];
const member = interaction?.member || message?.member;
async execute(impetus, args = []) {
const member = impetus.member;

if (!(member instanceof GuildMember)) return; // Interaction is in DM, return
if (!member?.permissions.has("BanMembers")) {
sendMessage(
message,
interaction,

if (!member.permissions.has("BanMembers")) {
replyToImpetus(
impetus,
"You do not have the necessary permissions to use this command."
);
return;
}
const targetUser =
interaction?.options.getUser("target") ||
message?.guild?.members.cache.get(filterUserId(args[0]))?.user;
const targetUser = getImpetusTarget(impetus, args);

if (!targetUser) return;

const reason =
(interaction?.options.get("reason")?.value as string) ||
args[1] ||
"No reason provided";
const reason = getImpetusOption(impetus, args, "reason");

const targetMember = member.guild?.members.cache.get(targetUser.id);
const targetMember = member.guild.members.cache.get(targetUser.id);

if (!targetMember) {
sendMessage(message, interaction, "User not found");
replyToImpetus(impetus, "User not found", undefined, true);
return;
}

try {
await member.ban({ reason });
sendMessage(
message,
interaction,
`${targetUser.tag} has been banned\nReason: \`\`\`${reason}\`\`\``
replyToImpetus(
impetus,
`${targetUser.tag} has been banned\nReason: \`\`\`${reason}\`\`\``,
undefined,
true
);
} catch (error) {
sendMessage(
message,
interaction,
"Can't ban this member, they might have a more powerful role than mine!"
replyToImpetus(
impetus,
"Can't ban this member, they might have a more powerful role than mine!",
undefined,
true
);
}
},
Expand Down
14 changes: 7 additions & 7 deletions src/commands/botInfo.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { CommandInteraction, EmbedBuilder, Message } from "discord.js";
import { EmbedBuilder } from "discord.js";
import { Command } from "../interfaces/command.interface";
import os from "os";
import { sendMessage } from "../utilities/sendMessage";
import { replyToImpetus } from "../utilities/Impetus";

export default {
name: "botinfo",
description: "Displays information about the bot",
category: "utility",
async execute(interaction?: CommandInteraction, message?: Message) {
const client = interaction?.client || message?.client;
async execute(impetus) {
const client = impetus.client;

if (!client) return;

const uptime = Math.floor(process.uptime());

const embed = new EmbedBuilder()
.setTitle(`${client.user?.username}'s Information`)
.setTitle(`${client.user.username}'s Information`)
.setColor("#0099ff")
.addFields(
{ name: "Uptime", value: `${uptime} seconds`, inline: true },
Expand All @@ -35,8 +35,8 @@ export default {
{ name: "OS", value: `${os.type()} ${os.release()}`, inline: true },
{ name: "Library", value: "discord.js", inline: true }
)
.setThumbnail(client.user?.avatarURL() || null);
.setThumbnail(client.user.avatarURL() || null);

sendMessage(message, interaction, { embeds: [embed] });
replyToImpetus(impetus, { embeds: [embed] });
},
} as Command;
7 changes: 3 additions & 4 deletions src/commands/dice.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { CommandInteraction, Message } from "discord.js";
import { Command } from "../interfaces/command.interface";
import { sendMessage } from "../utilities/sendMessage";
import { replyToImpetus } from "../utilities/Impetus";

export default {
name: "dice",
description: "Roll a virtual dice",
category: "fun",
async execute(interaction?: CommandInteraction, message?: Message) {
async execute(impetus) {
const result = Math.floor(Math.random() * 6) + 1;
sendMessage(message, interaction, `You rolled ${result}.`);
replyToImpetus(impetus, `You rolled ${result}.`);
},
} as Command;
14 changes: 6 additions & 8 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { CommandInteraction, EmbedBuilder, Message } from "discord.js";
import { Command } from "../interfaces/command.interface";
import { CustomClient } from "../interfaces/client.interface";
import { sendMessage } from "../utilities/sendMessage";
import { replyToImpetus } from "../utilities/Impetus";

export default {
name: "help",
description: "Displays a list of all available commands",
category: "utility",
async execute(
interaction?: CommandInteraction,
message?: Message,
args?: string[] // Use it for command categories
impetus,
args = [] // Use it for command categories
) {
// We retrieve all commands from the client
const client =
(interaction?.client as CustomClient) ||
(message?.client as CustomClient);
const client = impetus.client as CustomClient;

if (!client) return;
const commands = client.commands;

Expand All @@ -35,6 +33,6 @@ export default {
embed.addFields({ name: command.name, value: command.description });
});

sendMessage(message, interaction, { embeds: [embed] });
replyToImpetus(impetus, { embeds: [embed] });
},
} as Command;
10 changes: 5 additions & 5 deletions src/commands/invite.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { CommandInteraction, GuildMember, Message } from "discord.js";
import { GuildMember } from "discord.js";
import { Command } from "../interfaces/command.interface";
import { sendMessage } from "../utilities/sendMessage";
import { replyToImpetus } from "../utilities/Impetus";

export default {
name: "invite",
description: "Create an invite to the current channel.",
category: "utility",
execute: async (interaction?: CommandInteraction, message?: Message) => {
const member = interaction?.member || message?.member;
execute: async (impetus) => {
const member = impetus.member;
if (!(member instanceof GuildMember)) return;
/* FIXME */ sendMessage(message, interaction, "Not available yet");
/* FIXME */ replyToImpetus(impetus, "Not available yet");
},
} as Command;
9 changes: 4 additions & 5 deletions src/commands/joke.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { CommandInteraction, Message } from "discord.js";
import { Command } from "../interfaces/command.interface";
import { sendMessage } from "../utilities/sendMessage";
import { replyToImpetus } from "../utilities/Impetus";

// TODO Pull from a database or add your own local file logic
// TODO Pull from database
const jokes = [
"Why don't scientists trust atoms? Because they make up everything!",
"Why did the chicken go to the séance? To talk to the other side.",
Expand All @@ -15,9 +14,9 @@ export default {
name: "joke",
description: "Tells a random joke",
category: "fun",
async execute(interaction?: CommandInteraction, message?: Message) {
async execute(impetus) {
const joke = jokes[Math.floor(Math.random() * jokes.length)];

sendMessage(message, interaction, joke);
replyToImpetus(impetus, joke);
},
} as Command;
60 changes: 25 additions & 35 deletions src/commands/kickUser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {
ApplicationCommandOptionType,
CommandInteraction,
GuildMember,
Message,
} from "discord.js";
import { ApplicationCommandOptionType, GuildMember } from "discord.js";
import { Command } from "../interfaces/command.interface";
import { filterUserId } from "../utilities/filterUserId";
import { sendMessage } from "../utilities/sendMessage";
import {
getImpetusTarget,
replyToImpetus,
getImpetusOption,
} from "../utilities/Impetus";

export default {
name: "kickuser",
Expand All @@ -27,51 +25,43 @@ export default {
required: false,
},
],
async execute(
interaction?: CommandInteraction,
message?: Message,
args?: string[]
) {
async execute(impetus, args) {
if (!args) args = [];
const member = interaction?.member || message?.member;
const member = impetus.member;
if (!(member instanceof GuildMember)) return; // Interaction is in DM, return
if (!member?.permissions.has("KickMembers")) {
sendMessage(
message,
interaction,
if (!member.permissions.has("KickMembers")) {
replyToImpetus(
impetus,
"You do not have the necessary permissions to use this command."
);
return;
}
const targetUser =
interaction?.options.getUser("target") ||
message?.guild!.members.cache.get(filterUserId(args[0]))?.user;
const reason =
(interaction?.options.get("reason")?.value as string) ||
args[1] ||
"No reason provided";
const targetUser = getImpetusTarget(impetus, args);
const reason = getImpetusOption(impetus, args, "reason");

if (!targetUser) return;

const targetMember = member.guild?.members.cache.get(targetUser.id);
const targetMember = member.guild.members.cache.get(targetUser.id);

if (!targetMember) {
sendMessage(message, interaction, "User not found");
replyToImpetus(impetus, "User not found", undefined, true);
return;
}

try {
await member.kick(reason);
sendMessage(
message,
interaction,
`${targetUser.tag} has been kicked for reason: ${reason}`
replyToImpetus(
impetus,
`${targetUser.tag} has been kicked for reason: ${reason}`,
undefined,
true
);
} catch (error) {
sendMessage(
message,
interaction,
"Can't kick this member, they might have a role higher than mine"
replyToImpetus(
impetus,
"Can't kick this member, they might have a role higher than mine",
undefined,
true
);
}
},
Expand Down
11 changes: 3 additions & 8 deletions src/commands/ping.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { CommandInteraction, Message } from "discord.js";
import { Command } from "../interfaces/command.interface";
import { sendMessage } from "../utilities/sendMessage";
import { replyToImpetus } from "../utilities/Impetus";

export default {
name: "ping",
description: "Check the bot's response",
category: "utility",
execute: async (interaction?: CommandInteraction, message?: Message) => {
sendMessage(
message,
interaction,
`Pong! (${interaction?.client.ws.ping || message?.client.ws.ping}ms)`
);
execute: async (impetus) => {
replyToImpetus(impetus, `Pong! (${impetus.client.ws.ping}ms)`);
},
} as Command;
Loading