-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
guildEmojis.ts
132 lines (121 loc) · 4.01 KB
/
guildEmojis.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import type { Client } from '../client/mod.ts'
import { Emoji } from '../structures/emoji.ts'
import type { Guild } from '../structures/guild.ts'
import { Role } from '../structures/role.ts'
import type { EmojiPayload } from '../types/emoji.ts'
import { CHANNEL, GUILD_EMOJI, GUILD_EMOJIS } from '../types/endpoint.ts'
import { BaseChildManager } from './baseChild.ts'
import type { EmojisManager } from './emojis.ts'
import { fetchAuto } from '../../deps.ts'
export class GuildEmojisManager extends BaseChildManager<EmojiPayload, Emoji> {
guild: Guild
constructor(client: Client, parent: EmojisManager, guild: Guild) {
super(client, parent)
this.guild = guild
}
async get(id: string): Promise<Emoji | undefined> {
const res = await this.parent.get(id)
if (res !== undefined && res.guild?.id === this.guild.id) return res
else return undefined
}
async size(): Promise<number> {
return (
(await this.client.cache.size(
this.parent.cacheName,
(d: EmojiPayload) => d.guild_id === this.guild.id
)) ?? 0
)
}
async delete(id: string): Promise<boolean> {
return this.client.rest.delete(CHANNEL(id))
}
async fetch(id: string): Promise<Emoji | undefined> {
return await new Promise((resolve, reject) => {
this.client.rest
.get(GUILD_EMOJI(this.guild.id, id))
.then(async (data) => {
const emoji = new Emoji(this.client, data as EmojiPayload)
data.guild_id = this.guild.id
await this.set(id, data as EmojiPayload)
emoji.guild = this.guild
resolve(emoji)
})
.catch((e) => reject(e))
})
}
async fetchAll(): Promise<Emoji[]> {
return await new Promise((resolve, reject) => {
this.client.rest
.get(GUILD_EMOJIS(this.guild.id))
.then(async (data) => {
const emojis: Emoji[] = []
for (const emojiData of data as EmojiPayload[]) {
const emoji = new Emoji(this.client, emojiData)
emojiData.guild_id = this.guild.id
await this.set(emojiData.id!, emojiData)
emoji.guild = this.guild
emojis.push(emoji)
}
resolve(emojis)
})
.catch((e) => reject(e))
})
}
async create(
name: string,
url: string,
roles?: Role[] | string[] | string
): Promise<Emoji | undefined> {
let data = url
if (!data.startsWith('data:')) {
data = await fetchAuto(url)
}
return await new Promise((resolve, reject) => {
let roleIDs: string[] = []
if (roles !== undefined && typeof roles === 'string') roleIDs = [roles]
else if (roles !== undefined) {
if (roles?.length === 0)
reject(new Error('Empty Roles array was provided'))
if (roles[0] instanceof Role)
roleIDs = roles.map((r) => (typeof r === 'string' ? r : r.id))
else roleIDs = roles as string[]
} else roles = [this.guild.id]
this.client.rest
.post(GUILD_EMOJIS(this.guild.id), {
name,
image: data,
roles: roleIDs
})
.then(async (data) => {
const emoji = new Emoji(this.client, data as EmojiPayload)
data.guild_id = this.guild.id
await this.set(data.id, data as EmojiPayload)
emoji.guild = this.guild
resolve(emoji)
})
.catch((e) => reject(e))
})
}
async array(): Promise<Emoji[]> {
const arr = await this.parent.array()
return arr.filter(
(c) => c.guild !== undefined && c.guild.id === this.guild.id
)
}
async flush(): Promise<boolean> {
const arr = await this.array()
for (const elem of arr) {
const emojiID = elem.id !== null ? elem.id : elem.name
this.parent._delete(emojiID as string)
}
return true
}
async keys(): Promise<string[]> {
const result = []
for (const raw of ((await this.client.cache.array(this.parent.cacheName)) ??
[]) as EmojiPayload[]) {
if (raw.guild_id === this.guild.id) result.push(raw.id!)
}
return result
}
}