2022-09-20 19:32:41 -05:00
|
|
|
import { ComponentStyle, Context, Gateway, Guilds, Message, Embed } from './lib';
|
2022-10-07 01:12:31 -05:00
|
|
|
import { SlashCommand, SlashCommandFunction, ChatCommand, Command, CommandFunction, Intent, Presence } from './types';
|
|
|
|
|
export {
|
|
|
|
|
ComponentStyle,
|
|
|
|
|
Context,
|
|
|
|
|
Gateway,
|
|
|
|
|
Guilds,
|
|
|
|
|
Message,
|
|
|
|
|
Embed,
|
|
|
|
|
Command,
|
|
|
|
|
CommandFunction,
|
|
|
|
|
ChatCommand,
|
|
|
|
|
Intent,
|
|
|
|
|
Presence
|
|
|
|
|
};
|
2022-09-17 20:25:07 -05:00
|
|
|
|
|
|
|
|
type Config = {
|
|
|
|
|
token: string;
|
|
|
|
|
application_id: string;
|
|
|
|
|
prefix?: string;
|
|
|
|
|
intents?: Intent[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export class Client {
|
|
|
|
|
readonly prefix: string;
|
|
|
|
|
readonly token: string;
|
|
|
|
|
readonly application_id: string;
|
|
|
|
|
private readonly gateway: Gateway;
|
2022-10-07 01:12:31 -05:00
|
|
|
private readonly client_commands: Map<string, ChatCommand | CommandFunction> = new Map();
|
|
|
|
|
private readonly slash_commands: Map<string, SlashCommand | SlashCommandFunction> = new Map();
|
2022-09-17 20:25:07 -05:00
|
|
|
readonly guilds: Guilds;
|
|
|
|
|
|
|
|
|
|
constructor({ token, application_id, prefix, intents }: Config) {
|
|
|
|
|
this.token = token;
|
|
|
|
|
this.application_id = application_id;
|
|
|
|
|
this.prefix = prefix ?? '!';
|
|
|
|
|
this.gateway = new Gateway({ token, intents, client: this });
|
|
|
|
|
this.guilds = new Guilds({ token, gateway: this.gateway });
|
2022-09-20 19:32:41 -05:00
|
|
|
// Do some default stuff
|
|
|
|
|
this.initialize();
|
2022-09-17 20:25:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get user() {
|
|
|
|
|
return this.gateway.user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get handlers() {
|
|
|
|
|
return this.gateway.handlers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get interactions() {
|
|
|
|
|
return this.gateway.interactions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get api() {
|
|
|
|
|
return this.gateway.api;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public connect = () => {
|
|
|
|
|
this.gateway.connect();
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public reconnect = () => {
|
|
|
|
|
this.gateway.reconnect();
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public setPresence = (presence: Presence) => {
|
|
|
|
|
this.gateway.setPresence(presence);
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
send = (channel_id: string, message: Message | string) => {
|
|
|
|
|
if (typeof message === 'string') {
|
|
|
|
|
message = new Message().setContent(message);
|
|
|
|
|
}
|
|
|
|
|
return this.api.post(`/channels/${channel_id}/messages`, message);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public commands = {
|
2022-10-07 01:12:31 -05:00
|
|
|
chat: {
|
|
|
|
|
register: (command: ChatCommand | string, handler?: CommandFunction) => {
|
|
|
|
|
if (typeof command === 'string') {
|
|
|
|
|
this.client_commands.set(command, handler);
|
|
|
|
|
} else {
|
|
|
|
|
this.client_commands.set(command.name, command);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
get: (command_name: string) => {
|
|
|
|
|
return this.client_commands.get(command_name);
|
|
|
|
|
},
|
|
|
|
|
execute: (command_name: string, context: Context, ...args: any[]) => {
|
|
|
|
|
const command = this.client_commands.get(command_name);
|
|
|
|
|
if (command) {
|
|
|
|
|
command instanceof ChatCommand
|
|
|
|
|
? command.execute(this, context, args.slice(1))
|
|
|
|
|
: command(this, context, args.slice(1));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
delete: (command_name: string) => {
|
|
|
|
|
this.client_commands.delete(command_name);
|
|
|
|
|
},
|
|
|
|
|
list: () => {
|
|
|
|
|
return Array.from(this.client_commands.entries());
|
|
|
|
|
},
|
|
|
|
|
map: () => {
|
|
|
|
|
return this.client_commands;
|
2022-09-17 20:25:07 -05:00
|
|
|
}
|
|
|
|
|
},
|
2022-10-07 01:12:31 -05:00
|
|
|
application: {
|
|
|
|
|
register: (command: SlashCommand | Record<string, string>, handler?: SlashCommandFunction) => {
|
|
|
|
|
const payload =
|
|
|
|
|
command instanceof SlashCommand
|
|
|
|
|
? command.toJSON()
|
|
|
|
|
: {
|
|
|
|
|
name: command.name,
|
|
|
|
|
description: command.description,
|
|
|
|
|
type: command.type ?? 1
|
|
|
|
|
};
|
|
|
|
|
this.api
|
|
|
|
|
.post(`/applications/${this.application_id}/commands`, payload)
|
|
|
|
|
.then((response) => {
|
|
|
|
|
console.log(response);
|
|
|
|
|
if (command instanceof SlashCommand) {
|
|
|
|
|
this.slash_commands.set(command.name, command);
|
|
|
|
|
} else {
|
|
|
|
|
this.slash_commands.set(command.name, handler);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
this.log.error('Error registering application command', error);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
get: (command_name: string) => {
|
|
|
|
|
return this.slash_commands.get(command_name);
|
|
|
|
|
},
|
|
|
|
|
list: () => {
|
|
|
|
|
return Array.from(this.slash_commands.entries());
|
|
|
|
|
},
|
|
|
|
|
map: () => {
|
|
|
|
|
return this.slash_commands;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private log = {
|
|
|
|
|
error: (error_or_message: Error | string, error?: Error) => {
|
|
|
|
|
if (error && typeof error_or_message === 'string') {
|
|
|
|
|
console.error(`[discord.ts] ${error_or_message}:`, error);
|
|
|
|
|
} else {
|
|
|
|
|
console.error('[discord.ts] Error:', error_or_message);
|
2022-09-17 20:25:07 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-09-20 19:32:41 -05:00
|
|
|
|
|
|
|
|
private initialize = () => {
|
|
|
|
|
// Register default help command
|
2022-10-07 01:12:31 -05:00
|
|
|
this.commands.chat.register('help', (client, context, _args) => {
|
|
|
|
|
const embed = new Embed()
|
|
|
|
|
.setTitle('Commands')
|
|
|
|
|
.setDescription('This is a list of commands you can use.')
|
|
|
|
|
.setColor('#f6c221');
|
2022-09-20 19:32:41 -05:00
|
|
|
|
2022-10-07 01:12:31 -05:00
|
|
|
const commands = client.commands.chat.list();
|
2022-09-20 19:32:41 -05:00
|
|
|
|
|
|
|
|
for (const [name, command] of commands) {
|
|
|
|
|
embed.addField({
|
|
|
|
|
name: name,
|
2022-10-07 01:12:31 -05:00
|
|
|
value: command instanceof ChatCommand ? command.description : 'No description provided'
|
2022-09-20 19:32:41 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.reply(embed.toMessage());
|
|
|
|
|
});
|
2022-10-07 01:12:31 -05:00
|
|
|
};
|
2022-09-17 20:25:07 -05:00
|
|
|
}
|