-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
141 lines (121 loc) · 3.67 KB
/
index.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
133
134
135
136
137
138
139
140
141
import {
Client,
EmojiResolvable,
MessageReaction,
PartialMessageReaction,
PartialUser,
Role,
Snowflake,
User,
} from "discord.js";
export interface ReactionRoleConfiguration {
messageId: Snowflake;
roleId: Snowflake;
reaction: EmojiResolvable;
}
interface ReactionRoleReverseIndex {
[server: string]: { [emoji: string]: string };
}
function buildReverseSearch(
config: ReactionRoleConfiguration[]
): ReactionRoleReverseIndex {
return config.reduce((index, conf) => {
if (!index[conf.messageId]) {
index[conf.messageId] = {};
}
index[conf.messageId][conf.reaction.toString()] = conf.roleId;
return index;
}, {} as ReactionRoleReverseIndex);
}
export class ReactionRole {
private reverseConfig: ReactionRoleReverseIndex;
constructor(private client: Client, config: ReactionRoleConfiguration[]) {
this.reverseConfig = buildReverseSearch(config);
this.addReaction = this.addReaction.bind(this);
this.removeReaction = this.removeReaction.bind(this);
client.on("messageReactionAdd", this.addReaction);
client.on("messageReactionRemove", this.removeReaction);
}
private extractRole(
reaction: MessageReaction | PartialMessageReaction
): Promise<Role | null> {
const messageId = reaction.message.id;
const reactionId = reaction.emoji.id;
const reactionName = reaction.emoji.name;
if (this.reverseConfig[messageId]) {
const reverseConfig = this.reverseConfig[messageId];
// Attempt to parse the role from the configuration object.
let roleId;
if (reactionId && reverseConfig[reactionId]) {
roleId = reverseConfig[reactionId];
} else if (reactionName && reverseConfig[reactionName]) {
roleId = reverseConfig[reactionName];
}
// Attempt to extract and return the role
if (roleId && reaction.message.guild) {
return reaction.message.guild.roles.fetch(roleId);
}
}
return Promise.resolve(null);
}
private async addReaction(
reaction: MessageReaction | PartialMessageReaction,
user: User | PartialUser
): Promise<void> {
if (process.env.REACTION_DEBUG) {
console.debug("add", {
message: reaction.message.id,
user: user.id,
emoji: reaction.emoji.name,
partial: reaction.partial,
});
}
/* Early leave if the message is not sent to a guild. */
if (!reaction.message.guild) {
return;
}
/* Get the member that reacted originally. */
const member = await reaction.message.guild.members.fetch(user.id);
if (!member) {
return;
}
/* Try to add the member to the guild. */
await this.extractRole(reaction).then((role) => {
if (role) {
return member.roles.add(role);
}
});
}
async removeReaction(
reaction: MessageReaction | PartialMessageReaction,
user: User | PartialUser
): Promise<void> {
if (process.env.REACTION_DEBUG) {
console.debug("remove", {
message: reaction.message.id,
user: user.id,
emoji: reaction.emoji.name,
partial: reaction.partial,
});
}
/* Early leave if the message is not sent to a guild. */
if (!reaction.message.guild) {
return;
}
/* Get the member that reacted originally. */
const member = await reaction.message.guild.members.fetch(user.id);
if (!member) {
return;
}
/* Try to add the member to the guild. */
await this.extractRole(reaction).then((role) => {
if (role) {
return member.roles.remove(role);
}
});
}
teardown(): void {
this.client.off("messageReactionAdd", this.addReaction);
this.client.off("messageReactionRemove", this.removeReaction);
}
}