Skip to content

Commit

Permalink
fixed queries in work.js + modified index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
vb2007 committed Jun 30, 2024
1 parent f719df3 commit c842caa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
13 changes: 5 additions & 8 deletions commands/economy/work.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ module.exports = {
.setName("work")
.setDescription("Gives you a random amount of money."),
async execute(interaction) {
const interactionUserId = interaction.user.id;

const query = await db.query("SELECT userId, lastWorkTime FROM economy WHERE userId = ?", [interactionUserId]);

const query = await db.query("SELECT userId, lastWorkTime FROM economy WHERE userId = ?", [interaction.user.id]);
const userId = query[0]?.userId || null;
const lastWorkTime = query[0]?.lastWorkTime || null; //lastWorkTime is stored as UTC
// const now = new Date();
const thirtyMinutesAgoUTC = new Date(new Date().getTime() + new Date().getTimezoneOffset() * 60000 - 30 * 60000);

const amount = Math.floor(Math.random() * 100);
Expand All @@ -36,14 +32,15 @@ module.exports = {
}
else {
//if it's a user's first time using this command (so it's userId is not in the database yet...)
await db.query("INSERT INTO economy (userId, balance, lastWorkTime) VALUES (?, ?, ?)",
await db.query("INSERT INTO economy (userName, userId, balance, firstTransactionId, lastWorkTime) VALUES (?, ?, ?, ?, ?)",
[
interactionUserId,
interaction.user.username,
interaction.user.id,
amount,
new Date().toISOString().slice(0, 19).replace('T', ' '),
new Date().toISOString().slice(0, 19).replace('T', ' '),
]
);

var replyContent = `You've worked and succesfully earned $**${amount}** dollars.`;
}

Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ function setActivity() {

client.on("ready", () => {
setActivity();
})
});

//re-announces the bot's activity in every 20 minutes (in case of an internet outage or something)
setInterval(setActivity, 20 * 60 * 1000);

//closes connection to the database when closing the application
client.on("SIGINT", () => {
process.on("SIGINT", () => {
console.log("Closing MariaDB database pool connection(s)...");
db.end();
client.exit(0);
})
process.exit(0);
});

//logs in with given token
client.login(token);

0 comments on commit c842caa

Please sign in to comment.