-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (119 loc) · 5.01 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
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
142
143
144
145
146
147
148
//keeping the bot online
const http = require('http');
const express = require('express');
const app = express();
app.get("/", (request, response) => {
console.log(Date.now() + " Ping Received");
response.sendStatus(200);
});
app.listen(process.env.PORT);
setInterval(() => {
http.get(`http://${process.env.PROJECT_DOMAIN}.glitch.me/`);
}, 280000);
//bot
const Discord = require('discord.js');
const client = new Discord.Client();
const fs = require('fs');
const { prefix, token } = require('./config.json');
client.once('ready', () => {
console.log('Ready!');
});
/*╔═══════════════════════════════════════╗
Command handler
╚═══════════════════════════════════════╝*/
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on('message', async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(message, args);
}
catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
/*╔═══════════════════════════════════════╗
Reaction Role
╚═══════════════════════════════════════╝*/
//initial message
const initialMessage = "React with a game emoji to assign yourself the associated role";
//send initialMessage and add reactions
client.on('message', message => {
if (message.content === '!rr'&& message.member.permissions.has('ADMINISTRATOR')) {
message.channel.send(initialMessage).then(async msg => {
await msg.react('👍')
await msg.react('👎');
await msg.react('531436404203585536')
await msg.react('527157017807159324')
});
}
});
//track event
const events = {
MESSAGE_REACTION_ADD: 'messageReactionAdd',
MESSAGE_REACTION_REMOVE: 'messageReactionRemove',
};
client.on('raw', async event => {
if (!events.hasOwnProperty(event.t)) return;
const user = client.users.get(event.d.user_id);
const channel = client.channels.get(event.d.channel_id) || await user.createDM();
if (channel.messages.has(event.d.message_id)) return;
const message = await channel.fetchMessage(event.d.message_id);
const emojiKey = (event.d.emoji.id) ? `${event.d.emoji.name}:${event.d.emoji.id}` : event.d.emoji.name;
let reaction = message.reactions.get(emojiKey);
if (!reaction) {
const emoji = new Discord.Emoji(client.guilds.get(event.d.guild_id), event.d.emoji);
reaction = new Discord.MessageReaction(message, emoji, 1, event.d.user_id === client.user.id);
}
client.emit(events[event.t], reaction, user, message);
});
//add role based on reaction
client.on('messageReactionAdd', (reaction, user) => {
if (!user.bot) {
let Member = reaction.message.guild.roles.find(Member => Member.name === "Member");
console.log(`${user.username} reacted with "${reaction.emoji.name}".`)
switch (reaction.emoji.id || reaction.emoji.name) {
case '👍':
reaction.message.guild.member(user).addRole("527071813692293121");
break;
case '👎':
reaction.message.guild.member(user).addRole("527071837050634240");
break;
case '531436404203585536':
reaction.message.guild.member(user).addRole("530004472856969229");
break;
case '527157017807159324':
reaction.message.guild.member(user).addRole("530004514896478209");
break;
}
}
});
client.on('messageReactionRemove', (reaction, user) => {
if (!user.bot) {
let Member = reaction.message.guild.roles.find(Member => Member.name === "Member");
console.log(`${user.username} removed their "${reaction.emoji.name}reaction".`)
switch (reaction.emoji.id || reaction.emoji.name) {
case '👍':
reaction.message.guild.member(user).removeRole("527071813692293121");
break;
case '👎':
reaction.message.guild.member(user).removeRole("527071837050634240");
break;
case '531436404203585536':
reaction.message.guild.member(user).removeRole("530004472856969229");
break;
case '527157017807159324':
reaction.message.guild.member(user).removeRole("530004514896478209");
break;
}
}
});
client.login(token);