bro momento
bro momento

Reputation: 56

Discord.net: Why are my discord commands not being recognised at all?

I'm writing a discord bot in discord.net and am have trouble getting commands to work, my error handler is picking up that it is an unknown command. Any ideas?

these are my scripts:

Command Handler:

using System;
using System.Collections.Generic;
using System.Text;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using System.Threading.Tasks;
using Discord;
using System.Reflection;

namespace fucking_kill_me.Services
{

    public class CommandHandler
    {
        public static IServiceProvider _provider;
        public static DiscordSocketClient _discord;
        public static CommandService _commands;
        public static IConfigurationRoot _config;

        public CommandHandler(DiscordSocketClient discord, CommandService commands, IConfigurationRoot config, IServiceProvider provider)
        {
            _provider = provider;
            _config = config;
            _discord = discord;
            _commands = commands;

            _discord.Ready += OnReady;
            _discord.MessageReceived += OnMessageReceived;
        }

        private async Task OnMessageReceived(SocketMessage arg)
        {
            var msg = arg as SocketUserMessage;

            if (msg.Author.IsBot) return;
            var context = new SocketCommandContext(_discord, msg);

            int pos = 0;
            if(msg.HasStringPrefix(_config["prefix"], ref pos) || msg.HasMentionPrefix(_discord.CurrentUser, ref pos))
            {
                var result = await _commands.ExecuteAsync(context, pos, _provider);

                if (!result.IsSuccess)
                {
                    var reason = result.Error;

                    await context.Channel.SendMessageAsync($"The following error occured: \n{reason}");
                    Console.WriteLine(reason);
                }
            }
        }

        private Task OnReady()
        {
            Console.WriteLine($"Logged into {_discord.CurrentUser.Username}#{_discord.CurrentUser.Discriminator}");
            return Task.CompletedTask;
        }
    }
}

Commands:

using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace fucking_kill_me.Modules
{
    class GeneralCommands : ModuleBase
    {
        [Command("ping")]
        public async Task Ping()
        {
            await Context.Channel.SendMessageAsync("PongBama");
        }

    }
}

Some Help would be deeply appreciated, Thanks!

PS: if you need any more files for this, please let me know. I used this video as reference, https://www.youtube.com/watch?v=uOV1rg_ecMo&t=6s

Upvotes: 0

Views: 2780

Answers (2)

marens101
marens101

Reputation: 101

Module classes need to be public for Discord.NET to pick them up.

Try changing class GeneralCommands : ModuleBase to public class GeneralCommands : ModuleBase

Upvotes: 2

Cuppyzh
Cuppyzh

Reputation: 142

I tried to compare your code with my code, the thing that I noticed are missing from your code is _commands.AddModulesAsync.

So In my code I add GeneralCommands into service collection and added it to _commands.AddModulesAsync.

Here what it's looked like

private readonly ServiceProvider _serviceProvider = ServiceProviderUtilities.ConfigureServices();

public async Task MainAsync{
     ...
     await _commandService.AddModulesAsync(Assembly.GetEntryAssembly(), _serviceProvider);
      ...
      _client.MessageReceived += MessageReceivedAsync;
     ...
}

public class ServiceProviderUtilities
    {
        public static ServiceProvider ConfigureServices()
        {
            return new ServiceCollection()
                .AddSingleton<GeneralCommands>()
                .BuildServiceProvider();
        }
    }

Upvotes: 0

Related Questions