Guide

Use the RevoltSharp built-in command handler to use it for bots

You can use the build-in command handler to parse and run commands from messages!

1. Install the RevoltSharp.Commands package from NuGet

Download from NuGet or use the Visual Studio package manager.

2. Create the Command Handler class

Command Handler
public class CommandHandler
{
    public CommandHandler(RevoltClient client)
    {
        Client = client;
        Client.OnMessageRecieved += Client_OnMessageRecieved;
        Service.OnCommandExecuted += Service_OnCommandExecuted;
    }
    private RevoltClient Client;
    private CommandService Service = new CommandService();

    // Change this prefix
    public const string Prefix = "!";

    public async Task LoadCommands()
    {
        await Service.AddModulesAsync(Assembly.GetEntryAssembly(), null);
    }
    

    private void Client_OnMessageRecieved(Message msg)
    {
        UserMessage Message = msg as UserMessage;
        if (Message == null || Message.Author.IsBot)
            return;
        int argPos = 0;
        if (!(Message.HasStringPrefix(Prefix, ref argPos) || Message.HasMentionPrefix(Client.CurrentUser, ref argPos)))
            return;
        CommandContext context = new CommandContext(Client, Message);
        
        _ = Service.ExecuteAsync(context, argPos, null);
    }

    private void Service_OnCommandExecuted(Optional<CommandInfo> commandinfo, CommandContext context, IResult result)
    {
        if (result.IsSuccess)
            Console.WriteLine("Success command: " + commandinfo.Value.Name);
        else
        {
            if (!commandinfo.HasValue)
                return;
            context.Channel.SendMessageAsync("Error: " + result.ErrorReason);
        }
    }
}

3. Change your prefix

Update the CommandHandler.Prefix property to change the prefix.

4. Update your Program.cs file and add the command handler

 await Client.StartAsync();

CommandHandler CommandHandler = new CommandHandler(Client);
CommandHandler.LoadCommands();

5. Create a command module file

Test Module

Make sure the class is public and that : ModuleBase is added.

The command handler will load any commands inside these modules.

public class TestModule : ModuleBase
{
    [Command("test")]
    public async Task Test()
    {
        await ReplyAsync("Test!");
    }

    [Command("hello")]
    public async Task Hello()
    {
        await Context.Channel.SendMessageAsync("Hello " + Context.User.Username);
    }
    
    [Command("dm")]
    public async Task DM()
    {
        DMChannel DM = await Context.User.GetDMChannelAsync();
        if (DM == null)
        {
            await ReplyAsync("Could not open a DM :(");
            return;
        }

        await DM.SendMessageAsync("Hi :)");
    }
}

Extra

You can use command attributes to make life easier!

// Can run command as test or othertest     \\

[Command("test"), Alias("othertest")]

// You can put this on commands or the ModuleBase to lock commands to bot owner  \\
// Set bot owners in ClientConfig when creating a RevoltClient                   \\

[RequireOwner]

Last updated