Try to remove those anys (NOT TESTED)
This commit is contained in:
parent
ce444630d0
commit
be2945c5eb
16 changed files with 206 additions and 149 deletions
|
@ -1,13 +1,17 @@
|
||||||
import { Gateway, GatewayEventHandler } from '../index.ts'
|
import { Gateway, GatewayEventHandler } from '../index.ts'
|
||||||
import getChannelByType from '../../utils/getChannelByType.ts'
|
import getChannelByType from '../../utils/getChannelByType.ts'
|
||||||
import { ChannelPayload } from '../../types/channel.ts'
|
import { ChannelPayload, GuildChannelPayload } from '../../types/channel.ts'
|
||||||
import { Guild } from "../../structures/guild.ts"
|
import { Guild } from '../../structures/guild.ts'
|
||||||
|
|
||||||
export const channelCreate: GatewayEventHandler = async (
|
export const channelCreate: GatewayEventHandler = async (
|
||||||
gateway: Gateway,
|
gateway: Gateway,
|
||||||
d: ChannelPayload
|
d: ChannelPayload
|
||||||
) => {
|
) => {
|
||||||
const guild: undefined | Guild = (d as any).guild_id !== undefined ? await gateway.client.guilds.get((d as any).guild_id) : undefined
|
const guild: undefined | Guild =
|
||||||
|
'guild_id' in d
|
||||||
|
? // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
||||||
|
await gateway.client.guilds.get((d as GuildChannelPayload).guild_id)
|
||||||
|
: undefined
|
||||||
const channel = getChannelByType(gateway.client, d, guild)
|
const channel = getChannelByType(gateway.client, d, guild)
|
||||||
if (channel !== undefined) {
|
if (channel !== undefined) {
|
||||||
await gateway.client.channels.set(d.id, d)
|
await gateway.client.channels.set(d.id, d)
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
import { Gateway, GatewayEventHandler } from '../index.ts'
|
import { Gateway, GatewayEventHandler } from '../index.ts'
|
||||||
import { Guild } from '../../structures/guild.ts'
|
import { Guild } from '../../structures/guild.ts'
|
||||||
import { GuildPayload, MemberPayload } from '../../types/guild.ts'
|
import { GuildPayload } from '../../types/guild.ts'
|
||||||
import { MembersManager } from '../../managers/members.ts'
|
import { MembersManager } from '../../managers/members.ts'
|
||||||
import { GuildChannelPayload } from '../../types/channel.ts'
|
import { GuildChannelPayload } from '../../types/channel.ts'
|
||||||
import { RolePayload } from '../../types/role.ts'
|
|
||||||
import { RolesManager } from '../../managers/roles.ts'
|
import { RolesManager } from '../../managers/roles.ts'
|
||||||
|
|
||||||
export const guildCreate: GatewayEventHandler = async (
|
export const guildCreate: GatewayEventHandler = async (
|
||||||
|
@ -17,7 +16,7 @@ export const guildCreate: GatewayEventHandler = async (
|
||||||
|
|
||||||
if (d.members !== undefined) {
|
if (d.members !== undefined) {
|
||||||
const members = new MembersManager(gateway.client, guild)
|
const members = new MembersManager(gateway.client, guild)
|
||||||
await members.fromPayload(d.members as MemberPayload[])
|
await members.fromPayload(d.members)
|
||||||
guild.members = members
|
guild.members = members
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +29,7 @@ export const guildCreate: GatewayEventHandler = async (
|
||||||
|
|
||||||
if (d.roles !== undefined) {
|
if (d.roles !== undefined) {
|
||||||
const roles = new RolesManager(gateway.client, guild)
|
const roles = new RolesManager(gateway.client, guild)
|
||||||
await roles.fromPayload(d.roles as RolePayload[])
|
await roles.fromPayload(d.roles)
|
||||||
guild.roles = roles
|
guild.roles = roles
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,15 +38,15 @@ export const guildCreate: GatewayEventHandler = async (
|
||||||
await gateway.client.guilds.set(d.id, d)
|
await gateway.client.guilds.set(d.id, d)
|
||||||
guild = new Guild(gateway.client, d)
|
guild = new Guild(gateway.client, d)
|
||||||
|
|
||||||
if ((d as any).members !== undefined) {
|
if (d.members !== undefined) {
|
||||||
const members = new MembersManager(gateway.client, guild)
|
const members = new MembersManager(gateway.client, guild)
|
||||||
await members.fromPayload(d.members as MemberPayload[])
|
await members.fromPayload(d.members)
|
||||||
guild.members = members
|
guild.members = members
|
||||||
}
|
}
|
||||||
|
|
||||||
if (d.channels !== undefined) {
|
if (d.channels !== undefined) {
|
||||||
for (const ch of d.channels as GuildChannelPayload[]) {
|
for (const ch of d.channels as GuildChannelPayload[]) {
|
||||||
;(ch as any).guild_id = d.id
|
ch.guild_id = d.id
|
||||||
await gateway.client.channels.set(ch.id, ch)
|
await gateway.client.channels.set(ch.id, ch)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { Guild } from '../../structures/guild.ts'
|
||||||
|
|
||||||
export const guildMemberAdd: GatewayEventHandler = async (
|
export const guildMemberAdd: GatewayEventHandler = async (
|
||||||
gateway: Gateway,
|
gateway: Gateway,
|
||||||
d: any
|
d:
|
||||||
) => {
|
) => {
|
||||||
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id)
|
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id)
|
||||||
// Weird case, shouldn't happen
|
// Weird case, shouldn't happen
|
||||||
|
|
|
@ -1,21 +1,22 @@
|
||||||
import { Gateway, GatewayEventHandler } from '../index.ts'
|
import { Gateway, GatewayEventHandler } from '../index.ts'
|
||||||
import { Guild } from '../../structures/guild.ts'
|
import { Guild } from '../../structures/guild.ts'
|
||||||
import { User } from "../../structures/user.ts"
|
import { User } from '../../structures/user.ts'
|
||||||
|
import { GuildMemberRemovePayload } from '../../types/gateway.ts'
|
||||||
|
|
||||||
export const guildMemberRemove: GatewayEventHandler = async (
|
export const guildMemberRemove: GatewayEventHandler = async (
|
||||||
gateway: Gateway,
|
gateway: Gateway,
|
||||||
d: any
|
d: GuildMemberRemovePayload
|
||||||
) => {
|
) => {
|
||||||
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id)
|
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id)
|
||||||
// Weird case, shouldn't happen
|
// Weird case, shouldn't happen
|
||||||
if (guild === undefined) return
|
if (guild === undefined) return
|
||||||
|
|
||||||
const member = await guild.members.get(d.id)
|
const member = await guild.members.get(d.user.id)
|
||||||
await guild.members.delete(d.id)
|
await guild.members.delete(d.user.id)
|
||||||
|
|
||||||
if (member !== undefined) gateway.client.emit('guildMemberRemove', member)
|
if (member !== undefined) gateway.client.emit('guildMemberRemove', member)
|
||||||
else {
|
else {
|
||||||
const user = new User(gateway.client, d.user)
|
const user = new User(gateway.client, d.user)
|
||||||
gateway.client.emit('guildMemberRemoveUncached', user)
|
gateway.client.emit('guildMemberRemoveUncached', user)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,32 @@
|
||||||
import { Gateway, GatewayEventHandler } from '../index.ts'
|
import { Gateway, GatewayEventHandler } from '../index.ts'
|
||||||
import { Guild } from '../../structures/guild.ts'
|
import { Guild } from '../../structures/guild.ts'
|
||||||
|
import { GuildMemberUpdatePayload } from '../../types/gateway.ts'
|
||||||
|
import { MemberPayload } from '../../types/guild.ts'
|
||||||
|
|
||||||
export const guildMemberUpdate: GatewayEventHandler = async (
|
export const guildMemberUpdate: GatewayEventHandler = async (
|
||||||
gateway: Gateway,
|
gateway: Gateway,
|
||||||
d: any
|
d: GuildMemberUpdatePayload
|
||||||
) => {
|
) => {
|
||||||
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id)
|
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id)
|
||||||
// Weird case, shouldn't happen
|
// Weird case, shouldn't happen
|
||||||
if (guild === undefined) return
|
if (guild === undefined) return
|
||||||
|
|
||||||
const member = await guild.members.get(d.id)
|
const member = await guild.members.get(d.user.id)
|
||||||
await guild.members.set(d.id, d)
|
const newMemberPayload: MemberPayload = {
|
||||||
const newMember = await guild.members.get(d.id)
|
user: d.user,
|
||||||
|
roles: d.roles,
|
||||||
if (member !== undefined) gateway.client.emit('guildMemberRemove', member, newMember)
|
joined_at: d.joined_at,
|
||||||
else {
|
nick: d.nick,
|
||||||
gateway.client.emit('guildMemberUpdateUncached', newMember)
|
premium_since: d.premium_since,
|
||||||
|
deaf: member?.deaf ?? false,
|
||||||
|
mute: member?.mute ?? false
|
||||||
}
|
}
|
||||||
}
|
await guild.members.set(d.user.id, newMemberPayload)
|
||||||
|
const newMember = await guild.members.get(d.user.id)
|
||||||
|
|
||||||
|
if (member !== undefined)
|
||||||
|
gateway.client.emit('guildMemberRemove', member, newMember)
|
||||||
|
else {
|
||||||
|
gateway.client.emit('guildMemberUpdateUncached', newMember)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ export const messageCreate: GatewayEventHandler = async (
|
||||||
await guild.members.set(d.author.id, d.member)
|
await guild.members.set(d.author.id, d.member)
|
||||||
member = await guild.members.get(d.author.id)
|
member = await guild.members.get(d.author.id)
|
||||||
}
|
}
|
||||||
const message = new Message(gateway.client, d, channel as any, user)
|
const message = new Message(gateway.client, d, channel, user)
|
||||||
if (guild !== undefined) message.guild = guild
|
if (guild !== undefined) message.guild = guild
|
||||||
await message.mentions.fromPayload(d)
|
await message.mentions.fromPayload(d)
|
||||||
message.member = member
|
message.member = member
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import { Message } from "../../structures/message.ts"
|
import { Message } from '../../structures/message.ts'
|
||||||
import { TextChannel } from '../../structures/textChannel.ts'
|
import { TextChannel } from '../../structures/textChannel.ts'
|
||||||
import { User } from "../../structures/user.ts"
|
import { User } from '../../structures/user.ts'
|
||||||
|
import { MessagePayload } from '../../types/channel.ts'
|
||||||
import { Gateway, GatewayEventHandler } from '../index.ts'
|
import { Gateway, GatewayEventHandler } from '../index.ts'
|
||||||
|
|
||||||
export const messageUpdate: GatewayEventHandler = async (
|
export const messageUpdate: GatewayEventHandler = async (
|
||||||
gateway: Gateway,
|
gateway: Gateway,
|
||||||
d: any
|
d: MessagePayload
|
||||||
) => {
|
) => {
|
||||||
let channel = await gateway.client.channels.get<TextChannel>(d.channel_id)
|
let channel = await gateway.client.channels.get<TextChannel>(d.channel_id)
|
||||||
// Fetch the channel if not cached
|
// Fetch the channel if not cached
|
||||||
|
@ -14,7 +15,10 @@ export const messageUpdate: GatewayEventHandler = async (
|
||||||
channel = (await gateway.client.channels.fetch(d.channel_id)) as TextChannel
|
channel = (await gateway.client.channels.fetch(d.channel_id)) as TextChannel
|
||||||
|
|
||||||
const message = await channel.messages.get(d.id)
|
const message = await channel.messages.get(d.id)
|
||||||
const author = message?.author !== undefined ? message.author : new User(gateway.client, d)
|
const author =
|
||||||
|
message?.author !== undefined
|
||||||
|
? message.author
|
||||||
|
: new User(gateway.client, d.author)
|
||||||
const newMsg = new Message(gateway.client, d, channel, author)
|
const newMsg = new Message(gateway.client, d, channel, author)
|
||||||
if (message === undefined) {
|
if (message === undefined) {
|
||||||
await channel.messages.set(d.id, d)
|
await channel.messages.set(d.id, d)
|
||||||
|
|
|
@ -1,15 +1,19 @@
|
||||||
import { User } from '../../structures/user.ts'
|
import { User } from '../../structures/user.ts'
|
||||||
|
import { Ready } from "../../types/gateway.ts"
|
||||||
import { GuildPayload } from '../../types/guild.ts'
|
import { GuildPayload } from '../../types/guild.ts'
|
||||||
import { Gateway, GatewayEventHandler } from '../index.ts'
|
import { Gateway, GatewayEventHandler } from '../index.ts'
|
||||||
|
|
||||||
export const ready: GatewayEventHandler = async (gateway: Gateway, d: any) => {
|
export const ready: GatewayEventHandler = async (gateway: Gateway, d: Ready) => {
|
||||||
await gateway.client.guilds.flush()
|
await gateway.client.guilds.flush()
|
||||||
|
|
||||||
gateway.client.user = new User(gateway.client, d.user)
|
gateway.client.user = new User(gateway.client, d.user)
|
||||||
gateway.sessionID = d.session_id
|
gateway.sessionID = d.session_id
|
||||||
gateway.debug(`Received READY. Session: ${gateway.sessionID}`)
|
gateway.debug(`Received READY. Session: ${gateway.sessionID}`)
|
||||||
await gateway.cache.set("session_id", gateway.sessionID)
|
await gateway.cache.set("session_id", gateway.sessionID)
|
||||||
|
|
||||||
d.guilds.forEach((guild: GuildPayload) => {
|
d.guilds.forEach((guild: GuildPayload) => {
|
||||||
gateway.client.guilds.set(guild.id, guild)
|
gateway.client.guilds.set(guild.id, guild)
|
||||||
})
|
})
|
||||||
|
|
||||||
gateway.client.emit('ready')
|
gateway.client.emit('ready')
|
||||||
}
|
}
|
|
@ -1,10 +1,18 @@
|
||||||
import { User } from '../../structures/user.ts'
|
import { User } from '../../structures/user.ts'
|
||||||
import { CLIENT_USER } from '../../types/endpoint.ts'
|
import { CLIENT_USER } from '../../types/endpoint.ts'
|
||||||
|
import { Resume } from '../../types/gateway.ts'
|
||||||
import { Gateway, GatewayEventHandler } from '../index.ts'
|
import { Gateway, GatewayEventHandler } from '../index.ts'
|
||||||
|
|
||||||
export const resume: GatewayEventHandler = async (gateway: Gateway, d: any) => {
|
export const resume: GatewayEventHandler = async (
|
||||||
|
gateway: Gateway,
|
||||||
|
d: Resume
|
||||||
|
) => {
|
||||||
gateway.debug(`Session Resumed!`)
|
gateway.debug(`Session Resumed!`)
|
||||||
gateway.client.emit('resume')
|
gateway.client.emit('resume')
|
||||||
if (gateway.client.user === undefined) gateway.client.user = new User(gateway.client, await gateway.client.rest.get(CLIENT_USER()))
|
if (gateway.client.user === undefined)
|
||||||
|
gateway.client.user = new User(
|
||||||
|
gateway.client,
|
||||||
|
await gateway.client.rest.get(CLIENT_USER())
|
||||||
|
)
|
||||||
gateway.client.emit('ready')
|
gateway.client.emit('ready')
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,13 +8,14 @@ import { GatewayResponse } from '../types/gatewayResponse.ts'
|
||||||
import {
|
import {
|
||||||
GatewayOpcodes,
|
GatewayOpcodes,
|
||||||
GatewayIntents,
|
GatewayIntents,
|
||||||
GatewayCloseCodes
|
GatewayCloseCodes,
|
||||||
|
IdentityPayload,
|
||||||
|
StatusUpdatePayload
|
||||||
} from '../types/gateway.ts'
|
} from '../types/gateway.ts'
|
||||||
import { gatewayHandlers } from './handlers/index.ts'
|
import { gatewayHandlers } from './handlers/index.ts'
|
||||||
import { GATEWAY_BOT } from '../types/endpoint.ts'
|
import { GATEWAY_BOT } from '../types/endpoint.ts'
|
||||||
import { GatewayCache } from '../managers/gatewayCache.ts'
|
import { GatewayCache } from '../managers/gatewayCache.ts'
|
||||||
import { ClientActivityPayload } from '../structures/presence.ts'
|
import { delay } from '../utils/delay.ts'
|
||||||
import { delay } from "../utils/delay.ts"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles Discord gateway connection.
|
* Handles Discord gateway connection.
|
||||||
|
@ -116,7 +117,7 @@ class Gateway {
|
||||||
}
|
}
|
||||||
if (t !== null && t !== undefined) {
|
if (t !== null && t !== undefined) {
|
||||||
this.client.emit('raw', t, d)
|
this.client.emit('raw', t, d)
|
||||||
|
|
||||||
const handler = gatewayHandlers[t]
|
const handler = gatewayHandlers[t]
|
||||||
|
|
||||||
if (handler !== undefined) {
|
if (handler !== undefined) {
|
||||||
|
@ -181,7 +182,9 @@ class Gateway {
|
||||||
} else if (event.code === GatewayCloseCodes.DISALLOWED_INTENTS) {
|
} else if (event.code === GatewayCloseCodes.DISALLOWED_INTENTS) {
|
||||||
throw new Error("Given Intents aren't allowed")
|
throw new Error("Given Intents aren't allowed")
|
||||||
} else {
|
} else {
|
||||||
this.debug('Unknown Close code, probably connection error. Reconnecting in 5s.')
|
this.debug(
|
||||||
|
'Unknown Close code, probably connection error. Reconnecting in 5s.'
|
||||||
|
)
|
||||||
await delay(5000)
|
await delay(5000)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||||
this.reconnect()
|
this.reconnect()
|
||||||
|
@ -217,40 +220,39 @@ class Gateway {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload: any = {
|
const payload: IdentityPayload = {
|
||||||
op: GatewayOpcodes.IDENTIFY,
|
token: this.token,
|
||||||
d: {
|
properties: {
|
||||||
token: this.token,
|
$os: Deno.build.os,
|
||||||
properties: {
|
$browser: 'harmony',
|
||||||
$os: Deno.build.os,
|
$device: 'harmony'
|
||||||
$browser: 'harmony',
|
},
|
||||||
$device: 'harmony'
|
compress: true,
|
||||||
},
|
shard: [0, 1], // TODO: Make sharding possible
|
||||||
compress: true,
|
intents: this.intents.reduce(
|
||||||
shard: [0, 1], // TODO: Make sharding possible
|
(previous, current) => previous | current,
|
||||||
intents: this.intents.reduce(
|
0
|
||||||
(previous, current) => previous | current,
|
),
|
||||||
0
|
presence: this.client.presence.create()
|
||||||
),
|
|
||||||
presence: this.client.presence.create()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.client.bot === false) {
|
if (this.client.bot === false) {
|
||||||
this.debug('Modify Identify Payload for Self-bot..')
|
this.debug('Modify Identify Payload for Self-bot..')
|
||||||
delete payload.d.intents
|
delete payload.intents
|
||||||
payload.d.presence = null
|
payload.presence = undefined
|
||||||
payload.d.properties = {
|
payload.properties = {
|
||||||
$os: 'Windows',
|
$os: 'windows',
|
||||||
$browser: 'Firefox',
|
$browser: 'Firefox',
|
||||||
$device: '',
|
$device: '',
|
||||||
$referrer: '',
|
$referrer: '',
|
||||||
$referring_domain: ''
|
$referring_domain: ''
|
||||||
}
|
}
|
||||||
payload.d.synced_guilds = []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.send(payload)
|
this.send({
|
||||||
|
op: GatewayOpcodes.IDENTIFY,
|
||||||
|
d: payload
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private async sendResume (): Promise<void> {
|
private async sendResume (): Promise<void> {
|
||||||
|
@ -313,7 +315,7 @@ class Gateway {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
sendPresence (data: ClientActivityPayload): void {
|
sendPresence (data: StatusUpdatePayload): void {
|
||||||
this.send({
|
this.send({
|
||||||
op: GatewayOpcodes.PRESENCE_UPDATE,
|
op: GatewayOpcodes.PRESENCE_UPDATE,
|
||||||
d: data
|
d: data
|
||||||
|
|
|
@ -33,7 +33,8 @@ export class BaseManager<T, T2> {
|
||||||
}
|
}
|
||||||
|
|
||||||
async array (): Promise<undefined | T2[]> {
|
async array (): Promise<undefined | T2[]> {
|
||||||
const arr = await (this.client.cache.array(this.cacheName) as T[])
|
let arr = await (this.client.cache.array(this.cacheName) as T[])
|
||||||
|
if (arr === undefined) arr = []
|
||||||
return arr.map(e => new this.DataType(this.client, e)) as any
|
return arr.map(e => new this.DataType(this.client, e)) as any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +49,7 @@ export class BaseManager<T, T2> {
|
||||||
return collection
|
return collection
|
||||||
}
|
}
|
||||||
|
|
||||||
flush(): any {
|
flush (): any {
|
||||||
return this.client.cache.deleteCache(this.cacheName)
|
return this.client.cache.deleteCache(this.cacheName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,50 +1,61 @@
|
||||||
import { Client } from '../models/client.ts'
|
import { Client } from '../models/client.ts'
|
||||||
import { Channel } from '../structures/channel.ts'
|
import { Channel } from '../structures/channel.ts'
|
||||||
import { ChannelPayload } from '../types/channel.ts'
|
import { ChannelPayload, GuildChannelPayload } from '../types/channel.ts'
|
||||||
import { CHANNEL } from '../types/endpoint.ts'
|
import { CHANNEL } from '../types/endpoint.ts'
|
||||||
import getChannelByType from '../utils/getChannelByType.ts'
|
import getChannelByType from '../utils/getChannelByType.ts'
|
||||||
import { BaseManager } from './base.ts'
|
import { BaseManager } from './base.ts'
|
||||||
|
|
||||||
export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
|
export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
|
||||||
constructor(client: Client) {
|
constructor (client: Client) {
|
||||||
super(client, "channels", Channel)
|
super(client, 'channels', Channel)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override get method as Generic
|
// Override get method as Generic
|
||||||
async get<T = Channel>(key: string): Promise<T | undefined> {
|
async get<T = Channel> (key: string): Promise<T | undefined> {
|
||||||
const data = await this._get(key)
|
const data = await this._get(key)
|
||||||
if (data === undefined) return
|
if (data === undefined) return
|
||||||
let guild
|
let guild
|
||||||
if ((data as any).guild_id !== undefined) {
|
if ('guild_id' in data) {
|
||||||
guild = await this.client.guilds.get((data as any).guild_id)
|
guild = await this.client.guilds.get(
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
||||||
|
(data as GuildChannelPayload).guild_id
|
||||||
|
)
|
||||||
}
|
}
|
||||||
const res = getChannelByType(this.client, data, guild)
|
const res = getChannelByType(this.client, data, guild)
|
||||||
return res as any
|
return res as any
|
||||||
}
|
}
|
||||||
|
|
||||||
async array(): Promise<undefined | Channel[]> {
|
async array (): Promise<undefined | Channel[]> {
|
||||||
const arr = await (this.client.cache.array(this.cacheName) as ChannelPayload[])
|
const arr = await (this.client.cache.array(
|
||||||
|
this.cacheName
|
||||||
|
) as ChannelPayload[])
|
||||||
const result: any[] = []
|
const result: any[] = []
|
||||||
for (const elem of arr) {
|
for (const elem of arr) {
|
||||||
let guild
|
let guild
|
||||||
if ((elem as any).guild_id !== undefined) {
|
if ('guild_id' in elem) {
|
||||||
guild = await this.client.guilds.get((elem as any).guild_id)
|
guild = await this.client.guilds.get(
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
||||||
|
(elem as GuildChannelPayload).guild_id
|
||||||
|
)
|
||||||
}
|
}
|
||||||
result.push(getChannelByType(this.client, elem, guild))
|
result.push(getChannelByType(this.client, elem, guild))
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetch(id: string): Promise<Channel> {
|
async fetch (id: string): Promise<Channel> {
|
||||||
return await new Promise((resolve, reject) => {
|
return await new Promise((resolve, reject) => {
|
||||||
this.client.rest.get(CHANNEL(id)).then(async data => {
|
this.client.rest
|
||||||
this.set(id, data as ChannelPayload)
|
.get(CHANNEL(id))
|
||||||
let guild
|
.then(async data => {
|
||||||
if (data.guild_id !== undefined) {
|
this.set(id, data as ChannelPayload)
|
||||||
guild = await this.client.guilds.get(data.guild_id)
|
let guild
|
||||||
}
|
if (data.guild_id !== undefined) {
|
||||||
resolve(getChannelByType(this.client, data as ChannelPayload, guild))
|
guild = await this.client.guilds.get(data.guild_id)
|
||||||
}).catch(e => reject(e))
|
}
|
||||||
|
resolve(getChannelByType(this.client, data as ChannelPayload, guild))
|
||||||
|
})
|
||||||
|
.catch(e => reject(e))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,58 +1,37 @@
|
||||||
export type ActivityType = 'PLAYING' | 'STREAMING' | 'LISTENING' | 'WATCHING' | 'CUSTOM_STATUS' | 'COMPETING';
|
import { ActivityGame, ClientActivity, StatusType } from '../types/presence.ts'
|
||||||
export type StatusType = 'online' | 'invisible' | 'offline' | 'idle' | 'dnd';
|
import { StatusUpdatePayload } from '../types/gateway.ts'
|
||||||
|
|
||||||
export enum ActivityTypes {
|
enum ActivityTypes {
|
||||||
PLAYING = 0,
|
PLAYING = 0,
|
||||||
STREAMING = 1,
|
STREAMING = 1,
|
||||||
LISTENING = 2,
|
LISTENING = 2,
|
||||||
WATCHING = 3,
|
WATCHING = 3,
|
||||||
CUSTOM_STATUS = 4,
|
CUSTOM_STATUS = 4,
|
||||||
COMPETING = 5,
|
COMPETING = 5
|
||||||
}
|
|
||||||
|
|
||||||
export interface ActivityGame {
|
|
||||||
name: string;
|
|
||||||
type: 0 | 1 | 2 | 3 | 4 | 5 | ActivityType;
|
|
||||||
url?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ClientActivity {
|
|
||||||
status?: StatusType
|
|
||||||
activity?: ActivityGame | ActivityGame[]
|
|
||||||
since?: number | null
|
|
||||||
afk?: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ClientActivityPayload {
|
|
||||||
status: StatusType
|
|
||||||
activities: ActivityGame[] | null
|
|
||||||
since: number | null
|
|
||||||
afk: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ClientPresence {
|
export class ClientPresence {
|
||||||
status: StatusType = 'online'
|
status: StatusType = 'online'
|
||||||
activity?: ActivityGame | ActivityGame[]
|
activity?: ActivityGame | ActivityGame[]
|
||||||
since?: number | null
|
since?: number | null
|
||||||
afk?: boolean
|
afk?: boolean
|
||||||
|
|
||||||
constructor(data?: ClientActivity | ClientActivityPayload | ActivityGame) {
|
constructor (data?: ClientActivity | StatusUpdatePayload | ActivityGame) {
|
||||||
if (data !== undefined) {
|
if (data !== undefined) {
|
||||||
if ((data as ClientActivity).activity !== undefined) {
|
if ((data as ClientActivity).activity !== undefined) {
|
||||||
Object.assign(this, data)
|
Object.assign(this, data)
|
||||||
} else if ((data as ClientActivityPayload).activities !== undefined) {
|
} else if ((data as StatusUpdatePayload).activities !== undefined) {
|
||||||
|
|
||||||
} else if ((data as ActivityGame).name !== undefined) {
|
} else if ((data as ActivityGame).name !== undefined) {
|
||||||
if (this.activity === undefined) {
|
if (this.activity === undefined) {
|
||||||
this.activity = data as ActivityGame
|
this.activity = data as ActivityGame
|
||||||
} else if (this.activity instanceof Array) {
|
} else if (this.activity instanceof Array) {
|
||||||
this.activity.push(data as ActivityGame)
|
this.activity.push(data as ActivityGame)
|
||||||
} else this.activity = [ this.activity, data as ActivityGame ]
|
} else this.activity = [this.activity, data as ActivityGame]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parse(payload: ClientActivityPayload): ClientPresence {
|
parse (payload: StatusUpdatePayload): ClientPresence {
|
||||||
this.afk = payload.afk
|
this.afk = payload.afk
|
||||||
this.activity = payload.activities ?? undefined
|
this.activity = payload.activities ?? undefined
|
||||||
this.since = payload.since
|
this.since = payload.since
|
||||||
|
@ -60,11 +39,11 @@ export class ClientPresence {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
static parse(payload: ClientActivityPayload): ClientPresence {
|
static parse (payload: StatusUpdatePayload): ClientPresence {
|
||||||
return new ClientPresence().parse(payload)
|
return new ClientPresence().parse(payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
create(): ClientActivityPayload {
|
create (): StatusUpdatePayload {
|
||||||
return {
|
return {
|
||||||
afk: this.afk === undefined ? false : this.afk,
|
afk: this.afk === undefined ? false : this.afk,
|
||||||
activities: this.createActivity(),
|
activities: this.createActivity(),
|
||||||
|
@ -73,51 +52,56 @@ export class ClientPresence {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
createActivity(): ActivityGame[] | null {
|
createActivity (): ActivityGame[] | null {
|
||||||
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
||||||
const activity = this.activity === undefined ? null : (this.activity instanceof Array ? this.activity : [this.activity]) || null
|
const activity =
|
||||||
|
this.activity === undefined
|
||||||
|
? null
|
||||||
|
: this.activity instanceof Array
|
||||||
|
? this.activity
|
||||||
|
: [this.activity]
|
||||||
if (activity === null) return activity
|
if (activity === null) return activity
|
||||||
else {
|
else {
|
||||||
activity.map(e => {
|
activity.map(e => {
|
||||||
if (typeof e.type === "string") e.type = ActivityTypes[e.type]
|
if (typeof e.type === 'string') e.type = ActivityTypes[e.type]
|
||||||
return e
|
return e
|
||||||
})
|
})
|
||||||
return activity
|
return activity
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setStatus(status: StatusType): ClientPresence {
|
setStatus (status: StatusType): ClientPresence {
|
||||||
this.status = status
|
this.status = status
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
setActivity(activity: ActivityGame): ClientPresence {
|
setActivity (activity: ActivityGame): ClientPresence {
|
||||||
this.activity = activity
|
this.activity = activity
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
setActivities(activities: ActivityGame[]): ClientPresence {
|
setActivities (activities: ActivityGame[]): ClientPresence {
|
||||||
this.activity = activities
|
this.activity = activities
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
setAFK(afk: boolean): ClientPresence {
|
setAFK (afk: boolean): ClientPresence {
|
||||||
this.afk = afk
|
this.afk = afk
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
removeAFK(): ClientPresence {
|
removeAFK (): ClientPresence {
|
||||||
this.afk = false
|
this.afk = false
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleAFK(): ClientPresence {
|
toggleAFK (): ClientPresence {
|
||||||
this.afk = this.afk === undefined ? true : !this.afk
|
this.afk = this.afk === undefined ? true : !this.afk
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
setSince(since?: number): ClientPresence {
|
setSince (since?: number): ClientPresence {
|
||||||
this.since = since
|
this.since = since
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,10 +100,10 @@ export interface MessagePayload {
|
||||||
|
|
||||||
export interface MessageOption {
|
export interface MessageOption {
|
||||||
tts?: boolean
|
tts?: boolean
|
||||||
embed: Embed
|
embed?: Embed
|
||||||
file?: Attachment
|
file?: Attachment
|
||||||
allowedMention?: {
|
allowedMention?: {
|
||||||
parse: ['everyone', 'users', 'roles']
|
parse: 'everyone' | 'users' | 'roles'
|
||||||
roles: string[]
|
roles: string[]
|
||||||
users: string[]
|
users: string[]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
// https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway
|
// https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway
|
||||||
// https://discord.com/developers/docs/topics/gateway#commands-and-events-gateway-events
|
// https://discord.com/developers/docs/topics/gateway#commands-and-events-gateway-events
|
||||||
import { StatusType } from '../../mod.ts'
|
|
||||||
import { EmojiPayload } from './emoji.ts'
|
import { EmojiPayload } from './emoji.ts'
|
||||||
import { MemberPayload } from './guild.ts'
|
import { MemberPayload } from './guild.ts'
|
||||||
import { ActivityPayload } from './presence.ts'
|
import {
|
||||||
|
ActivityGame,
|
||||||
|
ActivityPayload,
|
||||||
|
StatusType,
|
||||||
|
ClientStatus
|
||||||
|
} from './presence.ts'
|
||||||
import { RolePayload } from './role.ts'
|
import { RolePayload } from './role.ts'
|
||||||
import { UserPayload } from './user.ts'
|
import { UserPayload } from './user.ts'
|
||||||
|
|
||||||
|
@ -107,23 +111,17 @@ export interface IdentityPayload {
|
||||||
compress?: boolean
|
compress?: boolean
|
||||||
large_threshold?: number
|
large_threshold?: number
|
||||||
shard?: number[]
|
shard?: number[]
|
||||||
presence?: UpdateStatus
|
presence?: StatusUpdatePayload
|
||||||
guildSubscriptions?: boolean
|
guildSubscriptions?: boolean
|
||||||
intents: number
|
intents?: number
|
||||||
}
|
|
||||||
|
|
||||||
export enum UpdateStatus {
|
|
||||||
online = 'online',
|
|
||||||
dnd = 'dnd',
|
|
||||||
afk = 'idle',
|
|
||||||
invisible = 'invisible',
|
|
||||||
offline = 'offline'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IdentityConnection {
|
export interface IdentityConnection {
|
||||||
$os: 'darwin' | 'windows' | 'linux' | 'custom os'
|
$os: 'darwin' | 'windows' | 'linux' | 'custom os'
|
||||||
$browser: 'harmony'
|
$browser: 'harmony' | 'Firefox'
|
||||||
$device: 'harmony'
|
$device: 'harmony' | ''
|
||||||
|
$referrer?: ''
|
||||||
|
$referring_domain?: ''
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Resume {
|
export interface Resume {
|
||||||
|
@ -159,7 +157,7 @@ export interface Hello {
|
||||||
heartbeat_interval: number
|
heartbeat_interval: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ReadyEvent {
|
export interface Ready {
|
||||||
v: number
|
v: number
|
||||||
user: UserPayload
|
user: UserPayload
|
||||||
privateChannels: []
|
privateChannels: []
|
||||||
|
@ -294,7 +292,14 @@ export interface PresenceUpdatePayload {
|
||||||
guild_id: string
|
guild_id: string
|
||||||
status: StatusType
|
status: StatusType
|
||||||
activities: ActivityPayload[]
|
activities: ActivityPayload[]
|
||||||
client_status: UpdateStatus[]
|
client_status: ClientStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface StatusUpdatePayload {
|
||||||
|
status: StatusType
|
||||||
|
activities: ActivityGame[] | null
|
||||||
|
since: number | null
|
||||||
|
afk: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TypeStart {
|
export interface TypeStart {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
export interface ClientStatus {
|
export interface ClientStatus {
|
||||||
desktop?: string
|
desktop?: StatusType
|
||||||
mobile?: string
|
mobile?: StatusType
|
||||||
web?: string
|
web?: StatusType
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ActivityPayload {
|
export interface ActivityPayload {
|
||||||
|
@ -57,4 +57,26 @@ export enum ActivityFlags {
|
||||||
JOIN_REQUEST = 1 << 3,
|
JOIN_REQUEST = 1 << 3,
|
||||||
SYNC = 1 << 4,
|
SYNC = 1 << 4,
|
||||||
PLAY = 1 << 5
|
PLAY = 1 << 5
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ActivityType =
|
||||||
|
| 'PLAYING'
|
||||||
|
| 'STREAMING'
|
||||||
|
| 'LISTENING'
|
||||||
|
| 'WATCHING'
|
||||||
|
| 'CUSTOM_STATUS'
|
||||||
|
| 'COMPETING'
|
||||||
|
export type StatusType = 'online' | 'invisible' | 'offline' | 'idle' | 'dnd'
|
||||||
|
|
||||||
|
export interface ActivityGame {
|
||||||
|
name: string
|
||||||
|
type: 0 | 1 | 2 | 3 | 4 | 5 | ActivityType
|
||||||
|
url?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ClientActivity {
|
||||||
|
status?: StatusType
|
||||||
|
activity?: ActivityGame | ActivityGame[]
|
||||||
|
since?: number | null
|
||||||
|
afk?: boolean
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue