-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
160 lines (138 loc) · 4.33 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
149
150
151
152
153
154
155
156
157
158
159
160
require("dotenv").config();
const prefix = process.env.PREFIX || "?";
const guild_id = process.env.GUILD_ID;
const verified_role_id = process.env.ROLE_ID;
const verification_channel_id = process.env.CHANNEL_ID;
const fs = require("fs");
const Discord = require("discord.js");
const client = new Discord.Client();
let roleName = "";
client.on("ready", async () => {
console.log(`Logged in as ${client.user.tag}!`);
await client.user.setActivity("Verify in #verification");
roleName = client.guilds.get(guild_id).roles.get(verified_role_id).name;
});
client.on("message", msg => {
const args = msg.content
.slice(prefix.length)
.trim()
.split(/ +/g);
const command = args.shift().toLowerCase();
// svm = set verification message
if (
!msg.author.bot &&
msg.content.indexOf(prefix) === 0 &&
command === "svm"
) {
// If sender of message is not the guild owner, cancel action
if (msg.member.guild.ownerID !== msg.member.id) return;
const introMessageContent = fs.readFileSync("intro-message.md", {
encoding: "utf8",
flag: "r"
});
const communityGuidelinesContent = fs.readFileSync(
"community-guidelines.md",
{ encoding: "utf8", flag: "r" }
);
const verificationMessageContent = fs.readFileSync(
"verification-message.md",
{ encoding: "utf8", flag: "r" }
);
const embed = new Discord.RichEmbed();
const welcomeTitle = `Welcome to ${msg.guild.name}!`;
embed.addField(welcomeTitle, introMessageContent);
embed.addField("🎗 Community Guidelines", communityGuidelinesContent);
embed.addField("🔐 Getting Verified", verificationMessageContent);
msg.channel.send({ embed }).then(message => {
message.react("👍");
fs.writeFileSync(
"data.json",
JSON.stringify({ messageId: message.id }, null, 4)
);
});
msg.delete();
}
return;
});
client.on("messageReactionAdd", ({ message: { channel } }, user) => {
if (/verification/.test(channel.name)) {
channel.guild
.fetchMember(user)
.then(member => {
member
.addRole(verified_role_id)
.then(() => {
console.log(
`The ${roleName} has been removed from member ${user.tag} successfully!`
);
})
.catch(e => console.error("Error adding role"));
})
.catch(error => {
console.error(error);
});
}
});
client.on("messageReactionRemove", ({ message: { channel } }, user) => {
if (/verification/.test(channel.name)) {
channel.guild
.fetchMember(user)
.then(member => {
member
.removeRole(verified_role_id)
.then(() => {
console.log(
`The ${roleName} has been removed from member ${user.tag} successfully!`
);
})
.catch(e => console.error("Error removing role"));
})
.catch(error => {
console.error(error);
});
}
});
client.on("raw", ({ d: data, t: event }) => {
if (["MESSAGE_REACTION_ADD", "MESSAGE_REACTION_REMOVE"].includes(event)) {
const { channel_id, user_id, message_id, emoji } = data;
const channel = client.channels.get(channel_id);
if (!channel.messages.has(message_id))
channel.fetchMessage(message_id).then(message => {
const reaction = message.reactions.get(
emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name
);
const user = client.users.get(user_id);
if (reaction) reaction.users.set(user_id, user);
return client.emit(
event === "MESSAGE_REACTION_ADD"
? "messageReactionAdd"
: "messageReactionRemove",
reaction,
user
);
});
}
});
client.on("guildMemberRemove", function(member) {
try {
const { messageId } = JSON.parse(fs.readFileSync("data.json"));
const channel = client.channels.get(verification_channel_id);
channel
.fetchMessage(messageId)
.then(message => {
message.reactions.map(reaction => {
reaction.remove(member.user.id);
});
})
.catch(console.error);
} catch (e) {
console.error(
`data.json doesn't exist. Re-run ${prefix}svm in the #verification channel and delete the previous message.`
);
}
});
if (TOKEN !== null) {
client.login(TOKEN);
} else {
console.error("Bot token is empty!");
}