This repository has been archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
144 lines (130 loc) · 4.99 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
const fs = require("fs")
const Discord = require("discord.js")
const config = require("./config.json")
const client = new Discord.Client({ws:{intents:["GUILDS", "GUILD_MEMBERS", "GUILD_MESSAGE_REACTIONS", "GUILD_MESSAGES", "DIRECT_MESSAGES"]}, partials:["MESSAGE", "CHANNEL", "USER", "REACTION", "GUILD_MEMBER"]})
client.commands = new Discord.Collection()
const Keyv = require('keyv');
const reactbotMessages = new Keyv('sqlite://./messages.sqlite');
const reactbotReactions = new Keyv(`sqlite://./reactions.sqlite`);
reactbotMessages.on('error', err => console.error('Keyv connection error:', err));
client.login(config.token)
client.once("ready", () => {
console.log("Bot started.")
console.log(`Currently in: ${client.guilds.cache.size} ${client.guilds.cache.size == 1?"guild":"guilds"}`)
function setStatus(){
client.user.setActivity(`${client.guilds.cache.size} ${client.guilds.cache.size == 1?"server":"servers"} | ${config.prefix}help`)
}
setStatus()
setInterval(() => {
setStatus()
}, 60000);
if(!config.logChannel)return;
client.channels.fetch(config.logChannel, true).then(channel => {
let startupEmbed = new Discord.MessageEmbed()
.setDescription(`✅ **Bot started**\nGuilds: ${client.guilds.cache.size}`)
channel.send(startupEmbed)
})
})
client.on("guildCreate", guild => {
if(!config.logChannel)return;
client.channels.fetch(config.logChannel, true).then(channel => {
let joinedGuildEmbed = new Discord.MessageEmbed()
.setDescription(`📥 **Joined Guild**\nName: ${guild.name}\nID: ${guild.id}`)
.setColor("00FF00")
channel.send(joinedGuildEmbed)
})
})
client.on("guildDelete", guild => {
if(!config.logChannel)return;
client.channels.fetch(config.logChannel, true).then(channel => {
let leftGuildEmbed = new Discord.MessageEmbed()
.setDescription(`📤 **Left Guild**\nName: ${guild.name}\nID: ${guild.id}`)
.setColor("FF0000")
channel.send(leftGuildEmbed)
})
})
const commandFiles = fs.readdirSync('./actions').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./actions/${file}`);
client.commands.set(command.name, command);
console.log(`Loaded: ${command.name}`)
}
client.on('message', async message => {
if(message.partial == true)return;
//Put a module called "commandHandler.js" in "modules/" to override built in command handler
if(fs.existsSync('./modules/commandHandler.js')){
const commandHandler = require('./commandHandler.js')
return commandHandler.execute(message, client)
}
if (!message.content.startsWith(config.prefix) || message.author.bot || message.channel.type == "dm") return;
const args = message.content.slice(config.prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) {
return;
}
if(!message.member.hasPermission("MANAGE_GUILD") && command.manageGuild && command.manageGuild == true){
return message.channel.send("You need to have the `MANAGE SERVER` permission to use this command.")
}
if(config.staff && !config.staff.includes(message.author.id) && command.staff && command.staff == true){
return message.channel.send(`This command is currently restricted to Justice Devs Staff members.`)
}
try {
command.execute(message, args, message.client);
} catch (err) {
console.error(err);
}
});
function getUser(reaction, user, action = "none"){
reactbotMessages.get(reaction.message.id).then(messageData => {
console.log(messageData)
if(messageData == undefined || messageData.message != reaction.message.id){
return
}else{
reactbotReactions.get(messageData.message).then(storedReaction => {
let requiredReaction = storedReaction.id?storedReaction.id:storedReaction
let givenReaction = reaction.emoji.id == null?reaction.emoji.name:reaction.emoji.id
if(requiredReaction == givenReaction){
switch(action){
case "addrole":{
return reaction.message.guild.members.fetch(user).then((guildMember = new Discord.GuildMember()) => {
return guildMember.roles.add(reaction.message.guild.roles.cache.get(messageData.role))
})
}
case "removerole":{
return reaction.message.guild.members.fetch(user).then((guildMember = new Discord.GuildMember()) => {
return guildMember.roles.remove(reaction.message.guild.roles.cache.get(messageData.role))
})
}
default:{
}
}
}
})
}
})
}
client.on("messageReactionAdd", (reaction, user) => {
try{
if(user.bot == true)return
reaction.fetch().then(fetchedReaction => {
return getUser(reaction, user, "addrole")
})
}catch(err){
console.error(err)
}
})
client.on("messageReactionRemove", (reaction, user) => {
try{
if(user.bot == true)return
reaction.fetch().then(fetchedReaction => {
return getUser(reaction, user, "removerole")
})
}catch(err){
console.error(err)
}
})
process.on("unhandledRejection", (err) => {
console.error(err)
})