-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
240 lines (212 loc) · 9.36 KB
/
bot.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const botSettings = require("./botsettings.json")
const prefix = botSettings.prefix
const { Client } = require("discord.js-commando")
const { RichEmbed } = require("discord.js")
const path = require("path")
const mongodb = require("mongodb")
const levelFunc = require("./function/levelFunc.js")
const editDoc = require("./function/editDoc.js")
const mongoUtil = require("./mongoUtil.js")
mongoUtil.connectToServer((err, client) => {
if (err) throw err
})
const bot = new Client({
commandPrefix: prefix,
owner: ["323161865205317632", "263275868313354240"],
disableEveryone: true,
unknownCommandResponse: false,
})
bot.registry
.registerDefaultTypes()
.registerGroups([
["fun", "🎉 Fun"],
["tvshow", "📺 TV Show"],
["userinfo", "ℹ User Info"],
["economy", "💎 Economy"],
["managment", "🔨 Server Managment"],
["moderation", "⚖ Moderation"]
])
.registerDefaultGroups()
.registerDefaultCommands({
help: false,
})
.registerCommandsIn(path.join(__dirname, "commands"))
bot.emotes = {
check: "<:Check:589792970266640413>",
cross: "<:Cross:589793004965855272>",
info: "<:Info:632686188687261716>",
question: "<:Question:632686615126474792>",
warn: "<:Warn:632686188704301142>"
}
bot.on("ready", () => {
bot.user.setActivity("!help", {type: "LISTENING" })
.then(r => console.log("Activity set"))
.catch(e => console.log(`Errors encountered when defining the activity. Error: ${e}`))
console.log("I am ready!")
bot.generateInvite(456518742).then(result => console.log(result))
//Re-create guilds if needed
const guildCollection = mongoUtil.getDb().collection("guilds")
bot.guilds.forEach(async guild => {
let guildDoc = await guildCollection.findOne({guild_id: guild.id})
if (guildDoc === null) {
await editDoc.createGuild(guild.id, guild.name)
}
})
//Handle tempban
const tempBanCollection = mongoUtil.getDb().collection("tempban")
setInterval(async () => {
const tempBanList = await tempBanCollection.find()
const currentTime = Date.now()
tempBanList.forEach(async ban => {
if (ban.date < currentTime) {
const bannedMemberGuild = bot.guilds.get(ban.guild)
const bannedUser = await bot.fetchUser(ban.discord_id, false)
const bannedDMChannel = await bannedUser.createDM()
bannedMemberGuild.unban(bannedUser, "Ban expired")
.then(user => {
const unbanTempEmbed = new RichEmbed()
.setTitle(`${bot.emotes.check} Vous avez été débanni du serveur ${bannedMemberGuild.name}`)
.setColor("#2ECC71")
bannedDMChannel.send(unbanTempEmbed)
tempBanCollection.deleteOne({discord_id: bannedUser.id})
})
.catch(() => {
tempBanCollection.deleteOne({discord_id: bannedUser.id})
})
}
})
}, 60000)
//Handle Reminder
const reminderCollection = mongoUtil.getDb().collection("reminders")
setInterval(async () => {
const reminderList = await reminderCollection.find()
const currentTime = Date.now()
reminderList.forEach(reminder => {
if (reminder.date < currentTime) {
bot.fetchUser(reminder.discord_id)
.then(async user => {
remindDMChannel = await user.createDM()
const remindEndEmbed = new RichEmbed()
.setTitle("Voici les informations que vous m'avez demandé de vous rappeler")
.setThumbnail(user.displayAvatarURL)
.setDescription(reminder.reason)
.setColor("#3498DB")
.setFooter(`Message envoyé`)
.setTimestamp(Date.now())
remindDMChannel.send(remindEndEmbed)
reminderCollection.deleteOne(reminder)
})
}
})
}, 60000)
//xp VocTime
async function createDocOrUpdateVocTime(member) {
const db = mongoUtil.getDb()
const collection = db.collection("members")
let userDoc = await collection.findOne({"discord_id": member.user.id.toString(10)})
if(userDoc === null) {
editDoc.insertDoc(member.user.id, member.guild, member.user.username, member.user.discriminator)
} else {
await editDoc.checkGuild(member.user.id, member.guild.id)
updateVocTime(userDoc, member)
}
}
function updateVocTime(userDoc, member) {
const db = mongoUtil.getDb()
const collection = db.collection("members")
let oldVoc = userDoc.voc_time
let newVoc = oldVoc + time / 1000
collection.updateOne({"discord_id": member.user.id},{$set: {"voc_time": mongodb.Int32(newVoc)}})
levelFunc.addPoints(member, (time / 1000) / 60 * 3)
}
//xp vocTime
let time = 60000
setInterval(() => {
bot.channels.forEach(channel => {
if (channel.type === "voice") {
channel.members.forEach(client => {
if(channel.members.size > 1 && client.user.bot !== true && client.mute !== true && channel.guild.afkChannelID !== channel.id) {
createDocOrUpdateVocTime(client)
}
}
)}
})
}, time)
})
bot.on("guildCreate", guild => {
editDoc.createGuild(guild.id, guild.name)
})
bot.on("guildMemberAdd", async member => {
const db = mongoUtil.getDb()
const collection = await db.collection("members")
const userDoc = await collection.findOne({"discord_id": member.user.id.toString(10)})
if (userDoc === null) {
editDoc.insertDoc(member.user.id, member.guild.id, member.user.username, member.user.discriminator)
}
const guildCollection = await db.collection('guilds')
const guildDoc = await guildCollection.findOne({guild_id: member.guild.id})
const bypassGlobalBans = guildDoc["bypassGlobalBans"] //get the admin channel of the guild if on
if (userDoc.isGlobalBanned && !bypassGlobalBans) {
const banEmbed = new RichEmbed()
.setTitle(`:warning: Vous avez été banni de ${member.guild.name}`)
.setDescription(`Vous avez été banni par <@${bot.user.id}>`)
.setColor("#C0392B")
.addField("Raison", "Global Banned...")
.addField("Durée", "Permanent")
member.send(banEmbed)
bot.registry.commands.get("ban").call(member, "Global Banned...", member.guild)
} else if (userDoc.isGlobalBanned && bypassGlobalBans) {
let adminChannel = guildDoc["adminChannel"] //get the admin channel of the guild if on
const banEmbedAdmin = new RichEmbed()
.setDescription(`:warning: <@${member.id}> est un utilisateur à risques! Faites attention <@${member.guild.owner.id}>`)
.setColor("#C0392B")
if (adminChannel == "undefined") { //if channelAdmin is not defiend send the report to the owner of the guild
banEmbedAdmin.setTitle(`:warning: Ce message est censé être envoyé dans un channel vous pouvez le définir avec \"!setchannel admin\"`)
await member.guild.owner.send(banEmbedAdmin)
} else { //if channelAdmin is defined send the report to this channel
await member.guild.channels.get(adminChannel).send(banEmbedAdmin)
}
}
})
bot.on("message", async message => {
//check
if(message.author.bot === true) return
if(message.channel.type === "dm") return
//exp
if (!message.content.startsWith(prefix)) {
const db = mongoUtil.getDb()
const collection = db.collection("members")
message.channel.fetchMessages({ limit: 2})
.then(async messages => {
let contents = messages.map((x) => [x.author.id, x.createdTimestamp])
if (contents[1] === undefined) return
if ((contents[0][1] - contents[1][1] < 1000) && (contents[0][0] === contents[1][0])) return
const info = {id: message.author.id, guild: message.member.guild.id, username: message.author.username, discriminator: message.author.discriminator}
let userDoc = await collection.findOne({"discord_id": info.id})
if (userDoc === null) {
editDoc.insertDoc(info.id, info.guild, info.username, info.discriminator)
} else {
await editDoc.checkGuild(info.id, info.guild)
updateMessage(userDoc, message.member)
}
})
}
function updateMessage(userDoc, member) {
const db = mongoUtil.getDb()
const collection = db.collection("members")
let oldMessage = userDoc.message_sent
let newMessage = oldMessage + 1
collection.updateOne({"discord_id": userDoc.discord_id},{$set: {"message_sent": mongodb.Int32(newMessage)}})
levelFunc.addPoints(member, 2)
}
})
bot.login(botSettings.token)
.then(() => console.log("Bot connected"))
.catch(e => console.log(e))
setTimeout(() => {
if (bot.status !== 0) {
err = "Connection issue with discord"
throw err
}
}, 10000)
module.exports.bot = bot