2021-04-04 09:29:56 +00:00
|
|
|
import type { Client } from '../client/mod.ts'
|
2020-11-08 07:57:24 +00:00
|
|
|
import { Channel } from '../structures/channel.ts'
|
|
|
|
import { Guild } from '../structures/guild.ts'
|
2021-04-04 09:29:56 +00:00
|
|
|
import type { CategoryChannel } from '../structures/guildCategoryChannel.ts'
|
2020-11-08 08:31:00 +00:00
|
|
|
import {
|
2021-01-01 05:55:23 +00:00
|
|
|
ChannelTypes,
|
|
|
|
GuildChannelPayload,
|
2021-03-19 15:39:14 +00:00
|
|
|
OverwritePayload
|
2020-11-08 08:31:00 +00:00
|
|
|
} from '../types/channel.ts'
|
2021-04-04 09:29:56 +00:00
|
|
|
import type { GuildChannels, GuildChannelPayloads } from '../types/guild.ts'
|
2021-01-01 05:55:23 +00:00
|
|
|
import { CHANNEL, GUILD_CHANNELS } from '../types/endpoint.ts'
|
2020-11-08 07:57:24 +00:00
|
|
|
import { BaseChildManager } from './baseChild.ts'
|
2021-04-04 09:29:56 +00:00
|
|
|
import type { ChannelsManager } from './channels.ts'
|
2020-11-01 13:42:00 +00:00
|
|
|
|
2021-01-01 05:55:23 +00:00
|
|
|
export interface CreateChannelOptions {
|
|
|
|
name: string
|
|
|
|
type?: ChannelTypes
|
|
|
|
topic?: string
|
|
|
|
bitrate?: number
|
|
|
|
userLimit?: number
|
|
|
|
rateLimitPerUser?: number
|
|
|
|
position?: number
|
2021-03-19 15:39:14 +00:00
|
|
|
permissionOverwrites?: OverwritePayload[]
|
2021-01-01 05:55:23 +00:00
|
|
|
parent?: CategoryChannel | string
|
|
|
|
nsfw?: boolean
|
|
|
|
}
|
|
|
|
|
2020-11-08 07:58:24 +00:00
|
|
|
export class GuildChannelsManager extends BaseChildManager<
|
|
|
|
GuildChannelPayloads,
|
2021-01-15 16:15:52 +00:00
|
|
|
GuildChannels
|
2020-11-08 07:58:24 +00:00
|
|
|
> {
|
2020-11-01 13:42:00 +00:00
|
|
|
guild: Guild
|
|
|
|
|
2020-12-02 12:29:52 +00:00
|
|
|
constructor(client: Client, parent: ChannelsManager, guild: Guild) {
|
2020-11-02 12:08:38 +00:00
|
|
|
super(client, parent as any)
|
2020-11-01 13:42:00 +00:00
|
|
|
this.guild = guild
|
|
|
|
}
|
|
|
|
|
2021-01-15 16:15:52 +00:00
|
|
|
async get(id: string): Promise<GuildChannels | undefined> {
|
2020-11-01 13:42:00 +00:00
|
|
|
const res = await this.parent.get(id)
|
2020-11-03 15:19:20 +00:00
|
|
|
if (res !== undefined && res.guild.id === this.guild.id) return res
|
2020-11-03 09:21:29 +00:00
|
|
|
else return undefined
|
2020-11-01 13:42:00 +00:00
|
|
|
}
|
2020-12-02 12:29:52 +00:00
|
|
|
|
2020-12-28 12:35:08 +00:00
|
|
|
/** Delete a Guild Channel */
|
2020-11-03 09:21:29 +00:00
|
|
|
async delete(id: string): Promise<boolean> {
|
2020-11-01 13:42:00 +00:00
|
|
|
return this.client.rest.delete(CHANNEL(id))
|
|
|
|
}
|
2020-11-02 12:08:38 +00:00
|
|
|
|
2021-01-15 16:15:52 +00:00
|
|
|
async array(): Promise<GuildChannels[]> {
|
2020-11-08 07:58:24 +00:00
|
|
|
const arr = (await this.parent.array()) as Channel[]
|
|
|
|
return arr.filter(
|
|
|
|
(c: any) => c.guild !== undefined && c.guild.id === this.guild.id
|
|
|
|
) as any
|
2020-11-02 12:08:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 12:29:52 +00:00
|
|
|
async flush(): Promise<boolean> {
|
2020-11-03 09:21:29 +00:00
|
|
|
const arr = await this.array()
|
|
|
|
for (const elem of arr) {
|
2021-01-16 14:22:13 +00:00
|
|
|
this.parent._delete(elem.id)
|
2020-11-02 12:08:38 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2021-01-01 05:55:23 +00:00
|
|
|
|
|
|
|
/** Create a new Guild Channel */
|
2021-01-15 16:15:52 +00:00
|
|
|
async create(options: CreateChannelOptions): Promise<GuildChannels> {
|
2021-01-01 05:55:23 +00:00
|
|
|
if (options.name === undefined)
|
|
|
|
throw new Error('name is required for GuildChannelsManager#create')
|
2021-02-23 03:13:46 +00:00
|
|
|
const res = ((await this.client.rest.post(GUILD_CHANNELS(this.guild.id), {
|
2021-01-01 05:55:23 +00:00
|
|
|
name: options.name,
|
|
|
|
type: options.type,
|
|
|
|
topic: options.topic,
|
|
|
|
bitrate: options.bitrate,
|
|
|
|
user_limit: options.userLimit,
|
|
|
|
rate_limit_per_user: options.rateLimitPerUser,
|
|
|
|
position: options.position,
|
|
|
|
permission_overwrites: options.permissionOverwrites,
|
|
|
|
parent_id:
|
|
|
|
options.parent === undefined
|
|
|
|
? undefined
|
|
|
|
: typeof options.parent === 'object'
|
|
|
|
? options.parent.id
|
|
|
|
: options.parent,
|
|
|
|
nsfw: options.nsfw
|
2021-02-23 03:13:46 +00:00
|
|
|
})) as unknown) as GuildChannelPayload
|
2021-01-01 05:55:23 +00:00
|
|
|
|
|
|
|
await this.set(res.id, res)
|
|
|
|
const channel = await this.get(res.id)
|
2021-01-15 16:15:52 +00:00
|
|
|
return (channel as unknown) as GuildChannels
|
2021-01-01 05:55:23 +00:00
|
|
|
}
|
2021-01-21 12:39:51 +00:00
|
|
|
|
|
|
|
/** Modify the positions of a set of channel positions for the guild. */
|
|
|
|
async editPositions(
|
2021-01-21 13:17:03 +00:00
|
|
|
...positions: Array<{ id: string | GuildChannels; position: number | null }>
|
2021-01-21 12:39:51 +00:00
|
|
|
): Promise<GuildChannelsManager> {
|
|
|
|
if (positions.length === 0)
|
|
|
|
throw new Error('No channel positions to change specified')
|
|
|
|
|
|
|
|
await this.client.rest.api.guilds[this.guild.id].channels.patch(
|
|
|
|
positions.map((e) => ({
|
|
|
|
id: typeof e.id === 'string' ? e.id : e.id.id,
|
|
|
|
position: e.position ?? null
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
return this
|
|
|
|
}
|
2020-11-08 07:58:24 +00:00
|
|
|
}
|