2020-10-22 15:50:47 +00:00
|
|
|
import { User } from '../structures/user.ts'
|
2020-10-31 12:33:34 +00:00
|
|
|
import { GatewayIntents } from '../types/gateway.ts'
|
2020-10-30 14:51:40 +00:00
|
|
|
import { Gateway } from '../gateway/index.ts'
|
2020-10-31 11:45:33 +00:00
|
|
|
import { RESTManager } from './rest.ts'
|
2020-10-25 17:03:53 +00:00
|
|
|
import EventEmitter from 'https://deno.land/std@0.74.0/node/events.ts'
|
2020-11-04 12:38:00 +00:00
|
|
|
import { DefaultCacheAdapter, ICacheAdapter } from "./cacheAdapter.ts"
|
2020-11-04 12:43:32 +00:00
|
|
|
import { UserManager } from "../managers/users.ts"
|
|
|
|
import { GuildManager } from "../managers/guilds.ts"
|
|
|
|
import { EmojisManager } from "../managers/emojis.ts"
|
|
|
|
import { ChannelsManager } from "../managers/channels.ts"
|
|
|
|
import { MessagesManager } from "../managers/messages.ts"
|
2020-11-03 09:21:29 +00:00
|
|
|
import { ActivityGame, ClientActivity, ClientPresence } from "../structures/presence.ts"
|
2020-10-31 11:45:33 +00:00
|
|
|
|
|
|
|
/** Some Client Options to modify behaviour */
|
|
|
|
export interface ClientOptions {
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Token of the Bot/User */
|
2020-10-31 11:45:33 +00:00
|
|
|
token?: string
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Gateway Intents */
|
2020-10-31 11:45:33 +00:00
|
|
|
intents?: GatewayIntents[]
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Cache Adapter to use, defaults to Collections one */
|
2020-11-01 13:42:00 +00:00
|
|
|
cache?: ICacheAdapter,
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Force New Session and don't use cached Session (by persistent caching) */
|
2020-11-02 07:27:14 +00:00
|
|
|
forceNewSession?: boolean,
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Startup presence of client */
|
2020-11-02 07:27:14 +00:00
|
|
|
presence?: ClientPresence | ClientActivity | ActivityGame
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Whether it's a bot user or not? Use this if selfbot! */
|
2020-11-06 10:42:00 +00:00
|
|
|
bot?: boolean
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Force all requests to Canary API */
|
2020-11-06 10:42:00 +00:00
|
|
|
canary?: boolean
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
2020-10-30 14:51:40 +00:00
|
|
|
|
2020-10-22 15:50:47 +00:00
|
|
|
/**
|
|
|
|
* Discord Client.
|
|
|
|
*/
|
2020-10-25 17:03:53 +00:00
|
|
|
export class Client extends EventEmitter {
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Gateway object */
|
2020-10-22 15:50:47 +00:00
|
|
|
gateway?: Gateway
|
2020-11-07 13:05:37 +00:00
|
|
|
/** REST Manager - used to make all requests */
|
2020-10-31 11:45:33 +00:00
|
|
|
rest: RESTManager = new RESTManager(this)
|
2020-11-07 13:05:37 +00:00
|
|
|
/** User which Client logs in to, undefined until logs in */
|
2020-10-22 15:50:47 +00:00
|
|
|
user?: User
|
2020-11-07 13:05:37 +00:00
|
|
|
/** WebSocket ping of Client */
|
2020-10-22 15:50:47 +00:00
|
|
|
ping = 0
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Token of the Bot/User */
|
2020-10-23 16:11:00 +00:00
|
|
|
token?: string
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Cache Adapter */
|
2020-11-06 08:27:56 +00:00
|
|
|
cache: ICacheAdapter = new DefaultCacheAdapter()
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Gateway Intents */
|
2020-10-31 11:45:33 +00:00
|
|
|
intents?: GatewayIntents[]
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Whether to force new session or not */
|
2020-11-01 13:42:00 +00:00
|
|
|
forceNewSession?: boolean
|
2020-11-07 13:05:37 +00:00
|
|
|
|
2020-10-31 11:45:33 +00:00
|
|
|
users: UserManager = new UserManager(this)
|
|
|
|
guilds: GuildManager = new GuildManager(this)
|
|
|
|
channels: ChannelsManager = new ChannelsManager(this)
|
2020-10-31 13:00:33 +00:00
|
|
|
messages: MessagesManager = new MessagesManager(this)
|
2020-10-31 11:45:33 +00:00
|
|
|
emojis: EmojisManager = new EmojisManager(this)
|
2020-11-07 13:05:37 +00:00
|
|
|
|
|
|
|
/** Whether this client will login as bot user or not */
|
2020-11-06 10:42:00 +00:00
|
|
|
bot: boolean = true
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Whether the REST Manager will use Canary API or not */
|
2020-11-06 10:42:00 +00:00
|
|
|
canary: boolean = false
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Client's presence. Startup one if set before connecting */
|
2020-11-02 07:27:14 +00:00
|
|
|
presence: ClientPresence = new ClientPresence()
|
|
|
|
|
2020-10-31 11:45:33 +00:00
|
|
|
constructor (options: ClientOptions = {}) {
|
|
|
|
super()
|
|
|
|
this.token = options.token
|
|
|
|
this.intents = options.intents
|
2020-11-01 13:42:00 +00:00
|
|
|
this.forceNewSession = options.forceNewSession
|
2020-11-02 06:58:23 +00:00
|
|
|
if (options.cache !== undefined) this.cache = options.cache
|
2020-11-02 16:31:10 +00:00
|
|
|
if (options.presence !== undefined) this.presence = options.presence instanceof ClientPresence ? options.presence : new ClientPresence(options.presence)
|
2020-11-06 10:42:00 +00:00
|
|
|
if (options.bot === false) this.bot = false
|
|
|
|
if (options.canary === true) this.canary = true
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Set Cache Adapter */
|
2020-11-02 06:58:23 +00:00
|
|
|
setAdapter (adapter: ICacheAdapter): Client {
|
2020-11-01 11:22:09 +00:00
|
|
|
this.cache = adapter
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Change Presence of Client */
|
2020-11-03 15:19:20 +00:00
|
|
|
setPresence (presence: ClientPresence | ClientActivity | ActivityGame): void {
|
|
|
|
if (presence instanceof ClientPresence) {
|
2020-11-02 07:27:14 +00:00
|
|
|
this.presence = presence
|
|
|
|
} else this.presence = new ClientPresence(presence)
|
|
|
|
this.gateway?.sendPresence(this.presence.create())
|
|
|
|
}
|
|
|
|
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Emit debug event */
|
2020-11-03 15:19:20 +00:00
|
|
|
debug (tag: string, msg: string): void {
|
2020-10-31 11:45:33 +00:00
|
|
|
this.emit("debug", `[${tag}] ${msg}`)
|
|
|
|
}
|
2020-10-22 15:50:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is used for connect to discord.
|
|
|
|
* @param token Your token. This is required.
|
|
|
|
* @param intents Gateway intents in array. This is required.
|
|
|
|
*/
|
2020-10-31 11:45:33 +00:00
|
|
|
connect (token?: string, intents?: GatewayIntents[]): void {
|
2020-11-02 06:58:23 +00:00
|
|
|
if (token === undefined && this.token !== undefined) token = this.token
|
|
|
|
else if (this.token === undefined && token !== undefined) {
|
2020-10-31 11:45:33 +00:00
|
|
|
this.token = token
|
2020-11-02 06:58:23 +00:00
|
|
|
} else throw new Error('No Token Provided')
|
|
|
|
if (intents === undefined && this.intents !== undefined)
|
|
|
|
intents = this.intents
|
|
|
|
else if (intents !== undefined && this.intents === undefined) {
|
2020-10-31 11:45:33 +00:00
|
|
|
this.intents = intents
|
2020-11-02 06:58:23 +00:00
|
|
|
} else throw new Error('No Gateway Intents were provided')
|
2020-10-22 15:50:47 +00:00
|
|
|
this.gateway = new Gateway(this, token, intents)
|
|
|
|
}
|
|
|
|
}
|