forked from roefinoavrililo/Discord.JS-Leveling-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
255 lines (214 loc) · 10.6 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Importing Packages
const Discord = require("discord.js")
const SQLite = require("better-sqlite3")
const sql = new SQLite('./mainDB.sqlite')
const { join } = require("path")
const { readdirSync } = require("fs");
const client = new Discord.Client()
client.commands = new Discord.Collection();
const cooldowns = new Discord.Collection();
const talkedRecently = new Map();
// Token, Prefix, and Owner ID
const config = require("./config.json")
// Events
client.login(config.token)
client.on("ready", () => {
// Check if the table "points" exists.
const levelTable = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'levels';").get();
if (!levelTable['count(*)']) {
sql.prepare("CREATE TABLE levels (id TEXT PRIMARY KEY, user TEXT, guild TEXT, xp INTEGER, level INTEGER, totalXP INTEGER);").run();
}
client.getLevel = sql.prepare("SELECT * FROM levels WHERE user = ? AND guild = ?");
client.setLevel = sql.prepare("INSERT OR REPLACE INTO levels (id, user, guild, xp, level, totalXP) VALUES (@id, @user, @guild, @xp, @level, @totalXP);");
// Role table for levels
const roleTable = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'roles';").get();
if (!roleTable['count(*)']) {
sql.prepare("CREATE TABLE roles (guildID TEXT, roleID TEXT, level INTEGER);").run();
}
// Prefix table
const prefixTable = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'prefix';").get();
if (!prefixTable['count(*)']) {
sql.prepare("CREATE TABLE prefix (serverprefix TEXT, guild TEXT PRIMARY KEY);").run();
}
// Blacklist table
const blacklistTable = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'blacklistTable';").get();
if (!blacklistTable['count(*)']) {
sql.prepare("CREATE TABLE blacklistTable (guild TEXT, typeId TEXT, type TEXT, id TEXT PRIMARY KEY);").run();
}
// Settings table
const settingsTable = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'settings';").get();
if (!settingsTable['count(*)']) {
sql.prepare("CREATE TABLE settings (guild TEXT PRIMARY KEY, levelUpMessage TEXT, customXP INTEGER, customCooldown INTEGER);").run();
}
const channelTable = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'channel';").get();
if (!channelTable['count(*)']) {
sql.prepare("CREATE TABLE channel (guild TEXT PRIMARY KEY, channel TEXT);").run();
}
// RankCard table (WORK IN PROGRESS, STILL IN THE WORKS)
const rankCardTable = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'rankCardTable';").get();
if (!rankCardTable['count(*)']) {
sql.prepare("CREATE TABLE rankCardTable (id TEXT PRIMARY KEY, user TEXT, guild TEXT, textColor TEXT, barColor TEXT, backgroundColor TEXT);").run();
}
console.log(`Logged in as ${client.user.username}`)
});
// Command Handler
const commandFiles = readdirSync(join(__dirname, "commands")).filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(join(__dirname, "commands", `${file}`));
client.commands.set(command.name, command);
}
// Message Events
client.on("message", (message) => {
if (message.author.bot) return;
if (!message.guild) return;
let card = sql.prepare("SELECT * FROM rankCardTable WHERE user = ? AND guild = ?");
if (!card.get(message.author.id, message.guild.id))
{
sql.prepare("INSERT OR REPLACE INTO rankCardTable (id, user, guild, textColor, barColor, backgroundColor) VALUES (?, ?, ?, ?, ?, ?);").run(`${message.author.id}-${message.guild.id}`, message.author.id, message.guild.id, "#beb1b1", "#838383", "#36393f");
}
const currentPrefix = sql.prepare("SELECT * FROM prefix WHERE guild = ?").get(message.guild.id);
const Prefix = config.prefix;
var getPrefix;
if(!currentPrefix) {
sql.prepare("INSERT OR REPLACE INTO prefix (serverprefix, guild) VALUES (?,?);").run(Prefix, message.guild.id)
getPrefix = Prefix.toString();
} else {
getPrefix = currentPrefix.serverprefix.toString();
}
const escapeRegex = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|${escapeRegex(getPrefix)})\\s*`);
if (!prefixRegex.test(message.content)) return;
const [, matchedPrefix] = message.content.match(prefixRegex);
const args = message.content.slice(matchedPrefix.length).trim().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 (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Discord.Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 1) * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(
`Please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`
);
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply("There was an error executing that command.").catch(console.error);
}
});
// XP Messages
client.on("message", message => {
if (message.author.bot) return;
if (!message.guild) return;
let blacklist = sql.prepare(`SELECT id FROM blacklistTable WHERE id = ?`);
if(blacklist.get(`${message.guild.id}-${message.author.id}`) || blacklist.get(`${message.guild.id}-${message.channel.id}`)) return;
// get level and set level
const level = client.getLevel.get(message.author.id, message.guild.id)
if(!level) {
let insertLevel = sql.prepare("INSERT OR REPLACE INTO levels (id, user, guild, xp, level, totalXP) VALUES (?,?,?,?,?,?);");
insertLevel.run(`${message.author.id}-${message.guild.id}`, message.author.id, message.guild.id, 0, 0, 0)
return;
}
let customSettings = sql.prepare("SELECT * FROM settings WHERE guild = ?").get(message.guild.id);
let channelLevel = sql.prepare("SELECT * FROM channel WHERE guild = ?").get(message.guild.id);
const lvl = level.level;
let getXpfromDB;
let getCooldownfromDB;
if(!customSettings)
{
getXpfromDB = 16; // Default
getCooldownfromDB = 1000;
} else {
getXpfromDB = customSettings.customXP;
getCooldownfromDB = customSettings.customCooldown;
}
// xp system
const generatedXp = Math.floor(Math.random() * getXpfromDB);
const nextXP = level.level * 2 * 250 + 250
// message content or characters length has to be more than 4 characters also cooldown
if(talkedRecently.get(message.author.id)) {
return;
} else { // cooldown is 10 seconds
level.xp += generatedXp;
level.totalXP += generatedXp;
// level up!
if(level.xp >= nextXP) {
level.xp = 0;
level.level += 1;
let levelUpMsg;
let embed = new Discord.MessageEmbed()
.setAuthor(message.author.tag, message.author.displayAvatarURL({ dynamic: true }))
.setColor("RANDOM")
.setThumbnail(message.author.displayAvatarURL({ dynamic: true }))
.setTimestamp();
if(!customSettings)
{
embed.setDescription(`**Congratulations** ${message.author}! You have now leveled up to **level ${level.level}**`);
levelUpMsg = `**Congratulations** ${message.author}! You have now leveled up to **level ${level.level}**`;
} else {
function antonymsLevelUp(string) {
return string
.replace(/{member}/i, `${message.member}`)
.replace(/{xp}/i, `${level.xp}`)
.replace(/{level}/i, `${level.level}`)
}
embed.setDescription(antonymsLevelUp(customSettings.levelUpMessage.toString()));
levelUpMsg = antonymsLevelUp(customSettings.levelUpMessage.toString());
}
// using try catch if bot have perms to send EMBED_LINKS
try {
if(!channelLevel || channelLevel.channel == "Default")
{
message.channel.send(embed);
} else {
let channel = message.guild.channels.cache.get(channelLevel.channel)
const permissionFlags = channel.permissionsFor(message.guild.me);
if(!permissionFlags.has("SEND_MESSAGES") || !permissionFlags.has("VIEW_CHANNEL")) return;
channel.send(embed);
}
} catch (err) {
if(!channelLevel || channelLevel.channel == "Default")
{
message.channel.send(levelUpMsg);
} else {
let channel = message.guild.channels.cache.get(channelLevel.channel)
const permissionFlags = channel.permissionsFor(message.guild.me);
if(!permissionFlags.has("SEND_MESSAGES") || !permissionFlags.has("VIEW_CHANNEL")) return;
channel.send(levelUpMsg);
}
}
};
client.setLevel.run(level);
// add cooldown to user
talkedRecently.set(message.author.id, Date.now() + getCooldownfromDB);
setTimeout(() => talkedRecently.delete(message.author.id, Date.now() + getCooldownfromDB))
}
// level up, time to add level roles
const member = message.member;
let Roles = sql.prepare("SELECT * FROM roles WHERE guildID = ? AND level = ?")
let roles = Roles.get(message.guild.id, lvl)
if(!roles) return;
if(lvl >= roles.level) {
if(roles) {
if (member.roles.cache.get(roles.roleID)) {
return;
}
if(!message.guild.me.hasPermission("MANAGE_ROLES")) {
return
}
member.roles.add(roles.roleID);
}}
})