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);
}
}
}
await Client.StartAsync();
CommandHandler CommandHandler = new CommandHandler(Client);
CommandHandler.LoadCommands();
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 :)");
}
}
// 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]