Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge dev-beg to dev #67

Merged
merged 9 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Run Tests

on:
pull_request:
branches: [ main ]
branches: [ main, dev ]

jobs:
test:
Expand Down
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"program": "${workspaceFolder}/deploy-commands.js"
},
{
"name": "Run unit test(s).",
"name": "Run unit tests.",
"type": "node",
"request": "launch",
"runtimeArgs": [
Expand All @@ -35,7 +35,7 @@
"internalConsoleOptions": "neverOpen"
},
{
"name": "(Re)create SQL tables.",
"name": "Create SQL tables.",
"type": "node",
"request": "launch",
"skipFiles": [
Expand Down
101 changes: 101 additions & 0 deletions commands/economy/beg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const { SlashCommandBuilder } = require("discord.js");
const { embedReplySuccessColor, embedReplyWarningColor, embedReplyFailureColor } = require("../../helpers/embed-reply");
const { logToFileAndDatabase } = require("../../helpers/logger");
const db = require("../../helpers/db");

module.exports = {
data: new SlashCommandBuilder()
.setName("beg")
.setDescription("Lets you beg for a random (or no) amount of money.")
.setDMPermission(false),
async execute(interaction) {
const query = await db.query("SELECT userId, lastBegTime, balance FROM economy WHERE userId = ?", [interaction.user.id]);
const userId = query[0]?.userId || null;
const lastBegTime = query[0]?.lastBegTime || null;
const balance = query[0]?.balance || null;
const nextApprovedBegTimeUTC = new Date(new Date().getTime() + new Date().getTimezoneOffset() * 60000 - 10 * 60000); //10 minutes

const outcomeChance = Math.floor(Math.random() * 100);
const amount = Math.floor(Math.random() * 85);

if (userId) {
if (!lastBegTime || lastBegTime <= nextApprovedBegTimeUTC) {
//60% chance for getting some money
if (outcomeChance < 60 || balance <= 100) {
await db.query("UPDATE economy SET balance = balance + ?, lastBegTime = ? WHERE userId = ?",
[
amount,
new Date().toISOString().slice(0, 19).replace('T', ' '),
userId
]
);

var embedReply = embedReplySuccessColor(
"Begging.",
`You've begged and some random guy gave you \`$${amount}\` dollars.`,
interaction
);
}
//30% chance for getting nothing
else if (outcomeChance < 90 || balance <= 100) {
var embedReply = embedReplyWarningColor(
"Begging.",
`While you were begging on the street, a random guy just kicked you in the balls and left you alone with nothing.`,
interaction
);
}
//10% chance for loosing money
else {
await db.query("UPDATE economy SET balance = balance - ?, lastBegTime = ? WHERE userId = ?",
[
amount,
new Date().toISOString().slice(0, 19).replace('T', ' '),
userId
]
);

var embedReply = embedReplyFailureColor(
"Begging.",
`While you were begging near a trash can, a random guy (with a dark skin tone) took the coins from you cup, then ran away.\nYou've lost \`$${amount}\` dollars.`,
interaction
);
}
}
else {
const remainingTimeInSeconds = Math.ceil((lastBegTime.getTime() - nextApprovedBegTimeUTC.getTime()) / 1000);
const remainingMinutes = Math.floor(remainingTimeInSeconds / 60);
const remainingSeconds = remainingTimeInSeconds % 60;

var embedReply = embedReplyFailureColor(
"Begging - Error",
`You've already begged in the last 10 minutes.\nPlease wait **${remainingMinutes} minute(s)** and **${remainingSeconds} second(s)** before trying to **beg** again.`,
interaction
);
}
}
else {
//if it's the executor's first time using any economy command (so it's userId is not in the database yet...)
await db.query("INSERT INTO economy (userName, userId, balance, firstTransactionDate, lastBegTime) VALUES (?, ?, ?, ?, ?)",
[
interaction.user.username,
interaction.user.id,
amount,
new Date().toISOString().slice(0, 19).replace('T', ' '),
new Date().toISOString().slice(0, 19).replace('T', ' ')
]
);

var embedReply = embedSuccessColor(
"Begging.",
`You've begged and some random guy gave you \`$${amount}\` dollars.`,
interaction
);
}

await interaction.reply({ embeds: [embedReply] });

//logging
const response = JSON.stringify(embedReply.toJSON());
await logToFileAndDatabase(interaction, response);
}
}
2 changes: 1 addition & 1 deletion commands/economy/work.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
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;
const nextApprovedWorkTimeUTC = new Date(new Date().getTime() + new Date().getTimezoneOffset() * 60000 - 5 * 60000);
const nextApprovedWorkTimeUTC = new Date(new Date().getTime() + new Date().getTimezoneOffset() * 60000 - 5 * 60000); //5 minutes

const amount = Math.floor(Math.random() * 100);
if (userId) {
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions sql/economy/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ CREATE TABLE IF NOT EXISTS `economy` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`userName` text DEFAULT NULL,
`userId` bigint(20) NOT NULL,
`balance` bigint(20) DEFAULT 0 NOT NULL,
`balanceInBank` bigint(20) DEFAULT 0 NOT NULL,
`balance` bigint(20) DEFAULT 0,
`balanceInBank` bigint(20) DEFAULT 0,
`firstTransactionDate` datetime NOT NULL DEFAULT current_timestamp(),
`lastWorkTime` datetime DEFAULT NULL,
`lastBegTime` datetime DEFAULT NULL,
`lastRobTime` datetime DEFAULT NULL,
`lastRouletteTime` datetime DEFAULT NULL,
`lastDepositTime` datetime DEFAULT NULL,
Expand Down
Loading