Add role edit feature, add guild property to role(idk why it wasn't there)

This commit is contained in:
Helloyunho 2021-01-21 01:28:18 +09:00
parent 0cae198f2c
commit 989706e71a
5 changed files with 55 additions and 21 deletions

View file

@ -3,36 +3,22 @@ import { Guild } from '../structures/guild.ts'
import { Role } from '../structures/role.ts' import { Role } from '../structures/role.ts'
import { GUILD, GUILDS, GUILD_PREVIEW } from '../types/endpoint.ts' import { GUILD, GUILDS, GUILD_PREVIEW } from '../types/endpoint.ts'
import { import {
GuildChannels,
GuildPayload, GuildPayload,
MemberPayload, MemberPayload,
GuildCreateRolePayload, GuildCreateRolePayload,
GuildCreatePayload, GuildCreatePayload,
Verification,
GuildCreateChannelOptions,
GuildCreateChannelPayload, GuildCreateChannelPayload,
GuildPreview, GuildPreview,
GuildPreviewPayload, GuildPreviewPayload,
GuildModifyOptions, GuildModifyOptions,
GuildModifyPayload GuildModifyPayload,
GuildCreateOptions
} from '../types/guild.ts' } from '../types/guild.ts'
import { BaseManager } from './base.ts' import { BaseManager } from './base.ts'
import { MembersManager } from './members.ts' import { MembersManager } from './members.ts'
import { fetchAuto } from '../../deps.ts' import { fetchAuto } from '../../deps.ts'
import { Emoji } from '../structures/emoji.ts' import { Emoji } from '../structures/emoji.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> { export class GuildManager extends BaseManager<GuildPayload, Guild> {
constructor(client: Client) { constructor(client: Client) {
super(client, 'guilds', Guild) super(client, 'guilds', Guild)
@ -66,6 +52,7 @@ export class GuildManager extends BaseManager<GuildPayload, Guild> {
* @param options Options for creating a guild * @param options Options for creating a guild
*/ */
async create(options: GuildCreateOptions): Promise<Guild> { async create(options: GuildCreateOptions): Promise<Guild> {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (options.icon !== undefined && !options.icon.startsWith('data:')) { if (options.icon !== undefined && !options.icon.startsWith('data:')) {
options.icon = await fetchAuto(options.icon) options.icon = await fetchAuto(options.icon)
} }
@ -145,7 +132,7 @@ export class GuildManager extends BaseManager<GuildPayload, Guild> {
} }
/** /**
* Edits a guild. * Edits a guild. Returns edited guild.
* @param guild Guild or guild id * @param guild Guild or guild id
* @param options Guild edit options * @param options Guild edit options
* @param asRaw true for get raw data, false for get guild(defaults to false) * @param asRaw true for get raw data, false for get guild(defaults to false)
@ -225,7 +212,7 @@ export class GuildManager extends BaseManager<GuildPayload, Guild> {
} }
/** /**
* Deletes a guild. * Deletes a guild. Returns deleted guild.
* @param guild Guild or guild id * @param guild Guild or guild id
*/ */
async delete(guild: Guild | string): Promise<Guild | undefined> { async delete(guild: Guild | string): Promise<Guild | undefined> {

View file

@ -3,7 +3,7 @@ import { Client } from '../models/client.ts'
import { Guild } from '../structures/guild.ts' import { Guild } from '../structures/guild.ts'
import { Role } from '../structures/role.ts' import { Role } from '../structures/role.ts'
import { GUILD_ROLE, GUILD_ROLES } from '../types/endpoint.ts' import { GUILD_ROLE, GUILD_ROLES } from '../types/endpoint.ts'
import { RolePayload } from '../types/role.ts' import { RoleModifyPayload, RolePayload } from '../types/role.ts'
import { BaseManager } from './base.ts' import { BaseManager } from './base.ts'
export interface CreateGuildRoleOptions { export interface CreateGuildRoleOptions {
@ -35,6 +35,12 @@ export class RolesManager extends BaseManager<RolePayload, Role> {
}) })
} }
async get(key: string): Promise<Role | undefined> {
const raw = await this._get(key)
if (raw === undefined) return
return new Role(this.client, raw, this.guild)
}
async fromPayload(roles: RolePayload[]): Promise<boolean> { async fromPayload(roles: RolePayload[]): Promise<boolean> {
for (const role of roles) { for (const role of roles) {
await this.set(role.id, role) await this.set(role.id, role)
@ -80,4 +86,17 @@ export class RolesManager extends BaseManager<RolePayload, Role> {
) )
return true return true
} }
async edit(role: Role | string, options: RoleModifyPayload): Promise<Role> {
if (role instanceof Role) {
role = role.id
}
const resp = await this.client.rest.patch(
GUILD_ROLE(this.guild.id, role),
options
)
const result = new Role(this.client, resp, this.guild)
return result
}
} }

View file

@ -1,10 +1,12 @@
import { Client } from '../models/client.ts' import { Client } from '../models/client.ts'
import { Base } from './base.ts' import { Base } from './base.ts'
import { RolePayload } from '../types/role.ts' import { RoleModifyPayload, RolePayload } from '../types/role.ts'
import { Permissions } from '../utils/permissions.ts' import { Permissions } from '../utils/permissions.ts'
import { Guild } from './guild.ts'
export class Role extends Base { export class Role extends Base {
id: string id: string
guild: Guild
name: string name: string
color: number color: number
hoist: boolean hoist: boolean
@ -14,9 +16,10 @@ export class Role extends Base {
mentionable: boolean mentionable: boolean
tags?: RoleTags tags?: RoleTags
constructor(client: Client, data: RolePayload) { constructor(client: Client, data: RolePayload, guild: Guild) {
super(client, data) super(client, data)
this.id = data.id this.id = data.id
this.guild = guild
this.name = data.name this.name = data.name
this.color = data.color this.color = data.color
this.hoist = data.hoist this.hoist = data.hoist
@ -46,6 +49,10 @@ export class Role extends Base {
this.managed = data.managed ?? this.managed this.managed = data.managed ?? this.managed
this.mentionable = data.mentionable ?? this.mentionable this.mentionable = data.mentionable ?? this.mentionable
} }
async edit(options: RoleModifyPayload): Promise<Role> {
return this.guild.roles.edit(this, options)
}
} }
export interface RoleTags { export interface RoleTags {

View file

@ -1,6 +1,7 @@
import { Emoji } from '../structures/emoji.ts' import { Emoji } from '../structures/emoji.ts'
import { CategoryChannel } from '../structures/guildCategoryChannel.ts' import { CategoryChannel } from '../structures/guildCategoryChannel.ts'
import { VoiceChannel } from '../structures/guildVoiceChannel.ts' import { VoiceChannel } from '../structures/guildVoiceChannel.ts'
import { Role } from '../structures/role.ts'
import { GuildTextChannel } from '../structures/textChannel.ts' import { GuildTextChannel } from '../structures/textChannel.ts'
import { ApplicationPayload } from './application.ts' import { ApplicationPayload } from './application.ts'
import { import {
@ -206,6 +207,18 @@ export interface GuildCreateChannelOptions {
parentID?: string parentID?: string
} }
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 interface GuildPreviewPayload { export interface GuildPreviewPayload {
id: string id: string
name: string name: string

View file

@ -18,3 +18,11 @@ export interface RoleTagsPayload {
/** The id of the integration this role belongs to */ /** The id of the integration this role belongs to */
integration_id?: string integration_id?: string
} }
export interface RoleModifyPayload {
name?: string | null
permissions?: string | null
color?: number | null
hoist?: boolean | null
mentionable?: boolean | null
}