Merge pull request #65 from Helloyunho/channel-edit

Add channel edit feature
This commit is contained in:
Aki 2020-12-20 18:20:40 +09:00 committed by GitHub
commit a8fc3ef463
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 180 additions and 28 deletions

2
mod.ts
View file

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

View file

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

View file

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

View file

@ -1,38 +1,56 @@
import { Client } from '../models/client.ts' import { Client } from '../models/client.ts'
import { Channel } from './channel.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 { Guild } from './guild.ts'
import { CHANNEL } from '../types/endpoint.ts'
export class CategoryChannel extends Channel { export class CategoryChannel extends Channel {
guildID: string guildID: string
name: string name: string
position: number position: number
permissionOverwrites: Overwrite[] permissionOverwrites: Overwrite[]
nsfw: boolean
guild: Guild guild: Guild
parentID?: string parentID?: string
constructor(client: Client, data: GuildChannelCategoryPayload, guild: Guild) { constructor(client: Client, data: GuildCategoryChannelPayload, guild: Guild) {
super(client, data) super(client, data)
this.guildID = data.guild_id this.guildID = data.guild_id
this.name = data.name this.name = data.name
this.guild = guild this.guild = guild
this.position = data.position this.position = data.position
this.permissionOverwrites = data.permission_overwrites this.permissionOverwrites = data.permission_overwrites
this.nsfw = data.nsfw
this.parentID = data.parent_id this.parentID = data.parent_id
// TODO: Cache in Gateway Event Code // TODO: Cache in Gateway Event Code
// cache.set('guildcategorychannel', this.id, this) // cache.set('guildcategorychannel', this.id, this)
} }
readFromData(data: GuildChannelCategoryPayload): void { readFromData(data: GuildCategoryChannelPayload): void {
super.readFromData(data) super.readFromData(data)
this.guildID = data.guild_id ?? this.guildID this.guildID = data.guild_id ?? this.guildID
this.name = data.name ?? this.name this.name = data.name ?? this.name
this.position = data.position ?? this.position this.position = data.position ?? this.position
this.permissionOverwrites = this.permissionOverwrites =
data.permission_overwrites ?? this.permissionOverwrites data.permission_overwrites ?? this.permissionOverwrites
this.nsfw = data.nsfw ?? this.nsfw
this.parentID = data.parent_id ?? this.parentID 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 { 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 { Guild } from './guild.ts'
import { TextChannel } from './textChannel.ts' import { TextChannel } from './textChannel.ts'
@ -36,4 +42,20 @@ export class NewsChannel extends TextChannel {
this.parentID = data.parent_id ?? this.parentID this.parentID = data.parent_id ?? this.parentID
this.topic = data.topic ?? this.topic 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 { VoiceServerUpdateData } from '../gateway/handlers/index.ts'
import { VoiceStateOptions } from '../gateway/index.ts' import { VoiceStateOptions } from '../gateway/index.ts'
import { Client } from '../models/client.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 { Channel } from './channel.ts'
import { Guild } from './guild.ts' import { Guild } from './guild.ts'
import { VoiceState } from './voiceState.ts' import { VoiceState } from './voiceState.ts'
@ -14,7 +20,6 @@ export class VoiceChannel extends Channel {
guild: Guild guild: Guild
position: number position: number
permissionOverwrites: Overwrite[] permissionOverwrites: Overwrite[]
nsfw: boolean
parentID?: string parentID?: string
constructor(client: Client, data: GuildVoiceChannelPayload, guild: Guild) { constructor(client: Client, data: GuildVoiceChannelPayload, guild: Guild) {
@ -26,7 +31,6 @@ export class VoiceChannel extends Channel {
this.position = data.position this.position = data.position
this.guild = guild this.guild = guild
this.permissionOverwrites = data.permission_overwrites this.permissionOverwrites = data.permission_overwrites
this.nsfw = data.nsfw
this.parentID = data.parent_id this.parentID = data.parent_id
// TODO: Cache in Gateway Event Code // TODO: Cache in Gateway Event Code
// cache.set('guildvoicechannel', this.id, this) // cache.set('guildvoicechannel', this.id, this)
@ -85,7 +89,21 @@ export class VoiceChannel extends Channel {
this.position = data.position ?? this.position this.position = data.position ?? this.position
this.permissionOverwrites = this.permissionOverwrites =
data.permission_overwrites ?? this.permissionOverwrites data.permission_overwrites ?? this.permissionOverwrites
this.nsfw = data.nsfw ?? this.nsfw
this.parentID = data.parent_id ?? this.parentID 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, GuildTextChannelPayload,
MessageOption, MessageOption,
MessageReference, MessageReference,
ModifyGuildTextChannelOption,
ModifyGuildTextChannelPayload,
Overwrite, Overwrite,
TextChannelPayload TextChannelPayload
} from '../types/channel.ts' } 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 { Channel } from './channel.ts'
import { Embed } from './embed.ts' import { Embed } from './embed.ts'
import { Guild } from './guild.ts' import { Guild } from './guild.ts'
@ -164,4 +170,19 @@ export class GuildTextChannel extends TextChannel {
this.topic = data.topic ?? this.topic this.topic = data.topic ?? this.topic
this.rateLimit = data.rate_limit_per_user ?? this.rateLimit 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) { if (b.type === ChannelTypes.GUILD_TEXT) {
const before = (b as unknown) as GuildTextChannel const before = (b as unknown) as GuildTextChannel
const after = (a as unknown) as GuildTextChannel const after = (a as unknown) as GuildTextChannel
console.log( before.send('', {
before.send('', { embed: new Embed({
embed: new Embed({ title: 'Channel Update',
title: 'Channel Update', description: `Name Before: ${before.name}\nName After: ${after.name}`
description: `Name Before: ${before.name}\nName After: ${after.name}`
})
}) })
) })
} }
}) })
@ -94,6 +92,26 @@ client.on('messageCreate', async (msg: Message) => {
}) })
.join('\n') as string .join('\n') as string
msg.channel.send('Channels List:\n' + data) 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

@ -18,13 +18,13 @@ export interface GuildChannelPayload extends ChannelPayload {
name: string name: string
position: number position: number
permission_overwrites: Overwrite[] permission_overwrites: Overwrite[]
nsfw: boolean
parent_id?: string parent_id?: string
} }
export interface GuildTextChannelPayload export interface GuildTextChannelPayload
extends TextChannelPayload, extends TextChannelPayload,
GuildChannelPayload { GuildChannelPayload {
nsfw: boolean
rate_limit_per_user: number rate_limit_per_user: number
topic?: string topic?: string
} }
@ -33,6 +33,7 @@ export interface GuildNewsChannelPayload
extends TextChannelPayload, extends TextChannelPayload,
GuildChannelPayload { GuildChannelPayload {
topic?: string topic?: string
nsfw: boolean
} }
export interface GuildVoiceChannelPayload extends GuildChannelPayload { export interface GuildVoiceChannelPayload extends GuildChannelPayload {
@ -50,10 +51,65 @@ export interface GroupDMChannelPayload extends DMChannelPayload {
owner_id: string owner_id: string
} }
export interface GuildChannelCategoryPayload export interface GuildCategoryChannelPayload
extends ChannelPayload, extends ChannelPayload,
GuildChannelPayload {} GuildChannelPayload {}
export interface ModifyChannelPayload {
name?: string
position?: number | null
permission_overwrites?: Overwrite[] | null
parent_id?: string
}
export interface ModifyGuildCategoryChannelPayload
extends ModifyChannelPayload {}
export interface ModifyGuildTextChannelPayload extends ModifyChannelPayload {
type?: number
topic?: string | null
nsfw?: boolean | null
rate_limit_per_user?: number | null
}
export interface ModifyGuildNewsChannelPayload extends ModifyChannelPayload {
type?: number
topic?: string | null
nsfw?: boolean | null
}
export interface ModifyVoiceChannelPayload extends ModifyChannelPayload {
bitrate?: number | null
user_limit?: number | null
}
export interface ModifyChannelOption {
name?: string
position?: number | null
permissionOverwrites?: Overwrite[] | null
parentID?: string
}
export interface ModifyGuildCategoryChannelOption extends ModifyChannelOption {}
export interface ModifyGuildTextChannelOption extends ModifyChannelOption {
type?: number
topic?: string | null
nsfw?: boolean | null
rateLimitPerUser?: number | null
}
export interface ModifyGuildNewsChannelOption extends ModifyChannelOption {
type?: number
topic?: string | null
nsfw?: boolean | null
}
export interface ModifyVoiceChannelOption extends ModifyChannelOption {
bitrate?: number | null
userLimit?: number | null
}
export interface Overwrite { export interface Overwrite {
id: string id: string
type: number type: number

View file

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