Holana/examples/custom-databases/mysql.js

144 lines
3.8 KiB
JavaScript
Raw Normal View History

2022-12-02 08:36:14 +00:00
const Discord = require("discord.js");
const client = new Discord.Client({
intents: [
Discord.IntentsBitField.Flags.Guilds,
Discord.IntentsBitField.Flags.GuildMessageReactions,
],
});
// Load mysql
const MySQL = require("mysql");
const sql = MySQL.createConnection({
host: "localhost",
user: "Your MySQL user",
password: "Your MySQL password",
database: "Your MySQL database name",
charset: "utf8mb4", // In order to save emojis correctly
});
sql.connect((err) => {
if (err) {
// Stop the process if we can't connect to the MySQL server
throw new Error("Impossible to connect to MySQL server. Code: " + err.code);
} else {
console.log(
"[SQL] Connected to the MySQL server! Connection ID: " + sql.threadId
);
}
});
// Create giveaways table
sql.query(
`
CREATE TABLE IF NOT EXISTS \`giveaways\`
(
\`id\` INT(1) NOT NULL AUTO_INCREMENT,
\`message_id\` VARCHAR(20) NOT NULL,
\`data\` JSON NOT NULL,
PRIMARY KEY (\`id\`)
);
`,
(err) => {
if (err) console.error(err);
console.log("[SQL] Created table `giveaways`");
}
);
const { GiveawaysManager } = require("discord-giveaways");
const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {
// This function is called when the manager needs to get all giveaways which are stored in the database.
async getAllGiveaways() {
return new Promise((resolve, reject) => {
sql.query("SELECT `data` FROM `giveaways`", (err, res) => {
if (err) {
console.error(err);
return reject(err);
}
const giveaways = res.map((row) =>
JSON.parse(row.data, (_, v) =>
typeof v === "string" && /BigInt\("(-?\d+)"\)/.test(v) ? eval(v) : v
)
);
resolve(giveaways);
});
});
}
// This function is called when a giveaway needs to be saved in the database.
async saveGiveaway(messageId, giveawayData) {
return new Promise((resolve, reject) => {
sql.query(
"INSERT INTO `giveaways` (`message_id`, `data`) VALUES (?,?)",
[
messageId,
JSON.stringify(giveawayData, (_, v) =>
typeof v === "bigint" ? `BigInt("${v}")` : v
),
],
(err, res) => {
if (err) {
console.error(err);
return reject(err);
}
resolve(true);
}
);
});
}
// This function is called when a giveaway needs to be edited in the database.
async editGiveaway(messageId, giveawayData) {
return new Promise((resolve, reject) => {
sql.query(
"UPDATE `giveaways` SET `data` = ? WHERE `message_id` = ?",
[
JSON.stringify(giveawayData, (_, v) =>
typeof v === "bigint" ? `BigInt("${v}")` : v
),
messageId,
],
(err, res) => {
if (err) {
console.error(err);
return reject(err);
}
resolve(true);
}
);
});
}
// This function is called when a giveaway needs to be deleted from the database.
async deleteGiveaway(messageId) {
return new Promise((resolve, reject) => {
sql.query(
"DELETE FROM `giveaways` WHERE `message_id` = ?",
messageId,
(err, res) => {
if (err) {
console.error(err);
return reject(err);
}
resolve(true);
}
);
});
}
};
// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
default: {
botsCanWin: false,
embedColor: "#FF0000",
embedColorEnd: "#000000",
reaction: "🎉",
},
});
// We now have a giveawaysManager property to access the manager everywhere!
client.giveawaysManager = manager;
client.on("ready", () => {
console.log("Bot is ready!");
});
client.login(process.env.DISCORD_BOT_TOKEN);