NodeJS TypeScript Setup (#1)
* bot can login to discord * changed node and npm to iron lts * added typescript runnables * more dev scripts * readme update on scripts * event handling skeleton * fixed compiler target issue
This commit is contained in:
24
src/client.ts
Normal file
24
src/client.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Client, GatewayIntentBits } from "discord.js";
|
||||
import { registerEvents } from "./utils/events.js";
|
||||
import Events from "./events/index.js";
|
||||
|
||||
// Import keys/tokens
|
||||
import Keys from "./keys.js";
|
||||
|
||||
const client = new Client({
|
||||
intents: [
|
||||
GatewayIntentBits.Guilds,
|
||||
GatewayIntentBits.GuildMembers,
|
||||
GatewayIntentBits.GuildMessages,
|
||||
GatewayIntentBits.MessageContent
|
||||
]
|
||||
});
|
||||
|
||||
registerEvents(client, Events)
|
||||
|
||||
// Try to log in the client
|
||||
client.login(Keys.clientToken)
|
||||
.catch((error) => {
|
||||
console.error('[Login Error]', error);
|
||||
process.exit(1);
|
||||
});
|
||||
7
src/events/index.ts
Normal file
7
src/events/index.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Event } from '../utils/index.js'
|
||||
import ready from './ready.js'
|
||||
|
||||
// Centralized export for all events
|
||||
export default [
|
||||
ready
|
||||
] as Event[] // staticly is better ts practice, dynamic exporting is possible
|
||||
5
src/events/ready.ts
Normal file
5
src/events/ready.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { event, Events } from '../utils/index.js'
|
||||
|
||||
export default event(Events.ClientReady, ({ log }, client) => {
|
||||
return log(`Logged in as ${client.user.username}.`)
|
||||
})
|
||||
1
src/index.ts
Normal file
1
src/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
import('./client.js')
|
||||
7
src/keys.ts
Normal file
7
src/keys.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { getEnvVar } from "./utils/env.js"
|
||||
|
||||
export const Keys = {
|
||||
clientToken: getEnvVar('CLIENT_TOKEN')
|
||||
} as const // readonly keys
|
||||
|
||||
export default Keys
|
||||
21
src/utils/env.ts
Normal file
21
src/utils/env.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { resolve } from "path"
|
||||
import { config } from "dotenv"
|
||||
|
||||
// Find config - ONLY WORKS WITH NODEMON
|
||||
const envFile = process.env.NODE_ENV === "development" ? ".dev.env" : ".env"
|
||||
|
||||
// resolve config file
|
||||
const envFilePath = resolve(process.cwd(), envFile)
|
||||
|
||||
// set current environment variable file
|
||||
config({ path: envFilePath })
|
||||
|
||||
// Getter for environment variables
|
||||
export function getEnvVar(name: string, fallback?: string): string {
|
||||
const value = process.env[name] ?? fallback
|
||||
if (value == undefined)
|
||||
throw new Error(`Environment variable ${name} is not set.`)
|
||||
|
||||
// return env variable
|
||||
return value
|
||||
}
|
||||
43
src/utils/events.ts
Normal file
43
src/utils/events.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import type { ClientEvents, Awaitable, Client } from 'discord.js';
|
||||
|
||||
// Export events through here to reduce amount of imports
|
||||
export { Events } from 'discord.js';
|
||||
|
||||
export type LogMethod = (...args: unknown[]) => void;
|
||||
export type EventKeys = keyof ClientEvents; // only wants keys of ClientEvents object
|
||||
|
||||
// Event properties
|
||||
export interface EventProps {
|
||||
client: Client;
|
||||
log: LogMethod;
|
||||
}
|
||||
export type EventCallback<T extends EventKeys> = (
|
||||
props: EventProps,
|
||||
...args: ClientEvents[T]
|
||||
) => Awaitable<unknown>; // Method can be synchronous or async, unknown so we can return anything
|
||||
|
||||
// Event interface
|
||||
export interface Event<T extends EventKeys = EventKeys> {
|
||||
key: T;
|
||||
callback: EventCallback<T>;
|
||||
}
|
||||
|
||||
export function event<T extends EventKeys>(key: T, callback: EventCallback<T>): Event<T> {
|
||||
return { key, callback };
|
||||
}
|
||||
|
||||
export function registerEvents(client: Client, events: Event[]): void {
|
||||
for (const { key, callback } of events) {
|
||||
client.on(key, (...args) => {
|
||||
// Create a new log method for this event
|
||||
const log = console.log.bind(console, `[Event: ${key}]`);
|
||||
|
||||
// Handle Errors, call callback, log errors as needed
|
||||
try {
|
||||
callback({ client, log }, ...args);
|
||||
} catch (error) {
|
||||
log('[Uncaught Error]', error);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
3
src/utils/index.ts
Normal file
3
src/utils/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
// Centralized import index
|
||||
export * from './env.js';
|
||||
export * from './events.js';
|
||||
Reference in New Issue
Block a user