Add cron job to add coins to a user every 30 minutes #219
6 changed files with 113 additions and 2 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -109,3 +109,4 @@ ormconfig.json
|
||||||
gdrive-credentials.json
|
gdrive-credentials.json
|
||||||
data/
|
data/
|
||||||
*.db
|
*.db
|
||||||
|
.temp/
|
23
package-lock.json
generated
23
package-lock.json
generated
|
@ -16,6 +16,7 @@
|
||||||
"@types/uuid": "^9.0.0",
|
"@types/uuid": "^9.0.0",
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
"clone-deep": "^4.0.1",
|
"clone-deep": "^4.0.1",
|
||||||
|
"cron": "^3.1.7",
|
||||||
"discord.js": "^14.3.0",
|
"discord.js": "^14.3.0",
|
||||||
"dotenv": "^16.0.0",
|
"dotenv": "^16.0.0",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
|
@ -1895,6 +1896,11 @@
|
||||||
"@types/node": "*"
|
"@types/node": "*"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/luxon": {
|
||||||
|
"version": "3.4.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.4.2.tgz",
|
||||||
|
"integrity": "sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA=="
|
||||||
|
},
|
||||||
"node_modules/@types/mime": {
|
"node_modules/@types/mime": {
|
||||||
"version": "3.0.4",
|
"version": "3.0.4",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
|
@ -3841,6 +3847,15 @@
|
||||||
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
|
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/cron": {
|
||||||
|
"version": "3.1.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/cron/-/cron-3.1.7.tgz",
|
||||||
|
"integrity": "sha512-tlBg7ARsAMQLzgwqVxy8AZl/qlTc5nibqYwtNGoCrd+cV+ugI+tvZC1oT/8dFH8W455YrywGykx/KMmAqOr7Jw==",
|
||||||
|
"dependencies": {
|
||||||
|
"@types/luxon": "~3.4.0",
|
||||||
|
"luxon": "~3.4.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/cross-spawn": {
|
"node_modules/cross-spawn": {
|
||||||
"version": "7.0.3",
|
"version": "7.0.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -7700,6 +7715,14 @@
|
||||||
"node": ">=10"
|
"node": ">=10"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/luxon": {
|
||||||
|
"version": "3.4.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/luxon/-/luxon-3.4.4.tgz",
|
||||||
|
"integrity": "sha512-zobTr7akeGHnv7eBOXcRgMeCP6+uyYsczwmeRCauvpvaAltgNyTbLH/+VaEAPUeWBT+1GuNmz4wC/6jtQzbbVA==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/magic-bytes.js": {
|
"node_modules/magic-bytes.js": {
|
||||||
"version": "1.6.0",
|
"version": "1.6.0",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
"@types/uuid": "^9.0.0",
|
"@types/uuid": "^9.0.0",
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
"clone-deep": "^4.0.1",
|
"clone-deep": "^4.0.1",
|
||||||
|
"cron": "^3.1.7",
|
||||||
"discord.js": "^14.3.0",
|
"discord.js": "^14.3.0",
|
||||||
"dotenv": "^16.0.0",
|
"dotenv": "^16.0.0",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
|
|
10
src/bot.ts
10
src/bot.ts
|
@ -4,6 +4,8 @@ import { IntentsBitField } from "discord.js";
|
||||||
import Registry from "./registry";
|
import Registry from "./registry";
|
||||||
import { existsSync } from "fs";
|
import { existsSync } from "fs";
|
||||||
import { ExecException, exec } from "child_process";
|
import { ExecException, exec } from "child_process";
|
||||||
|
import TimerHelper from "./helpers/TimerHelper";
|
||||||
Vylpes marked this conversation as resolved
Outdated
|
|||||||
|
import GiveCurrency from "./timers/GiveCurrency";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
|
@ -57,3 +59,11 @@ if (!existsSync(`${process.env.DATA_DIR}/cards`) && process.env.GDRIVESYNC_AUTO
|
||||||
}
|
}
|
||||||
|
|
||||||
client.start();
|
client.start();
|
||||||
|
|
||||||
|
const timerHelper = new TimerHelper();
|
||||||
|
|
||||||
|
const id = timerHelper.AddTimer('* * * * * *', 'Europe/London', GiveCurrency);
|
||||||
|
|
||||||
|
console.log(`Timer Created: ${id}`);
|
||||||
|
|
||||||
|
timerHelper.StartAllTimers();
|
67
src/helpers/TimerHelper.ts
Normal file
67
src/helpers/TimerHelper.ts
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
import { CronJob } from "cron";
|
||||||
|
import { v4 } from "uuid";
|
||||||
|
|
||||||
|
interface Timer {
|
||||||
|
id: string;
|
||||||
|
job: CronJob;
|
||||||
|
context: Map<string, any>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class TimerHelper {
|
||||||
|
private _timers: Timer[];
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this._timers = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public AddTimer(
|
||||||
|
cronTime: string,
|
||||||
|
timeZone: string,
|
||||||
|
onTick: ((context: Map<string, any>) => void) | ((context: Map<string, any>) => Promise<void>)): string {
|
||||||
|
const context = new Map<string, any>();
|
||||||
|
|
||||||
|
const job = new CronJob(
|
||||||
|
cronTime,
|
||||||
|
() => {
|
||||||
|
onTick(context);
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
timeZone,
|
||||||
|
);
|
||||||
|
|
||||||
|
const id = v4();
|
||||||
|
|
||||||
|
this._timers.push({
|
||||||
|
id,
|
||||||
|
job,
|
||||||
|
context,
|
||||||
|
});
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StartAllTimers() {
|
||||||
|
this._timers.forEach(timer => timer.job.start());
|
||||||
|
}
|
||||||
|
|
||||||
|
public StopAllTimers() {
|
||||||
|
this._timers.forEach(timer => timer.job.stop());
|
||||||
|
}
|
||||||
|
|
||||||
|
public StartTimer(id: string) {
|
||||||
|
const timer = this._timers.find(x => x.id == id);
|
||||||
|
|
||||||
|
if (!timer) return;
|
||||||
|
|
||||||
|
timer.job.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public StopTimer(id: string) {
|
||||||
|
const timer = this._timers.find(x => x.id == id);
|
||||||
|
|
||||||
|
if (!timer) return;
|
||||||
|
|
||||||
|
timer.job.stop();
|
||||||
|
}
|
||||||
|
}
|
9
src/timers/GiveCurrency.ts
Normal file
9
src/timers/GiveCurrency.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
export default function GiveCurrency(context: Map<string, any>) {
|
||||||
|
let calledTimes = context.get('times') as number;
|
||||||
|
|
||||||
|
if (!calledTimes) calledTimes = 1;
|
||||||
|
|
||||||
|
console.log(`This has been called ${calledTimes} times!`);
|
||||||
|
|
||||||
|
context.set('times', calledTimes + 1);
|
||||||
|
}
|
Loading…
Reference in a new issue
Remove unnecessary imports