-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
218 lines (169 loc) · 6.79 KB
/
server.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Mission:
* thinking on the bot. Do you think the basic bot can be made faster. I think we should have one that makes
* the welcome page the landing page for all new members and assigns them the role new. I would like to restrict
* their access to only channels to get enrolled until their roles are changed, etc. we would probably want to have the bot send
* them basic instruction messages to get them started with enrolling.
*/
const Discord = require('discord.js');
const fs = require('fs');
const bot = new Discord.Client({
partials: ['MESSAGE', 'CHANNEL', 'REACTION']
});
const prefix = `p>`
//setup dotenv
require('dotenv').config();
//on login trigget
bot.on('ready', () => {
console.log('Bot logged in and ready for operation.');
});
//on message being sent to the discord
bot.on('message', (msg) => {
//make sure the message is valid (starts with prefix and isn't from a bot)
if (!msg.content.startsWith(prefix) || msg.author.bot) return;
//parse args from the message
const args = msg.content.slice(prefix.length).trim().split(/ +/);
//get the command.
const command = args.shift().toLowerCase();
switch (command) {
//setjoinchannel command
case 'config':
configChannel(msg, args);
}
});
async function configChannel(msg, args) {
//check if args are valid
if (args.length === 0) {
msg.channel.send("Incorrect syntax, correct syntax is p>configure #RULES_CHANNEL_NAME");
return;
}
//if the user doesn't have the create permissions role, they can't use this command
if (!msg.member.roles.cache.find(role => role.permissions.has('MANAGE_CHANNELS'))) {
msg.channel.send(`You don't have permission to use this command.`);
return;
}
//get the id from the message
let id = args[0].substring(2, 20);
//get the channel by the loaded id
let rulesChannel = msg.guild.channels.cache.find(ch => ch.id === id);
//if the channel is undefined or null, return
if (rulesChannel == undefined || rulesChannel == null) {
msg.channel.send('The rules channel you tagged came back as undefined, please try again.');
return;
}
//Send the rules message
rulesChannel.send(`
Welcome to the PHEnix Robotics Discord server!\nReact with 👍 to get accesss to other channels!
`)
.then(msg => msg.react("👍"))
//path to data files
let path = `./data/${msg.guild.id}.json`;
//Create our role if we haven't yet
if (!fs.existsSync(path)) {
//create the role
await msg.guild.roles.create({
data: {
name: 'Member',
permissions: ['READ_MESSAGE_HISTORY', 'SEND_MESSAGES', 'ADD_REACTIONS', 'CHANGE_NICKNAME']
}
});
//get the role that we just created
let role = msg.guild.roles.cache.find(role => role.name === 'Member')
//if the roles are borked try again ig
if (role == undefined || role == null) {
msg.channel.send('Error creating roles, try again.')
return;
}
//create data to store locally
let data = {
'guild': msg.guild.id,
'channel': msg.channel.id,
'rules': rulesChannel.id,
'role': role.id
}
//write the data to json so we have persisting storage of the welcome channel
fs.writeFileSync(path, JSON.stringify(data))
//print that the command went well
msg.channel.send(`Configured new member log on channel <#${msg.channel.id}>, and configured rules channel <#${id}>!`)
}
}
//when a new member joins the guild
bot.on('guildMemberAdd', (member) => {
let guildId = member.guild.id;
let path = `./data/${guildId}.json`;
//try some file reading operations
try {
//check if the file exists
if (!fs.existsSync(path)) return;
//read the data
let data = fs.readFileSync(path, {
encoding: 'utf8',
flag: 'r'
});
let json = JSON.parse(data);
//get the channel by the loaded id
let channel = member.guild.channels.cache.find(ch => ch.id === json['channel']);
//if the channel is undefined or null, return
if (channel == undefined || channel == null) return;
//send the welcome message
let embed = new Discord.MessageEmbed()
.setColor("#0099ff")
.setTitle(`Welcome ${member.user.username}!`)
.setDescription(`Welcome to ${member.guild.name}! To unlock access to other rooms please view the #rules channel and configure your interests!`)
.setThumbnail(member.user.displayAvatarURL())
.setAuthor(bot.user.username, bot.user.displayAvatarURL());
channel.send(embed);
} catch (err) {
console.error(err);
}
});
/**
//when a message reaction is added
bot.on('messagereac', (reaction, user) => {
console.log("REACT ANDY KEKW");
let guildId = reaction.message.guild.id;
let channelId = reaction.message.channel.id;
let path = `./data/${guildId}.json`;
if(!fs.existsSync(path)) return;
let data = fs.readFileSync(path, {encoding:'utf8', flag:'r'});
let json = JSON.parse(data);
//if the channel id === the rules id
if(channelId !== json['rules']){
return;
}
//if they react with the green check mark, give them the new role
if(reaction.emoji.id == 766807538271649804){
}
});*/
//TODO check if it's in the help channel (where else could it be tho lol)
bot.on('messageReactionAdd', async (reaction, user) => {
// When we receive a reaction we check if the reaction is partial or not
if (reaction.partial) {
// If the message this reaction belongs to was removed the fetching might result in an API error, which we need to handle
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message: ', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
let path = `./data/${reaction.message.guild.id}.json`
if (!fs.existsSync(path)) return;
//get file from local stuff
let data = fs.readFileSync(path, {
encoding: 'utf8',
flag: 'r'
});
let json = JSON.parse(data);
//if the channel id === the rules id
if (reaction.message.channel.id !== json['rules']) {
return;
}
//TODO save role id and use that instead
let roleToAdd = reaction.message.guild.roles.cache.find(role => role.id == json['role'])
let member = reaction.message.guild.members.cache.find(member => member.id === user.id);
//add the "new" role to the member which gives them access to the channels they might wnat
member.roles.add(roleToAdd);
});
bot.login(process.env.token);