-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add online command and make it easier to run commands
This is a big commit (I probably should have split it up into multiple, but oh well). This adds the !online command to retrieve the players currently online on every server. I also added a `short_description` to each command which will be shown in the !help command. The command's description will be shown (in addition to the short description) when !help <command> is used. This commit also adds an easier way to run commands. A command context will be used that contains all necessary information for a Command class to process the command. I put control of sending the command results in the command class itself because it allows for more extensibility--specifically when sending more than one embed, which allows for waiting for reactions from the user to scroll through more of the output.
- Loading branch information
Showing
17 changed files
with
349 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./online.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Embed, ErrorEmbed, InfoEmbed } from "../../lib/embeds.js"; | ||
import { TextChannel, escapeMarkdown } from "discord.js"; | ||
import { Bridge } from "../../bridge.js"; | ||
import { Command } from "../command.js"; | ||
import { ICommandContext } from "../../lib/interfaces.js"; | ||
import { discordCommand } from "../commandHandler.js"; | ||
|
||
@discordCommand | ||
export class OnlineCommand extends Command { | ||
constructor() { | ||
const defaultCmdInfo = { | ||
name: "online", | ||
aliases: ["o"], | ||
short_description: "Get a list of all online players on the server", | ||
description: | ||
"By default, a list will be shown for each server. Provide a server " + | ||
"name to list players online on a specific server", | ||
usage: "online [serverName]", | ||
}; | ||
|
||
super(defaultCmdInfo); | ||
} | ||
|
||
async run(ctx: ICommandContext): Promise<void> { | ||
const queriedBridges = this.getQueriedBridges(ctx.args, ctx.bridges); | ||
|
||
const embeds = []; | ||
if (queriedBridges.length === 0) { | ||
// One bridge will always be set up if this point is reached, so zero queried bridges | ||
// always means at least one argument was provided. | ||
const embed = new ErrorEmbed(`No server named '${ctx.args[0]}'`); | ||
embeds.push(embed); | ||
} else { | ||
for (const bridge of queriedBridges) { | ||
const playerInfo = await this.getOnlinePlayers(bridge); | ||
const embed = this.formatEmbed(playerInfo, bridge, ctx.channel); | ||
embeds.push(embed); | ||
} | ||
} | ||
|
||
ctx.channel.send({ embeds: embeds }); | ||
} | ||
|
||
private getQueriedBridges(args: string[], bridges: Bridge[]): Bridge[] { | ||
if (args.length <= 0) { | ||
return bridges; | ||
} | ||
|
||
return bridges.filter((bridge) => bridge.name === args[0]); | ||
} | ||
|
||
private async getOnlinePlayers(bridge: Bridge): Promise<IPlayerInfo> { | ||
const data = await bridge.sendMinecraftCommand("list"); | ||
const response = data.split(" "); | ||
return { | ||
onlineCount: Number(response[2]), | ||
maxCount: Number(response[7]), | ||
playerList: response.slice(10), | ||
}; | ||
} | ||
|
||
private formatEmbed(playerInfo: IPlayerInfo, bridge: Bridge, channel: TextChannel): Embed { | ||
if (playerInfo.onlineCount === 0) { | ||
return new ErrorEmbed(`No players online on \`${bridge.name}\``); | ||
} | ||
|
||
const guildName = channel.guild.name; | ||
const guildIcon = channel.guild.iconURL(); | ||
const description = | ||
`\`${playerInfo.onlineCount}/${playerInfo.maxCount}\` players online ` + | ||
`on: \`${bridge.name}\``; | ||
return new InfoEmbed(guildName, description).setThumbnail(guildIcon).addFields({ | ||
name: "Player list:", | ||
value: escapeMarkdown(playerInfo.playerList.map((name) => `- ${name}`).join("\n")), | ||
}); | ||
} | ||
} | ||
|
||
interface IPlayerInfo { | ||
onlineCount: number; | ||
maxCount: number; | ||
playerList: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import "./commands/discord/index.js"; | ||
|
||
import { Endbot } from "./endbot.js"; | ||
|
||
new Endbot(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,40 @@ | ||
import { EmbedBuilder } from "discord.js"; | ||
|
||
export type Embed = ErrorEmbed | InfoEmbed; | ||
|
||
const Colors = { | ||
ERROR: 0xfc251e, | ||
INFO: 0x86ff40, | ||
}; | ||
|
||
export class CommandNotFoundEmbed extends EmbedBuilder { | ||
constructor(command: string) { | ||
export class ErrorEmbed extends EmbedBuilder { | ||
constructor(errorMsg: string, title: string = "") { | ||
super({ | ||
title: "Command Not Found", | ||
description: `Unable to find command: '${command}'`, | ||
title: title, | ||
description: errorMsg, | ||
color: Colors.ERROR, | ||
}); | ||
} | ||
} | ||
|
||
export class InvalidPermissionsEmbed extends EmbedBuilder { | ||
export class CommandNotFoundEmbed extends ErrorEmbed { | ||
constructor(command: string) { | ||
super(`Unable to find command: '${command}'`, "Command Not Found"); | ||
} | ||
} | ||
|
||
export class InvalidPermissionsEmbed extends ErrorEmbed { | ||
constructor(command: string) { | ||
super(`You don't have permission to run the command: '${command}'`, "Invalid Permissions"); | ||
} | ||
} | ||
|
||
export class InfoEmbed extends EmbedBuilder { | ||
constructor(title: string, description: string) { | ||
super({ | ||
title: "Invalid Permissions", | ||
description: `You don't have permission to run the command: '${command}'`, | ||
color: Colors.ERROR, | ||
title: title, | ||
description: description, | ||
color: Colors.INFO, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.