Add edit channel feature

This commit is contained in:
Helloyunho 2020-12-20 18:11:37 +09:00
parent dced786e60
commit 9688d248e3
9 changed files with 122 additions and 26 deletions

2
mod.ts
View file

@ -92,7 +92,7 @@ export type {
ChannelPayload,
FollowedChannel,
GuildNewsChannelPayload,
GuildChannelCategoryPayload,
GuildCategoryChannelPayload,
GuildChannelPayload,
GuildTextChannelPayload,
GuildVoiceChannelPayload,

View file

@ -6,7 +6,6 @@ export const guildIntegrationsUpdate: GatewayEventHandler = async (
gateway: Gateway,
d: GuildIntegrationsUpdatePayload
) => {
console.log(d)
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id)
if (guild === undefined) return

View file

@ -5,7 +5,7 @@ import { CategoryChannel } from '../structures/guildCategoryChannel.ts'
import { GuildTextChannel } from '../structures/textChannel.ts'
import { VoiceChannel } from '../structures/guildVoiceChannel.ts'
import {
GuildChannelCategoryPayload,
GuildCategoryChannelPayload,
GuildTextChannelPayload,
GuildVoiceChannelPayload
} from '../types/channel.ts'
@ -16,7 +16,7 @@ import { ChannelsManager } from './channels.ts'
export type GuildChannelPayloads =
| GuildTextChannelPayload
| GuildVoiceChannelPayload
| GuildChannelCategoryPayload
| GuildCategoryChannelPayload
export type GuildChannel = GuildTextChannel | VoiceChannel | CategoryChannel
export class GuildChannelsManager extends BaseChildManager<

View file

@ -1,38 +1,56 @@
import { Client } from '../models/client.ts'
import { Channel } from './channel.ts'
import { GuildChannelCategoryPayload, Overwrite } from '../types/channel.ts'
import {
GuildCategoryChannelPayload,
ModifyGuildCategoryChannelOption,
ModifyGuildCategoryChannelPayload,
Overwrite
} from '../types/channel.ts'
import { Guild } from './guild.ts'
import { CHANNEL } from '../types/endpoint.ts'
export class CategoryChannel extends Channel {
guildID: string
name: string
position: number
permissionOverwrites: Overwrite[]
nsfw: boolean
guild: Guild
parentID?: string
constructor(client: Client, data: GuildChannelCategoryPayload, guild: Guild) {
constructor(client: Client, data: GuildCategoryChannelPayload, guild: Guild) {
super(client, data)
this.guildID = data.guild_id
this.name = data.name
this.guild = guild
this.position = data.position
this.permissionOverwrites = data.permission_overwrites
this.nsfw = data.nsfw
this.parentID = data.parent_id
// TODO: Cache in Gateway Event Code
// cache.set('guildcategorychannel', this.id, this)
}
readFromData(data: GuildChannelCategoryPayload): void {
readFromData(data: GuildCategoryChannelPayload): void {
super.readFromData(data)
this.guildID = data.guild_id ?? this.guildID
this.name = data.name ?? this.name
this.position = data.position ?? this.position
this.permissionOverwrites =
data.permission_overwrites ?? this.permissionOverwrites
this.nsfw = data.nsfw ?? this.nsfw
this.parentID = data.parent_id ?? this.parentID
}
async edit(
options?: ModifyGuildCategoryChannelOption
): Promise<CategoryChannel> {
const body: ModifyGuildCategoryChannelPayload = {
name: options?.name,
position: options?.position,
permission_overwrites: options?.permissionOverwrites,
parent_id: options?.parentID
}
const resp = await this.client.rest.patch(CHANNEL(this.id), body)
return new CategoryChannel(this.client, resp, this.guild)
}
}

View file

@ -1,5 +1,11 @@
import { Client } from '../models/client.ts'
import { GuildNewsChannelPayload, Overwrite } from '../types/channel.ts'
import {
GuildNewsChannelPayload,
ModifyGuildNewsChannelOption,
ModifyGuildNewsChannelPayload,
Overwrite
} from '../types/channel.ts'
import { CHANNEL } from '../types/endpoint.ts'
import { Guild } from './guild.ts'
import { TextChannel } from './textChannel.ts'
@ -36,4 +42,20 @@ export class NewsChannel extends TextChannel {
this.parentID = data.parent_id ?? this.parentID
this.topic = data.topic ?? this.topic
}
async edit(options?: ModifyGuildNewsChannelOption): Promise<NewsChannel> {
const body: ModifyGuildNewsChannelPayload = {
name: options?.name,
position: options?.position,
permission_overwrites: options?.permissionOverwrites,
parent_id: options?.parentID,
type: options?.type,
topic: options?.topic,
nsfw: options?.nsfw
}
const resp = await this.client.rest.patch(CHANNEL(this.id), body)
return new NewsChannel(this.client, resp, this.guild)
}
}

View file

@ -1,7 +1,13 @@
import { VoiceServerUpdateData } from '../gateway/handlers/index.ts'
import { VoiceStateOptions } from '../gateway/index.ts'
import { Client } from '../models/client.ts'
import { GuildVoiceChannelPayload, Overwrite } from '../types/channel.ts'
import {
GuildVoiceChannelPayload,
ModifyVoiceChannelOption,
ModifyVoiceChannelPayload,
Overwrite
} from '../types/channel.ts'
import { CHANNEL } from '../types/endpoint.ts'
import { Channel } from './channel.ts'
import { Guild } from './guild.ts'
import { VoiceState } from './voiceState.ts'
@ -14,7 +20,6 @@ export class VoiceChannel extends Channel {
guild: Guild
position: number
permissionOverwrites: Overwrite[]
nsfw: boolean
parentID?: string
constructor(client: Client, data: GuildVoiceChannelPayload, guild: Guild) {
@ -26,7 +31,6 @@ export class VoiceChannel extends Channel {
this.position = data.position
this.guild = guild
this.permissionOverwrites = data.permission_overwrites
this.nsfw = data.nsfw
this.parentID = data.parent_id
// TODO: Cache in Gateway Event Code
// cache.set('guildvoicechannel', this.id, this)
@ -85,7 +89,21 @@ export class VoiceChannel extends Channel {
this.position = data.position ?? this.position
this.permissionOverwrites =
data.permission_overwrites ?? this.permissionOverwrites
this.nsfw = data.nsfw ?? this.nsfw
this.parentID = data.parent_id ?? this.parentID
}
async edit(options?: ModifyVoiceChannelOption): Promise<VoiceChannel> {
const body: ModifyVoiceChannelPayload = {
name: options?.name,
position: options?.position,
permission_overwrites: options?.permissionOverwrites,
parent_id: options?.parentID,
bitrate: options?.bitrate,
user_limit: options?.userLimit
}
const resp = await this.client.rest.patch(CHANNEL(this.id), body)
return new VoiceChannel(this.client, resp, this.guild)
}
}

View file

@ -4,10 +4,16 @@ import {
GuildTextChannelPayload,
MessageOption,
MessageReference,
ModifyGuildTextChannelOption,
ModifyGuildTextChannelPayload,
Overwrite,
TextChannelPayload
} from '../types/channel.ts'
import { CHANNEL_MESSAGE, CHANNEL_MESSAGES } from '../types/endpoint.ts'
import {
CHANNEL,
CHANNEL_MESSAGE,
CHANNEL_MESSAGES
} from '../types/endpoint.ts'
import { Channel } from './channel.ts'
import { Embed } from './embed.ts'
import { Guild } from './guild.ts'
@ -164,4 +170,19 @@ export class GuildTextChannel extends TextChannel {
this.topic = data.topic ?? this.topic
this.rateLimit = data.rate_limit_per_user ?? this.rateLimit
}
async edit(
options?: ModifyGuildTextChannelOption
): Promise<GuildTextChannel> {
const body: ModifyGuildTextChannelPayload = {
name: options?.name,
position: options?.position,
permission_overwrites: options?.permissionOverwrites,
parent_id: options?.parentID
}
const resp = await this.client.rest.patch(CHANNEL(this.id), body)
return new GuildTextChannel(this.client, resp, this.guild)
}
}

View file

@ -34,14 +34,12 @@ client.on('channelUpdate', (b: EveryChannelTypes, a: EveryChannelTypes) => {
if (b.type === ChannelTypes.GUILD_TEXT) {
const before = (b as unknown) as GuildTextChannel
const after = (a as unknown) as GuildTextChannel
console.log(
before.send('', {
embed: new Embed({
title: 'Channel Update',
description: `Name Before: ${before.name}\nName After: ${after.name}`
})
before.send('', {
embed: new Embed({
title: 'Channel Update',
description: `Name Before: ${before.name}\nName After: ${after.name}`
})
)
})
}
})
@ -94,6 +92,26 @@ client.on('messageCreate', async (msg: Message) => {
})
.join('\n') as string
msg.channel.send('Channels List:\n' + data)
} else if (msg.content === '!messages') {
const col = await msg.channel.messages.array()
const data = col
?.slice(-5)
.map((c: Message, i: number) => {
return `${i + 1}. ${c.content}`
})
.join('\n') as string
msg.channel.send('Top 5 Message List:\n' + data)
} else if (msg.content === '!editChannel') {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const channel = msg.channel as GuildTextChannel
const newChannel = await channel.edit({
name: 'gggg'
})
if (newChannel.name === 'gggg') {
msg.channel.send('Done!')
} else {
msg.channel.send('Failed...')
}
}
})

View file

@ -4,7 +4,7 @@ import {
ChannelTypes,
DMChannelPayload,
GroupDMChannelPayload,
GuildChannelCategoryPayload,
GuildCategoryChannelPayload,
GuildNewsChannelPayload,
GuildTextChannelPayload,
GuildVoiceChannelPayload,
@ -41,7 +41,7 @@ export type EveryChannelTypes =
export type EveryChannelPayloadTypes =
| ChannelPayload
| GuildChannelCategoryPayload
| GuildCategoryChannelPayload
| GuildVoiceChannelPayload
| EveryTextChannelPayloadTypes
@ -57,7 +57,7 @@ const getChannelByType = (
throw new Error('No Guild was provided to construct Channel')
return new CategoryChannel(
client,
data as GuildChannelCategoryPayload,
data as GuildCategoryChannelPayload,
guild
)
case ChannelTypes.GUILD_NEWS: