|
|
|
@ -1,24 +1,24 @@ |
|
|
|
|
import {Client, Collection, Events, GatewayIntentBits, Interaction, Message} from "discord.js" |
|
|
|
|
import {Client, Collection, Events, GatewayIntentBits, Interaction} from "discord.js" |
|
|
|
|
import {Player} from "./player"; |
|
|
|
|
import fs from "node:fs"; |
|
|
|
|
import {Command} from "./command"; |
|
|
|
|
import path from "node:path"; |
|
|
|
|
import {Command, loadCommands} from "./command"; |
|
|
|
|
|
|
|
|
|
export class UEMEloBot extends Client { |
|
|
|
|
|
|
|
|
|
players: Player[] = [] |
|
|
|
|
fileName = "players.json" |
|
|
|
|
commands: Collection<string, Command> = new Collection<string, Command>(); |
|
|
|
|
commands = new Collection<string, Command>(); |
|
|
|
|
|
|
|
|
|
constructor() { |
|
|
|
|
super({intents: [GatewayIntentBits.Guilds]}); |
|
|
|
|
this.once(Events.ClientReady, this.onReady); |
|
|
|
|
this.on(Events.InteractionCreate, this.onInteractionCreate); |
|
|
|
|
this.parsePlayersFromFile(); |
|
|
|
|
this.loadCommands().then(async () => { |
|
|
|
|
loadCommands().then(async commands => { |
|
|
|
|
this.commands = commands; |
|
|
|
|
const token = await this.login(); |
|
|
|
|
console.log(`Discord Token: ${token}`); |
|
|
|
|
setInterval(this.updateStartElo.bind(this), 5000); |
|
|
|
|
setInterval(this.updatePlayers.bind(this), 30 * 60 * 1000); |
|
|
|
|
await this.addPlayer("benjo", "tgm"); |
|
|
|
|
await this.addPlayer("unclebenss", "euw"); |
|
|
|
|
await this.addPlayer("moché", "EUw"); |
|
|
|
@ -29,7 +29,7 @@ export class UEMEloBot extends Client { |
|
|
|
|
if (!interaction.isChatInputCommand()) |
|
|
|
|
return; |
|
|
|
|
let commandName = interaction.commandName; |
|
|
|
|
let command = this.commands.ensure(commandName, () => this.commands.get("help") as Command); |
|
|
|
|
let command = this.commands.get(commandName) as Command; |
|
|
|
|
await command.execute(interaction); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -39,9 +39,7 @@ export class UEMEloBot extends Client { |
|
|
|
|
try { |
|
|
|
|
this.players = []; |
|
|
|
|
for (let obj of JSON.parse(fileContent)){ |
|
|
|
|
let p = new Player(obj.account); |
|
|
|
|
if (obj.hasOwnProperty("startElo")) |
|
|
|
|
p.startElo = obj.startElo; |
|
|
|
|
let p = Player.Load(obj); |
|
|
|
|
this.players.push(p); |
|
|
|
|
console.log(`Parsed player: ${p}`); |
|
|
|
|
} |
|
|
|
@ -52,13 +50,15 @@ export class UEMEloBot extends Client { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
savePlayersToFile(){ |
|
|
|
|
fs.writeFileSync(this.fileName, JSON.stringify(this.players)); |
|
|
|
|
fs.writeFileSync(this.fileName, JSON.stringify(this.players, null, 4)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async updateStartElo(){ |
|
|
|
|
async updatePlayers(){ |
|
|
|
|
for (let p of this.players){ |
|
|
|
|
await p.updateFullName(); |
|
|
|
|
await p.updateCurrentElo(); |
|
|
|
|
if (!p.startElo){ |
|
|
|
|
p.startElo = await p.fetchCurrentElo(); |
|
|
|
|
p.startElo = p.currentElo; |
|
|
|
|
console.log(`Updated start elo for ${p}`); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -66,40 +66,28 @@ export class UEMEloBot extends Client { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async addPlayer(gameName: string, tag: string){ |
|
|
|
|
let player = await Player.Create(gameName, tag) as Player; |
|
|
|
|
let player = await Player.TryCreateFrom(gameName, tag) as Player; |
|
|
|
|
|
|
|
|
|
if (player === undefined) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
if (this.players.find(p => p.account.puuid === player.account.puuid)){ |
|
|
|
|
if (this.players.find(p => p.puuid === player.puuid)){ |
|
|
|
|
console.error(`${player} already registered!`); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.players.push(player); |
|
|
|
|
|
|
|
|
|
console.log(`Added ${player}!`); |
|
|
|
|
|
|
|
|
|
this.savePlayersToFile(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async loadCommands(){ |
|
|
|
|
const commandFiles = fs.readdirSync(path.join(__dirname, "commands")).filter(file => file.endsWith('.js')); |
|
|
|
|
for (const file of commandFiles){ |
|
|
|
|
const module = await import(`./commands/${file}`); |
|
|
|
|
const cmd = module.default; |
|
|
|
|
if (cmd instanceof Command) |
|
|
|
|
this.commands.set(cmd.data.name, cmd); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async onReady(readyClient: Client<true>){ |
|
|
|
|
console.log(`Logged in as ${readyClient.user.tag}`); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
stop(){ |
|
|
|
|
this.destroy().then(() => { |
|
|
|
|
console.log("Bot stopped!"); |
|
|
|
|
console.log("Logged out!"); |
|
|
|
|
process.exit(0); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|