Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/managers/roles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Permissions } from '../../mod.ts'
import { fetchAuto } from '../../deps.ts'
import type { Client } from '../client/mod.ts'
import type { Guild } from '../structures/guild.ts'
import { Role } from '../structures/role.ts'
Expand Down Expand Up @@ -101,12 +102,28 @@ export class RolesManager extends BaseManager<RolePayload, Role> {
}

async edit(role: Role | string, options: RoleModifyPayload): Promise<Role> {
if (
options.icon !== undefined &&
options.icon !== null &&
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
!options.icon.startsWith('data:')
) {
options.icon = await fetchAuto(options.icon)
}
if (role instanceof Role) {
role = role.id
}
const resp: RolePayload = await this.client.rest.patch(
GUILD_ROLE(this.guild.id, role),
options
{
name: options.name,
permissions: options.permissions,
color: options.color,
hoist: options.hoist,
icon: options.icon,
unicode_emoji: options.unicodeEmoji,
mentionable: options.mentionable
}
)

return new Role(this.client, resp, this.guild)
Expand Down
17 changes: 17 additions & 0 deletions src/structures/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { Permissions } from '../utils/permissions.ts'
import type { Guild } from './guild.ts'
import type { Member } from './member.ts'
import { User } from './user.ts'
import { ImageURL } from './cdn.ts'
import { ImageFormats, ImageSize } from '../types/cdn.ts'
import { ROLE_ICON } from '../types/endpoint.ts'

/** Represents a Guild Role */
export class Role extends SnowflakeBase {
Expand All @@ -13,6 +16,8 @@ export class Role extends SnowflakeBase {
name!: string
color!: number
hoist!: boolean
icon?: string
unicodeEmoji?: string
position!: number
permissions!: Permissions
managed!: boolean
Expand All @@ -30,6 +35,8 @@ export class Role extends SnowflakeBase {
this.name = data.name ?? this.name
this.color = data.color ?? this.color
this.hoist = data.hoist ?? this.hoist
this.icon = data.icon ?? this.icon
this.unicodeEmoji = data.unicode_emoji ?? this.unicodeEmoji
this.position = data.position ?? this.position
this.permissions =
data.permissions !== undefined
Expand Down Expand Up @@ -90,6 +97,16 @@ export class Role extends SnowflakeBase {

return member.roles.remove(this.id)
}

/** Get the icon for the role. If set, is either a URL to an icon, or a Unicode emoji. */
roleIcon(
format: ImageFormats = 'png',
size: ImageSize = 512
): string | undefined {
return this.icon !== undefined
? `${ImageURL(ROLE_ICON(this.id, this.icon), format, size)}`
: this.unicodeEmoji
}
}

export interface RoleTags {
Expand Down
3 changes: 3 additions & 0 deletions src/types/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ const ACHIEVEMENT_ICON = (
`${Constants.DISCORD_CDN_URL}/app-assets/${applicationID}/achievements/${achievementID}/icons/${iconHASH}`
const TEAM_ICON = (teamID: string, iconID: string): string =>
`${Constants.DISCORD_CDN_URL}/team-icons/${teamID}/${iconID}`
const ROLE_ICON = (roleID: string, iconID: string): string =>
`${Constants.DISCORD_CDN_URL}/role-icons/${roleID}/${iconID}`

// Emoji Endpoints
const EMOJI = (guildID: string, emojiID: string): string =>
Expand Down Expand Up @@ -291,6 +293,7 @@ export {
APPLICATION_ASSET,
ACHIEVEMENT_ICON,
TEAM_ICON,
ROLE_ICON,
EMOJI,
TEMPLATE,
INVITE,
Expand Down
4 changes: 4 additions & 0 deletions src/types/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export interface RolePayload {
name: string
color: number
hoist: boolean
icon?: string
unicode_emoji?: string
position: number
permissions: string
managed: boolean
Expand All @@ -24,5 +26,7 @@ export interface RoleModifyPayload {
permissions?: string | null
color?: number | null
hoist?: boolean | null
icon?: string | null
unicodeEmoji?: string | null
mentionable?: boolean | null
}
18 changes: 18 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,24 @@ client.on('messageCreate', async (msg: Message) => {
} else {
msg.channel.send(msg.author.avatarURL())
}
} else if (msg.content.startsWith('!roleIcon') === true) {
const size = msg.mentions.roles.size
const role = msg.mentions.roles.first()
const icon = role?.roleIcon()
if (size === 0) {
msg.channel.send('no role mentioned')
} else {
if (icon === undefined) {
msg.channel.send('no icon')
} else {
msg.channel.send(icon)
}
}
} else if (msg.content === '!roleSmile') {
const role = await msg.guild?.roles.get('834440844270501888')
if (role !== undefined) {
role.edit({ unicodeEmoji: '😀' })
}
} else if (msg.content === '!color') {
msg.channel.send(
msg.member !== undefined
Expand Down