From dce4c99cbda9e601385d1de652639bbc65530c3e Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Mon, 25 Jan 2021 17:04:43 +0530 Subject: [PATCH] many things --- src/gateway/handlers/guildEmojiUpdate.ts | 8 ++- src/gateway/handlers/index.ts | 16 +++-- src/gateway/handlers/reconnect.ts | 1 + src/structures/message.ts | 12 +++- src/structures/role.ts | 4 +- src/structures/textChannel.ts | 77 ++++++++++++++++++++++-- 6 files changed, 100 insertions(+), 18 deletions(-) diff --git a/src/gateway/handlers/guildEmojiUpdate.ts b/src/gateway/handlers/guildEmojiUpdate.ts index fddb657..fbdb380 100644 --- a/src/gateway/handlers/guildEmojiUpdate.ts +++ b/src/gateway/handlers/guildEmojiUpdate.ts @@ -44,16 +44,18 @@ export const guildEmojiUpdate: GatewayEventHandler = async ( } } + gateway.client.emit('guildEmojisUpdate', guild) + for (const emoji of deleted) { - gateway.client.emit('guildEmojiDelete', guild, emoji) + gateway.client.emit('guildEmojiDelete', emoji) } for (const emoji of added) { - gateway.client.emit('guildEmojiAdd', guild, emoji) + gateway.client.emit('guildEmojiAdd', emoji) } for (const emoji of updated) { - gateway.client.emit('guildEmojiUpdate', guild, emoji.before, emoji.after) + gateway.client.emit('guildEmojiUpdate', emoji.before, emoji.after) } } } diff --git a/src/gateway/handlers/index.ts b/src/gateway/handlers/index.ts index 43499cd..1f4e326 100644 --- a/src/gateway/handlers/index.ts +++ b/src/gateway/handlers/index.ts @@ -113,11 +113,12 @@ export interface VoiceServerUpdateData { guild: Guild } +/** All Client Events */ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions export type ClientEvents = { /** When Client has successfully connected to Discord */ ready: [shard: number] - /** When a successful reconnect has been made */ + /** When a reconnect was requested by Discord */ reconnect: [shard: number] /** When a successful session resume has been done */ resumed: [shard: number] @@ -178,25 +179,28 @@ export type ClientEvents = { * @param guild Guild in which Emoji was added * @param emoji The Emoji which was added */ - guildEmojiAdd: [guild: Guild, emoji: Emoji] + guildEmojiAdd: [emoji: Emoji] /** * An Emoji was deleted from Guild - * @param guild Guild from which Emoji was deleted * @param emoji Emoji which was deleted */ - guildEmojiDelete: [Guild, Emoji] + guildEmojiDelete: [emoji: Emoji] /** * An Emoji in a Guild was updated - * @param guild Guild in which Emoji was updated * @param before Emoji object before update * @param after Emoji object after update */ - guildEmojiUpdate: [guild: Guild, before: Emoji, after: Emoji] + guildEmojiUpdate: [before: Emoji, after: Emoji] /** * Guild's Integrations were updated * @param guild The Guild object */ guildIntegrationsUpdate: [guild: Guild] + /** + * Guild's Emojis were updated + * @param guild The Guild object + */ + guildEmojisUpdate: [guild: Guild] /** * A new Member has joined a Guild * @param member The Member object diff --git a/src/gateway/handlers/reconnect.ts b/src/gateway/handlers/reconnect.ts index 747273d..013c613 100644 --- a/src/gateway/handlers/reconnect.ts +++ b/src/gateway/handlers/reconnect.ts @@ -4,5 +4,6 @@ export const reconnect: GatewayEventHandler = async ( gateway: Gateway, d: any ) => { + gateway.client.emit('reconnect', gateway.shards?.[0] ?? 0) gateway.reconnect() } diff --git a/src/structures/message.ts b/src/structures/message.ts index 1cb6f36..b697220 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -30,7 +30,7 @@ export class Message extends SnowflakeBase { author: User member?: Member content: string - editedTimestamp?: string + editedTimestamp?: Date tts: boolean mentions: MessageMentions attachments: Attachment[] @@ -62,7 +62,10 @@ export class Message extends SnowflakeBase { this.guildID = data.guild_id this.author = author this.content = data.content - this.editedTimestamp = data.edited_timestamp + this.editedTimestamp = + data.edited_timestamp === undefined + ? undefined + : new Date(data.edited_timestamp) this.tts = data.tts this.mentions = new MessageMentions(this.client, this) this.attachments = data.attachments @@ -89,7 +92,10 @@ export class Message extends SnowflakeBase { this.channelID = data.channel_id ?? this.channelID this.guildID = data.guild_id ?? this.guildID this.content = data.content ?? this.content - this.editedTimestamp = data.edited_timestamp ?? this.editedTimestamp + this.editedTimestamp = + data.edited_timestamp === undefined + ? this.editedTimestamp + : new Date(data.edited_timestamp) this.tts = data.tts ?? this.tts this.attachments = data.attachments ?? this.attachments this.embeds = data.embeds.map((v) => new Embed(v)) ?? this.embeds diff --git a/src/structures/role.ts b/src/structures/role.ts index 4d3b68c..91050aa 100644 --- a/src/structures/role.ts +++ b/src/structures/role.ts @@ -1,10 +1,10 @@ import { Client } from '../models/client.ts' -import { SnowflakeBase } from './base.ts' +import { Base } from './base.ts' import { RoleModifyPayload, RolePayload } from '../types/role.ts' import { Permissions } from '../utils/permissions.ts' import { Guild } from './guild.ts' -export class Role extends SnowflakeBase { +export class Role extends Base { id: string guild: Guild name: string diff --git a/src/structures/textChannel.ts b/src/structures/textChannel.ts index b2e6916..062a1ed 100644 --- a/src/structures/textChannel.ts +++ b/src/structures/textChannel.ts @@ -24,6 +24,7 @@ import { Channel } from './channel.ts' import { Embed } from './embed.ts' import { Emoji } from './emoji.ts' import { Guild } from './guild.ts' +import { CategoryChannel } from './guildCategoryChannel.ts' import { Invite } from './invite.ts' import { Member } from './member.ts' import { Message } from './message.ts' @@ -244,7 +245,7 @@ export class GuildTextChannel extends TextChannel { permissionOverwrites: Overwrite[] nsfw: boolean parentID?: string - rateLimit: number + slowmode: number topic?: string guild: Guild @@ -266,7 +267,7 @@ export class GuildTextChannel extends TextChannel { this.nsfw = data.nsfw this.parentID = data.parent_id this.topic = data.topic - this.rateLimit = data.rate_limit_per_user + this.slowmode = data.rate_limit_per_user } readFromData(data: GuildTextChannelPayload): void { @@ -279,7 +280,7 @@ export class GuildTextChannel extends TextChannel { this.nsfw = data.nsfw ?? this.nsfw this.parentID = data.parent_id ?? this.parentID this.topic = data.topic ?? this.topic - this.rateLimit = data.rate_limit_per_user ?? this.rateLimit + this.slowmode = data.rate_limit_per_user ?? this.slowmode } /** Edit the Guild Text Channel */ @@ -290,7 +291,10 @@ export class GuildTextChannel extends TextChannel { name: options?.name, position: options?.position, permission_overwrites: options?.permissionOverwrites, - parent_id: options?.parentID + parent_id: options?.parentID, + nsfw: options?.nsfw, + topic: options?.topic, + rate_limit_per_user: options?.rateLimitPerUser } const resp = await this.client.rest.patch(CHANNEL(this.id), body) @@ -388,4 +392,69 @@ export class GuildTextChannel extends TextChannel { .remove(memberOWs.length === 0 ? 0 : memberOWs.map((e) => Number(e.deny))) .add(memberOWs.length === 0 ? 0 : memberOWs.map((e) => Number(e.allow))) } + + /** Edit name of the channel */ + async setName(name: string): Promise { + return await this.edit({ name }) + } + + /** Edit topic of the channel */ + async setTopic(topic: string): Promise { + return await this.edit({ topic }) + } + + /** Edit topic of the channel */ + async setCategory( + category: CategoryChannel | string + ): Promise { + return await this.edit({ + parentID: typeof category === 'object' ? category.id : category + }) + } + + /** Edit position of the channel */ + async setPosition(position: number): Promise { + return await this.edit({ position }) + } + + /** Edit Slowmode of the channel */ + async setSlowmode(slowmode?: number | null): Promise { + return await this.edit({ rateLimitPerUser: slowmode ?? null }) + } + + /** Edit NSFW property of the channel */ + async setNSFW(nsfw: boolean): Promise { + return await this.edit({ nsfw }) + } + + /** Set Permission Overwrites of the Channel */ + async setOverwrites(overwrites: Overwrite[]): Promise { + return await this.edit({ permissionOverwrites: overwrites }) + } + + /** Add a Permission Overwrite */ + async addOverwrite(overwrite: Overwrite): Promise { + const overwrites = this.permissionOverwrites + overwrites.push(overwrite) + return await this.edit({ permissionOverwrites: overwrites }) + } + + /** Remove a Permission Overwrite */ + async removeOverwrite(id: string): Promise { + if (this.permissionOverwrites.findIndex((e) => e.id === id) < 0) + throw new Error('Permission Overwrite not found') + const overwrites = this.permissionOverwrites.filter((e) => e.id !== id) + return await this.edit({ permissionOverwrites: overwrites }) + } + + /** Edit a Permission Overwrite */ + async editOverwrite(overwrite: Overwrite): Promise { + const index = this.permissionOverwrites.findIndex( + (e) => e.id === overwrite.id + ) + if (index < 0) throw new Error('Permission Overwrite not found') + const overwrites = this.permissionOverwrites + overwrites[index] = overwrite + return await this.edit({ permissionOverwrites: overwrites }) + } }