-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (84 loc) · 3.32 KB
/
index.js
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
const { Client, Intents } = require("discord.js");
/**
* @class ReactionRoleClient
* @extends {Client}
*/
class ReactionRoleClient extends Client {
/**
* Creates an instance of ReactionRoleClient.
* @param {import("discord.js").ClientOptions} [options]
* @memberof ReactionRoleClient
*/
constructor() {
super({intents : [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_BANS,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_INTEGRATIONS,
Intents.FLAGS.GUILD_WEBHOOKS,
Intents.FLAGS.GUILD_INVITES,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MESSAGE_TYPING,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_TYPING
]});
this.config = require("./config.json");
this.data = require("./data.json");
this.on("ready", async (client) => {
console.log(`[INFO] Logged in as ${this.user.tag}.`);
// eslint-disable-next-line no-restricted-syntax
for (const data of Object.values(this.data)) {
try {
await client.channels.fetch(data.channelID).then(async channel => {
await channel.messages.fetch(data.messageID).then(async message => {
console.log("Fetched " + message.id + " successfully !");
});
});
} catch (e) {
console.log(`[ERR] Error when fetching ${data.messageID}, because ${e.message}.`);
}
}
});
this.on("messageReactionAdd", async (messageReaction, user) => {
await this.handleRole(messageReaction, user, "add");
});
this.on("messageReactionRemove", async (messageReaction, user) => {
await this.handleRole(messageReaction, user, "remove");
});
this.login(this.config.token);
}
/**
* @param {import("discord.js").MessageReaction} messageReaction
* @param {import("discord.js").User} user
* @param {String} type
* @returns {Promise<void>}
* @memberof ReactionRoleClient
*/
async handleRole(messageReaction, user, type) {
if (!type || !["add", "remove"].includes(type.toLowerCase())) {
return undefined;
}
if (messageReaction.me){
return undefined;
}
const { message } = messageReaction;
const data = this.data[message.id];
if (data) {
const emoji = data.emojis[messageReaction._emoji.name];
if (!emoji) return undefined;
await this.guilds.fetch(message.guildId).then(async guild => {
await guild.members.fetch(user.id).then(async member => {
if (type.toLowerCase() === "add") await member.roles.add(emoji);
if (type.toLowerCase() === "remove") await member.roles.remove(emoji);
})
});
}
return undefined;
}
}
module.exports = new ReactionRoleClient();