diff --git a/src/Commands.ts b/src/Commands.ts index dc69527..891fff9 100644 --- a/src/Commands.ts +++ b/src/Commands.ts @@ -1,7 +1,8 @@ import { Command } from "./types/Command"; import { List } from "./commands/List"; +import { Online } from "./commands/Online"; import { PlayerAdd } from "./commands/PlayerAdd"; import { PlayerRemove } from "./commands/PlayerRemove"; import { Status } from "./commands/Status"; -export const Commands: Command[] = [List, PlayerAdd, PlayerRemove, Status]; \ No newline at end of file +export const Commands: Command[] = [List, Online, PlayerAdd, PlayerRemove, Status]; \ No newline at end of file diff --git a/src/commands/List.ts b/src/commands/List.ts index 134433c..1481108 100644 --- a/src/commands/List.ts +++ b/src/commands/List.ts @@ -20,9 +20,32 @@ export const List: Command = { const res = await rcon.send("whitelist list").catch(console.error); + if (!res){ + await interaction.reply({ + content: "ERROR: Missing response", + ephemeral: true, + }); + return; + } + + let listOfUsersString: string; + + if (res === "There are no whitelisted players") { + listOfUsersString = "There are no players in the allow-list"; + } else { + listOfUsersString = "*Players in the allow-list:* "; + + const listOfUsers = res.match(/(?<=: ).*/)[0].split(", "); + + listOfUsers.map((user, index) => { + listOfUsersString += `\`${user}\``; + listOfUsersString += index === listOfUsers.length - 1 ? "" : ", "; + }); + } + await interaction.reply({ ephemeral: true, - content: res ? res : "ERROR: Missing response" + content: listOfUsersString }); } }; \ No newline at end of file diff --git a/src/commands/Online.ts b/src/commands/Online.ts new file mode 100644 index 0000000..631bc75 --- /dev/null +++ b/src/commands/Online.ts @@ -0,0 +1,28 @@ +import { CommandInteraction, Client } from "discord.js"; +import { ApplicationCommandType, ApplicationCommandOptionType } from 'discord-api-types/v10'; +import { Command } from "../types/Command"; +import { rcon, verifyConnection } from '../setRCON'; + +export const Online: Command = { + name: "online", + description: "Lists the players online in the Minecraft Server", + type: ApplicationCommandType.ChatInput, + run: async (client: Client, interaction: CommandInteraction) => { + const connectStatus = await verifyConnection(); + + if (!connectStatus) { + await interaction.reply({ + content: "NOT CONNECTED to Minecraft Server!", + ephemeral: true, + }); + return; + } + + const res = await rcon.send("list").catch(console.error); + + await interaction.reply({ + ephemeral: true, + content: res ? res : "ERROR: Missing response", + }); + } +}; \ No newline at end of file diff --git a/src/commands/PlayerAdd.ts b/src/commands/PlayerAdd.ts index da33693..cca45f5 100644 --- a/src/commands/PlayerAdd.ts +++ b/src/commands/PlayerAdd.ts @@ -61,9 +61,17 @@ export const PlayerAdd: Command = { const res = await rcon.send(`whitelist add ${user}`).catch(console.error); + if (!res){ + await interaction.reply({ + content: "ERROR: Missing response", + ephemeral: true, + }); + return; + } + await interaction.reply({ ephemeral: true, - content: res ? res : "ERROR: Missing response" + content: res.replace(/whitelist/gi, "allow-list").replace(user, `**${user}**`).replace(/player/gi, `**${user}**`) }); } }; \ No newline at end of file diff --git a/src/commands/PlayerRemove.ts b/src/commands/PlayerRemove.ts index e257188..04551e1 100644 --- a/src/commands/PlayerRemove.ts +++ b/src/commands/PlayerRemove.ts @@ -61,9 +61,17 @@ export const PlayerRemove: Command = { const res = await rcon.send(`whitelist remove ${user}`).catch(console.error); + if (!res){ + await interaction.reply({ + content: "ERROR: Missing response", + ephemeral: true, + }); + return; + } + await interaction.reply({ ephemeral: true, - content: res ? res : "ERROR: Missing response" + content: res.replace(/whitelist/gi, "allow-list").replace(user, `**${user}**`).replace(/player/gi, `**${user}**`) }); } }; \ No newline at end of file