-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
93 lines (79 loc) · 3.23 KB
/
index.js
File metadata and controls
93 lines (79 loc) · 3.23 KB
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
// @ts-check
const encoding = require("@amanda/scratch")
/** @type {{ [route: string]: (button: import("discord-api-types/v10").APIMessageComponentInteractionData, user: import("discord-api-types/v10").APIUser) => unknown }} */
let handlers = {}
/** @type {(button: import("discord-api-types/v10").APIMessageComponentInteractionData, user: import("discord-api-types/v10").APIUser) => string} */
let routeHandler = (button, user) => button.custom_id
/** @type {Map<string, typeof cc.BetterComponent["prototype"]>} */
const components = new Map()
// This string is important to create truly random IDs across restarts as the sequencing may produce an identical ID.
const randomString = Math.random().toString(36).substring(7)
let idSequence = BigInt(0)
/** @type {[2, 3, 5, 6, 7, 8]} */
const bcAcceptableTypes = [2, 3, 5, 6, 7, 8]
class BetterComponent {
info
/** @type {((interaction: import("discord-api-types/v10").APIMessageComponentInteraction, component: BetterComponent) => unknown) | null} */
callback = null
id = BetterComponent.#nextID
/** @type {import("discord-api-types/v10").APIButtonComponentWithCustomId | import("discord-api-types/v10").APISelectMenuComponent} */
component
/**
* @param {Omit<import("discord-api-types/v10").APIButtonComponentWithCustomId | import("discord-api-types/v10").APISelectMenuComponent, "custom_id">} info
* @param {Record<string, any>} [extraEncodedInfo]
*/
constructor(info, extraEncodedInfo) {
this.info = info
components.set(this.id, this)
// @ts-expect-error It's fine!
this.component = { custom_id: encoding.encode({ mid: this.id, ...(extraEncodedInfo || {}) }), ...this.info }
}
/** @returns {string} */
static get #nextID() {
return `menu-${randomString}-${idSequence++}`
}
/**
* @param {(interaction: import("discord-api-types/v10").APIMessageComponentInteraction, component: BetterComponent) => unknown} fn
* @returns {this}
*/
setCallback(fn) {
this.callback = fn
return this
}
/** @returns {this} */
destroy() {
components.delete(this.id)
return this
}
}
const cc = {
/**
* @param {(button: import("discord-api-types/v10").APIMessageComponentInteractionData, user: import("discord-api-types/v10").APIUser) => string} router
* @param {{ [route: string]: (button: import("discord-api-types/v10").APIMessageComponentInteractionData, user: import("discord-api-types/v10").APIUser) => unknown }} info
*/
setHandlers(router, info) {
routeHandler = router
handlers = info
},
/**
* @param {import("discord-api-types/v10").APIInteraction} interaction
* @returns {void}
*/
handle(interaction) {
if (interaction.type !== 3 || !interaction.data) return
if (bcAcceptableTypes.includes(interaction.data.component_type)) {
const decoded = encoding.decode(interaction.data.custom_id)
const btn = components.get(decoded?.mid ?? interaction.data.custom_id)
if (btn) {
btn.callback?.(interaction, btn)
return
}
}
// @ts-expect-error interaction.user will be there
const route = routeHandler(interaction.data, interaction.member?.user ?? interaction.user)
// @ts-expect-error interaction.user will be there
if (handlers[route]) handlers[route](interaction.data, interaction.member?.user ?? interaction.user)
},
BetterComponent
}
module.exports = cc