Skip to content

Commit

Permalink
refactor/chore: various overdue fixes and dep updates
Browse files Browse the repository at this point in the history
  • Loading branch information
juddisjudd committed Jul 24, 2024
1 parent 11e9163 commit a9d2190
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 286 deletions.
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc",
"start": "node build/index.js",
"start:production": "pnpm build && pnpm start",
"dev": "nodemon -L"
},
"build": "pnpm tsc",
"start": "pnpm node build/index.js",
"start:production": "pnpm build && pnpm start",
"dev": "pnpm nodemon -L"
},
"devDependencies": {
"@types/node": "^20.10.7",
"@types/node": "^20.14.12",
"@types/node-fetch": "^2.6.4",
"concurrently": "^8.2.2",
"eslint": "^9.1.1",
"eslint": "^9.7.0",
"nodemon": "^3.1.0",
"prettier": "3.2.5",
"prettier": "3.3.3",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
"typescript": "^5.5.4"
},
"dependencies": {
"@prisma/client": "^5.8.0",
"axios": "^1.6.8",
"discord.js": "^14.14.1",
"@prisma/client": "^5.17.0",
"axios": "^1.7.2",
"discord.js": "^14.15.3",
"dotenv": "^16.4.5",
"pino": "^9.0.0",
"pino-pretty": "^11.0.0",
"prisma": "^5.8.0"
"pino": "^9.3.1",
"pino-pretty": "^11.2.1",
"prisma": "^5.17.0"
}
}
460 changes: 229 additions & 231 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/commands/misc/whois.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SlashCommandBuilder, CommandInteraction, EmbedBuilder } from 'discord.js';
import { SlashCommandBuilder, CommandInteraction, EmbedBuilder, ChatInputCommandInteraction } from 'discord.js';
import { Command } from '../../interfaces/command';

const Whois: Command = {
Expand All @@ -9,7 +9,7 @@ const Whois: Command = {
option.setName('user').setDescription('The user to get information about').setRequired(true)
),

async run(interaction: CommandInteraction) {
async run(interaction: ChatInputCommandInteraction) {
const userOption = interaction.options.getUser('user', true);
const member = await interaction.guild?.members.fetch(userOption.id);

Expand Down
6 changes: 3 additions & 3 deletions src/commands/moderation/clearwarns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SlashCommandBuilder, PermissionFlagsBits, CommandInteraction, GuildMember } from 'discord.js';
import { SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction, GuildMember } from 'discord.js';
import { PrismaClient } from '@prisma/client';
import { Command } from '../../interfaces/command';
import logger from '../../utils/logger';
Expand All @@ -19,7 +19,7 @@ const ClearWarn: Command = {
.setRequired(true)
),

async run(interaction: CommandInteraction) {
async run(interaction: ChatInputCommandInteraction) {
if (!interaction.guild) {
await interaction.reply({ content: 'This command can only be used in a guild.', ephemeral: true });
return;
Expand All @@ -37,7 +37,7 @@ const ClearWarn: Command = {
await interaction.reply({ content: 'User not found in the guild.', ephemeral: true });
return;
}
const warnIdOption = (interaction.options as any).getString('warnid', true);
const warnIdOption = interaction.options.getString('warnid', true);

try {
if (warnIdOption.toLowerCase() === 'all') {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/moderation/fetchwarns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SlashCommandBuilder, CommandInteraction } from 'discord.js';
import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
import { PrismaClient } from '@prisma/client';
import { Command } from '../../interfaces/command';
import logger from '../../utils/logger';
Expand All @@ -11,7 +11,7 @@ const FetchWarns: Command = {
.setDescription('Display all warnings for a specific user')
.addUserOption((option) => option.setName('user').setDescription('The user to check').setRequired(true)),

async run(interaction: CommandInteraction) {
async run(interaction: ChatInputCommandInteraction) {
if (!interaction.guild) {
await interaction.reply({ content: 'This command can only be used in a guild.', ephemeral: true });
return;
Expand All @@ -30,7 +30,7 @@ const FetchWarns: Command = {
return;
}

const warningMessages = warnings.map((warn) => `ID: ${warn.id}, Reason: ${warn.reason}`).join('\n');
const warningMessages = warnings.map((warn: { id: number, reason: string }) => `ID: ${warn.id}, Reason: ${warn.reason}`).join('\n');
await interaction.reply({ content: `Warnings for <@${userOption.id}>:\n${warningMessages}`, ephemeral: false });
} catch (error) {
logger.error(error);
Expand Down
13 changes: 8 additions & 5 deletions src/commands/moderation/kick.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GuildMember, SlashCommandBuilder, PermissionFlagsBits, CommandInteraction } from 'discord.js';
import { GuildMember, SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction } from 'discord.js';
import { Command } from '../../interfaces/command';
import logger from '../../utils/logger';

Expand All @@ -9,7 +9,7 @@ const Kick: Command = {
.addUserOption((option) => option.setName('user').setDescription('User to kick').setRequired(true))
.addStringOption((option) => option.setName('reason').setDescription('Reason for kicking')),

async run(interaction: CommandInteraction) {
async run(interaction: ChatInputCommandInteraction) {
if (!interaction.guild) {
await interaction.reply({ content: 'This command can only be used in a server.', ephemeral: true });
return;
Expand All @@ -28,12 +28,15 @@ const Kick: Command = {
return;
}

const reasonOption = interaction.options.get('reason');
const reason = (reasonOption?.value as string) || "No reason provided";
const reasonOption = interaction.options.getString('reason');
const reason = reasonOption || 'No reason provided';

try {
await member.kick(reason);
await interaction.reply({ content: `${member.user.tag} has been **kicked**. Reason: ${reason}`, ephemeral: false });
await interaction.reply({
content: `${member.user.tag} has been **kicked**. Reason: ${reason}`,
ephemeral: false,
});
} catch (error) {
logger.error(error);
await interaction.reply({ content: 'Failed to kick the user.', ephemeral: true });
Expand Down
26 changes: 11 additions & 15 deletions src/commands/moderation/purge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SlashCommandBuilder, CommandInteraction, PermissionFlagsBits, TextChannel } from 'discord.js';
import { SlashCommandBuilder, ChatInputCommandInteraction, PermissionFlagsBits, TextChannel } from 'discord.js';
import { Command } from '../../interfaces/command';
import logger from '../../utils/logger';

Expand Down Expand Up @@ -37,13 +37,9 @@ const Purge: Command = {
)
),

async run(interaction: CommandInteraction) {
if (!(interaction instanceof CommandInteraction)) {
return;
}

const subcommand = (interaction.options as any).getSubcommand();
const count = (interaction.options as any).getInteger('count', true);
async run(interaction: ChatInputCommandInteraction) {
const subcommand = interaction.options.getSubcommand();
const count = interaction.options.getInteger('count');

if (!(interaction.channel instanceof TextChannel)) {
await interaction.reply({ content: 'This command can only be used in text channels.', ephemeral: true });
Expand All @@ -52,7 +48,7 @@ const Purge: Command = {

if (subcommand === 'any') {
try {
const messages = await interaction.channel.messages.fetch({ limit: count });
const messages = await interaction.channel.messages.fetch({ limit: count ?? undefined });
await interaction.channel.bulkDelete(messages, true);
await interaction.reply({ content: `Successfully deleted ${messages.size} messages.`, ephemeral: true });
} catch (error) {
Expand All @@ -65,7 +61,7 @@ const Purge: Command = {
const allMessages = await interaction.channel.messages.fetch({ limit: 100 });
const userMessages = allMessages.filter((msg) => msg.author.id === user.id);

const messagesToDelete = userMessages.first(count);
const messagesToDelete = userMessages.first(count ?? 0);
const deletedMessages = await interaction.channel.bulkDelete(messagesToDelete, true);

await interaction.reply({
Expand All @@ -82,16 +78,16 @@ const Purge: Command = {
}

if (subcommand === 'after') {
const messageIdOption = interaction.options.get('messageid');
const countOption = interaction.options.get('count');
const messageIdOption = interaction.options.getString('messageid');
const countOption = interaction.options.getInteger('count');

if (!messageIdOption || typeof messageIdOption.value !== 'string') {
if (!messageIdOption) {
await interaction.reply({ content: 'Invalid message ID.', ephemeral: true });
return;
}

const messageId = messageIdOption.value;
const count = countOption && typeof countOption.value === 'number' ? countOption.value : 1;
const messageId = messageIdOption;
const count = countOption || 1;

try {
const messages = await interaction.channel.messages.fetch({ limit: 100 });
Expand Down
18 changes: 12 additions & 6 deletions src/commands/moderation/warn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { SlashCommandBuilder, PermissionFlagsBits, CommandInteraction, User, DiscordAPIError } from 'discord.js';
import {
SlashCommandBuilder,
PermissionFlagsBits,
ChatInputCommandInteraction,
User,
DiscordAPIError,
} from 'discord.js';
import { PrismaClient, Prisma } from '@prisma/client';
import { Command } from '../../interfaces/command';
import { handleMemberWarn } from '../../events/onMemberWarn';
Expand All @@ -15,7 +21,7 @@ const Warn: Command = {
option.setName('reason').setDescription('The reason for the warning').setRequired(true)
),

async run(interaction: CommandInteraction) {
async run(interaction: ChatInputCommandInteraction) {
if (!interaction.guild) {
await interaction.reply({ content: 'This command can only be used in a guild.', ephemeral: true });
return;
Expand All @@ -27,10 +33,10 @@ const Warn: Command = {
}

const userOption = interaction.options.getUser('user', true);
const reasonOptionRaw = interaction.options.get('reason')?.value;
const reasonOptionRaw = interaction.options.getString('reason');

if (typeof reasonOptionRaw !== 'string') {
await interaction.reply({ content: 'The reason must be a string.', ephemeral: true });
if (reasonOptionRaw === null) {
await interaction.reply({ content: 'Please provide a reason for the warning.', ephemeral: true });
return;
}

Expand Down Expand Up @@ -101,7 +107,7 @@ const Warn: Command = {
};

async function sendWarningDM(
interaction: CommandInteraction,
interaction: ChatInputCommandInteraction,
user: User,
reason: string,
timeoutDuration: number | null
Expand Down
11 changes: 9 additions & 2 deletions src/events/onPurge.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { Collection, Message, EmbedBuilder, TextChannel, GuildTextBasedChannel, PartialMessage } from 'discord.js';
import {
ReadonlyCollection,
Message,
EmbedBuilder,
TextChannel,
GuildTextBasedChannel,
PartialMessage,
} from 'discord.js';
import { Bot } from '..';
import { log_channel } from '../settings.json';
import logger from '../utils/logger';

export const onMessageDeleteBulk = async (
messages: Collection<string, Message | PartialMessage>,
messages: ReadonlyCollection<string, Message<boolean> | PartialMessage>,
channel: GuildTextBasedChannel
) => {
if (!messages.first()?.guild) return;
Expand Down
13 changes: 9 additions & 4 deletions src/interfaces/command.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {
CommandInteraction,
ChatInputCommandInteraction,
AutocompleteInteraction,
SlashCommandBuilder,
SlashCommandSubcommandsOnlyBuilder,
SlashCommandOptionsOnlyBuilder,
CacheType,
} from 'discord.js';

export interface Command {
data: Omit<SlashCommandBuilder, 'addSubcommandGroup' | 'addSubcommand'> | SlashCommandSubcommandsOnlyBuilder;
run?: (interaction: CommandInteraction) => Promise<void>;
autocomplete?: (interaction: AutocompleteInteraction) => Promise<void>;
data:
| Omit<SlashCommandBuilder, 'addSubcommandGroup' | 'addSubcommand'>
| SlashCommandSubcommandsOnlyBuilder
| SlashCommandOptionsOnlyBuilder;
run?: (interaction: ChatInputCommandInteraction<CacheType>) => Promise<void>;
autocomplete?: (interaction: AutocompleteInteraction<CacheType>) => Promise<void>;
}

0 comments on commit a9d2190

Please sign in to comment.