无法执行命令
Unable to execute command
我为我的 discord 机器人写了一个简单的模块。
机器人:
_client = new DiscordSocketClient();
_commandService = new CommandService();
_serviceProvider = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commandService)
.BuildServiceProvider();
模块:
public class MyModule: ModuleBase<ICommandContext>
{
private readonly MyService _service;
public MyModule(MyService service)
{
_service = service;
}
[Command("DoStuff", RunMode = RunMode.Async)]
public async Task DoStuffCmd()
{
await _service.DoStuff(Context.Guild, (Context.User as IVoiceState).VoiceChannel);
}
}
模块是这样添加的:
await _commandService.AddModulesAsync(Assembly.GetEntryAssembly());
显式添加模块将导致异常,即已添加此模块,因此我认为它有效。
我是这样处理命令的。
// Create a number to track where the prefix ends and the command begins
int argPos = 0;
// Determine if the message is a command, based on if it starts with '!' or a mention prefix
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return;
// Create a Command Context
var context = new CommandContext(_client, message);
// Execute the command. (result does not indicate a return value,
// rather an object stating if the command executed successfully)
var result = await _commandService.ExecuteAsync(context, argPos, _serviceProvider);
result
变量总是 returns Success
但 DoStuffCmd
中的方法 MyModule
永远不会被调用。
我在这里错过了什么?
您似乎没有将 MyService
注入您的 ServiceCollection
。
如果您不注入服务,您的模块将永远无法创建,因为您将其定义为依赖项
private readonly MyService _service;
public MyModule(MyService service)
{
_service = service;
}
要解决此问题,您可以将 MyService
添加到 ServiceCollection
。
为此,最好创建一个 IMyService
(接口)并将其添加到您的注入中
_serviceProvider = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commandService)
.AddSingleton<IMyService, MyService>()
.BuildServiceProvider();
我为我的 discord 机器人写了一个简单的模块。
机器人:
_client = new DiscordSocketClient();
_commandService = new CommandService();
_serviceProvider = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commandService)
.BuildServiceProvider();
模块:
public class MyModule: ModuleBase<ICommandContext>
{
private readonly MyService _service;
public MyModule(MyService service)
{
_service = service;
}
[Command("DoStuff", RunMode = RunMode.Async)]
public async Task DoStuffCmd()
{
await _service.DoStuff(Context.Guild, (Context.User as IVoiceState).VoiceChannel);
}
}
模块是这样添加的:
await _commandService.AddModulesAsync(Assembly.GetEntryAssembly());
显式添加模块将导致异常,即已添加此模块,因此我认为它有效。
我是这样处理命令的。
// Create a number to track where the prefix ends and the command begins
int argPos = 0;
// Determine if the message is a command, based on if it starts with '!' or a mention prefix
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return;
// Create a Command Context
var context = new CommandContext(_client, message);
// Execute the command. (result does not indicate a return value,
// rather an object stating if the command executed successfully)
var result = await _commandService.ExecuteAsync(context, argPos, _serviceProvider);
result
变量总是 returns Success
但 DoStuffCmd
中的方法 MyModule
永远不会被调用。
我在这里错过了什么?
您似乎没有将 MyService
注入您的 ServiceCollection
。
如果您不注入服务,您的模块将永远无法创建,因为您将其定义为依赖项
private readonly MyService _service;
public MyModule(MyService service)
{
_service = service;
}
要解决此问题,您可以将 MyService
添加到 ServiceCollection
。
为此,最好创建一个 IMyService
(接口)并将其添加到您的注入中
_serviceProvider = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commandService)
.AddSingleton<IMyService, MyService>()
.BuildServiceProvider();