-
Notifications
You must be signed in to change notification settings - Fork 0
/
send-log.js
90 lines (72 loc) · 2.92 KB
/
send-log.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
const mongo = require('./libraries/mongo')
const mongoose = require ('mongoose')
const actionLogSchema = require('./schemas/actionLog-schema')
const deleteLogSchema = require('./schemas/deleteLog-schema')
const embeds = require('./libraries/embeds')
const NodeChache = require('node-cache')
const myCache = new NodeChache( { stdTTL: 0, checkperiod: 0 } )
module.exports = async (recievedMessage, type, log, roleId) => {
const { guild, content, author, channel } = recievedMessage
if (type === 'cache'){
if (log == 'alog'){
alogCache = myCache.set(`${guild.id}.alog`, roleId)
}else{//guaranteed dlog
dlogCache = myCache.set(`${guild.id}.dlog`, roleId)
}
}
if (type === 'alog'){
role = myCache.get((`${guild.id}.alog`))
if (!role){
try{
await mongo().then(async mongoose => {
console.log('FETCHING FROM DB (ALOG)');
const result = await actionLogSchema.findOne({ _id:`${guild.id}.alog`})
if (!result){
alogCache = myCache.set(`${guild.id}.alog`, null)
}else{
alogCache = myCache.set(`${guild.id}.alog`, result.actionLog)
}
});
}finally{
mongoose.connection.close()
}
}
alog = myCache.get((`${guild.id}.alog`))
if(alog){
if(log == 'slur'){
embeds.slur(recievedMessage, alog)
}
else if (log == 'mute'){
//typeId is the muted person in this case
embeds.muteLog(recievedMessage, alog, roleId)
}
else if(log == 'unmute'){
embeds.unMuteLog(recievedMessage, alog, roleId)
}
}else{
recievedMessage.channel.send('`no logging channel set!`')
}
}
else if (type === 'delete') {
dlog = myCache.get((`${guild.id}.dlog`))
if (!dlog){
try{
await mongo().then(async mongoose => {
console.log('FETCHING FROM DB (DLOG)')
const result = await deleteLogSchema.findOne({ _id:`${guild.id}.dlog`})
dlogCache = myCache.set(`${guild.id}.dlog`, result.deleteLog)
})
}catch{
console.log('unable to log deletion, only a occasional one time issue');
}finally{
mongoose.connection.close()
}
}
dlog = myCache.get((`${guild.id}.dlog`))
if(dlog){
var logChannel = guild.channels.cache.find(
channel => channel.id === dlog)
logChannel.send(`Message: "${content}" from ${author.toString()} was deleted in ${channel.toString()}`)
}
}
}