Discord.NET引入Discord.Addons.Interactive后报CommandContext读取器缺失错误
Discord.NET 引入Discord.Addons.Interactive后启动抛出No type reader for CommandContext异常
问题描述
- 开发背景:Discord机器人开发新手,具备C#开发基础
- 前置状态:原有基于Discord.NET开发的机器人可正常运行
- 异常触发条件:引入Discord.Addons.Interactive插件后,程序启动抛出异常:
System.InvalidOperationException: 'No type reader found for type CommandContext, one must be specified' - 已做操作:多次搜索未找到解决方案,核心代码如下
核心代码
Program.cs
class Program { public static void Main(string[] args) { new Program().MainAsync().GetAwaiter().GetResult(); } private DiscordSocketClient _client; private CommandService _commands; private IServiceProvider _services; public Program() { } public async Task MainAsync() { _client = new DiscordSocketClient(); _commands = new CommandService(); _services = new ServiceCollection() .AddSingleton(_client) .AddSingleton(_commands) .AddSingleton<CommandHandler>() .AddSingleton<InteractiveService>() .BuildServiceProvider(); _client.Log += LogAsync; _client.Ready += ReadyAsync; var tocken = "my_tocken"; await _client.LoginAsync(TokenType.Bot, tocken); await _client.StartAsync(); await _services.GetRequiredService<CommandHandler>().InstallCommandAsync(); await Task.Delay(-1); } private Task LogAsync(LogMessage msg) { Console.WriteLine(msg.ToString()); return Task.CompletedTask; } private Task ReadyAsync() { Console.WriteLine($"Connected as -> [{_client.CurrentUser}]"); return Task.CompletedTask; } }
CommandHandler.cs
public class CommandHandler { private readonly DiscordSocketClient _client; private readonly CommandService _commands; private readonly IServiceProvider _services; public CommandHandler(DiscordSocketClient client, CommandService commands, IServiceProvider services, InteractiveService interactions) { _commands = commands; _client = client; _services = services; _commands.CommandExecuted += CommandExecutedAsync; _client.MessageReceived += HandleCommandAsync; } public async Task InstallCommandAsync() { await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services); } private async Task HandleCommandAsync(SocketMessage messageParam) { var message = messageParam as SocketUserMessage; var context = new SocketCommandContext(_client, message); if (message.Author.IsBot) return; int argPos = 0; if (message.HasCharPrefix('-', ref argPos)) { await _commands.ExecuteAsync(context, argPos, _services); } } public async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandContext context, IResult result) { if (!command.IsSpecified) { Console.WriteLine($"Command failed to execute for [{context.User.Username}] <-> [{result.ErrorReason}]!"); return; } if (result.IsSuccess) { Console.WriteLine($"Command [{command.Value.Name}] execute for [{context.User.Username}]!"); return; } await context.Channel.SendMessageAsync($"{context.User.Username} Something went wrong -> [{result}]!"); } }
排查结论
问题根源为:某一个命令方法中错误添加了CommandContext类型的参数。
Discord.NET 命令框架会自动注入实现了ICommandContext的上下文实例(如SocketCommandContext),但CommandContext作为未注册类型读取器的基类,无法被框架自动解析,因此触发该异常。移除命令方法中多余的CommandContext类型参数即可解决问题。
内容的提问来源于stack exchange,提问作者Monkey
相关产品推荐
相关产品推荐

