Skip to content

Commit 533f0d7

Browse files
committed
Added MySQL and fix reload
1 parent d234e85 commit 533f0d7

File tree

8 files changed

+1142
-49
lines changed

8 files changed

+1142
-49
lines changed

package-lock.json

Lines changed: 1003 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"dependencies": {
2424
"chalk": "^5.1.2",
2525
"discord.js": "^14.6.0",
26-
"fs": "^0.0.1-security"
26+
"fs": "^0.0.1-security",
27+
"mysql2": "^2.3.3",
28+
"util": "^0.12.5"
2729
}
2830
}

src/commands/reload.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import commandHandler from '../handlers/commands.js';
2+
import config from '../data/config.js';
23

34
export default {
45
name: 'reload',
56
description: 'Reloads all commands',
6-
aliases: [],
7+
aliases: ['rl'],
78

89
async execute(message, args, client) {
910
try {
11+
if (!message.member.id == `${config.bot.eval}`) return message.reply('You do not have the permission to use this command!');
1012
let cmd = await commandHandler.reloadCommands(client);
1113
message.reply(`${cmd} commands have been reloaded!`);
1214
} catch (error) {

src/commands/setprefix.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Discord from 'discord.js';
2+
import util from 'util';
3+
import mysql from 'mysql2';
4+
import config from '../data/config.js';
5+
6+
var con = mysql.createPool({
7+
host: `${config.mysql.host}`,
8+
port: `${config.mysql.port}`,
9+
user: `${config.mysql.user}`,
10+
password: `${config.mysql.password}`,
11+
database: `${config.mysql.database}`,
12+
multipleStatements: true
13+
});
14+
const dbquery = util.promisify(con.query).bind(con);
15+
16+
export default {
17+
name: 'setprefix',
18+
description: 'Reloads all commands',
19+
aliases: [],
20+
21+
async execute(message, args) {
22+
try {
23+
if (!message.member.permissions.has('ADMINISTRATOR')) return message.reply('You do not have the permission to use this command!');
24+
25+
let rows = await dbquery(`SELECT * FROM guilds WHERE guildid = '${message.guild.id}'`);
26+
27+
if (!args[1]) return message.reply(`Use ${rows[0].prefix}setprefix <prefix>`);
28+
29+
await dbquery(`UPDATE guilds SET prefix = '${args[1]}' WHERE guildid = '${message.guild.id}'`);
30+
31+
const embed = new Discord.EmbedBuilder()
32+
.setTitle('Prefix changed!')
33+
.setDescription(`The prefix has been changed to ${args[1]}`)
34+
.setColor('149C51');
35+
36+
message.reply({embeds: [embed]});
37+
} catch (error) {
38+
console.log(error);
39+
}
40+
}
41+
};

src/data/config.js.template

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
const config = {
2-
token: 'TOKEN',
2+
bot: {
3+
token: 'TOKEN',
4+
intents: 'INTENT',
5+
eval: '0',
6+
},
7+
mysql: {
8+
host: 'HOST',
9+
port: 'PORT',
10+
user: 'USER',
11+
password: 'PASSWORD',
12+
database: 'DATABASE'
13+
},
314
};
415

516
export default config;

src/events/messageCreate.js

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
1+
import util from 'util';
2+
import mysql from 'mysql2';
3+
import config from '../data/config.js';
4+
5+
var con = mysql.createPool({
6+
host: `${config.mysql.host}`,
7+
port: `${config.mysql.port}`,
8+
user: `${config.mysql.user}`,
9+
password: `${config.mysql.password}`,
10+
database: `${config.mysql.database}`,
11+
multipleStatements: true
12+
});
13+
const dbquery = util.promisify(con.query).bind(con);
14+
115
export default {
216
event: 'messageCreate',
317
async execute(client, message) {
4-
let prefix = '!'; //await getprefix(message.guild.id); // MySQL WIP
5-
6-
if (message.content.startsWith(prefix)) {
7-
const args = message.content.substring(prefix.length).split(/ +/);
8-
const command = client.commands.find(cmd => cmd.name == args[0] || cmd.aliases.includes(args[0]));
9-
if (!command) return; //message.reply(`${args[0]} is not a valid command!`); //uncomment if you want that the bot replies when the command is not a valid command!
10-
command.execute(message, args, client);
11-
} else {
12-
// Here you can add commands that are not have a prefix.
13-
// like when somebody pings the bot.
18+
try {
19+
if (message.author.bot) return;
20+
let prefix = await getPrefix(message.guild.id);
21+
22+
if (message.content.startsWith(prefix)) {
23+
const args = message.content.substring(prefix.length).split(/ +/);
24+
const command = client.commands.find(cmd => cmd.name == args[0] || cmd.aliases.includes(args[0]));
25+
if (!command) return; //message.reply(`${args[0]} is not a valid command!`); //uncomment if you want that the bot replies when the command is not a valid command!
26+
command.execute(message, args, client);
27+
} else {
28+
// Here you can add commands that are not have a prefix.
29+
// like when somebody pings the bot.
30+
}
31+
} catch (error) {
32+
console.log(error);
1433
}
1534
}
16-
};
35+
};
36+
37+
async function getPrefix(guildid) {
38+
let rows = await dbquery(`SELECT prefix FROM guilds WHERE guildid = ${guildid}`);
39+
if (rows.length < 1) {
40+
await dbquery(`INSERT IGNORE INTO guilds (id, guildid) VALUES (NULL, '${guildid}')`);
41+
return '!';
42+
}
43+
if (rows[0].prefix == null) {
44+
await dbquery(`UPDATE guilds SET prefix = '!' WHERE guildid = ${guildid}`);
45+
return '!';
46+
}
47+
return rows[0].prefix;
48+
}

src/handlers/commands.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ async function loadCommands(client) {
1717
}
1818

1919
async function reloadCommands(client) {
20-
client.commands.clear();
21-
client.aliases.clear();
20+
await client.commands.clear();
21+
await client.aliases.clear();
2222
const commandFiles = readdirSync('./src/commands').filter(file => file.endsWith('.js'));
2323
for (let i = 0; i < commandFiles.length; i++) {
24-
const cmd = await import(`../commands/${commandFiles[i]}`);
25-
client.commands.set(cmd.default.name, cmd.default);
26-
console.log(chalk.greenBright(`[COMMAND] Reloaded ${(chalk.yellow(commandFiles[i]))} with command ${(chalk.yellow(cmd.default.name))} ${(chalk.yellow(`[${cmd.default.aliases}]`))}`));
24+
const cmd = await import(`../commands/${commandFiles[i]}?${Date.now()}`);
25+
await client.commands.set(cmd.default.name, cmd.default);
26+
console.log(chalk.greenBright(`[COMMAND] Reloaded ${chalk.yellow(commandFiles[i])} with command ${chalk.yellow(cmd.default.name)} ${chalk.yellow(`[${cmd.default.aliases}]`)}`));
2727

2828
if (cmd.default.aliases) {
29-
cmd.default.aliases.forEach(alias => {
30-
client.aliases.set(alias, cmd.default);
29+
cmd.default.aliases.forEach(async (alias) => {
30+
await client.aliases.set(alias, cmd.default);
3131
});
3232
}
3333
}

src/index.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
11
import Discord from 'discord.js';
22
import config from './data/config.js';
3+
import chalk from 'chalk';
4+
import util from 'util';
5+
import mysql from 'mysql2';
36

47
import commandHandler from './handlers/commands.js';
58
import eventHandler from './handlers/events.js';
69

710
console.clear();
811

12+
var con = mysql.createPool({
13+
host: `${config.mysql.host}`,
14+
port: `${config.mysql.port}`,
15+
user: `${config.mysql.user}`,
16+
password: `${config.mysql.password}`,
17+
database: `${config.mysql.database}`,
18+
multipleStatements: true
19+
});
20+
const dbquery = util.promisify(con.query).bind(con);
21+
22+
// Check if Table Exists if not the bot will create it
23+
await dbquery('SHOW TABLES LIKE \'guilds\'').then(async (rows) => {
24+
if (rows.length < 1) {
25+
dbquery(`CREATE TABLE guilds (
26+
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
27+
guildid VARCHAR(50) NULL DEFAULT '0' COLLATE 'utf8mb4_0900_ai_ci',
28+
prefix VARCHAR(50) NULL DEFAULT '!' COLLATE 'utf8mb4_0900_ai_ci',
29+
PRIMARY KEY (id) USING BTREE,
30+
UNIQUE INDEX guildid (guildid) USING BTREE
31+
);`);
32+
console.log(chalk.yellow('[MySQL] Created table guilds'));
33+
} else console.log(chalk.green('[MySQL] Table guilds exists'));
34+
});
35+
936
const client = new Discord.Client({
10-
intents: 3276799,
37+
intents: config.bot.intents,
1138
allowedMentions: {
1239
repliedUser: true
13-
},
40+
}
1441
});
1542

1643
client.commands = new Discord.Collection();
@@ -23,4 +50,4 @@ process.on('uncaughtException', function (err) {
2350
console.error(err);
2451
});
2552

26-
client.login(`${config.token}`);
53+
client.login(`${config.bot.token}`);

0 commit comments

Comments
 (0)