【问题标题】:it says "try specifying the type arguments explicitly". i have a problem with AddModulesAsync, tryin to make a bot它说“尝试明确指定类型参数”。我对 AddModulesAsync 有问题,尝试制作一个机器人
【发布时间】:2019-06-21 16:08:19
【问题描述】:

我试图用 Discord.NET 包制作一个新的 Discord Bot,我认为它已经完成了。只有命令代码在等待。现在我不明白这句话是什么意思,我该如何解决这个问题。

我尝试将“AddModulesAsync”更改为“AddModuleAsync”,但仍然报错。

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);
            }
        }
    }
}

如果可行,我可以运行我的机器人来发出一些命令,Discord

【问题讨论】:

  • 使用AddModulesAsync()时遇到什么错误? (如果您尝试使用AddModulesAsync<T>() 而不指定类型,那么您确实会收到需要类型参数的错误。)
  • 无法从用法中推断方法“CommandService.AddModuleAsync(IServiceProvider)”的类型参数。尝试明确指定类型参数。
  • 是的,但是在您发布到问题的代码中,您没有调用它,而是调用了AddModulesAsync(Assembly.GetEntryAssembly()); - 你会遇到什么错误?
  • 好的,那我可以这样写吗?这是真正的用法,你怎么看?等待_commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
  • 我上面链接的docs 建议您只需将正确的程序集传递给AddModulesAsync()

标签: c# bots discord invalidoperationexception argumentexception


【解决方案1】:

感谢@stuartd 的回复。我错过了。 AdModulesAsync() 还需要一个参数。我在那里写了_services,它运行良好,感谢@Kaynn 隐藏了我的令牌,我在我的第一个问题帖子中透露了这一点。新代码如下所示: await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 2021-06-27
    • 2021-08-20
    • 2019-10-24
    相关资源
    最近更新 更多