Skip to content

Commit

Permalink
Improve TypeScript config (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
JstnMcBrd authored Aug 12, 2024
1 parent e3a62c4 commit 07ac0b2
Show file tree
Hide file tree
Showing 15 changed files with 928 additions and 474 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"eslint.experimental.useFlatConfig": true,
"eslint.useFlatConfig": true,
"files.associations": {
".env.example": "properties"
},
Expand Down
1,330 changes: 897 additions & 433 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
"type": "module",
"scripts": {
"build": "tsc",
"commands": "node ./dist/deployCommands.js",
"commands": "node dist/deployCommands.js",
"lint": "eslint .",
"start": "node ./dist/main.js",
"start": "node dist/main.js",
"test": "echo \"No tests\""
},
"dependencies": {
"@colors/colors": "^1.6.0",
"cleverbot-free": "^2.0.3",
"discord.js": "^14.15.2",
"discord.js": "^14.15.3",
"dotenv": "^16.4.5"
},
"devDependencies": {
"@jstnmcbrd/eslint-config": "^1.0.0",
"eslint": "^8.57.0",
"typescript": "^5.4.5"
"typescript": "^5.5.4"
}
}
4 changes: 2 additions & 2 deletions src/commands/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class CommandHandler extends SlashCommandBuilder {
* @param id The new ID to use
*/
public setId(id: Snowflake): this {
Reflect.set(this, 'id', id);
Object.assign(this, { id });
return this;
}

Expand All @@ -49,7 +49,7 @@ export class CommandHandler extends SlashCommandBuilder {
await this.execution(interaction);
}
catch (err) {
error(`Command handler for /${this.name} encountered an error:`);
error(`Command handler for ${this.getSlashName()} encountered an error:`);
error(err);
void replyWithError(interaction, err);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function createHelpEmbed(user: ClientUser): EmbedBuilder {
)
.setFooter({
text: `Version ${version}\nLast Updated`,
iconURL: avatarURL ?? undefined,
...(avatarURL && { iconURL: avatarURL }),
})
.setTimestamp(lastUpdated);
}
Expand Down
4 changes: 3 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { exit } from 'node:process';

import type { Client } from 'discord.js';

import type { CommandHandler } from './CommandHandler.js';
Expand Down Expand Up @@ -59,7 +61,7 @@ export async function syncCommands(client: Client<true>): Promise<void> {

if (!areCommandsInSync(deployedCommands, localCommands)) {
error('Deployed commands are outdated. Please run the deployment script to update them.');
process.exit(1);
exit(1);
}

deployedCommands.forEach(command => getCommandHandler(command.name)?.setId(command.id));
Expand Down
4 changes: 2 additions & 2 deletions src/commands/invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const invite = new CommandHandler()
.addComponents(button);

await interaction.reply({
embeds: embed ? [embed] : undefined,
...(embed && { embeds: [embed] }),
components: [row],
ephemeral: true,
});
Expand All @@ -39,7 +39,7 @@ function createDisabledEmbed(): EmbedBuilder {
* @param disabled Whether the button should be disabled
* @returns A button that invites the bot to a new server
*/
function createInviteButton(user: ClientUser, disabled: boolean): ButtonBuilder {
function createInviteButton(user: ClientUser, disabled = false): ButtonBuilder {
const inviteLink = createInviteLink(user);
return new ButtonBuilder()
.setLabel('Add to Server')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/unwhitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const unwhitelist = new CommandHandler()
.setName('unwhitelist')
.setDescription('Disallow me from responding to messages in this channel')
.setExecution(async (interaction) => {
if (interaction.channel === null) {
throw new TypeError('Channel cannot be null.');
if (!interaction.channel) {
throw new TypeError('Channel must be defined.');
}

let embed: EmbedBuilder;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/whitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const whitelist = new CommandHandler()
.setName('whitelist')
.setDescription('Allow me to respond to messages in this channel')
.setExecution(async (interaction) => {
if (interaction.channel === null) {
throw new TypeError('Channel cannot be null.');
if (!interaction.channel) {
throw new TypeError('Channel must be defined.');
}

let embed: EmbedBuilder;
Expand Down
2 changes: 1 addition & 1 deletion src/deployCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getCommandHandlers } from './commands/index.js';
import { getToken, load as loadEnv } from './memory/env.js';
import { debug, error, info } from './logger.js';

async function deployCommands(c: Client<true>) {
async function deployCommands(c: Client<true>): Promise<void> {
debug(`\tUser: ${c.user.username} (${c.user.id})`);

info('Deploying commands...');
Expand Down
16 changes: 1 addition & 15 deletions src/events/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,13 @@ export class EventHandler<K extends keyof ClientEvents = keyof ClientEvents> {
this.name = name;
}

/**
* Unnecessary because the name is set in the constructor and defines the generic class type.
* Unsafe because the generic type should never change, so the name should never change.
*/
// /**
// * Sets the name of the event this handler is for.
// *
// * @param name The name to use
// */
// public setName (name: K): this {
// Reflect.set(this, 'name', name);
// return this;
// }

/**
* Sets whether this event can only fire once.
*
* @param once Whether this event can only fire once
*/
public setOnce(once: boolean): this {
Reflect.set(this, 'once', once);
Object.assign(this, { once });
return this;
}

Expand Down
8 changes: 4 additions & 4 deletions src/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { ready } from './ready.js';
/** The list of all event handlers. */
const eventHandlers = new Map<string, EventHandler>();

addEventHandler(error as EventHandler);
addEventHandler(interactionCreate as EventHandler);
addEventHandler(messageCreate as EventHandler);
addEventHandler(ready as EventHandler);
addEventHandler(error);
addEventHandler(interactionCreate);
addEventHandler(messageCreate);
addEventHandler(ready);

/**
* Add the given event handler to the list of event handlers.
Expand Down
3 changes: 1 addition & 2 deletions src/memory/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ export async function generateContext(channel: TextBasedChannel): Promise<void>
function isWhitelistCommandReply(message: Message): boolean {
return isEmpty(message)
&& isFromSelf(message)
&& message.interaction !== null
&& message.interaction.commandName === whitelist.name;
&& message.interaction?.commandName === whitelist.name;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const version = getVersion();
export const githubURL = new URL('https://github.com/JstnMcBrd/discord-cleverbot');

/** When this code was last changed. */
export const lastUpdated = new Date(2024, 3, 24, 1, 0);
export const lastUpdated = new Date(2024, 7, 11, 19, 0);
// Year, month (0-11), day of month, hour (0-23), minutes

/** How fast the bot sends messages (in characters per second). */
Expand Down
9 changes: 6 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
"outDir": "dist",
"sourceMap": true,
"strict": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"noImplicitReturns": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noUncheckedIndexedAccess": true,
"verbatimModuleSyntax": true,
"isolatedModules": true,
// FIXME https://github.com/discordjs/discord.js/issues/10358
"skipLibCheck": true,
},
}

0 comments on commit 07ac0b2

Please sign in to comment.