Merge pull request 'Strongly typed config (and ts strict mode)' (#1) from nea/amyjr:typed-config into main
Reviewed-on: https://git.amy.rip/amy/amyjr/pulls/1
This commit is contained in:
commit
a2cf556bcf
7 changed files with 31 additions and 16 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import {ChatInputCommandInteraction, SharedSlashCommand} from "discord.js";
|
||||
import { Config } from "./config";
|
||||
|
||||
export class Command {
|
||||
constructor(){}
|
||||
async run (interaction: ChatInputCommandInteraction, config): Promise<void>{};
|
||||
slashCommand: SharedSlashCommand
|
||||
}
|
||||
export abstract class Command {
|
||||
abstract run(interaction: ChatInputCommandInteraction, config: Config): Promise<void>;
|
||||
abstract slashCommand: SharedSlashCommand
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,8 +11,9 @@ import {
|
|||
} from "discord.js";
|
||||
|
||||
import { getSongOnPreferredProvider } from "../helper.ts"
|
||||
import { Config } from "../config.ts";
|
||||
|
||||
function keepV(url) {
|
||||
function keepV(url: string): string {
|
||||
const urlObj = new URL(url);
|
||||
const vParam = urlObj.searchParams.get("v");
|
||||
|
||||
|
|
@ -26,7 +27,7 @@ function keepV(url) {
|
|||
}
|
||||
|
||||
export default class PingCommand extends Command {
|
||||
async run(interaction: ChatInputCommandInteraction, config) {
|
||||
async run(interaction: ChatInputCommandInteraction, config: Config) {
|
||||
await interaction.deferReply()
|
||||
|
||||
|
||||
|
|
@ -76,4 +77,4 @@ export default class PingCommand extends Command {
|
|||
InteractionContextType.Guild,
|
||||
InteractionContextType.PrivateChannel
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,9 +5,10 @@ import {
|
|||
InteractionContextType,
|
||||
SlashCommandBuilder
|
||||
} from "discord.js";
|
||||
import { Config } from "../config.ts";
|
||||
|
||||
export default class PingCommand extends Command {
|
||||
async run(interaction: ChatInputCommandInteraction, config) {
|
||||
async run(interaction: ChatInputCommandInteraction, config: Config) {
|
||||
await interaction.reply({
|
||||
content: 'Pong!',
|
||||
});
|
||||
|
|
@ -23,4 +24,4 @@ export default class PingCommand extends Command {
|
|||
InteractionContextType.Guild,
|
||||
InteractionContextType.PrivateChannel
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,9 +6,10 @@ import {
|
|||
InteractionContextType,
|
||||
SlashCommandBuilder
|
||||
} from "discord.js";
|
||||
import { Config } from "../config.ts";
|
||||
|
||||
export default class PingCommand extends Command {
|
||||
async run(interaction: ChatInputCommandInteraction, config) {
|
||||
async run(interaction: ChatInputCommandInteraction, config: Config) {
|
||||
const repo = interaction.options.getString("repo")
|
||||
const meow = await fetch(config.gitapi + repo).then(res => res.json());
|
||||
const embed = new EmbedBuilder()
|
||||
|
|
@ -38,4 +39,4 @@ export default class PingCommand extends Command {
|
|||
InteractionContextType.Guild,
|
||||
InteractionContextType.PrivateChannel
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
src/config.ts
Normal file
11
src/config.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import rawconfig from "../config.json" with {type: "json"};
|
||||
import {z} from 'zod';
|
||||
const configT = z.object({
|
||||
token: z.string(),
|
||||
listenbrainzAccount: z.string(),
|
||||
gitapi: z.string(),
|
||||
});
|
||||
export type Config = z.infer<typeof configT>;
|
||||
export const config: Config = configT.parse(rawconfig);
|
||||
|
||||
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
import config from "../config.json" with {type: "json"};
|
||||
import {
|
||||
Client,
|
||||
Events,
|
||||
|
|
@ -10,6 +9,7 @@ import path from "node:path";
|
|||
import fs from "node:fs";
|
||||
import { Command } from "./command.ts";
|
||||
import { fileURLToPath } from "url";
|
||||
import { config } from "./config.ts";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
|
@ -36,7 +36,7 @@ client.once(Events.ClientReady, async () => {
|
|||
console.log("Ready");
|
||||
const rest = new REST().setToken(config.token);
|
||||
const data = await rest.put(
|
||||
Routes.applicationCommands(client.user.id), { body: commands.map(command => command.slashCommand.toJSON()) },
|
||||
Routes.applicationCommands(client.user!.id), { body: commands.map(command => command.slashCommand.toJSON()) },
|
||||
);
|
||||
// @ts-ignore
|
||||
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
||||
|
|
@ -62,4 +62,4 @@ client.on(Events.InteractionCreate, async (interaction) => {
|
|||
|
||||
})
|
||||
|
||||
await client.login(config.token);
|
||||
await client.login(config.token);
|
||||
|
|
|
|||
|
|
@ -8,7 +8,8 @@
|
|||
"esModuleInterop": true,
|
||||
"allowImportingTsExtensions": true,
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true
|
||||
},
|
||||
"ts-node": {
|
||||
"compilerOptions": {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue