parent
f2d39cce0f
commit
2a188a91f4
15
src/bot.ts
15
src/bot.ts
|
@ -27,7 +27,7 @@ export class Bot {
|
||||||
|
|
||||||
public init() {
|
public init() {
|
||||||
const client = new Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]});
|
const client = new Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]});
|
||||||
this.connectDatabase()
|
this.connectDatabase(true);
|
||||||
|
|
||||||
this.registerCommands();
|
this.registerCommands();
|
||||||
const commands: Collection<any, any> = new Collection();
|
const commands: Collection<any, any> = new Collection();
|
||||||
|
@ -105,7 +105,7 @@ export class Bot {
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
|
|
||||||
private connectDatabase() {
|
private connectDatabase(closeConnection: boolean) {
|
||||||
this.connection = sql.createConnection({
|
this.connection = sql.createConnection({
|
||||||
host: config.dbHost,
|
host: config.dbHost,
|
||||||
user: config.dbUser,
|
user: config.dbUser,
|
||||||
|
@ -115,9 +115,14 @@ export class Bot {
|
||||||
})
|
})
|
||||||
|
|
||||||
this.connection.query('CREATE TABLE if NOT EXISTS facts (id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, content TEXT NOT NULL, active TINYINT(1) NOT NULL DEFAULT 1)');
|
this.connection.query('CREATE TABLE if NOT EXISTS facts (id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, content TEXT NOT NULL, active TINYINT(1) NOT NULL DEFAULT 1)');
|
||||||
|
|
||||||
|
if (closeConnection) {
|
||||||
|
this.connection.end();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getConnection() {
|
public getConnection() {
|
||||||
|
this.connectDatabase(false);
|
||||||
return this.connection;
|
return this.connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,7 +135,9 @@ export class Bot {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async sendEmbed(msg: Message<boolean> | ChatInputCommandInteraction) {
|
public async sendEmbed(msg: Message<boolean> | ChatInputCommandInteraction) {
|
||||||
const [results] = await this.connection.promise().execute('SELECT * FROM facts;');
|
const [results] = await this.getConnection().promise().execute('SELECT * FROM facts;');
|
||||||
|
this.connection.end();
|
||||||
|
|
||||||
let fields = [];
|
let fields = [];
|
||||||
let embeds = [];
|
let embeds = [];
|
||||||
for (let index in results) {
|
for (let index in results) {
|
||||||
|
@ -154,7 +161,7 @@ export class Bot {
|
||||||
inline: true,
|
inline: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (fields.length < 27) {
|
if (fields.length < 24) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ export default {
|
||||||
'INSERT INTO facts SET content = ?, active = ?;',
|
'INSERT INTO facts SET content = ?, active = ?;',
|
||||||
[interaction.options.data.at(0).value, interaction.options.data.at(1)?.value ?? true]
|
[interaction.options.data.at(0).value, interaction.options.data.at(1)?.value ?? true]
|
||||||
);
|
);
|
||||||
|
bot.getConnection().end();
|
||||||
console.log('Fact has been saved!');
|
console.log('Fact has been saved!');
|
||||||
|
|
||||||
return await interaction.reply({
|
return await interaction.reply({
|
||||||
|
|
|
@ -24,7 +24,7 @@ export default {
|
||||||
'UPDATE facts SET active = 1 WHERE id = ?;',
|
'UPDATE facts SET active = 1 WHERE id = ?;',
|
||||||
[interaction.options.data.at(0).value]
|
[interaction.options.data.at(0).value]
|
||||||
);
|
);
|
||||||
|
bot.getConnection().end();
|
||||||
console.log('Fact has been enabled!');
|
console.log('Fact has been enabled!');
|
||||||
|
|
||||||
return await interaction.reply({
|
return await interaction.reply({
|
||||||
|
|
|
@ -12,6 +12,7 @@ export default {
|
||||||
const bot = Bot.getInstance();
|
const bot = Bot.getInstance();
|
||||||
const [results] = await bot.getConnection().promise().query('SELECT * FROM facts WHERE active = 1 ORDER BY RAND() LIMIT 1;');
|
const [results] = await bot.getConnection().promise().query('SELECT * FROM facts WHERE active = 1 ORDER BY RAND() LIMIT 1;');
|
||||||
let fact = results[0];
|
let fact = results[0];
|
||||||
|
bot.getConnection().end();
|
||||||
console.log('Sent beaver fact with id: ' + fact.id);
|
console.log('Sent beaver fact with id: ' + fact.id);
|
||||||
|
|
||||||
return await interaction.reply({
|
return await interaction.reply({
|
||||||
|
|
|
@ -41,6 +41,7 @@ export default {
|
||||||
|
|
||||||
console.log('Fact has been disabled!');
|
console.log('Fact has been disabled!');
|
||||||
}
|
}
|
||||||
|
bot.getConnection().end();
|
||||||
|
|
||||||
return await interaction.reply({
|
return await interaction.reply({
|
||||||
content: 'Done!',
|
content: 'Done!',
|
||||||
|
|
Loading…
Reference in New Issue