diff --git a/mod.ts b/mod.ts index 55bee3e..7d53cf4 100644 --- a/mod.ts +++ b/mod.ts @@ -28,7 +28,6 @@ export { ChannelsManager } from './src/managers/channels.ts' export { EmojisManager } from './src/managers/emojis.ts' export { GatewayCache } from './src/managers/gatewayCache.ts' export { GuildChannelsManager } from './src/managers/guildChannels.ts' -export type { GuildChannel } from './src/managers/guildChannels.ts' export { GuildManager } from './src/managers/guilds.ts' export * from './src/structures/slash.ts' export * from './src/types/slash.ts' @@ -102,7 +101,8 @@ export type { GuildBanPayload, GuildFeatures, GuildIntegrationPayload, - GuildPayload + GuildPayload, + GuildChannels } from './src/types/guild.ts' export type { InvitePayload, PartialInvitePayload } from './src/types/invite.ts' export { PermissionFlags } from './src/types/permissionFlags.ts' diff --git a/src/managers/guildChannels.ts b/src/managers/guildChannels.ts index bbc279e..0cbd52e 100644 --- a/src/managers/guildChannels.ts +++ b/src/managers/guildChannels.ts @@ -2,26 +2,16 @@ import { Client } from '../models/client.ts' import { Channel } from '../structures/channel.ts' import { Guild } from '../structures/guild.ts' import { CategoryChannel } from '../structures/guildCategoryChannel.ts' -import { GuildTextChannel } from '../structures/textChannel.ts' -import { VoiceChannel } from '../structures/guildVoiceChannel.ts' import { ChannelTypes, - GuildCategoryChannelPayload, GuildChannelPayload, - GuildTextChannelPayload, - GuildVoiceChannelPayload, Overwrite } from '../types/channel.ts' +import { GuildChannels, GuildChannelPayloads } from '../types/guild.ts' import { CHANNEL, GUILD_CHANNELS } from '../types/endpoint.ts' import { BaseChildManager } from './baseChild.ts' import { ChannelsManager } from './channels.ts' -export type GuildChannelPayloads = - | GuildTextChannelPayload - | GuildVoiceChannelPayload - | GuildCategoryChannelPayload -export type GuildChannel = GuildTextChannel | VoiceChannel | CategoryChannel - export interface CreateChannelOptions { name: string type?: ChannelTypes @@ -37,7 +27,7 @@ export interface CreateChannelOptions { export class GuildChannelsManager extends BaseChildManager< GuildChannelPayloads, - GuildChannel + GuildChannels > { guild: Guild @@ -46,7 +36,7 @@ export class GuildChannelsManager extends BaseChildManager< this.guild = guild } - async get(id: string): Promise { + async get(id: string): Promise { const res = await this.parent.get(id) if (res !== undefined && res.guild.id === this.guild.id) return res else return undefined @@ -57,7 +47,7 @@ export class GuildChannelsManager extends BaseChildManager< return this.client.rest.delete(CHANNEL(id)) } - async array(): Promise { + async array(): Promise { const arr = (await this.parent.array()) as Channel[] return arr.filter( (c: any) => c.guild !== undefined && c.guild.id === this.guild.id @@ -73,7 +63,7 @@ export class GuildChannelsManager extends BaseChildManager< } /** Create a new Guild Channel */ - async create(options: CreateChannelOptions): Promise { + async create(options: CreateChannelOptions): Promise { if (options.name === undefined) throw new Error('name is required for GuildChannelsManager#create') const res = ((await this.client.rest.post(GUILD_CHANNELS(this.guild.id)), @@ -97,6 +87,6 @@ export class GuildChannelsManager extends BaseChildManager< await this.set(res.id, res) const channel = await this.get(res.id) - return (channel as unknown) as GuildChannel + return (channel as unknown) as GuildChannels } } diff --git a/src/managers/guilds.ts b/src/managers/guilds.ts index b90f247..1fcdf37 100644 --- a/src/managers/guilds.ts +++ b/src/managers/guilds.ts @@ -1,9 +1,32 @@ import { Client } from '../models/client.ts' import { Guild } from '../structures/guild.ts' -import { GUILD } from '../types/endpoint.ts' -import { GuildPayload, MemberPayload } from '../types/guild.ts' +import { Role } from '../structures/role.ts' +import { GUILD, GUILDS } from '../types/endpoint.ts' +import { + GuildChannels, + GuildPayload, + MemberPayload, + GuildCreateRolePayload, + GuildCreatePayload, + Verification, + GuildCreateChannelOptions, + GuildCreateChannelPayload +} from '../types/guild.ts' import { BaseManager } from './base.ts' import { MembersManager } from './members.ts' +import { fetchAuto } from '../../deps.ts' + +export interface GuildCreateOptions { + name: string + region?: string + icon?: string + verificationLevel?: Verification + roles?: Array + channels?: Array + afkChannelID?: string + afkTimeout?: number + systemChannelID?: string +} export class GuildManager extends BaseManager { constructor(client: Client) { @@ -32,4 +55,58 @@ export class GuildManager extends BaseManager { .catch((e) => reject(e)) }) } + + async create(options: GuildCreateOptions): Promise { + if (options.icon !== undefined && !options.icon.startsWith('data:')) { + options.icon = await fetchAuto(options.icon) + } + + const body: GuildCreatePayload = { + name: options.name, + region: options.region, + icon: options.icon, + verification_level: options.verificationLevel, + roles: + options.roles !== undefined + ? options.roles.map((obj) => { + let result: GuildCreateRolePayload + if (obj instanceof Role) { + result = { + id: obj.id, + name: obj.name, + color: obj.color, + hoist: obj.hoist, + position: obj.position, + permissions: obj.permissions.bitfield.toString(), + managed: obj.managed, + mentionable: obj.mentionable + } + } else { + result = obj + } + + return result + }) + : undefined, + channels: + options.channels !== undefined + ? options.channels.map( + (obj): GuildCreateChannelPayload => ({ + id: obj.id, + name: obj.name, + type: obj.type, + parent_id: obj.parentID + }) + ) + : undefined, + afk_channel_id: options.afkChannelID, + afk_timeout: options.afkTimeout, + system_channel_id: options.systemChannelID + } + + const result: GuildPayload = await this.client.rest.post(GUILDS(), body) + const guild = new Guild(this.client, result) + + return guild + } } diff --git a/src/structures/guild.ts b/src/structures/guild.ts index 4fe7f9c..26fbf90 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -5,14 +5,15 @@ import { GuildIntegrationPayload, GuildPayload, IntegrationAccountPayload, - IntegrationExpireBehavior + IntegrationExpireBehavior, + Verification, + GuildChannels } from '../types/guild.ts' import { Base } from './base.ts' import { CreateGuildRoleOptions, RolesManager } from '../managers/roles.ts' import { InviteManager } from '../managers/invites.ts' import { CreateChannelOptions, - GuildChannel, GuildChannelsManager } from '../managers/guildChannels.ts' import { MembersManager } from '../managers/members.ts' @@ -131,9 +132,9 @@ export class Guild extends Base { afkTimeout?: number widgetEnabled?: boolean widgetChannelID?: string - verificationLevel?: string - defaultMessageNotifications?: string - explicitContentFilter?: string + verificationLevel?: Verification + defaultMessageNotifications?: number + explicitContentFilter?: number roles: RolesManager emojis: GuildEmojisManager invites: InviteManager @@ -264,7 +265,7 @@ export class Guild extends Base { } /** Create a new Guild Channel */ - async createChannel(options: CreateChannelOptions): Promise { + async createChannel(options: CreateChannelOptions): Promise { return this.channels.create(options) } @@ -292,14 +293,14 @@ export class Guild extends Base { const listener = (guild: Guild): void => { if (guild.id === this.id) { chunked = true - this.client.removeListener('guildMembersChunked', listener) + this.client.off('guildMembersChunked', listener) resolve(this) } } this.client.on('guildMembersChunked', listener) setTimeout(() => { if (!chunked) { - this.client.removeListener('guildMembersChunked', listener) + this.client.off('guildMembersChunked', listener) } }, timeout) } @@ -312,19 +313,19 @@ export class Guild extends Base { */ async awaitAvailability(timeout: number = 1000): Promise { return await new Promise((resolve, reject) => { - if(!this.unavailable) resolve(this); + if (!this.unavailable) resolve(this) const listener = (guild: Guild): void => { if (guild.id === this.id) { - this.client.removeListener('guildLoaded', listener); - resolve(this); + this.client.removeListener('guildLoaded', listener) + resolve(this) } - }; - this.client.on('guildLoaded', listener); + } + this.client.on('guildLoaded', listener) setTimeout(() => { - this.client.removeListener('guildLoaded', listener); - reject(Error("Timeout. Guild didn't arrive in time.")); - }, timeout); - }); + this.client.removeListener('guildLoaded', listener) + reject(Error("Timeout. Guild didn't arrive in time.")) + }, timeout) + }) } } diff --git a/src/test/cmd.ts b/src/test/cmd.ts index 75050eb..db58ca4 100644 --- a/src/test/cmd.ts +++ b/src/test/cmd.ts @@ -4,7 +4,7 @@ import { Intents, CommandContext, Extension, - GuildChannel + GuildChannels } from '../../mod.ts' import { Invite } from '../structures/invite.ts' import { TOKEN } from './config.ts' @@ -80,7 +80,7 @@ client.on('inviteDeleteUncached', (invite: Invite) => { client.on('commandError', console.error) class ChannelLog extends Extension { - onChannelCreate(ext: Extension, channel: GuildChannel): void { + onChannelCreate(ext: Extension, channel: GuildChannels): void { console.log(`Channel Created: ${channel.name}`) } @@ -111,8 +111,8 @@ client.on('messageDeleteBulk', (channel, messages, uncached) => { client.on('channelUpdate', (before, after) => { console.log( - `Channel Update: ${(before as GuildChannel).name}, ${ - (after as GuildChannel).name + `Channel Update: ${(before as GuildChannels).name}, ${ + (after as GuildChannels).name }` ) }) diff --git a/src/test/index.ts b/src/test/index.ts index 72a4fcf..a710b3d 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -4,7 +4,7 @@ import { Message, Member, Role, - GuildChannel, + GuildChannels, Embed, Guild, EveryChannelTypes, @@ -87,7 +87,7 @@ client.on('messageCreate', async (msg: Message) => { } else if (msg.content === '!channels') { const col = await msg.guild?.channels.array() const data = col - ?.map((c: GuildChannel, i: number) => { + ?.map((c: GuildChannels, i: number) => { return `${i + 1}. ${c.name}` }) .join('\n') as string diff --git a/src/types/guild.ts b/src/types/guild.ts index 6825398..3787ab2 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -1,5 +1,14 @@ +import { CategoryChannel } from '../structures/guildCategoryChannel.ts' +import { VoiceChannel } from '../structures/guildVoiceChannel.ts' +import { GuildTextChannel } from '../structures/textChannel.ts' import { ApplicationPayload } from './application.ts' -import { ChannelPayload } from './channel.ts' +import { + ChannelPayload, + ChannelTypes, + GuildCategoryChannelPayload, + GuildTextChannelPayload, + GuildVoiceChannelPayload +} from './channel.ts' import { EmojiPayload } from './emoji.ts' import { PresenceUpdatePayload } from './gateway.ts' import { RolePayload } from './role.ts' @@ -21,9 +30,9 @@ export interface GuildPayload { afk_timeout: number widget_enabled?: boolean widget_channel_id?: string - verification_level: string - default_message_notifications: string - explicit_content_filter: string + verification_level: Verification + default_message_notifications: number + explicit_content_filter: number roles: RolePayload[] emojis: EmojiPayload[] features: GuildFeatures[] @@ -150,3 +159,48 @@ export interface GuildBanPayload { reason: string | null user: UserPayload } + +export type GuildChannelPayloads = + | GuildTextChannelPayload + | GuildVoiceChannelPayload + | GuildCategoryChannelPayload +export type GuildChannels = GuildTextChannel | VoiceChannel | CategoryChannel + +export interface GuildCreatePayload { + name: string + region?: string + icon?: string + verification_level?: number + default_message_notifications?: number + explicit_content_filter?: number + roles?: GuildCreateRolePayload[] + channels?: GuildCreateChannelPayload[] + afk_channel_id?: string + afk_timeout?: number + system_channel_id?: string +} + +export interface GuildCreateRolePayload { + id?: string + name: string + color?: number + hoist?: boolean + position?: number + permissions?: string + managed?: boolean + mentionable?: boolean +} + +export interface GuildCreateChannelPayload { + id?: string + name: string + type: ChannelTypes + parent_id?: string +} + +export interface GuildCreateChannelOptions { + id?: string + name: string + type: ChannelTypes + parentID?: string +}