|
| 1 | +/****************************************************************************** |
| 2 | + * @file src/lib/Client.ts |
| 3 | + * @fileoverview Exports the Client class - the main class in Fuwa.JS - alongside other |
| 4 | + * helper functions, interfaces, types, etc. |
| 5 | + *****************************************************************************/ |
| 6 | +/// <reference types="node" /> |
| 7 | +import Request from './Request'; |
| 8 | +import Cache from './_Cache'; |
| 9 | +import Debug from './_Debug'; |
| 10 | +import { UserStatus, ActivityType } from './_DiscordAPI'; |
| 11 | +import User from './discord/User'; |
| 12 | +import Response from './Response'; |
| 13 | +import Emitter from './Emitter'; |
| 14 | +import { CommandCallback, commandOptions } from './Command'; |
| 15 | +import Reaction from './discord/Reaction'; |
| 16 | +export declare type statusType = 'playing' | 'listening' | 'streaming' | 'competing'; |
| 17 | +/** |
| 18 | + * status options for bot |
| 19 | + */ |
| 20 | +export interface StatusOptions { |
| 21 | + /** |
| 22 | + * The status message to be displayed |
| 23 | + */ |
| 24 | + name: string; |
| 25 | + /** |
| 26 | + * The available status types are playing, listening, streaming, and |
| 27 | + * competing. |
| 28 | + */ |
| 29 | + type?: ActivityType; |
| 30 | + /** |
| 31 | + * The URL of a stream |
| 32 | + */ |
| 33 | + url?: string; |
| 34 | + /** |
| 35 | + * The status of your bot. Online by default |
| 36 | + */ |
| 37 | + status?: UserStatus; |
| 38 | + /** |
| 39 | + * Whether or not the bot is afk. |
| 40 | + */ |
| 41 | + afk?: boolean; |
| 42 | +} |
| 43 | +export interface Events { |
| 44 | + ready(): void | Promise<void>; |
| 45 | + message(req: Request, res: Response): void | Promise<void>; |
| 46 | + commandNotFound(req: Request, cmd: CommandCallback): void | Promise<void>; |
| 47 | + reaction(reaction: Reaction): any; |
| 48 | +} |
| 49 | +export interface clientOptions { |
| 50 | + /** |
| 51 | + * The owners' discord ID |
| 52 | + */ |
| 53 | + owners?: string[] | string; |
| 54 | + /** |
| 55 | + * To turn on the debug mode, not recommed to turn this on unless your debugging |
| 56 | + * the library. |
| 57 | + */ |
| 58 | + debug?: boolean; |
| 59 | + /** |
| 60 | + * If this is turned on (true) When someone mentions your bot it will behave |
| 61 | + * as a prefix. |
| 62 | + */ |
| 63 | + useMentionPrefix?: boolean; |
| 64 | + /** |
| 65 | + * |
| 66 | + */ |
| 67 | + builtinCommands?: { |
| 68 | + help?: { |
| 69 | + embedColor?: string | number; |
| 70 | + } | false; |
| 71 | + }; |
| 72 | + /** |
| 73 | + * @see GatewayIntents |
| 74 | + */ |
| 75 | + intents: number; |
| 76 | + /** |
| 77 | + * If the bot should cache guilds/channels/users or not. |
| 78 | + * It's suggested to keep this on for smaller bots |
| 79 | + * but for larger ones turn this off, |
| 80 | + * caching increases the speed of sending messages, but takes up memory. |
| 81 | + * meaning caching on = faster guild replies |
| 82 | + * caching off = more memory for other tasks |
| 83 | + */ |
| 84 | + cache?: true; |
| 85 | + /** |
| 86 | + * Settings for caching |
| 87 | + */ |
| 88 | + cachingSettings?: { |
| 89 | + /** |
| 90 | + * Clear the cache after a certain amount of time (in ms) |
| 91 | + * If this is false then the cache will never be cleared |
| 92 | + */ |
| 93 | + clearAfter?: number | false; |
| 94 | + cacheOptions?: { |
| 95 | + guilds: boolean; |
| 96 | + channels: boolean; |
| 97 | + users: boolean; |
| 98 | + }; |
| 99 | + /** |
| 100 | + * Maximum amount of items to cache at once. Set this to 0 if you want |
| 101 | + * an unlimited cache size |
| 102 | + */ |
| 103 | + maxSize?: number; |
| 104 | + }; |
| 105 | +} |
| 106 | +/** |
| 107 | + * The Client Class |
| 108 | + * @description The client class is the main starting point of your discord bot. |
| 109 | + * ```typescript |
| 110 | + * const fuwa = require('fuwa.js'); // Import Fuwa library |
| 111 | + * const client = new fuwa.Client('?'); // Create and initialize a Client |
| 112 | + * ``` |
| 113 | + */ |
| 114 | +declare class Client extends Emitter { |
| 115 | + bot: User; |
| 116 | + protected debug: Debug; |
| 117 | + private sessionId; |
| 118 | + cache: Cache; |
| 119 | + protected status: any; |
| 120 | + protected events: Map<keyof Events, Function>; |
| 121 | + protected prefix: string | string[] | ((req: Request) => Promise<string> | string); |
| 122 | + protected options: clientOptions; |
| 123 | + protected loop?: NodeJS.Timeout; |
| 124 | + protected commands: Map<string, { |
| 125 | + cb: CommandCallback; |
| 126 | + options: commandOptions; |
| 127 | + }[]>; |
| 128 | + protected middleware: CommandCallback[]; |
| 129 | + /** |
| 130 | + * The Bot Token |
| 131 | + */ |
| 132 | + token: string; |
| 133 | + /** |
| 134 | + * @param prefix The prefix for your bot |
| 135 | + */ |
| 136 | + constructor(prefix: string | string[] | ((req: Request) => Promise<string> | string), options?: clientOptions); |
| 137 | + /** |
| 138 | + * Command function |
| 139 | + * @param name Command name(s). |
| 140 | + * @param cb The function that is called when the command is ran. |
| 141 | + * @param options Options for your command. |
| 142 | + * @returns Command Options |
| 143 | + * @example |
| 144 | + * ```typescript |
| 145 | + * cli.command(['ping', 'latency'], (req, res) => { |
| 146 | + * res.send('Pong!'); |
| 147 | + * |
| 148 | + * }); |
| 149 | + * ``` |
| 150 | + */ |
| 151 | + command(name: string | string[], cb: CommandCallback, options?: commandOptions): { |
| 152 | + addAlias: (...aliases: string[]) => any; |
| 153 | + addArgument: <T>(name: string, desc: string, defaultVal?: T) => any; |
| 154 | + }; |
| 155 | + /** |
| 156 | + * @typeParam T The event name |
| 157 | + * @param cb The callback function |
| 158 | + * ```typescript |
| 159 | + * cli.on('ready', () => console.log ('Up and ready to go!')); |
| 160 | + * ``` |
| 161 | + */ |
| 162 | + on<T extends keyof Events>(event: T, cb: Events[T]): this; |
| 163 | + /** |
| 164 | + * @description A function that is ran before every command |
| 165 | + * @param cb Your middleware function |
| 166 | + * @returns A **client** so you can *chain* methods. |
| 167 | + * @example |
| 168 | + * ```typescript |
| 169 | + * cli.use((req, res, next) => { |
| 170 | + * req.send(`${req.command} has been used!`); |
| 171 | + * next(); // call the next middlware/command |
| 172 | + * }) |
| 173 | + * ``` |
| 174 | + */ |
| 175 | + use(cb: CommandCallback): this; |
| 176 | + /** |
| 177 | + * options for bot status |
| 178 | + */ |
| 179 | + /** |
| 180 | + * @description Log your bot into discord |
| 181 | + * @param token Your bot token |
| 182 | + * @param status Your Bot Status Options |
| 183 | + */ |
| 184 | + login(token: string | Buffer): Promise<void>; |
| 185 | + logout(end?: boolean): void; |
| 186 | + set<T extends keyof clientOptions>(key: T, val: clientOptions[T]): this; |
| 187 | + setStatus(status: StatusOptions): void; |
| 188 | + deleteMessages(amt: number, channelID: string): Promise<void>; |
| 189 | +} |
| 190 | +export default Client; |
0 commit comments