Merge branch 'develop' into feature/203-sacrifice-command
All checks were successful
Test / build (push) Successful in 10s

This commit is contained in:
Vylpes 2024-05-17 21:00:33 +01:00
commit a59d3ca887
10 changed files with 138 additions and 10 deletions

23
package-lock.json generated
View file

@ -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"

View file

@ -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",

View file

@ -37,7 +37,6 @@ const client = new CoreClient([
]); ]);
Registry.RegisterCommands(); Registry.RegisterCommands();
Registry.RegisterEvents();
Registry.RegisterButtonEvents(); Registry.RegisterButtonEvents();
if (!existsSync(`${process.env.DATA_DIR}/cards`) && process.env.GDRIVESYNC_AUTO && process.env.GDRIVESYNC_AUTO == "true") { if (!existsSync(`${process.env.DATA_DIR}/cards`) && process.env.GDRIVESYNC_AUTO && process.env.GDRIVESYNC_AUTO == "true") {

View file

@ -14,6 +14,8 @@ import Webhooks from "../webhooks";
import CardMetadataFunction from "../Functions/CardMetadataFunction"; import CardMetadataFunction from "../Functions/CardMetadataFunction";
import { SeriesMetadata } from "../contracts/SeriesMetadata"; import { SeriesMetadata } from "../contracts/SeriesMetadata";
import AppLogger from "./appLogger"; import AppLogger from "./appLogger";
import TimerHelper from "../helpers/TimerHelper";
import GiveCurrency from "../timers/GiveCurrency";
export class CoreClient extends Client { export class CoreClient extends Client {
private static _commandItems: ICommandItem[]; private static _commandItems: ICommandItem[];
@ -23,6 +25,7 @@ export class CoreClient extends Client {
private _events: Events; private _events: Events;
private _util: Util; private _util: Util;
private _webhooks: Webhooks; private _webhooks: Webhooks;
private _timerHelper: TimerHelper;
public static ClaimId: string; public static ClaimId: string;
public static Environment: Environment; public static Environment: Environment;
@ -59,6 +62,7 @@ export class CoreClient extends Client {
this._events = new Events(); this._events = new Events();
this._util = new Util(); this._util = new Util();
this._webhooks = new Webhooks(); this._webhooks = new Webhooks();
this._timerHelper = new TimerHelper();
AppLogger.LogInfo("Client", `Environment: ${CoreClient.Environment}`); AppLogger.LogInfo("Client", `Environment: ${CoreClient.Environment}`);
@ -72,7 +76,12 @@ export class CoreClient extends Client {
} }
await AppDataSource.initialize() await AppDataSource.initialize()
.then(() => AppLogger.LogInfo("Client", "App Data Source Initialised")) .then(() => {
AppLogger.LogInfo("Client", "App Data Source Initialised");
const timerId = this._timerHelper.AddTimer("*/30 * * * * *", "Europe/London", GiveCurrency, false);
this._timerHelper.StartTimer(timerId);
})
.catch(err => { .catch(err => {
AppLogger.LogError("Client", "App Data Source Initialisation Failed"); AppLogger.LogError("Client", "App Data Source Initialisation Failed");
AppLogger.LogError("Client", err); AppLogger.LogError("Client", err);

View file

@ -27,6 +27,12 @@ export default class AppBaseEntity {
await repository.save(entity); await repository.save(entity);
} }
public static async SaveAll<T extends AppBaseEntity>(target: EntityTarget<T>, entities: DeepPartial<T>[]): Promise<void> {
const repository = AppDataSource.getRepository<T>(target);
await repository.save(entities);
}
public static async Remove<T extends AppBaseEntity>(target: EntityTarget<T>, entity: T): Promise<void> { public static async Remove<T extends AppBaseEntity>(target: EntityTarget<T>, entity: T): Promise<void> {
const repository = AppDataSource.getRepository<T>(target); const repository = AppDataSource.getRepository<T>(target);

View file

@ -13,10 +13,6 @@ export default class User extends AppBaseEntity {
@Column() @Column()
Currency: number; Currency: number;
public UpdateCurrency(currency: number) {
this.Currency = currency;
}
public AddCurrency(amount: number) { public AddCurrency(amount: number) {
this.Currency += amount; this.Currency += amount;
} }

View file

@ -0,0 +1,81 @@
import { CronJob } from "cron";
import { v4 } from "uuid";
import { Primitive } from "../type/primitive";
interface Timer {
id: string;
job: CronJob;
context: Map<string, Primitive>;
onTick: ((context: Map<string, Primitive>) => void) | ((context: Map<string, Primitive>) => Promise<void>);
runOnStart: boolean;
}
export default class TimerHelper {
private _timers: Timer[];
constructor() {
this._timers = [];
}
public AddTimer(
cronTime: string,
timeZone: string,
onTick: ((context: Map<string, Primitive>) => void) | ((context: Map<string, Primitive>) => Promise<void>),
runOnStart: boolean = false): string {
const context = new Map<string, Primitive>();
const job = new CronJob(
cronTime,
() => {
onTick(context);
},
null,
false,
timeZone,
);
const id = v4();
this._timers.push({
id,
job,
context,
onTick,
runOnStart,
});
return id;
}
public StartAllTimers() {
this._timers.forEach(timer => this.StartJob(timer));
}
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;
this.StartJob(timer);
}
public StopTimer(id: string) {
const timer = this._timers.find(x => x.id == id);
if (!timer) return;
timer.job.stop();
}
private StartJob(timer: Timer) {
timer.job.start();
if (timer.runOnStart) {
timer.onTick(timer.context);
}
}
}

View file

@ -44,10 +44,6 @@ export default class Registry {
CoreClient.RegisterCommand("droprarity", new Droprarity(), Environment.Test); CoreClient.RegisterCommand("droprarity", new Droprarity(), Environment.Test);
} }
public static RegisterEvents() {
}
public static RegisterButtonEvents() { public static RegisterButtonEvents() {
CoreClient.RegisterButtonEvent("claim", new Claim()); CoreClient.RegisterButtonEvent("claim", new Claim());
CoreClient.RegisterButtonEvent("inventory", new InventoryButtonEvent()); CoreClient.RegisterButtonEvent("inventory", new InventoryButtonEvent());

View file

@ -0,0 +1,16 @@
import AppLogger from "../client/appLogger";
import User from "../database/entities/app/User";
export default async function GiveCurrency() {
AppLogger.LogInfo("Timers/GiveCurrency", "Giving currency to every known user");
const users = await User.FetchAll(User);
for (const user of users) {
user.AddCurrency(5);
}
User.SaveAll(User, users);
AppLogger.LogInfo("Timers/GiveCurrency", `Successfully gave +5 currency to ${users.length} users`);
}

1
src/type/primitive.ts Normal file
View file

@ -0,0 +1 @@
export type Primitive = string | number | boolean;