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 { 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'

View file

@ -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<GuildChannel | undefined> {
async get(id: string): Promise<GuildChannels | undefined> {
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<GuildChannel[]> {
async array(): Promise<GuildChannels[]> {
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<GuildChannel> {
async create(options: CreateChannelOptions): Promise<GuildChannels> {
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
}
}

View file

@ -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<Role | GuildCreateRolePayload>
channels?: Array<GuildChannels | GuildCreateChannelOptions>
afkChannelID?: string
afkTimeout?: number
systemChannelID?: string
}
export class GuildManager extends BaseManager<GuildPayload, Guild> {
constructor(client: Client) {
@ -32,4 +55,58 @@ export class GuildManager extends BaseManager<GuildPayload, Guild> {
.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,
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<GuildChannel> {
async createChannel(options: CreateChannelOptions): Promise<GuildChannels> {
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<Guild> {
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)
})
}
}

View file

@ -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
}`
)
})

View file

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

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 { 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
}