This commit is contained in:
socram03 2022-06-25 16:29:19 -04:00
parent dca91be86e
commit c76982541e
14 changed files with 167 additions and 168 deletions

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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<DiscordChannel>(
this.session.rest,
"DELETE",
Routes.CHANNEL(this.id)
)
Routes.CHANNEL(this.id),
);
return new DMChannel(this.session, channel);
}
}

View File

@ -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);

View File

@ -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<DiscordChannel>(
this.session.rest,
"DELETE",
Routes.CHANNEL(this.id),
{
reason,
},
);
}
delete(reason?: string) {
return this.session.rest.runMethod<DiscordChannel>(
this.session.rest,
"DELETE",
Routes.CHANNEL(this.id),
{
reason,
},
);
}
}

View File

@ -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<DiscordMember, "user">) {
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<Member> {
await this.session.rest.runMethod<undefined>(
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<Member> {
await this.session.rest.runMethod<undefined>(
this.session.rest,
"DELETE",
Routes.GUILD_MEMBER(guildId, this.id),
{ reason }
{ reason },
);
return this;

View File

@ -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;

View File

@ -1,9 +1,9 @@
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;
}

View File

@ -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")}`;
}
/*

View File

@ -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<Message[] | []> {
const messages = await this.session.rest.runMethod<DiscordMessage[]>(
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<DiscordInviteCreate>(
this.session.rest,
"POST",
Routes.CHANNEL_INVITES(this.id),
options,
);
}
async fetchPins(): Promise<Message[] | []> {
const messages = await this.session.rest.runMethod<DiscordMessage[]>(
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<DiscordInviteCreate>(
this.session.rest,
"POST",
Routes.CHANNEL_INVITES(this.id),
options,
);
}
async createThread(options: ThreadCreateOptions): Promise<ThreadChannel> {
const thread = await this.session.rest.runMethod<DiscordChannel>(
this.session.rest,
"POST",
Routes.CHANNEL_CREATE_THREAD(this.id),
options,
);
return new ThreadChannel(this.session, thread, this.guildId);
}
async createThread(options: ThreadCreateOptions): Promise<ThreadChannel> {
const thread = await this.session.rest.runMethod<DiscordChannel>(
this.session.rest,
"POST",
Routes.CHANNEL_CREATE_THREAD(this.id),
options,
);
return new ThreadChannel(this.session, thread, this.guildId);
}
async fetchMessages(options?: GetMessagesOptions): Promise<Message[] | []> {
if (options?.limit! > 100) throw Error("Values must be between 0-100")
const messages = await this.session.rest.runMethod<DiscordMessage[]>(
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<Message[] | []> {
if (options?.limit! > 100) throw Error("Values must be between 0-100");
const messages = await this.session.rest.runMethod<DiscordMessage[]>(
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<undefined>(
this.session.rest,
"POST",
Routes.CHANNEL_TYPING(this.id),
);
}
sendTyping() {
this.session.rest.runMethod<undefined>(
this.session.rest,
"POST",
Routes.CHANNEL_TYPING(this.id),
);
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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 */