Skip to content

Commit

Permalink
feat(discord): add $setGroupName, $setGroupCard (#283)
Browse files Browse the repository at this point in the history
Co-authored-by: Shigma <1700011071@pku.edu.cn>
  • Loading branch information
dragon-fish and shigma authored Jun 21, 2021
1 parent 0426a86 commit 83cfa2a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
46 changes: 27 additions & 19 deletions packages/adapter-discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class DiscordBot extends Bot<'discord'> {
return session.messageId
}

async deleteMessage(channelId: string, messageId: string) {
deleteMessage(channelId: string, messageId: string) {
return this.request('DELETE', `/channels/${channelId}/messages/${messageId}`)
}

Expand All @@ -176,7 +176,7 @@ export class DiscordBot extends Bot<'discord'> {
})
}

async $getMessage(channelId: string, messageId: string) {
$getMessage(channelId: string, messageId: string) {
return this.request<DC.Message>('GET', `/channels/${channelId}/messages/${messageId}`)
}

Expand Down Expand Up @@ -226,7 +226,7 @@ export class DiscordBot extends Bot<'discord'> {
return await this.sendFullMessage(`/webhooks/${id}/${token}?wait=${wait}`, data.content, data)
}

async $getGuildMember(guildId: string, userId: string) {
$getGuildMember(guildId: string, userId: string) {
return this.request<DC.GuildMember>('GET', `/guilds/${guildId}/members/${userId}`)
}

Expand All @@ -238,15 +238,15 @@ export class DiscordBot extends Bot<'discord'> {
}
}

async $getGuildRoles(guildId: string) {
$getGuildRoles(guildId: string) {
return this.request<DC.Role[]>('GET', `/guilds/${guildId}/roles`)
}

async $getChannel(channelId: string) {
$getChannel(channelId: string) {
return this.request<DC.Channel>('GET', `/channels/${channelId}`)
}

async $listGuildMembers(guildId: string, limit?: number, after?: string) {
$listGuildMembers(guildId: string, limit?: number, after?: string) {
return this.request<DC.GuildMember[]>('GET', `/guilds/${guildId}/members?limit=${limit || 1000}&after=${after || '0'}`)
}

Expand All @@ -265,58 +265,66 @@ export class DiscordBot extends Bot<'discord'> {
return members.filter(v => v.roles.includes(roleId))
}

async $modifyGuildMember(guildId: string, userId: string, data: Partial<DC.ModifyGuildMember>) {
$modifyGuildMember(guildId: string, userId: string, data: Partial<DC.ModifyGuildMember>) {
return this.request('PATCH', `/guilds/${guildId}/members/${userId}`, data)
}

async $addGuildMemberRole(guildId: string, userId: string, roleId: string) {
$setGroupCard(guildId: string, userId: string, nick: string) {
return this.$modifyGuildMember(guildId, userId, { nick })
}

$addGuildMemberRole(guildId: string, userId: string, roleId: string) {
return this.request('PUT', `/guilds/${guildId}/members/${userId}/roles/${roleId}`)
}

async $removeGuildMemberRole(guildId: string, userId: string, roleId: string) {
$removeGuildMemberRole(guildId: string, userId: string, roleId: string) {
return this.request('DELETE', `/guilds/${guildId}/members/${userId}/roles/${roleId}`)
}

async $createGuildRole(guildId: string, data: DC.GuildRoleBody) {
$createGuildRole(guildId: string, data: DC.GuildRoleBody) {
return this.request('POST', `/guilds/${guildId}/roles`, data)
}

async $modifyGuildRole(guildId: string, roleId: string, data: Partial<DC.GuildRoleBody>) {
$modifyGuildRole(guildId: string, roleId: string, data: Partial<DC.GuildRoleBody>) {
return this.request('PATCH', `/guilds/${guildId}/roles/${roleId}`, data)
}

async $modifyGuild(guildId: string, data: DC.GuildBody) {
$modifyGuild(guildId: string, data: DC.GuildModify) {
return this.request('PATCH', `/guilds/${guildId}`, data)
}

async $createWebhook(channelId: string, data: {
$setGroupName(guildId: string, name: string) {
return this.$modifyGuild(guildId, { name })
}

$createWebhook(channelId: string, data: {
name: string;
avatar?: string
}) {
return this.request('POST', `/channels/${channelId}/webhooks`, data)
}

async $modifyWebhook(webhookId: string, data: {
$modifyWebhook(webhookId: string, data: {
name?: string;
avatar?: string
channel_id?: string
}) {
return this.request('PATCH', `/webhooks/${webhookId}`, data)
}

async $getChannelWebhooks(channelId: string) {
$getChannelWebhooks(channelId: string) {
return this.request<DC.Webhook[]>('GET', `/channels/${channelId}/webhooks`)
}

async $getGuildWebhooks(guildId: string) {
$getGuildWebhooks(guildId: string) {
return this.request<DC.Webhook[]>('GET', `/guilds/${guildId}/webhooks`)
}

async $modifyChannel(channelId, data: Partial<DC.ModifyGuild>) {
$modifyChannel(channelId: string, data: DC.ModifyChannel) {
return this.request('PATCH', `/channels/${channelId}`, data)
}

async $getGuild(guildId: string) {
$getGuild(guildId: string) {
return this.request<DC.Guild>('GET', `/guilds/${guildId}`)
}

Expand All @@ -334,7 +342,7 @@ export class DiscordBot extends Bot<'discord'> {
return data.map(v => adaptGroup(v))
}

async $getGuildChannels(guildId: string) {
$getGuildChannels(guildId: string) {
return this.request<DC.Channel[]>('GET', `/guilds/${guildId}/channels`)
}

Expand Down
34 changes: 31 additions & 3 deletions packages/adapter-discord/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,19 @@ export interface Channel {
last_pin_timestamp?: ISO8601;
}

export interface ModifyGuild extends Pick<Channel, 'name' | 'type' | 'position' | 'topic' | 'nsfw' | 'rate_limit_per_user' | 'bitrate' | 'user_limit' | 'permission_overwrites' | 'parent_id'> {}
type ChannelModifyProps =
| 'name'
| 'type'
| 'position'
| 'topic'
| 'nsfw'
| 'rate_limit_per_user'
| 'bitrate'
| 'user_limit'
| 'permission_overwrites'
| 'parent_id'

export type ModifyChannel = Partial<Pick<Channel, ChannelModifyProps>>

/** https://discord.com/developers/docs/resources/guild#guild-object-guild-structure */
export interface Guild {
Expand Down Expand Up @@ -133,8 +145,24 @@ export interface Guild {
welcome_screen?: any;
}

export interface GuildBody extends Pick<Guild, 'name' | 'region' | 'verification_level' | 'default_message_notifications' | 'explicit_content_filter' | 'afk_channel_id' | 'afk_timeout' | 'icon' | 'owner_id' | 'splash' | 'banner' | 'system_channel_id' | 'rules_channel_id' | 'public_updates_channel_id' | 'preferred_locale'> {
}
type GuildModifyProps =
| 'name'
| 'region'
| 'verification_level'
| 'default_message_notifications'
| 'explicit_content_filter'
| 'afk_channel_id'
| 'afk_timeout'
| 'icon'
| 'owner_id'
| 'splash'
| 'banner'
| 'system_channel_id'
| 'rules_channel_id'
| 'public_updates_channel_id'
| 'preferred_locale'

export type GuildModify = Partial<Pick<Guild, GuildModifyProps>>

/** https://discord.com/developers/docs/resources/user#user-object-user-structure */
export interface User {
Expand Down

0 comments on commit 83cfa2a

Please sign in to comment.