-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
76 lines (63 loc) · 2.09 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
const { Client, GatewayIntentBits } = require('discord.js')
const config = require('./config.json')
const client = {}
config.forEach((reaction, index) => {
client[index] = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions]
})
const onReady = async () => {
const channel = client[index].channels.cache.get(reaction.channel_id)
console.log(
`Logged in as ${client[index].user.tag} & watching ${channel.name} message id (${reaction.message_id}) !`
)
try {
await channel.messages.fetch(reaction.message_id)
} catch (error) {
console.log(error)
}
}
const addRole = async ({ message, _emoji }, user) => {
console.log(`${user.tag} reacted with ${_emoji.name} (${_emoji.id})`)
if (user.bot || message.id !== reaction.message_id) return
if (message.partial) {
try {
await message.fetch()
} catch (error) {
console.log(error)
}
}
const { guild } = message
const member = guild.members.cache.get(user.id)
const roleId = reaction.reaction.find(role => role.emoji_id === _emoji.id)?.role_id
if (!roleId) return
try {
await member.roles.add(roleId)
} catch (error) {
console.log(error)
}
}
const removeRole = async ({ message, _emoji }, user) => {
console.log(`${user.tag} removed reaction ${_emoji.name} (${_emoji.id})`)
if (user.bot || message.id !== reaction.message_id) return
if (message.partial) {
try {
await message.fetch()
} catch (error) {
console.log(error)
}
}
const { guild } = message
const member = guild.members.cache.get(user.id)
const roleId = reaction.reaction.find(role => role.emoji_id === _emoji.id)?.role_id
if (!roleId) return
try {
await member.roles.remove(roleId)
} catch (error) {
console.log(error)
}
}
client[index].on('ready', onReady)
client[index].on('messageReactionAdd', addRole)
client[index].on('messageReactionRemove', removeRole)
client[index].login(process.env.BOT_TOKEN)
})