This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mysql.js
138 lines (124 loc) · 4.84 KB
/
mysql.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
const Discord = require('discord.js');
const client = new Discord.Client({
intents: [Discord.IntentsBitField.Flags.Guilds, Discord.IntentsBitField.Flags.GuildMembers]
});
// 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: {
buttonEmoji: '🎉',
buttonStyle: Discord.ButtonStyle.Secondary,
embedColor: '#FF0000',
embedColorEnd: '#000000',
}
});
// We now have a giveawaysManager property to access the manager everywhere!
client.giveawaysManager = manager;
client.giveawaysManager.on('giveawayJoined', (giveaway, member, interaction) => {
if (!giveaway.isDrop) return interaction.reply({ content: `:tada: Congratulations **${member.user.username}**, you have joined the giveaway`, ephemeral: true })
interaction.reply({ content: `:tada: Congratulations **${member.user.username}**, you have joined the drop giveaway`, ephemeral: true })
});
client.giveawaysManager.on('giveawayLeaved', (giveaway, member, interaction) => {
if (!giveaway.isDrop) return interaction.reply({ content: `**${member.user.username}**, you have left the giveaway`, ephemeral: true })
interaction.reply({ content: `**${member.user.username}**, you have left the drop giveaway`, ephemeral: true })
});
client.on('ready', () => {
console.log('Bot is ready!');
});
client.login(process.env.DISCORD_BOT_TOKEN);