Files
unicord/src/services/client.ts
T

89 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-11-10 01:55:07 -06:00
import { API } from './api';
import { Gateway } from './gateway';
import { Processor } from './processor';
import { Intent, type ClientConfig } from '../types/common';
import { exists, isNil } from '../utils/index';
import type { Handler } from '../types/hander';
import { DiscordMessage, Message } from '../types/message';
2024-11-10 01:55:07 -06:00
import { Guilds } from './guilds';
import { Users } from './users';
import { Messages } from './messages';
2024-11-10 01:55:07 -06:00
export class Client {
readonly token: string;
readonly application_id: string;
readonly intents: Intent[] = [Intent.DEFAULT];
prefix: string;
// Internal services
protected api: API;
protected processor: Processor;
protected gateway: Gateway;
2024-11-10 01:55:07 -06:00
guilds: Guilds;
users: Users;
messages: Messages
2024-11-10 01:55:07 -06:00
constructor(config: ClientConfig) {
// Validate the configuration
Client.validateConfig(config);
// Save the configuration
this.token = config.token;
this.application_id = config.application_id;
this.intents = config.intents ?? this.intents;
this.prefix = config.prefix ?? '!';
// Initialize internal services
this.api = new API(this);
this.processor = new Processor(this, this.api);
this.gateway = new Gateway(this, this.processor, this.api);
2024-11-10 01:55:07 -06:00
this.guilds = new Guilds(this.api, this.processor);
this.users = new Users(this.api, this.processor);
this.messages = new Messages(this, this.api, this.processor);
2024-11-10 01:55:07 -06:00
}
private static validateConfig(config: ClientConfig) {
if (isNil(config.token)) {
throw new Error('Token is required');
}
if (isNil(config.application_id)) {
throw new Error('Application ID is required');
}
if (exists(config.prefix) && typeof config.prefix !== 'string') {
throw new Error('Prefix must be a string');
}
if (exists(config.intents)) {
if (!Array.isArray(config.intents)) {
throw new Error('Intents must be an array');
}
for (const intent of config.intents) {
if (!Intent[intent]) {
throw new Error(`Invalid intent: ${intent}`);
}
}
}
}
sendMessage = async (channel_id: string, message: Message | string) => {
if (typeof message === 'string') {
message = new Message().setContent(message);
}
return Message.hydrate(DiscordMessage.fromAPIResponse(await this.api.post(`/channels/${channel_id}/messages`, message.toJSON())), this, this.api);
2024-11-10 01:55:07 -06:00
}
chatCommands = {
register: (name: string | string[], handler: Handler) => this.processor.chat_commands.register(name, handler),
unregister: (name: string | string[]) => this.processor.chat_commands.unregister(name),
has: (name: string) => this.processor.chat_commands.has(name)
}
slashCommands = {
register: (name: string, handler: Handler) => this.processor.slash_commands.register(name, handler),
unregister: (name: string) => this.processor.slash_commands.unregister(name),
has: (name: string) => this.processor.slash_commands.has(name)
2024-11-10 01:55:07 -06:00
}
connect = () => this.gateway.connect();
}