Add cron job to add coins to a user every 30 minutes #219
7 changed files with 51 additions and 20 deletions
10
src/bot.ts
10
src/bot.ts
|
@ -58,12 +58,4 @@ if (!existsSync(`${process.env.DATA_DIR}/cards`) && process.env.GDRIVESYNC_AUTO
|
|||
});
|
||||
}
|
||||
|
||||
client.start();
|
||||
|
||||
const timerHelper = new TimerHelper();
|
||||
|
||||
const id = timerHelper.AddTimer('* * * * * *', 'Europe/London', GiveCurrency);
|
||||
|
||||
console.log(`Timer Created: ${id}`);
|
||||
|
||||
timerHelper.StartAllTimers();
|
||||
client.start();
|
|
@ -14,6 +14,8 @@ import Webhooks from "../webhooks";
|
|||
import CardMetadataFunction from "../Functions/CardMetadataFunction";
|
||||
import { SeriesMetadata } from "../contracts/SeriesMetadata";
|
||||
import AppLogger from "./appLogger";
|
||||
import TimerHelper from "../helpers/TimerHelper";
|
||||
import GiveCurrency from "../timers/GiveCurrency";
|
||||
|
||||
export class CoreClient extends Client {
|
||||
private static _commandItems: ICommandItem[];
|
||||
|
@ -23,6 +25,7 @@ export class CoreClient extends Client {
|
|||
private _events: Events;
|
||||
private _util: Util;
|
||||
private _webhooks: Webhooks;
|
||||
private _timerHelper: TimerHelper;
|
||||
|
||||
public static ClaimId: string;
|
||||
public static Environment: Environment;
|
||||
|
@ -59,6 +62,7 @@ export class CoreClient extends Client {
|
|||
this._events = new Events();
|
||||
this._util = new Util();
|
||||
this._webhooks = new Webhooks();
|
||||
this._timerHelper = new TimerHelper();
|
||||
|
||||
AppLogger.LogInfo("Client", `Environment: ${CoreClient.Environment}`);
|
||||
|
||||
|
@ -72,7 +76,12 @@ export class CoreClient extends Client {
|
|||
}
|
||||
|
||||
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 => {
|
||||
AppLogger.LogError("Client", "App Data Source Initialisation Failed");
|
||||
AppLogger.LogError("Client", err);
|
||||
|
|
|
@ -27,6 +27,12 @@ export default class AppBaseEntity {
|
|||
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> {
|
||||
const repository = AppDataSource.getRepository<T>(target);
|
||||
|
||||
|
|
|
@ -13,8 +13,8 @@ export default class User extends AppBaseEntity {
|
|||
@Column()
|
||||
Currency: number;
|
||||
|
||||
public UpdateCurrency(currency: number) {
|
||||
this.Currency = currency;
|
||||
public AddCurrency(amount: number) {
|
||||
this.Currency += amount;
|
||||
}
|
||||
|
||||
public RemoveCurrency(amount: number): boolean {
|
||||
|
|
|
@ -5,6 +5,8 @@ interface Timer {
|
|||
id: string;
|
||||
job: CronJob;
|
||||
context: Map<string, any>;
|
||||
onTick: ((context: Map<string, any>) => void) | ((context: Map<string, any>) => Promise<void>);
|
||||
runOnStart: boolean;
|
||||
}
|
||||
|
||||
export default class TimerHelper {
|
||||
|
@ -17,7 +19,8 @@ export default class TimerHelper {
|
|||
public AddTimer(
|
||||
cronTime: string,
|
||||
timeZone: string,
|
||||
onTick: ((context: Map<string, any>) => void) | ((context: Map<string, any>) => Promise<void>)): string {
|
||||
onTick: ((context: Map<string, any>) => void) | ((context: Map<string, any>) => Promise<void>),
|
||||
runOnStart: boolean = false): string {
|
||||
const context = new Map<string, any>();
|
||||
|
||||
const job = new CronJob(
|
||||
|
@ -36,13 +39,15 @@ export default class TimerHelper {
|
|||
id,
|
||||
job,
|
||||
context,
|
||||
onTick,
|
||||
runOnStart,
|
||||
});
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
public StartAllTimers() {
|
||||
this._timers.forEach(timer => timer.job.start());
|
||||
this._timers.forEach(timer => this.StartJob(timer));
|
||||
}
|
||||
|
||||
public StopAllTimers() {
|
||||
|
@ -54,7 +59,7 @@ export default class TimerHelper {
|
|||
|
||||
if (!timer) return;
|
||||
|
||||
timer.job.start();
|
||||
this.StartJob(timer);
|
||||
}
|
||||
|
||||
public StopTimer(id: string) {
|
||||
|
@ -64,4 +69,12 @@ export default class TimerHelper {
|
|||
|
||||
timer.job.stop();
|
||||
}
|
||||
|
||||
private StartJob(timer: Timer) {
|
||||
timer.job.start();
|
||||
|
||||
if (timer.runOnStart) {
|
||||
timer.onTick(timer.context);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -52,4 +52,8 @@ export default class Registry {
|
|||
CoreClient.RegisterButtonEvent("series", new SeriesEvent());
|
||||
CoreClient.RegisterButtonEvent("trade", new TradeButtonEvent());
|
||||
}
|
||||
|
||||
public static RegisterTimers() {
|
||||
|
||||
}
|
||||
}
|
|
@ -1,9 +1,16 @@
|
|||
export default function GiveCurrency(context: Map<string, any>) {
|
||||
let calledTimes = context.get('times') as number;
|
||||
import AppLogger from "../client/appLogger";
|
||||
import User from "../database/entities/app/User";
|
||||
|
||||
if (!calledTimes) calledTimes = 1;
|
||||
export default async function GiveCurrency(context: Map<string, any>) {
|
||||
AppLogger.LogInfo("Timers/GiveCurrency", "Giving currency to every known user");
|
||||
|
||||
console.log(`This has been called ${calledTimes} times!`);
|
||||
const users = await User.FetchAll(User);
|
||||
|
||||
context.set('times', calledTimes + 1);
|
||||
for (const user of users) {
|
||||
user.AddCurrency(5);
|
||||
}
|
||||
|
||||
User.SaveAll(User, users);
|
||||
|
||||
AppLogger.LogInfo("Timers/GiveCurrency", `Successfully gave +5 currency to ${users.length} users`);
|
||||
}
|
Loading…
Reference in a new issue