Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Discord Commands framework, and some basic commands as an example. #5

Open
wants to merge 10 commits into
base: dev/1.19.x
Choose a base branch
from

Conversation

TheCurle
Copy link
Contributor

@TheCurle TheCurle commented May 2, 2022

The Command Framework

Because we're using raw JDA and not one of the extras libraries like jdautils or chewutils, I've had to implement my own Slash Commands wrapper system.

Enter, the CommandDispatcher.
In short, it stores a list of Slash Commands internally and invokes them at the appropriate time.

Slash Commands have a whole system to themselves, based around the abstract class named SlashCommand.
A Slash Command stores a few key points of data; the primary name they should be invoked by, the description that should be shown to the user, and some extra, more detailed help data that should be shown in the help command.

However, because of aforementioned lack of extras library, the Slash Command is also in charge of adding its' own Sub-Commands and Options. A simple example is provided in the code. For example, the KickCommand's setup:

    @Override
    public CommandCreateAction setup(CommandCreateAction action) {
        return action.addOptions(USER_OPTION, REASON_OPTION);
    }

When the command is executed by the user, the execute method is invoked with the SlashCommandEvent context passed from JDA.

This roughly mirrors how JDAUtils' dispatcher works, but I took it one step further.

Since a command is a couple bits of text and a consumer of a SlashCommandEvent, i added a method that takes... a few Strings and a Consumer<SlashCommandEvent>, for easier implementation of these commands. Observe the implementation of the Stop command:

        dispatcher.registerSingle("stop", "Shut down your Minecraft server.", "Immediately schedule the shutdown of the Minecraft server, akin to /stop from in-game.", (event) -> {
            // Short-circuit if on integrated server
            if(FMLLoader.getDist() == Dist.CLIENT) {
                event.reply("Sorry! This command is disabled on Integrated servers.").setEphemeral(true).queue();
                return;
            }

            event.reply("Shutting the server down..").queue();
            Concord.BOT.getServer().halt(false);
        });

Upsert

The CommandDispatcher itself will handle upserting commands, and since Concord is configured to "belong" to a single guild at a time, it exists constantly in a guild-locked mode.

This has two effects; first, commands are updated immediately. Second, moderation commands cannot be invoked from a guild other than the one that Concord is relaying to.

However, the Dispatcher has an optional global mode (more specifically, the optional guild-only mode is always enabled by default in the Concord configuration) that you can use if you wish the bot to not be restricted like this.

The implemented commands

As mentioned, i've included some commands with this PR to demonstrate the Dispatcher system, as well as begin implementation of a well structured Discord interface for a Minecraft server.

It is my hope that Concord may one day be used in the core of the Hammer ecosystem, but that seems far away right now.

Anyway. The commands implented come in two major forms; moderation and casual.

Moderation commands

There are four moderation commands.

  • /kick <user> [reason]; remove a player from the server by their username, optionally with a reason.
  • /ban <user> [reason]; remove a player from the server and prevent them from joining again; optionally with a reason.
  • /whitelist <add|remove> <user>; add or remove a player from the server's whitelist. Only usable if the whitelist is enabled, which must be done from in-game.
  • /stop; schedule the immediate shut-down of the Minecraft server.

These commands are disabled by default, and are enabled by the setting of the Moderator role ID in the Concord configuration file.

Casual commands

There are also three casual commands.

  • /list; show the number and names of online players.
  • /tps; show the performance of the server in terms of TPS readouts, similar to /forge tps from in-game.
  • /help; show detailed information about all registered commands, and how to use them.

Future plans

  • Implement the Help Command
  • Clean up the implementation of ConcordDiscordCommand
  • Implement some form of API so that other mods can integrate with Concord to allow commands to modify things in their mod

@sciwhiz12 sciwhiz12 added the feature New feature or request label May 2, 2022
@Matyrobbrt
Copy link

I'd point out that it would be beneficial if this PR also updates JDA to alphas, knowing that when JDA v5 releases, it will bring breaking changes to the JDA v4 command code.

@TheCurle TheCurle changed the base branch from dev/1.18.x to dev/1.19.x September 4, 2022 14:07
@TheCurle
Copy link
Contributor Author

TheCurle commented Sep 4, 2022

I've updated this PR to the latest 1.19 branch, continuing work..

@TheCurle TheCurle marked this pull request as ready for review September 4, 2022 14:39
@TheCurle
Copy link
Contributor Author

TheCurle commented Sep 4, 2022

I believe this is ready for review now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants