From 0f68995136971fd687d5850d9811777425c04598 Mon Sep 17 00:00:00 2001 From: socram03 Date: Thu, 23 Jun 2022 20:39:18 -0400 Subject: [PATCH 01/10] fix: supress embeds --- structures/Message.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/structures/Message.ts b/structures/Message.ts index 9bff529..5d4de74 100644 --- a/structures/Message.ts +++ b/structures/Message.ts @@ -103,11 +103,11 @@ export class Message implements Model { async suppressEmbeds(suppress: true): Promise; async suppressEmbeds(suppress: false): Promise; async suppressEmbeds(suppress = true) { - if (this.flags === MessageFlags.SUPPRESS_EMBEDS && suppress === false) { + if (this.flags === MessageFlags.SupressEmbeds && suppress === false) { return; } - const message = await this.edit({ flags: MessageFlags.SUPPRESS_EMBEDS }); + const message = await this.edit({ flags: MessageFlags.SupressEmbeds }); return message; } From ebca648b61af85b88910d84f31bf39ac53bd6220 Mon Sep 17 00:00:00 2001 From: socram03 Date: Thu, 23 Jun 2022 21:35:00 -0400 Subject: [PATCH 02/10] feat: more structures --- structures/Channel.ts | 20 ++++++++++++++++++++ structures/GuildChannel.ts | 30 ++++++++++++++++++++++++++++++ structures/TextChannel.ts | 27 +++++++++++++++++++++++++++ util/Routes.ts | 4 ++++ 4 files changed, 81 insertions(+) create mode 100644 structures/Channel.ts create mode 100644 structures/GuildChannel.ts create mode 100644 structures/TextChannel.ts diff --git a/structures/Channel.ts b/structures/Channel.ts new file mode 100644 index 0000000..f7d33f6 --- /dev/null +++ b/structures/Channel.ts @@ -0,0 +1,20 @@ +import type { Model } from "./Base.ts"; +import { Snowflake, Session, DiscordChannel, ChannelTypes } from "../mod.ts"; + +export class Channel implements Model { + constructor(session: Session, data: DiscordChannel) { + this.id = data.id; + this.data = data; + this.session = session; + this.name = this.data.name; + } + readonly data: DiscordChannel; + readonly id: Snowflake; + readonly session: Session; + public readonly name: string | undefined; + + get type() { + return ChannelTypes[this.data.type]; + } + +} \ No newline at end of file diff --git a/structures/GuildChannel.ts b/structures/GuildChannel.ts new file mode 100644 index 0000000..56529e2 --- /dev/null +++ b/structures/GuildChannel.ts @@ -0,0 +1,30 @@ +import { Channel } from "./Channel.ts"; +import { DiscordChannel, Routes, Session } from "../mod.ts"; + +export class GuildChannel extends Channel { + constructor(session: Session, data: DiscordChannel) { + super(session, data); + this.guild_id = data.guild_id; + } + + guild_id?: string; + + get topic() { + return this.data.topic; + } + + get guildId() { + return this.guild_id; + } + + delete(reason?: string) { + return this.session.rest.runMethod( + this.session.rest, + "DELETE", + Routes.CHANNEL(this.id), + { + reason, + }, + ); + } +} diff --git a/structures/TextChannel.ts b/structures/TextChannel.ts new file mode 100644 index 0000000..eff5c15 --- /dev/null +++ b/structures/TextChannel.ts @@ -0,0 +1,27 @@ +import { GuildChannel } from "./GuildChannel.ts"; +import { DiscordChannel, Session } from "../mod.ts"; + +export class TextChannel extends GuildChannel { + constructor(session: Session, data: DiscordChannel) { + super(session, data); + } + + get lastMessageId() { + return this.data.last_message_id; + } + get rateLimitPerUser() { + return this.data.rate_limit_per_user; + } + + get parentId() { + return this.data.parent_id; + } + + get permissionOverwrites() { + return this.data.permission_overwrites; + } + + get nsfw() { + return this.data.nsfw; + } +} diff --git a/util/Routes.ts b/util/Routes.ts index a6adec0..375c4e5 100644 --- a/util/Routes.ts +++ b/util/Routes.ts @@ -26,6 +26,10 @@ export interface GetMessagesOptions { limit?: number; } +export function CHANNEL(channelId: Snowflake) { + return `/channels/${channelId}`; +} + /** used to send messages */ export function CHANNEL_MESSAGES(channelId: Snowflake, options?: GetMessagesOptions) { let url = `/channels/${channelId}/messages?`; From e2ceefdc0a7efd4169290bd381177a2d737e5414 Mon Sep 17 00:00:00 2001 From: socram03 Date: Sat, 25 Jun 2022 13:31:24 -0400 Subject: [PATCH 03/10] feat: Channels structures --- structures/Channel.ts | 16 +++---- structures/DMChannel.ts | 22 +++++++++ structures/GuildChannel.ts | 24 +++++----- structures/NewsChannel.ts | 11 +++++ structures/TextChannel.ts | 89 +++++++++++++++++++++++++++++++------ structures/ThreadChannel.ts | 23 ++++++++++ structures/VoiceChannel.ts | 19 ++++++++ util/Routes.ts | 20 +++++++++ vendor/types/discord.ts | 2 + 9 files changed, 190 insertions(+), 36 deletions(-) create mode 100644 structures/DMChannel.ts create mode 100644 structures/NewsChannel.ts create mode 100644 structures/ThreadChannel.ts create mode 100644 structures/VoiceChannel.ts diff --git a/structures/Channel.ts b/structures/Channel.ts index f7d33f6..0013d98 100644 --- a/structures/Channel.ts +++ b/structures/Channel.ts @@ -1,20 +1,16 @@ import type { Model } from "./Base.ts"; import { Snowflake, Session, DiscordChannel, ChannelTypes } from "../mod.ts"; -export class Channel implements Model { +export abstract class Channel implements Model { constructor(session: Session, data: DiscordChannel) { this.id = data.id; - this.data = data; this.session = session; - this.name = this.data.name; + this.name = data.name; + this.type = data.type; } - readonly data: DiscordChannel; readonly id: Snowflake; readonly session: Session; - public readonly name: string | undefined; - - get type() { - return ChannelTypes[this.data.type]; - } - + readonly name: string | undefined; + readonly type: ChannelTypes; + } \ No newline at end of file diff --git a/structures/DMChannel.ts b/structures/DMChannel.ts new file mode 100644 index 0000000..4d4b758 --- /dev/null +++ b/structures/DMChannel.ts @@ -0,0 +1,22 @@ +import { Channel } from "./Channel.ts"; +//import { User } from "./User.ts"; +import { Session, DiscordChannel, Snowflake, Routes } from "../mod.ts"; + +export class DMChannel extends Channel { + constructor(session: Session, data: DiscordChannel) { + super(session, data); + data.last_message_id ? this.lastMessageId = data.last_message_id : undefined; + // Waiting for implementation of botId in session + //this.user = new User(this.session, data.recipents!.find((r) => r.id !== this.session.botId)); + } + lastMessageId?: Snowflake; + + async close() { + const channel = await this.session.rest.runMethod( + this.session.rest, + "DELETE", + Routes.CHANNEL(this.id) + ) + return new DMChannel(this.session, channel); + } +} \ No newline at end of file diff --git a/structures/GuildChannel.ts b/structures/GuildChannel.ts index 56529e2..a4a941c 100644 --- a/structures/GuildChannel.ts +++ b/structures/GuildChannel.ts @@ -1,21 +1,21 @@ import { Channel } from "./Channel.ts"; -import { DiscordChannel, Routes, Session } from "../mod.ts"; +import { Guild } from "./Guild.ts"; +import { DiscordChannel, Routes, Session, Snowflake } from "../mod.ts"; + export class GuildChannel extends Channel { - constructor(session: Session, data: DiscordChannel) { + constructor(session: Session, data: DiscordChannel, guild: Guild) { super(session, data); - this.guild_id = data.guild_id; + this.guildId = guild.id; + this.position = data.position; + data.topic ? this.topic = data.topic : null; + data.parent_id ? this.parentId = data.parent_id : undefined; } - guild_id?: string; - - get topic() { - return this.data.topic; - } - - get guildId() { - return this.guild_id; - } + guildId: Snowflake; + topic?: string; + position?: number; + parentId?: Snowflake; delete(reason?: string) { return this.session.rest.runMethod( diff --git a/structures/NewsChannel.ts b/structures/NewsChannel.ts new file mode 100644 index 0000000..ede8a7a --- /dev/null +++ b/structures/NewsChannel.ts @@ -0,0 +1,11 @@ +import { Guild } from "./Guild.ts"; +import { TextChannel } from "./TextChannel.ts" +import { Session, DiscordChannel } from "../mod.ts"; + +export class NewsChannel extends TextChannel { + constructor(session: Session, data: DiscordChannel , guild: Guild) { + super(session, data, guild); + this.defaultAutoArchiveDuration = data.default_auto_archive_duration; + } + defaultAutoArchiveDuration?: number; +} \ No newline at end of file diff --git a/structures/TextChannel.ts b/structures/TextChannel.ts index eff5c15..f901d57 100644 --- a/structures/TextChannel.ts +++ b/structures/TextChannel.ts @@ -1,27 +1,88 @@ import { GuildChannel } from "./GuildChannel.ts"; -import { DiscordChannel, Session } from "../mod.ts"; +import { Guild } from "./Guild.ts"; +import { ThreadChannel } from "./ThreadChannel.ts"; +import { Message } from "./Message.ts"; +import { + DiscordChannel, + DiscordInviteCreate, + Routes, + Session, + Snowflake, + DiscordMessage +} from "../mod.ts"; +import { GetMessagesOptions } from "../util/Routes.ts" + +export interface DiscordInvite { + max_age?: number; + max_uses?: number; + unique?: boolean; + temporary: boolean; + reason?: string; +} + +export interface ThreadCreateOptions { + name: string; + autoArchiveDuration: 60 | 1440 | 4320 | 10080; + type: 10 | 11 | 12; + invitable?: boolean; + reason?: string; +} export class TextChannel extends GuildChannel { - constructor(session: Session, data: DiscordChannel) { - super(session, data); + constructor(session: Session, data: DiscordChannel, guild: Guild) { + super(session, data, guild); + data.last_message_id ? this.lastMessageId = data.last_message_id : undefined; + data.last_pin_timestamp ? this.lastPinTimestamp = data.last_pin_timestamp : undefined; + this.rateLimitPerUser = data.rate_limit_per_user ?? 0; + this.nsfw = !!data.nsfw ?? false; } - get lastMessageId() { - return this.data.last_message_id; - } - get rateLimitPerUser() { - return this.data.rate_limit_per_user; + lastMessageId?: Snowflake; + lastPinTimestamp?: string; + rateLimitPerUser: number; + nsfw: boolean; + + async fetchPins(): Promise { + const messages = await this.session.rest.runMethod( + this.session.rest, + "GET", + Routes.CHANNEL_PINS(this.id), + ); + return messages[0] ? messages.map((x: DiscordMessage) => new Message(this.session, x)) : undefined; } - get parentId() { - return this.data.parent_id; + createInvite(options?: DiscordInvite) { + return this.session.rest.runMethod( + this.session.rest, + "POST", + Routes.CHANNEL_INVITES(this.id), + options, + ); } - get permissionOverwrites() { - return this.data.permission_overwrites; + createThread(options: ThreadCreateOptions) { + this.session.rest.runMethod( + this.session.rest, + "POST", + Routes.CHANNEL_CREATE_THREAD(this.id), + options, + ); } - get nsfw() { - return this.data.nsfw; + fetchMessages(options?: GetMessagesOptions) { + if (options?.limit! > 100) throw Error("Values must be between 0-100") + return this.session.rest.runMethod( + this.session.rest, + "GET", + Routes.CHANNEL_MESSAGES(this.id, options) + ) + } + + sendTyping() { + this.session.rest.runMethod( + this.session.rest, + "POST", + Routes.CHANNEL_TYPING(this.id), + ); } } diff --git a/structures/ThreadChannel.ts b/structures/ThreadChannel.ts new file mode 100644 index 0000000..50daa18 --- /dev/null +++ b/structures/ThreadChannel.ts @@ -0,0 +1,23 @@ +import { GuildChannel } from "./GuildChannel.ts"; +import { Guild } from "./Guild.ts"; +import { DiscordChannel, Session, Snowflake } from "../mod.ts"; + +export class ThreadChannel extends GuildChannel { + constructor(session: Session, data: DiscordChannel, guild: Guild) { + super(session, data, guild); + this.archived = !!data.thread_metadata?.archived; + this.archiveTimestamp = data.thread_metadata?.archive_timestamp; + this.autoArchiveDuration = data.thread_metadata?.auto_archive_duration; + this.locked = !!data.thread_metadata?.locked; + this.messageCount = data.message_count; + this.memberCount = data.member_count; + this.ownerId = data.owner_id; + } + archived?: boolean; + archiveTimestamp?: string; + autoArchiveDuration?: number; + locked?: boolean; + messageCount?: number; + memberCount?: number; + ownerId?: Snowflake; +} diff --git a/structures/VoiceChannel.ts b/structures/VoiceChannel.ts new file mode 100644 index 0000000..8496cd7 --- /dev/null +++ b/structures/VoiceChannel.ts @@ -0,0 +1,19 @@ +import { GuildChannel } from "./GuildChannel.ts"; +import { Guild } from "./Guild.ts"; +import { DiscordChannel, Session, VideoQualityModes } from "../mod.ts"; + +export class VoiceChannel extends GuildChannel { + constructor(session: Session, data: DiscordChannel, guild: Guild) { + super(session, data, guild); + this.bitRate = data.bitrate; + this.userLimit = data.user_limit; + data.rtc_region ? this.rtcRegion = data.rtc_region : undefined; + this.videoQuality = data.video_quality_mode; + this.nsfw = !!data.nsfw; + } + bitRate?: number; + userLimit?: number; + rtcRegion?: string; + videoQuality?: VideoQualityModes; + nsfw?: boolean; +} diff --git a/util/Routes.ts b/util/Routes.ts index d207558..ae76a34 100644 --- a/util/Routes.ts +++ b/util/Routes.ts @@ -30,6 +30,26 @@ export function CHANNEL(channelId: Snowflake) { return `/channels/${channelId}`; } +export function CHANNEL_INVITES(channelId: Snowflake) { + return `/channels/${channelId}/invites`; +} + +export function CHANNEL_TYPING(channelId: Snowflake) { + return `/channels/${channelId}/typing`; +} + +export function CHANNEL_CREATE_THREAD(channelId: Snowflake) { + return `/channels/${channelId}/threads`; +} + +export function MESSAGE_CREATE_THREAD(channelId: Snowflake, messageId: Snowflake) { + return `/channels/${channelId}/messages/${messageId}/threads`; +} + +export function CHANNEL_PINS(channelId: Snowflake) { + return `/channels/${channelId}/pins`; +} + /** used to send messages */ export function CHANNEL_MESSAGES(channelId: Snowflake, options?: GetMessagesOptions) { let url = `/channels/${channelId}/messages?`; diff --git a/vendor/types/discord.ts b/vendor/types/discord.ts index 5fec56a..5123cbb 100644 --- a/vendor/types/discord.ts +++ b/vendor/types/discord.ts @@ -752,6 +752,8 @@ export interface DiscordChannel { permissions?: string; /** When a thread is created this will be true on that channel payload for the thread. */ newly_created?: boolean; + /** The recipients of the DM*/ + recipents?: DiscordUser[]; } /** https://discord.com/developers/docs/topics/gateway#presence-update */ From dca91be86e43473a886b8b2fe32ba7329b0583a9 Mon Sep 17 00:00:00 2001 From: socram03 Date: Sat, 25 Jun 2022 14:01:58 -0400 Subject: [PATCH 04/10] feat: fix and docs --- structures/GuildChannel.ts | 4 ++-- structures/NewsChannel.ts | 4 ++-- structures/TextChannel.ts | 32 ++++++++++++++++++++++---------- structures/ThreadChannel.ts | 4 ++-- structures/VoiceChannel.ts | 9 +++++---- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/structures/GuildChannel.ts b/structures/GuildChannel.ts index a4a941c..748fc0e 100644 --- a/structures/GuildChannel.ts +++ b/structures/GuildChannel.ts @@ -4,9 +4,9 @@ import { DiscordChannel, Routes, Session, Snowflake } from "../mod.ts"; export class GuildChannel extends Channel { - constructor(session: Session, data: DiscordChannel, guild: Guild) { + constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { super(session, data); - this.guildId = guild.id; + this.guildId = guildId; this.position = data.position; data.topic ? this.topic = data.topic : null; data.parent_id ? this.parentId = data.parent_id : undefined; diff --git a/structures/NewsChannel.ts b/structures/NewsChannel.ts index ede8a7a..258cc93 100644 --- a/structures/NewsChannel.ts +++ b/structures/NewsChannel.ts @@ -3,8 +3,8 @@ import { TextChannel } from "./TextChannel.ts" import { Session, DiscordChannel } from "../mod.ts"; export class NewsChannel extends TextChannel { - constructor(session: Session, data: DiscordChannel , guild: Guild) { - super(session, data, guild); + constructor(session: Session, data: DiscordChannel , guildId: Guild["id"]) { + super(session, data, guildId); this.defaultAutoArchiveDuration = data.default_auto_archive_duration; } defaultAutoArchiveDuration?: number; diff --git a/structures/TextChannel.ts b/structures/TextChannel.ts index f901d57..dc17f23 100644 --- a/structures/TextChannel.ts +++ b/structures/TextChannel.ts @@ -12,6 +12,11 @@ import { } from "../mod.ts"; import { GetMessagesOptions } from "../util/Routes.ts" +/** + * Represents the options object to create an invitation + * https://discord.com/developers/docs/resources/channel#create-channel-invite-json-params + */ + export interface DiscordInvite { max_age?: number; max_uses?: number; @@ -20,6 +25,11 @@ export interface DiscordInvite { reason?: string; } +/** + * Represent the options object to create a Thread Channel + * https://discord.com/developers/docs/resources/channel#start-thread-without-message + */ + export interface ThreadCreateOptions { name: string; autoArchiveDuration: 60 | 1440 | 4320 | 10080; @@ -29,8 +39,8 @@ export interface ThreadCreateOptions { } export class TextChannel extends GuildChannel { - constructor(session: Session, data: DiscordChannel, guild: Guild) { - super(session, data, guild); + constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { + super(session, data, guildId); data.last_message_id ? this.lastMessageId = data.last_message_id : undefined; data.last_pin_timestamp ? this.lastPinTimestamp = data.last_pin_timestamp : undefined; this.rateLimitPerUser = data.rate_limit_per_user ?? 0; @@ -42,15 +52,15 @@ export class TextChannel extends GuildChannel { rateLimitPerUser: number; nsfw: boolean; - async fetchPins(): Promise { - const messages = await this.session.rest.runMethod( + async fetchPins(): Promise { + const messages = await this.session.rest.runMethod( this.session.rest, "GET", Routes.CHANNEL_PINS(this.id), ); - return messages[0] ? messages.map((x: DiscordMessage) => new Message(this.session, x)) : undefined; + return messages[0] ? messages.map((x: DiscordMessage) => new Message(this.session, x)) : []; } - + // TODO return Invite Class createInvite(options?: DiscordInvite) { return this.session.rest.runMethod( this.session.rest, @@ -60,22 +70,24 @@ export class TextChannel extends GuildChannel { ); } - createThread(options: ThreadCreateOptions) { - this.session.rest.runMethod( + async createThread(options: ThreadCreateOptions): Promise { + const thread = await this.session.rest.runMethod( this.session.rest, "POST", Routes.CHANNEL_CREATE_THREAD(this.id), options, ); + return new ThreadChannel(this.session, thread, this.guildId); } - fetchMessages(options?: GetMessagesOptions) { + async fetchMessages(options?: GetMessagesOptions): Promise { if (options?.limit! > 100) throw Error("Values must be between 0-100") - return this.session.rest.runMethod( + const messages = await this.session.rest.runMethod( this.session.rest, "GET", Routes.CHANNEL_MESSAGES(this.id, options) ) + return messages[0] ? messages.map((x) => new Message(this.session, x)) : []; } sendTyping() { diff --git a/structures/ThreadChannel.ts b/structures/ThreadChannel.ts index 50daa18..dd03abb 100644 --- a/structures/ThreadChannel.ts +++ b/structures/ThreadChannel.ts @@ -3,8 +3,8 @@ import { Guild } from "./Guild.ts"; import { DiscordChannel, Session, Snowflake } from "../mod.ts"; export class ThreadChannel extends GuildChannel { - constructor(session: Session, data: DiscordChannel, guild: Guild) { - super(session, data, guild); + constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { + super(session, data, guildId); this.archived = !!data.thread_metadata?.archived; this.archiveTimestamp = data.thread_metadata?.archive_timestamp; this.autoArchiveDuration = data.thread_metadata?.auto_archive_duration; diff --git a/structures/VoiceChannel.ts b/structures/VoiceChannel.ts index 8496cd7..b7872eb 100644 --- a/structures/VoiceChannel.ts +++ b/structures/VoiceChannel.ts @@ -1,19 +1,20 @@ import { GuildChannel } from "./GuildChannel.ts"; import { Guild } from "./Guild.ts"; -import { DiscordChannel, Session, VideoQualityModes } from "../mod.ts"; +import { DiscordChannel, Session, VideoQualityModes, Snowflake } from "../mod.ts"; export class VoiceChannel extends GuildChannel { constructor(session: Session, data: DiscordChannel, guild: Guild) { super(session, data, guild); this.bitRate = data.bitrate; - this.userLimit = data.user_limit; + this.userLimit = data.user_limit ?? 0; data.rtc_region ? this.rtcRegion = data.rtc_region : undefined; this.videoQuality = data.video_quality_mode; this.nsfw = !!data.nsfw; } bitRate?: number; - userLimit?: number; - rtcRegion?: string; + userLimit: number; + rtcRegion?: Snowflake; + videoQuality?: VideoQualityModes; nsfw?: boolean; } From c76982541e09e04492a0b5921269f1bdeaf02eda Mon Sep 17 00:00:00 2001 From: socram03 Date: Sat, 25 Jun 2022 16:29:19 -0400 Subject: [PATCH 05/10] fix: fmt --- structures/Attachment.ts | 4 +- structures/BaseGuild.ts | 3 +- structures/Channel.ts | 5 +- structures/DMChannel.ts | 8 +-- structures/Guild.ts | 15 ++-- structures/GuildChannel.ts | 43 ++++++----- structures/Member.ts | 32 +++++---- structures/Message.ts | 10 +-- structures/NewsChannel.ts | 8 +-- structures/Role.ts | 2 +- structures/TextChannel.ts | 137 +++++++++++++++++------------------- structures/ThreadChannel.ts | 34 ++++----- structures/VoiceChannel.ts | 28 ++++---- util/Routes.ts | 6 +- 14 files changed, 167 insertions(+), 168 deletions(-) diff --git a/structures/Attachment.ts b/structures/Attachment.ts index 9fc700c..6f4e578 100644 --- a/structures/Attachment.ts +++ b/structures/Attachment.ts @@ -6,7 +6,7 @@ import type { DiscordAttachment } from "../vendor/external.ts"; /** * Represents an attachment * @link https://discord.com/developers/docs/resources/channel#attachment-object - * */ + */ export class Attachment implements Model { constructor(session: Session, data: DiscordAttachment) { this.session = session; @@ -19,7 +19,7 @@ export class Attachment implements Model { this.size = data.size; this.height = data.height ? data.height : undefined; this.width = data.width ? data.width : undefined; - this.ephemeral = !!data.ephemeral + this.ephemeral = !!data.ephemeral; } readonly session: Session; diff --git a/structures/BaseGuild.ts b/structures/BaseGuild.ts index 4c1d24b..6b47a9e 100644 --- a/structures/BaseGuild.ts +++ b/structures/BaseGuild.ts @@ -6,7 +6,7 @@ import { iconHashToBigInt } from "../util/hash.ts"; /** * Class for {@link Guild} and {@link AnonymousGuild} - * */ + */ export abstract class BaseGuild implements Model { constructor(session: Session, data: DiscordGuild) { this.session = session; @@ -37,4 +37,3 @@ export abstract class BaseGuild implements Model { return this.name; } } - diff --git a/structures/Channel.ts b/structures/Channel.ts index 0013d98..3aebeaa 100644 --- a/structures/Channel.ts +++ b/structures/Channel.ts @@ -1,5 +1,5 @@ import type { Model } from "./Base.ts"; -import { Snowflake, Session, DiscordChannel, ChannelTypes } from "../mod.ts"; +import { ChannelTypes, DiscordChannel, Session, Snowflake } from "../mod.ts"; export abstract class Channel implements Model { constructor(session: Session, data: DiscordChannel) { @@ -12,5 +12,4 @@ export abstract class Channel implements Model { readonly session: Session; readonly name: string | undefined; readonly type: ChannelTypes; - -} \ No newline at end of file +} diff --git a/structures/DMChannel.ts b/structures/DMChannel.ts index 4d4b758..d2f0719 100644 --- a/structures/DMChannel.ts +++ b/structures/DMChannel.ts @@ -1,6 +1,6 @@ import { Channel } from "./Channel.ts"; //import { User } from "./User.ts"; -import { Session, DiscordChannel, Snowflake, Routes } from "../mod.ts"; +import { DiscordChannel, Routes, Session, Snowflake } from "../mod.ts"; export class DMChannel extends Channel { constructor(session: Session, data: DiscordChannel) { @@ -15,8 +15,8 @@ export class DMChannel extends Channel { const channel = await this.session.rest.runMethod( this.session.rest, "DELETE", - Routes.CHANNEL(this.id) - ) + Routes.CHANNEL(this.id), + ); return new DMChannel(this.session, channel); } -} \ No newline at end of file +} diff --git a/structures/Guild.ts b/structures/Guild.ts index 2f88322..b447e58 100644 --- a/structures/Guild.ts +++ b/structures/Guild.ts @@ -2,8 +2,12 @@ import type { Model } from "./Base.ts"; import type { Snowflake } from "../util/Snowflake.ts"; import type { Session } from "../session/Session.ts"; import type { DiscordGuild, DiscordRole } from "../vendor/external.ts"; -import { DefaultMessageNotificationLevels, ExplicitContentFilterLevels, VerificationLevels } from "../vendor/external.ts"; -import { iconHashToBigInt, iconBigintToHash } from "../util/hash.ts"; +import { + DefaultMessageNotificationLevels, + ExplicitContentFilterLevels, + VerificationLevels, +} from "../vendor/external.ts"; +import { iconBigintToHash, iconHashToBigInt } from "../util/hash.ts"; import { Member } from "./Member.ts"; import { BaseGuild } from "./BaseGuild.ts"; import { Role } from "./Role.ts"; @@ -21,7 +25,7 @@ export interface CreateRole { /** * Represents a guild * @link https://discord.com/developers/docs/resources/guild#guild-object - * */ + */ export class Guild extends BaseGuild implements Model { constructor(session: Session, data: DiscordGuild) { super(session, data); @@ -55,8 +59,7 @@ export class Guild extends BaseGuild implements Model { if (options.iconHash) { if (typeof options.iconHash === "string") { icon = options.iconHash; - } - else { + } else { icon = iconBigintToHash(options.iconHash); } } @@ -72,7 +75,7 @@ export class Guild extends BaseGuild implements Model { unicode_emoji: options.unicodeEmoji, hoist: options.hoist, mentionable: options.mentionable, - } + }, ); return new Role(this.session, this, role); diff --git a/structures/GuildChannel.ts b/structures/GuildChannel.ts index 748fc0e..c0a986f 100644 --- a/structures/GuildChannel.ts +++ b/structures/GuildChannel.ts @@ -2,29 +2,28 @@ import { Channel } from "./Channel.ts"; import { Guild } from "./Guild.ts"; import { DiscordChannel, Routes, Session, Snowflake } from "../mod.ts"; - export class GuildChannel extends Channel { - constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { - super(session, data); - this.guildId = guildId; - this.position = data.position; - data.topic ? this.topic = data.topic : null; - data.parent_id ? this.parentId = data.parent_id : undefined; - } + constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { + super(session, data); + this.guildId = guildId; + this.position = data.position; + data.topic ? this.topic = data.topic : null; + data.parent_id ? this.parentId = data.parent_id : undefined; + } - guildId: Snowflake; - topic?: string; - position?: number; - parentId?: Snowflake; + guildId: Snowflake; + topic?: string; + position?: number; + parentId?: Snowflake; - delete(reason?: string) { - return this.session.rest.runMethod( - this.session.rest, - "DELETE", - Routes.CHANNEL(this.id), - { - reason, - }, - ); - } + delete(reason?: string) { + return this.session.rest.runMethod( + this.session.rest, + "DELETE", + Routes.CHANNEL(this.id), + { + reason, + }, + ); + } } diff --git a/structures/Member.ts b/structures/Member.ts index dcae31f..f5a5baf 100644 --- a/structures/Member.ts +++ b/structures/Member.ts @@ -9,19 +9,19 @@ import { User } from "./User.ts"; /** * @link https://discord.com/developers/docs/resources/guild#create-guild-ban - * */ + */ export interface CreateGuildBan { - /** Number of days to delete messages for (0-7) */ - deleteMessageDays?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7; - /** Reason for the ban */ - reason?: string; + /** Number of days to delete messages for (0-7) */ + deleteMessageDays?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7; + /** Reason for the ban */ + reason?: string; } /** * Represents a guild member * TODO: add a `guild` property somehow * @link https://discord.com/developers/docs/resources/guild#guild-member-object - * */ + */ export class Member implements Model { constructor(session: Session, data: MakeRequired) { this.session = session; @@ -33,7 +33,9 @@ export class Member implements Model { this.deaf = !!data.deaf; this.mute = !!data.mute; this.pending = !!data.pending; - this.communicationDisabledUntilTimestamp = data.communication_disabled_until ? Number.parseInt(data.communication_disabled_until) : undefined; + this.communicationDisabledUntilTimestamp = data.communication_disabled_until + ? Number.parseInt(data.communication_disabled_until) + : undefined; } readonly session: Session; @@ -63,16 +65,18 @@ export class Member implements Model { /** * Bans the member - * */ + */ async ban(guildId: Snowflake, options: CreateGuildBan): Promise { await this.session.rest.runMethod( this.session.rest, "PUT", Routes.GUILD_BAN(guildId, this.id), - options ? { - delete_message_days: options.deleteMessageDays, - reason: options.reason - } : {} + options + ? { + delete_message_days: options.deleteMessageDays, + reason: options.reason, + } + : {}, ); return this; @@ -80,13 +84,13 @@ export class Member implements Model { /** * Kicks the member - * */ + */ async kick(guildId: Snowflake, { reason }: { reason?: string }): Promise { await this.session.rest.runMethod( this.session.rest, "DELETE", Routes.GUILD_MEMBER(guildId, this.id), - { reason } + { reason }, ); return this; diff --git a/structures/Message.ts b/structures/Message.ts index 5d4de74..b545221 100644 --- a/structures/Message.ts +++ b/structures/Message.ts @@ -55,10 +55,12 @@ export class Message implements Model { this.attachments = data.attachments.map((attachment) => new Attachment(session, attachment)); // user is always null on MessageCreate and its replaced with author - this.member = data.member ? new Member(session, { - ...data.member, - user: data.author, - }) : undefined; + this.member = data.member + ? new Member(session, { + ...data.member, + user: data.author, + }) + : undefined; } readonly session: Session; diff --git a/structures/NewsChannel.ts b/structures/NewsChannel.ts index 258cc93..bd47f06 100644 --- a/structures/NewsChannel.ts +++ b/structures/NewsChannel.ts @@ -1,11 +1,11 @@ import { Guild } from "./Guild.ts"; -import { TextChannel } from "./TextChannel.ts" -import { Session, DiscordChannel } from "../mod.ts"; +import { TextChannel } from "./TextChannel.ts"; +import { DiscordChannel, Session } from "../mod.ts"; export class NewsChannel extends TextChannel { - constructor(session: Session, data: DiscordChannel , guildId: Guild["id"]) { + constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { super(session, data, guildId); this.defaultAutoArchiveDuration = data.default_auto_archive_duration; } defaultAutoArchiveDuration?: number; -} \ No newline at end of file +} diff --git a/structures/Role.ts b/structures/Role.ts index fd754f7..7ebf3fc 100644 --- a/structures/Role.ts +++ b/structures/Role.ts @@ -39,7 +39,7 @@ export class Role implements Model { } get hexColor() { - return `#${this.color.toString(16).padStart(6, '0')}`; + return `#${this.color.toString(16).padStart(6, "0")}`; } /* diff --git a/structures/TextChannel.ts b/structures/TextChannel.ts index dc17f23..4c82bee 100644 --- a/structures/TextChannel.ts +++ b/structures/TextChannel.ts @@ -2,15 +2,8 @@ import { GuildChannel } from "./GuildChannel.ts"; import { Guild } from "./Guild.ts"; import { ThreadChannel } from "./ThreadChannel.ts"; import { Message } from "./Message.ts"; -import { - DiscordChannel, - DiscordInviteCreate, - Routes, - Session, - Snowflake, - DiscordMessage -} from "../mod.ts"; -import { GetMessagesOptions } from "../util/Routes.ts" +import { DiscordChannel, DiscordInviteCreate, DiscordMessage, Routes, Session, Snowflake } from "../mod.ts"; +import { GetMessagesOptions } from "../util/Routes.ts"; /** * Represents the options object to create an invitation @@ -18,11 +11,11 @@ import { GetMessagesOptions } from "../util/Routes.ts" */ export interface DiscordInvite { - max_age?: number; - max_uses?: number; - unique?: boolean; - temporary: boolean; - reason?: string; + max_age?: number; + max_uses?: number; + unique?: boolean; + temporary: boolean; + reason?: string; } /** @@ -31,70 +24,70 @@ export interface DiscordInvite { */ export interface ThreadCreateOptions { - name: string; - autoArchiveDuration: 60 | 1440 | 4320 | 10080; - type: 10 | 11 | 12; - invitable?: boolean; - reason?: string; + name: string; + autoArchiveDuration: 60 | 1440 | 4320 | 10080; + type: 10 | 11 | 12; + invitable?: boolean; + reason?: string; } export class TextChannel extends GuildChannel { - constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { - super(session, data, guildId); - data.last_message_id ? this.lastMessageId = data.last_message_id : undefined; - data.last_pin_timestamp ? this.lastPinTimestamp = data.last_pin_timestamp : undefined; - this.rateLimitPerUser = data.rate_limit_per_user ?? 0; - this.nsfw = !!data.nsfw ?? false; - } + constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { + super(session, data, guildId); + data.last_message_id ? this.lastMessageId = data.last_message_id : undefined; + data.last_pin_timestamp ? this.lastPinTimestamp = data.last_pin_timestamp : undefined; + this.rateLimitPerUser = data.rate_limit_per_user ?? 0; + this.nsfw = !!data.nsfw ?? false; + } - lastMessageId?: Snowflake; - lastPinTimestamp?: string; - rateLimitPerUser: number; - nsfw: boolean; + lastMessageId?: Snowflake; + lastPinTimestamp?: string; + rateLimitPerUser: number; + nsfw: boolean; - async fetchPins(): Promise { - const messages = await this.session.rest.runMethod( - this.session.rest, - "GET", - Routes.CHANNEL_PINS(this.id), - ); - return messages[0] ? messages.map((x: DiscordMessage) => new Message(this.session, x)) : []; - } - // TODO return Invite Class - createInvite(options?: DiscordInvite) { - return this.session.rest.runMethod( - this.session.rest, - "POST", - Routes.CHANNEL_INVITES(this.id), - options, - ); - } + async fetchPins(): Promise { + const messages = await this.session.rest.runMethod( + this.session.rest, + "GET", + Routes.CHANNEL_PINS(this.id), + ); + return messages[0] ? messages.map((x: DiscordMessage) => new Message(this.session, x)) : []; + } + // TODO return Invite Class + createInvite(options?: DiscordInvite) { + return this.session.rest.runMethod( + this.session.rest, + "POST", + Routes.CHANNEL_INVITES(this.id), + options, + ); + } - async createThread(options: ThreadCreateOptions): Promise { - const thread = await this.session.rest.runMethod( - this.session.rest, - "POST", - Routes.CHANNEL_CREATE_THREAD(this.id), - options, - ); - return new ThreadChannel(this.session, thread, this.guildId); - } + async createThread(options: ThreadCreateOptions): Promise { + const thread = await this.session.rest.runMethod( + this.session.rest, + "POST", + Routes.CHANNEL_CREATE_THREAD(this.id), + options, + ); + return new ThreadChannel(this.session, thread, this.guildId); + } - async fetchMessages(options?: GetMessagesOptions): Promise { - if (options?.limit! > 100) throw Error("Values must be between 0-100") - const messages = await this.session.rest.runMethod( - this.session.rest, - "GET", - Routes.CHANNEL_MESSAGES(this.id, options) - ) - return messages[0] ? messages.map((x) => new Message(this.session, x)) : []; - } + async fetchMessages(options?: GetMessagesOptions): Promise { + if (options?.limit! > 100) throw Error("Values must be between 0-100"); + const messages = await this.session.rest.runMethod( + this.session.rest, + "GET", + Routes.CHANNEL_MESSAGES(this.id, options), + ); + return messages[0] ? messages.map((x) => new Message(this.session, x)) : []; + } - sendTyping() { - this.session.rest.runMethod( - this.session.rest, - "POST", - Routes.CHANNEL_TYPING(this.id), - ); - } + sendTyping() { + this.session.rest.runMethod( + this.session.rest, + "POST", + Routes.CHANNEL_TYPING(this.id), + ); + } } diff --git a/structures/ThreadChannel.ts b/structures/ThreadChannel.ts index dd03abb..9987c4a 100644 --- a/structures/ThreadChannel.ts +++ b/structures/ThreadChannel.ts @@ -3,21 +3,21 @@ import { Guild } from "./Guild.ts"; import { DiscordChannel, Session, Snowflake } from "../mod.ts"; export class ThreadChannel extends GuildChannel { - constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { - super(session, data, guildId); - this.archived = !!data.thread_metadata?.archived; - this.archiveTimestamp = data.thread_metadata?.archive_timestamp; - this.autoArchiveDuration = data.thread_metadata?.auto_archive_duration; - this.locked = !!data.thread_metadata?.locked; - this.messageCount = data.message_count; - this.memberCount = data.member_count; - this.ownerId = data.owner_id; - } - archived?: boolean; - archiveTimestamp?: string; - autoArchiveDuration?: number; - locked?: boolean; - messageCount?: number; - memberCount?: number; - ownerId?: Snowflake; + constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { + super(session, data, guildId); + this.archived = !!data.thread_metadata?.archived; + this.archiveTimestamp = data.thread_metadata?.archive_timestamp; + this.autoArchiveDuration = data.thread_metadata?.auto_archive_duration; + this.locked = !!data.thread_metadata?.locked; + this.messageCount = data.message_count; + this.memberCount = data.member_count; + this.ownerId = data.owner_id; + } + archived?: boolean; + archiveTimestamp?: string; + autoArchiveDuration?: number; + locked?: boolean; + messageCount?: number; + memberCount?: number; + ownerId?: Snowflake; } diff --git a/structures/VoiceChannel.ts b/structures/VoiceChannel.ts index b7872eb..065c693 100644 --- a/structures/VoiceChannel.ts +++ b/structures/VoiceChannel.ts @@ -1,20 +1,20 @@ import { GuildChannel } from "./GuildChannel.ts"; import { Guild } from "./Guild.ts"; -import { DiscordChannel, Session, VideoQualityModes, Snowflake } from "../mod.ts"; +import { DiscordChannel, Session, Snowflake, VideoQualityModes } from "../mod.ts"; export class VoiceChannel extends GuildChannel { - constructor(session: Session, data: DiscordChannel, guild: Guild) { - super(session, data, guild); - this.bitRate = data.bitrate; - this.userLimit = data.user_limit ?? 0; - data.rtc_region ? this.rtcRegion = data.rtc_region : undefined; - this.videoQuality = data.video_quality_mode; - this.nsfw = !!data.nsfw; - } - bitRate?: number; - userLimit: number; - rtcRegion?: Snowflake; + constructor(session: Session, data: DiscordChannel, guild: Guild) { + super(session, data, guild); + this.bitRate = data.bitrate; + this.userLimit = data.user_limit ?? 0; + data.rtc_region ? this.rtcRegion = data.rtc_region : undefined; + this.videoQuality = data.video_quality_mode; + this.nsfw = !!data.nsfw; + } + bitRate?: number; + userLimit: number; + rtcRegion?: Snowflake; - videoQuality?: VideoQualityModes; - nsfw?: boolean; + videoQuality?: VideoQualityModes; + nsfw?: boolean; } diff --git a/util/Routes.ts b/util/Routes.ts index ae76a34..68865eb 100644 --- a/util/Routes.ts +++ b/util/Routes.ts @@ -80,9 +80,9 @@ export function GUILD_BAN(guildId: Snowflake, userId: Snowflake) { } export interface GetBans { - limit?: number; - before?: Snowflake; - after?: Snowflake; + limit?: number; + before?: Snowflake; + after?: Snowflake; } /** used to unban members */ From f364d55c597d417fa9e19c7a1af8d559982e2542 Mon Sep 17 00:00:00 2001 From: socram03 Date: Sat, 25 Jun 2022 16:35:16 -0400 Subject: [PATCH 06/10] fixes --- mod.ts | 5 +++++ structures/NewsChannel.ts | 4 +--- structures/TextChannel.ts | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/mod.ts b/mod.ts index 9336da3..2663b56 100644 --- a/mod.ts +++ b/mod.ts @@ -10,3 +10,8 @@ export * from "./structures/Message.ts"; export * from "./structures/BaseGuild.ts"; export * from "./structures/Attachment.ts"; export * from "./structures/AnonymousGuild.ts"; +export * from "./structures/DMChannel.ts"; +export * from "./structures/TextChannel.ts"; +export * from "./structures/VoiceChannel.ts"; +export * from "./structures/ThreadChannel.ts"; +export * from "./structures/NewsChannel.ts"; diff --git a/structures/NewsChannel.ts b/structures/NewsChannel.ts index bd47f06..664532e 100644 --- a/structures/NewsChannel.ts +++ b/structures/NewsChannel.ts @@ -1,6 +1,4 @@ -import { Guild } from "./Guild.ts"; -import { TextChannel } from "./TextChannel.ts"; -import { DiscordChannel, Session } from "../mod.ts"; +import { DiscordChannel, Guild, Session, TextChannel } from "../mod.ts"; export class NewsChannel extends TextChannel { constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { diff --git a/structures/TextChannel.ts b/structures/TextChannel.ts index 4c82bee..6c9a729 100644 --- a/structures/TextChannel.ts +++ b/structures/TextChannel.ts @@ -10,7 +10,7 @@ import { GetMessagesOptions } from "../util/Routes.ts"; * https://discord.com/developers/docs/resources/channel#create-channel-invite-json-params */ -export interface DiscordInvite { +export interface DiscordInviteOptions { max_age?: number; max_uses?: number; unique?: boolean; @@ -54,7 +54,7 @@ export class TextChannel extends GuildChannel { return messages[0] ? messages.map((x: DiscordMessage) => new Message(this.session, x)) : []; } // TODO return Invite Class - createInvite(options?: DiscordInvite) { + createInvite(options?: DiscordInviteOptions) { return this.session.rest.runMethod( this.session.rest, "POST", From aa1aaf6682a03e748246ec6c55621f68d6dba24c Mon Sep 17 00:00:00 2001 From: socram03 Date: Sat, 25 Jun 2022 16:43:00 -0400 Subject: [PATCH 07/10] feat: toString() --- structures/Channel.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/structures/Channel.ts b/structures/Channel.ts index 3aebeaa..40e3bd7 100644 --- a/structures/Channel.ts +++ b/structures/Channel.ts @@ -8,8 +8,12 @@ export abstract class Channel implements Model { this.name = data.name; this.type = data.type; } - readonly id: Snowflake; - readonly session: Session; - readonly name: string | undefined; - readonly type: ChannelTypes; + id: Snowflake; + session: Session; + name?: string; + type: ChannelTypes; + + toString(): string { + return `<#${this.id}>`; + } } From 373c46471d4c8709f08f4197e277f94389b06c6f Mon Sep 17 00:00:00 2001 From: socram03 Date: Sat, 25 Jun 2022 19:38:34 -0400 Subject: [PATCH 08/10] feat: some changes --- mod.ts | 1 + structures/Channel.ts | 4 ++-- structures/Emoji.ts | 18 ++++++++++++++++++ structures/Guild.ts | 4 ++-- structures/GuildChannel.ts | 2 +- structures/GuildEmoji.ts | 15 +++++++++++++++ structures/Role.ts | 13 ++++++------- structures/TextChannel.ts | 4 ++-- structures/VoiceChannel.ts | 4 ++-- 9 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 structures/Emoji.ts create mode 100644 structures/GuildEmoji.ts diff --git a/mod.ts b/mod.ts index 2663b56..7304f31 100644 --- a/mod.ts +++ b/mod.ts @@ -15,3 +15,4 @@ export * from "./structures/TextChannel.ts"; export * from "./structures/VoiceChannel.ts"; export * from "./structures/ThreadChannel.ts"; export * from "./structures/NewsChannel.ts"; +export * from "./structures/Emoji.ts"; diff --git a/structures/Channel.ts b/structures/Channel.ts index 40e3bd7..f2fae2e 100644 --- a/structures/Channel.ts +++ b/structures/Channel.ts @@ -8,8 +8,8 @@ export abstract class Channel implements Model { this.name = data.name; this.type = data.type; } - id: Snowflake; - session: Session; + readonly id: Snowflake; + readonly session: Session; name?: string; type: ChannelTypes; diff --git a/structures/Emoji.ts b/structures/Emoji.ts new file mode 100644 index 0000000..78df164 --- /dev/null +++ b/structures/Emoji.ts @@ -0,0 +1,18 @@ +import { Session, DiscordEmoji, Snowflake } from "../mod.ts"; + +export class Emoji { + constructor(session: Session, data: DiscordEmoji) { + this.id = data.id; + this.name = data.name; + this.animated = !!data.animated; + this.available = !!data.available; + this.requireColons = !!data.require_colons; + this.session = session; + } + readonly id?: Snowflake; + readonly session: Session; + name?: string; + animated: boolean; + available: boolean; + requireColons: boolean; +} \ No newline at end of file diff --git a/structures/Guild.ts b/structures/Guild.ts index b447e58..f18eb86 100644 --- a/structures/Guild.ts +++ b/structures/Guild.ts @@ -39,7 +39,7 @@ export class Guild extends BaseGuild implements Model { this.defaultMessageNotificationLevel = data.default_message_notifications; this.explicitContentFilterLevel = data.explicit_content_filter; this.members = data.members?.map((member) => new Member(session, { ...member, user: member.user! })) ?? []; - this.roles = data.roles.map((role) => new Role(session, this, role)); + this.roles = data.roles.map((role) => new Role(session, this.id, role)); } splashHash?: bigint; @@ -78,7 +78,7 @@ export class Guild extends BaseGuild implements Model { }, ); - return new Role(this.session, this, role); + return new Role(this.session, this.id, role); } async deleteRole(roleId: Snowflake): Promise { diff --git a/structures/GuildChannel.ts b/structures/GuildChannel.ts index c0a986f..8ca5873 100644 --- a/structures/GuildChannel.ts +++ b/structures/GuildChannel.ts @@ -2,7 +2,7 @@ import { Channel } from "./Channel.ts"; import { Guild } from "./Guild.ts"; import { DiscordChannel, Routes, Session, Snowflake } from "../mod.ts"; -export class GuildChannel extends Channel { +export abstract class GuildChannel extends Channel { constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { super(session, data); this.guildId = guildId; diff --git a/structures/GuildEmoji.ts b/structures/GuildEmoji.ts new file mode 100644 index 0000000..f0056b8 --- /dev/null +++ b/structures/GuildEmoji.ts @@ -0,0 +1,15 @@ +import { Snowflake, Session, Emoji, DiscordEmoji, User } from "../mod.ts"; + +export class GuildEmoji extends Emoji { + constructor(session: Session, data: DiscordEmoji, guildId: Snowflake) { + super(session, data); + this.guildId = guildId; + this.roles = data.roles; + this.user = data.user ? new User(this.session, data.user) : undefined; + this.managed = !!data.managed; + } + guildId: Snowflake; + roles?: Snowflake[]; + user?: User; + managed?: boolean; +} \ No newline at end of file diff --git a/structures/Role.ts b/structures/Role.ts index 2bb3cb1..9b71a56 100644 --- a/structures/Role.ts +++ b/structures/Role.ts @@ -1,15 +1,14 @@ import type { Model } from "./Base.ts"; import type { Session } from "../session/Session.ts"; import type { DiscordRole } from "../vendor/external.ts"; -import { Snowflake } from "../util/Snowflake.ts"; +import { Snowflake, Routes } from "../mod.ts"; import { iconHashToBigInt } from "../util/hash.ts"; -import { Guild } from "./Guild.ts"; export class Role implements Model { - constructor(session: Session, guild: Guild, data: DiscordRole) { + constructor(session: Session, guildId: Snowflake, data: DiscordRole) { this.session = session; this.id = data.id; - this.guild = guild; + this.guildId = guildId; this.hoist = data.hoist; this.iconHash = data.icon ? iconHashToBigInt(data.icon) : undefined; this.color = data.color; @@ -21,7 +20,7 @@ export class Role implements Model { session: Session; id: Snowflake; - guild: Guild; + guildId: Snowflake; hoist: boolean; iconHash?: bigint; color: number; @@ -43,12 +42,12 @@ export class Role implements Model { } async delete() { - await this.guild.deleteRole(this.id); + await this.session.rest.runMethod(this.session.rest, "DELETE", Routes.GUILD_ROLE(this.guildId, this.id)); } toString() { switch (this.id) { - case this.guild.id: + case this.guildId: return "@everyone"; default: return `<@&${this.id}>`; diff --git a/structures/TextChannel.ts b/structures/TextChannel.ts index 6c9a729..446bdcc 100644 --- a/structures/TextChannel.ts +++ b/structures/TextChannel.ts @@ -7,7 +7,7 @@ import { GetMessagesOptions } from "../util/Routes.ts"; /** * Represents the options object to create an invitation - * https://discord.com/developers/docs/resources/channel#create-channel-invite-json-params + * @link https://discord.com/developers/docs/resources/channel#create-channel-invite-json-params */ export interface DiscordInviteOptions { @@ -20,7 +20,7 @@ export interface DiscordInviteOptions { /** * Represent the options object to create a Thread Channel - * https://discord.com/developers/docs/resources/channel#start-thread-without-message + * @link https://discord.com/developers/docs/resources/channel#start-thread-without-message */ export interface ThreadCreateOptions { diff --git a/structures/VoiceChannel.ts b/structures/VoiceChannel.ts index 065c693..7f285c3 100644 --- a/structures/VoiceChannel.ts +++ b/structures/VoiceChannel.ts @@ -3,8 +3,8 @@ import { Guild } from "./Guild.ts"; import { DiscordChannel, Session, Snowflake, VideoQualityModes } from "../mod.ts"; export class VoiceChannel extends GuildChannel { - constructor(session: Session, data: DiscordChannel, guild: Guild) { - super(session, data, guild); + constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { + super(session, data, guildId); this.bitRate = data.bitrate; this.userLimit = data.user_limit ?? 0; data.rtc_region ? this.rtcRegion = data.rtc_region : undefined; From 3c1d53d5ff4511d4c306819687757cfb3315e3fc Mon Sep 17 00:00:00 2001 From: socram03 Date: Sat, 25 Jun 2022 19:44:30 -0400 Subject: [PATCH 09/10] Merge branch 'main' of github.com:socram03/biscuit into main --- structures/Guild.ts | 2 +- structures/Role.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/structures/Guild.ts b/structures/Guild.ts index f18eb86..37dfe5b 100644 --- a/structures/Guild.ts +++ b/structures/Guild.ts @@ -39,7 +39,7 @@ export class Guild extends BaseGuild implements Model { this.defaultMessageNotificationLevel = data.default_message_notifications; this.explicitContentFilterLevel = data.explicit_content_filter; this.members = data.members?.map((member) => new Member(session, { ...member, user: member.user! })) ?? []; - this.roles = data.roles.map((role) => new Role(session, this.id, role)); + this.roles = data.roles.map((role) => new Role(session, data.id, role)); } splashHash?: bigint; diff --git a/structures/Role.ts b/structures/Role.ts index 34b5d03..be4c156 100644 --- a/structures/Role.ts +++ b/structures/Role.ts @@ -47,7 +47,7 @@ export class Role implements Model { async delete(): Promise { // cool jS trick - await Guild.prototype.deleteRole.call({ id: this.guildId }, this.id); + await Guild.prototype.deleteRole.call({ id: this.guildId, session: this.session }, this.id); } toString() { From 51778153094f45b1dc6869a49d0963519784eb6c Mon Sep 17 00:00:00 2001 From: socram03 Date: Sat, 25 Jun 2022 19:45:09 -0400 Subject: [PATCH 10/10] format --- structures/Emoji.ts | 6 +++--- structures/GuildEmoji.ts | 4 ++-- structures/Permissions.ts | 6 ++++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/structures/Emoji.ts b/structures/Emoji.ts index 78df164..9473b67 100644 --- a/structures/Emoji.ts +++ b/structures/Emoji.ts @@ -1,6 +1,6 @@ -import { Session, DiscordEmoji, Snowflake } from "../mod.ts"; +import { DiscordEmoji, Session, Snowflake } from "../mod.ts"; -export class Emoji { +export class Emoji { constructor(session: Session, data: DiscordEmoji) { this.id = data.id; this.name = data.name; @@ -15,4 +15,4 @@ export class Emoji { animated: boolean; available: boolean; requireColons: boolean; -} \ No newline at end of file +} diff --git a/structures/GuildEmoji.ts b/structures/GuildEmoji.ts index f0056b8..8b022e9 100644 --- a/structures/GuildEmoji.ts +++ b/structures/GuildEmoji.ts @@ -1,4 +1,4 @@ -import { Snowflake, Session, Emoji, DiscordEmoji, User } from "../mod.ts"; +import { DiscordEmoji, Emoji, Session, Snowflake, User } from "../mod.ts"; export class GuildEmoji extends Emoji { constructor(session: Session, data: DiscordEmoji, guildId: Snowflake) { @@ -12,4 +12,4 @@ export class GuildEmoji extends Emoji { roles?: Snowflake[]; user?: User; managed?: boolean; -} \ No newline at end of file +} diff --git a/structures/Permissions.ts b/structures/Permissions.ts index d334337..c212686 100644 --- a/structures/Permissions.ts +++ b/structures/Permissions.ts @@ -1,7 +1,7 @@ import { BitwisePermissionFlags } from "../vendor/external.ts"; export type PermissionString = keyof typeof BitwisePermissionFlags; -export type PermissionResolvable= +export type PermissionResolvable = | bigint | PermissionString | PermissionString[] @@ -32,7 +32,9 @@ export class Permissions { case "string": return BigInt(Permissions.Flags[bit]); case "object": - return Permissions.resolve(bit.map((p) => BigInt(Permissions.Flags[p])).reduce((acc, cur) => acc | cur, 0n)); + return Permissions.resolve( + bit.map((p) => BigInt(Permissions.Flags[p])).reduce((acc, cur) => acc | cur, 0n), + ); default: throw new TypeError(`Cannot resolve permission: ${bit}`); }