Add guild create function and change the name of GuildChannel

This commit is contained in:
Helloyunho 2021-01-16 01:15:52 +09:00
parent e3033b4cbf
commit d8e65a4328
7 changed files with 169 additions and 47 deletions

4
mod.ts
View file

@ -28,7 +28,6 @@ export { ChannelsManager } from './src/managers/channels.ts'
export { EmojisManager } from './src/managers/emojis.ts' export { EmojisManager } from './src/managers/emojis.ts'
export { GatewayCache } from './src/managers/gatewayCache.ts' export { GatewayCache } from './src/managers/gatewayCache.ts'
export { GuildChannelsManager } from './src/managers/guildChannels.ts' export { GuildChannelsManager } from './src/managers/guildChannels.ts'
export type { GuildChannel } from './src/managers/guildChannels.ts'
export { GuildManager } from './src/managers/guilds.ts' export { GuildManager } from './src/managers/guilds.ts'
export * from './src/structures/slash.ts' export * from './src/structures/slash.ts'
export * from './src/types/slash.ts' export * from './src/types/slash.ts'
@ -102,7 +101,8 @@ export type {
GuildBanPayload, GuildBanPayload,
GuildFeatures, GuildFeatures,
GuildIntegrationPayload, GuildIntegrationPayload,
GuildPayload GuildPayload,
GuildChannels
} from './src/types/guild.ts' } from './src/types/guild.ts'
export type { InvitePayload, PartialInvitePayload } from './src/types/invite.ts' export type { InvitePayload, PartialInvitePayload } from './src/types/invite.ts'
export { PermissionFlags } from './src/types/permissionFlags.ts' export { PermissionFlags } from './src/types/permissionFlags.ts'

View file

@ -2,26 +2,16 @@ import { Client } from '../models/client.ts'
import { Channel } from '../structures/channel.ts' import { Channel } from '../structures/channel.ts'
import { Guild } from '../structures/guild.ts' import { Guild } from '../structures/guild.ts'
import { CategoryChannel } from '../structures/guildCategoryChannel.ts' import { CategoryChannel } from '../structures/guildCategoryChannel.ts'
import { GuildTextChannel } from '../structures/textChannel.ts'
import { VoiceChannel } from '../structures/guildVoiceChannel.ts'
import { import {
ChannelTypes, ChannelTypes,
GuildCategoryChannelPayload,
GuildChannelPayload, GuildChannelPayload,
GuildTextChannelPayload,
GuildVoiceChannelPayload,
Overwrite Overwrite
} from '../types/channel.ts' } from '../types/channel.ts'
import { GuildChannels, GuildChannelPayloads } from '../types/guild.ts'
import { CHANNEL, GUILD_CHANNELS } from '../types/endpoint.ts' import { CHANNEL, GUILD_CHANNELS } from '../types/endpoint.ts'
import { BaseChildManager } from './baseChild.ts' import { BaseChildManager } from './baseChild.ts'
import { ChannelsManager } from './channels.ts' import { ChannelsManager } from './channels.ts'
export type GuildChannelPayloads =
| GuildTextChannelPayload
| GuildVoiceChannelPayload
| GuildCategoryChannelPayload
export type GuildChannel = GuildTextChannel | VoiceChannel | CategoryChannel
export interface CreateChannelOptions { export interface CreateChannelOptions {
name: string name: string
type?: ChannelTypes type?: ChannelTypes
@ -37,7 +27,7 @@ export interface CreateChannelOptions {
export class GuildChannelsManager extends BaseChildManager< export class GuildChannelsManager extends BaseChildManager<
GuildChannelPayloads, GuildChannelPayloads,
GuildChannel GuildChannels
> { > {
guild: Guild guild: Guild
@ -46,7 +36,7 @@ export class GuildChannelsManager extends BaseChildManager<
this.guild = guild this.guild = guild
} }
async get(id: string): Promise<GuildChannel | undefined> { async get(id: string): Promise<GuildChannels | undefined> {
const res = await this.parent.get(id) const res = await this.parent.get(id)
if (res !== undefined && res.guild.id === this.guild.id) return res if (res !== undefined && res.guild.id === this.guild.id) return res
else return undefined else return undefined
@ -57,7 +47,7 @@ export class GuildChannelsManager extends BaseChildManager<
return this.client.rest.delete(CHANNEL(id)) return this.client.rest.delete(CHANNEL(id))
} }
async array(): Promise<GuildChannel[]> { async array(): Promise<GuildChannels[]> {
const arr = (await this.parent.array()) as Channel[] const arr = (await this.parent.array()) as Channel[]
return arr.filter( return arr.filter(
(c: any) => c.guild !== undefined && c.guild.id === this.guild.id (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 */ /** Create a new Guild Channel */
async create(options: CreateChannelOptions): Promise<GuildChannel> { async create(options: CreateChannelOptions): Promise<GuildChannels> {
if (options.name === undefined) if (options.name === undefined)
throw new Error('name is required for GuildChannelsManager#create') throw new Error('name is required for GuildChannelsManager#create')
const res = ((await this.client.rest.post(GUILD_CHANNELS(this.guild.id)), 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) await this.set(res.id, res)
const channel = await this.get(res.id) const channel = await this.get(res.id)
return (channel as unknown) as GuildChannel return (channel as unknown) as GuildChannels
} }
} }

View file

@ -1,9 +1,32 @@
import { Client } from '../models/client.ts' import { Client } from '../models/client.ts'
import { Guild } from '../structures/guild.ts' import { Guild } from '../structures/guild.ts'
import { GUILD } from '../types/endpoint.ts' import { Role } from '../structures/role.ts'
import { GuildPayload, MemberPayload } from '../types/guild.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 { BaseManager } from './base.ts'
import { MembersManager } from './members.ts' import { MembersManager } from './members.ts'
import { fetchAuto } from '../../deps.ts'
export interface GuildCreateOptions {
name: string
region?: string
icon?: string
verificationLevel?: Verification
roles?: Array<Role | GuildCreateRolePayload>
channels?: Array<GuildChannels | GuildCreateChannelOptions>
afkChannelID?: string
afkTimeout?: number
systemChannelID?: string
}
export class GuildManager extends BaseManager<GuildPayload, Guild> { export class GuildManager extends BaseManager<GuildPayload, Guild> {
constructor(client: Client) { constructor(client: Client) {
@ -32,4 +55,58 @@ export class GuildManager extends BaseManager<GuildPayload, Guild> {
.catch((e) => reject(e)) .catch((e) => reject(e))
}) })
} }
async create(options: GuildCreateOptions): Promise<Guild> {
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
}
} }

View file

@ -5,14 +5,15 @@ import {
GuildIntegrationPayload, GuildIntegrationPayload,
GuildPayload, GuildPayload,
IntegrationAccountPayload, IntegrationAccountPayload,
IntegrationExpireBehavior IntegrationExpireBehavior,
Verification,
GuildChannels
} from '../types/guild.ts' } from '../types/guild.ts'
import { Base } from './base.ts' import { Base } from './base.ts'
import { CreateGuildRoleOptions, RolesManager } from '../managers/roles.ts' import { CreateGuildRoleOptions, RolesManager } from '../managers/roles.ts'
import { InviteManager } from '../managers/invites.ts' import { InviteManager } from '../managers/invites.ts'
import { import {
CreateChannelOptions, CreateChannelOptions,
GuildChannel,
GuildChannelsManager GuildChannelsManager
} from '../managers/guildChannels.ts' } from '../managers/guildChannels.ts'
import { MembersManager } from '../managers/members.ts' import { MembersManager } from '../managers/members.ts'
@ -131,9 +132,9 @@ export class Guild extends Base {
afkTimeout?: number afkTimeout?: number
widgetEnabled?: boolean widgetEnabled?: boolean
widgetChannelID?: string widgetChannelID?: string
verificationLevel?: string verificationLevel?: Verification
defaultMessageNotifications?: string defaultMessageNotifications?: number
explicitContentFilter?: string explicitContentFilter?: number
roles: RolesManager roles: RolesManager
emojis: GuildEmojisManager emojis: GuildEmojisManager
invites: InviteManager invites: InviteManager
@ -264,7 +265,7 @@ export class Guild extends Base {
} }
/** Create a new Guild Channel */ /** Create a new Guild Channel */
async createChannel(options: CreateChannelOptions): Promise<GuildChannel> { async createChannel(options: CreateChannelOptions): Promise<GuildChannels> {
return this.channels.create(options) return this.channels.create(options)
} }
@ -292,14 +293,14 @@ export class Guild extends Base {
const listener = (guild: Guild): void => { const listener = (guild: Guild): void => {
if (guild.id === this.id) { if (guild.id === this.id) {
chunked = true chunked = true
this.client.removeListener('guildMembersChunked', listener) this.client.off('guildMembersChunked', listener)
resolve(this) resolve(this)
} }
} }
this.client.on('guildMembersChunked', listener) this.client.on('guildMembersChunked', listener)
setTimeout(() => { setTimeout(() => {
if (!chunked) { if (!chunked) {
this.client.removeListener('guildMembersChunked', listener) this.client.off('guildMembersChunked', listener)
} }
}, timeout) }, timeout)
} }
@ -312,19 +313,19 @@ export class Guild extends Base {
*/ */
async awaitAvailability(timeout: number = 1000): Promise<Guild> { async awaitAvailability(timeout: number = 1000): Promise<Guild> {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
if(!this.unavailable) resolve(this); if (!this.unavailable) resolve(this)
const listener = (guild: Guild): void => { const listener = (guild: Guild): void => {
if (guild.id === this.id) { if (guild.id === this.id) {
this.client.removeListener('guildLoaded', listener); this.client.removeListener('guildLoaded', listener)
resolve(this); resolve(this)
} }
}; }
this.client.on('guildLoaded', listener); this.client.on('guildLoaded', listener)
setTimeout(() => { setTimeout(() => {
this.client.removeListener('guildLoaded', listener); this.client.removeListener('guildLoaded', listener)
reject(Error("Timeout. Guild didn't arrive in time.")); reject(Error("Timeout. Guild didn't arrive in time."))
}, timeout); }, timeout)
}); })
} }
} }

View file

@ -4,7 +4,7 @@ import {
Intents, Intents,
CommandContext, CommandContext,
Extension, Extension,
GuildChannel GuildChannels
} from '../../mod.ts' } from '../../mod.ts'
import { Invite } from '../structures/invite.ts' import { Invite } from '../structures/invite.ts'
import { TOKEN } from './config.ts' import { TOKEN } from './config.ts'
@ -80,7 +80,7 @@ client.on('inviteDeleteUncached', (invite: Invite) => {
client.on('commandError', console.error) client.on('commandError', console.error)
class ChannelLog extends Extension { class ChannelLog extends Extension {
onChannelCreate(ext: Extension, channel: GuildChannel): void { onChannelCreate(ext: Extension, channel: GuildChannels): void {
console.log(`Channel Created: ${channel.name}`) console.log(`Channel Created: ${channel.name}`)
} }
@ -111,8 +111,8 @@ client.on('messageDeleteBulk', (channel, messages, uncached) => {
client.on('channelUpdate', (before, after) => { client.on('channelUpdate', (before, after) => {
console.log( console.log(
`Channel Update: ${(before as GuildChannel).name}, ${ `Channel Update: ${(before as GuildChannels).name}, ${
(after as GuildChannel).name (after as GuildChannels).name
}` }`
) )
}) })

View file

@ -4,7 +4,7 @@ import {
Message, Message,
Member, Member,
Role, Role,
GuildChannel, GuildChannels,
Embed, Embed,
Guild, Guild,
EveryChannelTypes, EveryChannelTypes,
@ -87,7 +87,7 @@ client.on('messageCreate', async (msg: Message) => {
} else if (msg.content === '!channels') { } else if (msg.content === '!channels') {
const col = await msg.guild?.channels.array() const col = await msg.guild?.channels.array()
const data = col const data = col
?.map((c: GuildChannel, i: number) => { ?.map((c: GuildChannels, i: number) => {
return `${i + 1}. ${c.name}` return `${i + 1}. ${c.name}`
}) })
.join('\n') as string .join('\n') as string

View file

@ -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 { ApplicationPayload } from './application.ts'
import { ChannelPayload } from './channel.ts' import {
ChannelPayload,
ChannelTypes,
GuildCategoryChannelPayload,
GuildTextChannelPayload,
GuildVoiceChannelPayload
} from './channel.ts'
import { EmojiPayload } from './emoji.ts' import { EmojiPayload } from './emoji.ts'
import { PresenceUpdatePayload } from './gateway.ts' import { PresenceUpdatePayload } from './gateway.ts'
import { RolePayload } from './role.ts' import { RolePayload } from './role.ts'
@ -21,9 +30,9 @@ export interface GuildPayload {
afk_timeout: number afk_timeout: number
widget_enabled?: boolean widget_enabled?: boolean
widget_channel_id?: string widget_channel_id?: string
verification_level: string verification_level: Verification
default_message_notifications: string default_message_notifications: number
explicit_content_filter: string explicit_content_filter: number
roles: RolePayload[] roles: RolePayload[]
emojis: EmojiPayload[] emojis: EmojiPayload[]
features: GuildFeatures[] features: GuildFeatures[]
@ -150,3 +159,48 @@ export interface GuildBanPayload {
reason: string | null reason: string | null
user: UserPayload 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
}