supports multiple servers

main
Benjamin Kraft 6 months ago
parent fa3d74e8a8
commit 67a7e165a1
  1. 50
      src/bot.ts
  2. 12
      src/commands/add.ts
  3. 9
      src/commands/list.ts
  4. 8
      src/commands/remove.ts

@ -5,7 +5,7 @@ import {Command, loadCommands} from "./command";
export class UEMEloBot extends Client { export class UEMEloBot extends Client {
players: Player[] = [] servers = new Collection<string, Player[]>();
fileName = "players.json" fileName = "players.json"
commands = new Collection<string, Command>(); commands = new Collection<string, Command>();
@ -35,11 +35,16 @@ export class UEMEloBot extends Client {
if (fs.existsSync(this.fileName)){ if (fs.existsSync(this.fileName)){
let fileContent = fs.readFileSync(this.fileName).toString(); let fileContent = fs.readFileSync(this.fileName).toString();
try { try {
this.players = []; this.servers.clear();
for (let obj of JSON.parse(fileContent)){ for (let [serverID, players] of Object.entries(JSON.parse(fileContent))){
let p = Player.Load(obj); console.log(`Server: ${serverID}`);
this.players.push(p); let playersList: Player[] = [];
console.log(`Parsed player: ${p}`); for (let obj of (players as Object[])){
let p = Player.Load(obj);
playersList.push(p);
console.log(`Parsed player: ${p}`);
}
this.servers.set(serverID, playersList);
} }
} catch (error) { } catch (error) {
console.error(`Failed to parse players: ${error}`); console.error(`Failed to parse players: ${error}`);
@ -48,39 +53,41 @@ export class UEMEloBot extends Client {
} }
savePlayersToFile(){ savePlayersToFile(){
fs.writeFileSync(this.fileName, JSON.stringify(this.players, null, 4)); fs.writeFileSync(this.fileName, JSON.stringify(Object.fromEntries(this.servers), null, 4));
} }
async updatePlayers(){ async updatePlayers(){
for (let p of this.players){ for (let [_, players] of this.servers){
await p.updateFullName(); for (let p of players){
await p.updateCurrentElo(); await p.updateFullName();
if (!p.startElo && p.currentElo){ await p.updateCurrentElo();
p.startElo = p.currentElo; if (!p.startElo && p.currentElo){
console.log(`Updated start elo for ${p}`); p.startElo = p.currentElo;
console.log(`Updated start elo for ${p}`);
}
} }
} }
this.savePlayersToFile(); this.savePlayersToFile();
} }
async addPlayer(gameName: string, tagLine: string){ async addPlayer(gameName: string, tagLine: string, serverID: string){
let player = await Player.TryCreateFrom(gameName, tagLine) as Player; let player = await Player.TryCreateFrom(gameName, tagLine) as Player;
if (!player) if (!player)
return; return;
if (this.players.find(p => p.puuid === player.puuid)){ if (this.servers.ensure(serverID, () => []).find(p => p.puuid === player.puuid)) {
console.error(`${player} already registered!`); console.error(`${player} already registered!`);
return; return;
} }
this.players.push(player); this.servers.ensure(serverID, () => []).push(player);
console.log(`Added ${player}!`); console.log(`Added ${player} on Server: ${serverID}!`);
this.savePlayersToFile(); this.savePlayersToFile();
return player; return player;
} }
async removePlayer(gameName: string, tagLine: string){ async removePlayer(gameName: string, tagLine: string, serverID: string){
let playerCopy = await Player.TryCreateFrom(gameName, tagLine); let playerCopy = await Player.TryCreateFrom(gameName, tagLine);
if (!playerCopy){ if (!playerCopy){
@ -88,13 +95,16 @@ export class UEMEloBot extends Client {
return; return;
} }
let player = this.players.find(p => p.puuid === playerCopy?.puuid); let serverPlayers = this.servers.ensure(serverID, () => []);
let player = serverPlayers.find(p => p.puuid === playerCopy?.puuid);
if (!player){ if (!player){
console.error(`Tried to remove non-added player ${playerCopy}!`); console.error(`Tried to remove non-added player ${playerCopy}!`);
return; return;
} }
this.players.splice(this.players.indexOf(player), 1); serverPlayers.splice(serverPlayers.indexOf(player), 1);
this.servers.set(serverID, serverPlayers);
console.log(`Removed ${player}!`); console.log(`Removed ${player}!`);
this.savePlayersToFile(); this.savePlayersToFile();
return player; return player;

@ -1,4 +1,4 @@
import {Interaction} from "discord.js"; import {Interaction, PermissionsBitField} from "discord.js";
import {Command} from "../command"; import {Command} from "../command";
import {UEMEloBot} from "../bot"; import {UEMEloBot} from "../bot";
@ -7,7 +7,7 @@ class Add extends Command {
super("add", "Spieler hinzufügen"); super("add", "Spieler hinzufügen");
this.data.addStringOption(option => { this.data.addStringOption(option => {
return option.setName("riot-id").setDescription("Riot ID: 'Name#Tag'").setRequired(true) return option.setName("riot-id").setDescription("Riot ID: 'Name#Tag'").setRequired(true)
}); }).setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator);
} }
async execute(interaction: Interaction) { async execute(interaction: Interaction) {
@ -18,8 +18,14 @@ class Add extends Command {
const riotId = interaction.options.getString("riot-id", true); const riotId = interaction.options.getString("riot-id", true);
const [gameName, tagLine] = riotId.split("#"); const [gameName, tagLine] = riotId.split("#");
const serverID = interaction.guild?.id;
if (!serverID){
console.error("Server ID is missing!");
await interaction.reply({content: "Fehler!", ephemeral: true});
return;
}
const player = await client.addPlayer(gameName, tagLine); const player = await client.addPlayer(gameName, tagLine, serverID);
if (player) if (player)
await interaction.reply({content: `${player} hinzugefügt!`, ephemeral: true}); await interaction.reply({content: `${player} hinzugefügt!`, ephemeral: true});
else else

@ -11,7 +11,14 @@ class List extends Command {
if (!interaction.isChatInputCommand()) if (!interaction.isChatInputCommand())
return; return;
let players = (interaction.client as UEMEloBot).players; const serverID = interaction.guild?.id;
if (!serverID){
console.error("Server ID is missing!");
await interaction.reply({content: "Fehler!", ephemeral: true});
return;
}
let players = (interaction.client as UEMEloBot).servers.ensure(serverID, () => []);
if (players.length === 0){ if (players.length === 0){
await interaction.reply("0 Spieler registriert!"); await interaction.reply("0 Spieler registriert!");
return; return;

@ -18,8 +18,14 @@ class Remove extends Command {
const riotId = interaction.options.getString("riot-id", true); const riotId = interaction.options.getString("riot-id", true);
const [gameName, tagLine] = riotId.split("#"); const [gameName, tagLine] = riotId.split("#");
const serverID = interaction.guild?.id;
if (!serverID){
console.error("Server ID is missing!");
await interaction.reply({content: "Fehler!", ephemeral: true});
return;
}
const player = await client.removePlayer(gameName, tagLine); const player = await client.removePlayer(gameName, tagLine, serverID);
if (player) if (player)
await interaction.reply({content: `${player} entfernt!`, ephemeral: true}); await interaction.reply({content: `${player} entfernt!`, ephemeral: true});
else else

Loading…
Cancel
Save