-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (53 loc) · 1.96 KB
/
index.js
File metadata and controls
63 lines (53 loc) · 1.96 KB
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
// 🌐 Load ENV Variables & Config
require("dotenv").config();
const { Client, GatewayIntentBits, Partials, Collection } = require("discord.js");
const fs = require("fs");
const path = require("path");
// 🔧 Configs & SQLite DB
const config = require("./settings/config.json");
const db = require("./database/db"); // ✅ Using better-sqlite3
const defaultPrefix = process.env.DEFAULT_PREFIX || "%";
// 🤖 Create Discord Client
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.MessageContent
],
partials: [Partials.Channel]
});
// 📂 Load Commands
client.commands = new Collection();
const commandFiles = fs.readdirSync("./commands").filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
// ✅ Bot Ready Event
client.once("ready", () => {
console.log(`✅ ${client.user.username} is online!`);
client.user.setActivity("your voice ✨", { type: "LISTENING" });
});
// 💬 Message Handler
client.on("messageCreate", async message => {
if (message.author.bot || !message.guild) return;
// 🔑 Get Prefix from DB or use default
let prefixRow = db.prepare("SELECT prefix FROM prefixes WHERE guild_id = ?").get(message.guild.id);
const prefix = prefixRow?.prefix || defaultPrefix;
if (!message.content.startsWith(prefix)) return;
// 🧠 Parse command and arguments
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const cmd = args.shift()?.toLowerCase();
const command = client.commands.get(cmd);
if (command) {
try {
await command.run(client, message, args);
} catch (err) {
console.error(`❌ Error running command ${cmd}:`, err);
message.reply("An error occurred while executing that command.");
}
}
});
// 🔐 Login to Discord
client.login(process.env.DISCORD_TOKEN);