Initial commit
This commit is contained in:
20
wwwroot/core/base/MessageSender.js
Normal file
20
wwwroot/core/base/MessageSender.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import {data} from "../appData.js";
|
||||
|
||||
export class MessageSender {
|
||||
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
async send(channelID, message) {
|
||||
|
||||
const channel = await data.client.channels.fetch(channelID);
|
||||
|
||||
if (channel && channel.isTextBased()) {
|
||||
await channel.send(message);
|
||||
} else {
|
||||
console.log('❌ Channel not found!');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
32
wwwroot/core/base/basePoller.js
Normal file
32
wwwroot/core/base/basePoller.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import EventEmitter from "events";
|
||||
import {Logger} from "../logging/logger.js";
|
||||
|
||||
export class BasePoller extends EventEmitter {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.task = null;
|
||||
}
|
||||
|
||||
async pollOnce(){
|
||||
throw new Error("Not implemented.");
|
||||
}
|
||||
|
||||
start(interval) {
|
||||
if(this.task){
|
||||
Logger.error("A task has already been scheduled");
|
||||
return;
|
||||
}
|
||||
|
||||
super.task = setInterval(async () => {
|
||||
await this.pollOnce();
|
||||
}, interval);
|
||||
}
|
||||
|
||||
stop() {
|
||||
if (this.task) {
|
||||
clearInterval(this.task);
|
||||
super.task = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
47
wwwroot/core/base/baseTokenManager.js
Normal file
47
wwwroot/core/base/baseTokenManager.js
Normal file
@@ -0,0 +1,47 @@
|
||||
export class BaseTokenManager {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param usersToken {UsersToken}
|
||||
*/
|
||||
constructor(usersToken) {
|
||||
this.usersToken = usersToken;
|
||||
this.watchDelay = 3600*1000; //1 hour in milliseconds
|
||||
}
|
||||
|
||||
/**
|
||||
* Launches watch for all users
|
||||
*/
|
||||
startWatching() {
|
||||
this.usersToken.getAll().forEach((user, index) => {
|
||||
this.watchUser(index);
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches token for a single user
|
||||
*/
|
||||
watchUser(userIndex){
|
||||
setInterval(async () => {
|
||||
let userData = await this.getUserData(userIndex);
|
||||
if(this.mustBeRenewed(userData.expiresAt)){
|
||||
await this.renew(userData);
|
||||
}
|
||||
}, this.watchDelay);
|
||||
}
|
||||
|
||||
renew(){
|
||||
throw new Error("Not implemented.");
|
||||
}
|
||||
|
||||
mustBeRenewed(expiresAt) {
|
||||
const expirationThreshold = 3600*1000; //1 hour in milliseconds
|
||||
const timeBeforeExpiration = expiresAt - Date.now();
|
||||
return timeBeforeExpiration <= expirationThreshold;
|
||||
}
|
||||
|
||||
getUserData(index){
|
||||
return this.usersToken.get(index);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user