Skip to content

Instantly share code, notes, and snippets.

@favna
Last active January 11, 2022 16:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save favna/26ed5b2df82d424203b71b9ffbce85c4 to your computer and use it in GitHub Desktop.
Save favna/26ed5b2df82d424203b71b9ffbce85c4 to your computer and use it in GitHub Desktop.
Custom Sapphire Client with TypeScript
import { isGuildMessage } from '#utils/common';
import { container, SapphireClient } from '@sapphire/framework';
import type { Message } from 'discord.js';
export class MyCustomClient extends SapphireClient {
public music: MusicManager;
public constructor() {
super({
caseInsensitiveCommands: true,
caseInsensitivePrefixes: true,
defaultPrefix: '!',
intents: [
'GUILDS',
'GUILD_MEMBERS',
'GUILD_BANS',
'GUILD_EMOJIS_AND_STICKERS',
'GUILD_VOICE_STATES',
'GUILD_MESSAGES',
'GUILD_MESSAGE_REACTIONS',
'DIRECT_MESSAGES',
'DIRECT_MESSAGE_REACTIONS'
],
loadDefaultErrorListeners: false,
partials: ['CHANNEL']
});
this.music = new MusicManager(this);
}
public async login(token?: string) {
container.database = await createMyDatabaseConnection();
return super.login(token);
}
public async destroy() {
await container.database.destroy();
return super.destroy();
}
/**
* Retrieves the prefix for the guild.
* @param message The message that gives context.
*/
public fetchPrefix = async (message: Message) => {
if (isGuildMessage(message)) return container.database.read('guilds', message.guild.id, 'prefix');
return ['!', ''] as readonly string[];
};
}
declare module 'discord.js' {
interface Client {
readonly music: MusicManager;
}
}
declare module '@sapphire/pieces' {
interface Container {
database: MyDatabase;
}
}
// ===========================================================================================
// | IGNORE ANYTHING BELOW. IT IS ONLY SO TYPESCRIPT DOES NOT COMPLAIN WITH THIS SAMPLE CODE |
// ===========================================================================================
declare interface MyDatabase {
read(tableName: string, guildId: string, databaseKey: string): Promise<string>;
destroy(): Promise<void>;
}
declare function createMyDatabaseConnection(): Promise<MyDatabase>;
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
declare class MusicManager {
public constructor(client: MyCustomClient);
public addSong(songName: string): void;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment