-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
121 lines (99 loc) · 3.84 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
// Some hardcoded variables that you can change
const prefix = "!"
const guild_id = "488540747361026058"
const verified_role_id = "652669689683509249"
const welcome_emoji_id = "637434582500900864"
const reaction_emoji_id = "720420182538977281"
// Don't touch the code beflow this line!
if (process.env.NODE_ENV === 'development') {
require('dotenv').config()
}
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()
.replace('/', '')
// 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.owner.id !== 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 welcomeEmoji = `<:${msg.guild.emojis.get(welcome_emoji_id).identifier}>`
const welcomeTitle = `${welcomeEmoji} Welcome to ${msg.guild.name}!`
embed.addField(welcomeTitle, introMessageContent)
embed.addField('🎗 Community Guidelines', communityGuidelinesContent)
embed.addField('🔐 Getting Verified', verificationMessageContent)
msg.channel.send({embed}).then(theVerificationMessage => theVerificationMessage.react(reaction_emoji_id))
msg.delete()
}
return
})
client.on('messageReactionAdd', ( { message: { channel } }, user ) => {
if (/verification/.test(channel.name)) {
channel.guild.fetchMember(user).then(member => {
return member.addRole(verified_role_id)
}).then(() => {
console.log(`The ${roleName} role has been added to member ${user.tag} successfully!`)
}).catch(error => {
console.error(error)
})
}
})
client.on('messageReactionRemove', ( { message: { channel } }, user ) => {
if (/verification/.test(channel.name)) {
channel.guild.fetchMember(user).then(member => {
return member.removeRole(verified_role_id)
}).then(() => {
console.log(`The ${roleName} has been removed from member ${user.tag} successfully!`)
}).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
)
})
}
})
if (process.env.TOKEN !== null) {
client.login(process.env.TOKEN)
} else {
console.error('Bot token is empty!')
}