-
Notifications
You must be signed in to change notification settings - Fork 0
/
event-handler.js
95 lines (80 loc) · 3.39 KB
/
event-handler.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
const NodeChache = require('node-cache')
const myCache = new NodeChache( { stdTTL: 0, checkperiod: 0 } )
const mongo = require('./libraries/mongo')
const mongoose = require('mongoose')
const staffRoleSchema = require('./schemas/staffRole-schema')
const loggg = require('./send-log')
const embeds = require('./libraries/embeds')
module.exports = async (eventType, message, role) =>{
const { guild } = message
console.log(eventType);
//this checks if there is a Muted role in the guild, if not it will make one
if(!guild.roles.cache.find(r => r.name === "Muted")){
guild.roles.create({
data: {
name: 'Muted',
},
reason: 'muted role for members that are muted, You will have to configure permissions yourself!'
})
}
// checks if it is a update from the setup command for staffrole
if (eventType == 'cachestaffrole'){
//sets the updated role i cache
roleCache = myCache.set(guild.id, role)
return
}
//gets cached staff role, if its not cached it returns undefined
staffRole = myCache.get( guild.id )
//if the staff role isnt cached it has to fetch from the database
if (!staffRole){
console.log('FETCHING FROM DB (sRole)');
try{
await mongo().then(async mongoose =>{
const result = await staffRoleSchema.findOne({_id:`${guild.id}.srole`})
//if there is no staffrole in the database it will set the value in cache to null
if(!result){
myCache.set(guild.id, null)
}else{
myCache.set(guild.id, result.staffRole)
}
//sets the result in the cache and gets the new cache
staffRole = myCache.get( guild.id )
})
}finally{
mongoose.connection.close()
}
}
//if there was a staffrole in cache it will return true and continue
if(staffRole){
if(eventType == 'slur'){
if(!message.member.roles.cache.get(r => r.id === staffRole)){
//sends a warning that slurs are not tolerated, and deleted the message containing the slur
message.channel.send("Slurs are not tolerated <@" + message.author.id + ">")
message.delete()
.catch(console.error);
//gives the offending member the role named Muted
let role = message.guild.roles.cache.find(r => r.name === "Muted");
let member = message.member
//mutedroleid for test server 793074569433972736
member.roles.add(role);
//sends slur log embed
//send in log channel
loggg(message, 'alog', 'slur')
embeds.muteDM(message)
}
}
else if(eventType == 'delete'){
if (message.channel.type == "dm"){
return
}else{
if (message.member.roles.cache.find(r => r.id === staffRole)){
return
}else{
loggg(message, 'delete')
}
}
}
}else{//this is if what the bot does if there is no staffrole in database
message.channel.send('`hey admins you need to provide me a staffrole! do !setup help to see how`')
}
}