Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit 8c1c902

Browse files
database query completed
1 parent a98734f commit 8c1c902

File tree

2 files changed

+89
-77
lines changed

2 files changed

+89
-77
lines changed

src/commands/main/log.ts

Lines changed: 83 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
/* import { SlashCommandBuilder, EmbedBuilder, ChatInputCommandInteraction } from "discord.js";
2-
import mysql from "mysql";
3-
import { KOGBot } from "index.ts";
4-
import { SlashCommand } from "main.d.ts";
5-
6-
const allowedroleID = ""; // Set this later when I have perms
7-
const logchannel = ""; // log channel
1+
/* import { SlashCommandBuilder, EmbedBuilder, ChatInputCommandInteraction } from 'discord.js';
2+
import mysql from 'mysql';
3+
import { KOGBot } from 'index.ts';
4+
import { SlashCommand } from 'main.d.ts';
5+
import config from '../../config';
6+
7+
const allowedroleID = config.discord.mr_role; // Set this later when I have perms
8+
const logchannel = config.discord.log_channel; // log channel
89
const connection = mysql.createConnection({
9-
// database connection details
10+
host: config.database.host,
11+
port: config.database.port,
12+
user: config.database.user,
13+
password: config.database.password,
14+
database: config.database.schema,
1015
});
1116
1217
class LogEventCommand implements SlashCommand {
13-
name = "log";
14-
description = "Logs official events for KOG.";
18+
name = 'log';
19+
description = 'Logs official events for KOG.';
1520
subcommands = [];
1621
parameters = [];
1722
dev = true;
@@ -26,8 +31,8 @@ class LogEventCommand implements SlashCommand {
2631
// Check for required role
2732
if (!interaction.member?.roles.cache.has(allowedroleID)) {
2833
const noperms = new EmbedBuilder()
29-
.setColor("#E73A3A")
30-
.setTitle("Error")
34+
.setColor('#E73A3A')
35+
.setTitle('Error')
3136
.setDescription("You don't have the required role to use this command.")
3237
.setTimestamp();
3338
@@ -36,9 +41,9 @@ class LogEventCommand implements SlashCommand {
3641
}
3742
3843
const logStart = new EmbedBuilder()
39-
.setColor("#9033FF")
40-
.setTitle("Logging")
41-
.setDescription("To log an event, please follow the format:\n\n<@user1>,<@user2>,<@user3>...\n\nNames must be separated by commas and must be mentions.")
44+
.setColor('#9033FF')
45+
.setTitle('Logging')
46+
.setDescription('To log an event, please follow the format:\n\n<@user1>,<@user2>,<@user3>...\n\nNames must be separated by commas and must be mentions.')
4247
.setTimestamp();
4348
4449
await interaction.reply({ embeds: [logStart], ephemeral: true });
@@ -50,7 +55,7 @@ class LogEventCommand implements SlashCommand {
5055
const mentionRegexthing = /^<@\d+>(?:,\s?<@\d+>)*$/;
5156
5257
if (!mentionRegexthing.test(response!)) {
53-
await interaction.followUp("Invalid format. Please make sure the names are separated by commas and each name is a mention. Run the command again with the correct format.");
58+
await interaction.followUp('Invalid format. Please make sure the names are separated by commas and each name is a mention. Run the command again with the correct format.');
5459
return;
5560
}
5661
@@ -73,16 +78,20 @@ class LogEventCommand implements SlashCommand {
7378
}
7479
7580
if (userIds.length === 0) {
76-
await interaction.reply("No users mentioned. Please try again.");
81+
await interaction.reply('No users mentioned. Please try again.');
7782
return;
7883
}
7984
85+
86+
const host = interaction.user.id;
87+
userIds.push(host);
88+
8089
// Log the event in the log channel
8190
const timestamp = Math.floor(Date.now() / 1000);
8291
const logEmbed = new EmbedBuilder()
83-
.setColor("#9033FF")
84-
.setTitle("Log Event")
85-
.setDescription(`A new event was logged.\n\nHost: ${interaction.user.id}\n\nTime: <t:${timestamp}:F>\n\nAttendees: ${mentions.map(id => `<@${id}>`).join(', ')}\n\nSquadron Rally: False`)
92+
.setColor('#9033FF')
93+
.setTitle('Event log')
94+
.setDescription(`A new event was logged.\n\nHost: <@${host}>\n\nTime: <t:${timestamp}:F>\n\nAttendees: ${mentions.map(id => `<@${id}>`).join(', ')}\n\nSquadron Rally: False`)
8695
.setTimestamp();
8796
8897
const logChannel = await interaction.client.channels.fetch(logchannel);
@@ -91,72 +100,71 @@ class LogEventCommand implements SlashCommand {
91100
}
92101
93102
for (const userId of userIds) {
94-
connection.query(
95-
'SELECT COUNT(*) AS eventCount FROM events WHERE userId = ?',
96-
[userId],
97-
async (err, results) => {
98-
if (err) {
99-
console.error(err);
100-
await interaction.followUp("There was an error querying the database.");
101-
return;
102-
}
103+
connection.query('SELECT * FROM KOGDB WHERE userId = ?', [userId], async (err, results) => {
104+
if (err) {
105+
console.error(err);
106+
await interaction.followUp('There was an error querying the database.');
107+
return;
108+
}
103109
104-
const eventCount = results[0]?.eventCount || 0;
105-
let promotionEmbed: any;
106-
107-
if (eventCount === 5) {
108-
promotionEmbed = new EmbedBuilder()
109-
.setColor("#FFBF00")
110-
.setTitle("Promotion Needed")
111-
.setDescription(`<@${userId}> has reached 5 events! This user needs promoting.`)
112-
.setTimestamp();
113-
} else if (eventCount >= 10) {
114-
promotionEmbed = new EmbedBuilder()
115-
.setColor("#FFD700")
116-
.setTitle("Promotion Needed")
117-
.setDescription(`<@${userId}> has reached 10 events! Consider promoting them.`)
118-
.setTimestamp();
110+
if (results.length > 0) {
111+
112+
if (userId === host) {
113+
connection.query('UPDATE KOGDB SET eventsAttended = eventsAttended + 1, eventsHosted = eventsHosted + 1 WHERE userId = ?', [userId], async (updateErr) => {
114+
if (updateErr) {
115+
console.error(updateErr);
116+
await interaction.followUp('There was an error updating the database.');
117+
return;
118+
}
119+
});
120+
} else {
121+
connection.query('UPDATE KOGDB SET eventsAttended = eventsAttended + 1 WHERE userId = ?', [userId], async (updateErr) => {
122+
if (updateErr) {
123+
console.error(updateErr);
124+
await interaction.followUp('There was an error updating the database.');
125+
return;
126+
}
127+
});
119128
}
120-
121-
if (promotionEmbed && logChannel?.isText()) {
122-
await logChannel.send({ embeds: [promotionEmbed] });
129+
} else {
130+
131+
if (userId === host) {
132+
connection.query('INSERT INTO KOGDB (userId, eventsAttended, eventsHosted) VALUES (?, 1, 1)', [userId], async (insertErr) => {
133+
if (insertErr) {
134+
console.error(insertErr);
135+
await interaction.followUp('There was an error updating the database.');
136+
return;
137+
}
138+
});
139+
} else {
140+
connection.query('INSERT INTO KOGDB (userId, eventsAttended, eventsHosted) VALUES (?, 1, 0)', [userId], async (insertErr) => {
141+
if (insertErr) {
142+
console.error(insertErr);
143+
await interaction.followUp('There was an error updating the database.');
144+
return;
145+
}
146+
});
123147
}
124148
}
125-
);
149+
});
126150
}
127151
128-
// database updating goes here
129-
130-
const isDatabaseUpdated = true; // "oh its updated"
131-
132-
if (isDatabaseUpdated) {
133-
const dbEmbed = new EmbedBuilder()
134-
.setColor("#9033FF")
135-
.setTitle("Log Event")
136-
.setDescription("Database updated, event has been logged successfully.")
137-
.setTimestamp();
138-
139-
if (logChannel) {
140-
await logChannel.send({ embeds: [dbEmbed] });
141-
}
142-
} else {
143-
const errorEmbed = new EmbedBuilder()
144-
.setColor("#E73A3A")
145-
.setTitle("Error")
146-
.setDescription("An error occurred while updating the DB. The database has not been updated, and the log has failed.\nContact the admin for support.")
147-
.setTimestamp();
152+
const dbEmbed = new EmbedBuilder()
153+
.setColor('#9033FF')
154+
.setTitle('Log Event')
155+
.setDescription('Database updated, event has been logged successfully.')
156+
.setTimestamp();
148157
149-
if (logChannel) {
150-
await logChannel.send({ embeds: [errorEmbed] });
151-
}
158+
if (logChannel) {
159+
await logChannel.send({ embeds: [dbEmbed] });
152160
}
153161
154162
} catch (error) {
155163
console.log(error);
156164
const errorEmbed = new EmbedBuilder()
157-
.setColor("#E73A3A")
158-
.setTitle("Error")
159-
.setDescription("An error occurred while executing this command.")
165+
.setColor('#E73A3A')
166+
.setTitle('Error')
167+
.setDescription('An error occurred while executing this command.')
160168
.setTimestamp();
161169
162170
await interaction.reply({ embeds: [errorEmbed], ephemeral: true });
@@ -166,5 +174,4 @@ class LogEventCommand implements SlashCommand {
166174
167175
export default LogEventCommand;
168176
169-
*/
170-
177+
*/

src/commands/main/mydata.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
import mysql from "mysql";
33
import { KOGBot } from "index.ts";
44
import { SlashCommand } from "main.d.ts";
5+
import config from "../../config"
56
67
const connection = mysql.createConnection({
7-
// cool connections goes here
8+
host: config.database.host,
9+
port: config.database.port,
10+
user: config.database.user,
11+
password: config.database.password,
12+
database: config.database.schema
813
});
914
1015
class GetDataCommand implements SlashCommand {

0 commit comments

Comments
 (0)