-1

I was trying to make a new Discord Bot with Discord.NET packages and I think it's done. Only command codes are waiting. Now I couldn't figure out what's this saying and how can I get this over with.

I tried to change "AddModulesAsync" to "AddModuleAsync" but it's still giving an error.

namespace AsunaBot
{
    class Program
    {
        static void Main(string[] args) => new Program().RunBotAsync().GetAwaiter().GetResult();
        private DiscordSocketClient _client;
        private CommandService _commands;
        private IServiceProvider _services;

        public async Task RunBotAsync()
        {
            _client = new DiscordSocketClient();
            _commands = new CommandService();
            _services = new ServiceCollection()
                .AddSingleton(_client)
                .AddSingleton(_commands)
                .BuildServiceProvider();

            string botToken = "TOKEN_HERE";
            _client.Log += Log;
            await RegisterCommandsAsync();
            await _client.LoginAsync(TokenType.Bot, botToken);
            await _client.StartAsync();
            await Task.Delay(-1);
        }

        private Task Log(LogMessage arg)
        {
            Console.WriteLine(arg);
            return Task.CompletedTask;
        }

        public async Task RegisterCommandsAsync()
        {
            _client.MessageReceived += HandleCommandAsync;
            await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
        }

        private async Task HandleCommandAsync(SocketMessage arg)
        {
            var message = arg as SocketUserMessage;
            if (message is null || message.Author.IsBot) return;
            int argPos = 0;
            if (message.HasStringPrefix("tnt!", ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))
            {
                var context = new SocketCommandContext(_client, message);
                var result = await _commands.ExecuteAsync(context, argPos, _services);
                if (!result.IsSuccess)
                    Console.WriteLine(result.ErrorReason);
            }
        }
    }
}

If it works, I can run my bot to give some commands, Discord

WQYeo
  • 3,973
  • 2
  • 17
  • 26
  • What error do you get when using [`AddModulesAsync()`](https://discord.foxbot.me/docs/api/Discord.Commands.CommandService.html#Discord_Commands_CommandService_AddModulesAsync_Assembly_)? (If you try to use [`AddModulesAsync()`](https://discord.foxbot.me/docs/api/Discord.Commands.CommandService.html#Discord_Commands_CommandService_AddModuleAsync__1) without specifying a type, then you will indeed get an error that type arguments are required.) – stuartd Jan 28 '19 at 16:09
  • The type arguments for method 'CommandService.AddModuleAsync(IServiceProvider)' cannot be inferred from the usage. Try specifying the type arguments explicitly. – Dogukan Aydin Jan 28 '19 at 16:12
  • Yes, but in the code you have posted to the question you aren't calling that, you're calling `AddModulesAsync(Assembly.GetEntryAssembly());` - what error do you get with that? – stuartd Jan 28 '19 at 16:25
  • Ok, then can I write like this? is this the true usage, what do you think? await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services); – Dogukan Aydin Jan 28 '19 at 16:41
  • The [docs](https://discord.foxbot.me/docs/api/Discord.Commands.CommandService.html#Discord_Commands_CommandService_AddModulesAsync_Assembly_) I linked to above suggest you just have to pass the correct assembly to `AddModulesAsync()` – stuartd Jan 28 '19 at 16:49
  • Do not publicly show your discord's bot token. Anyone with your bot's token can access your bot. – WQYeo Jan 28 '19 at 18:41

1 Answers1

0

Thank you to @stuartd for the reply. I missed it. AdModulesAsync() needs one more argument. I wrote there _services and it works perfectly and thanks to @Kaynn for hiding my Token, I revealed that in my first question post. New code looks like this: await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);