.net core 如何修复';MessageReceived处理程序正在阻止网关任务;不和谐
我正试图建立我自己的不和谐机器人,软禁令的人,谁写种族主义或反犹太主义的话。 我尝试使用MessageReceivedAsync执行此操作,但它总是崩溃,错误为“MessageReceived处理程序正在阻止网关任务。” 以下是my Program.cs的代码:.net core 如何修复';MessageReceived处理程序正在阻止网关任务;不和谐,.net-core,discord,discord.net,.net Core,Discord,Discord.net,我正试图建立我自己的不和谐机器人,软禁令的人,谁写种族主义或反犹太主义的话。 我尝试使用MessageReceivedAsync执行此操作,但它总是崩溃,错误为“MessageReceived处理程序正在阻止网关任务。” 以下是my Program.cs的代码: namespace NoNetworcc { class Program : ModuleBase<SocketCommandContext> { static void Main(string[]
namespace NoNetworcc
{
class Program : ModuleBase<SocketCommandContext>
{
static void Main(string[] args)
=> new Program().MainAsync().GetAwaiter().GetResult();
public async Task MainAsync()
{
using (var services = ConfigureServices())
{
var client = services.GetRequiredService<DiscordSocketClient>();
client.Log += LogAsync;
client.MessageReceived += MessageReceivedAsync;
services.GetRequiredService<CommandService>().Log += LogAsync;
await client.LoginAsync(TokenType.Bot, "NzEyNDA2MDAxMjAxOTcxMjcw.XsRF4A.8YENNInx3D4kqJyK9N8xjTU3mcs");
await client.StartAsync();
await services.GetRequiredService<CommandHandlingService>().InitializeAsync();
await Task.Delay(Timeout.Infinite);
}
}
private Task LogAsync(LogMessage log)
{
Console.WriteLine(log.ToString());
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(SocketMessage message)
{
using (BlacklistDatabaseContext lite = new BlacklistDatabaseContext())
{
var blacklistWords = lite.BlacklistWords;
foreach(var word in blacklistWords)
{
if(message.Content.Contains(word.Blacklistword.ToString()))
{
ulong roleID = 756500011331616840;
var role = Context.Guild.GetRole(roleID);
await ((IGuildUser)Context.User).AddRoleAsync(role);
await message.Channel.SendMessageAsync($"{Context.User} got softbanned for using the word '{word}'");
}
}
}
}
private ServiceProvider ConfigureServices()
{
return new ServiceCollection()
.AddSingleton<DiscordSocketClient>()
.AddSingleton<CommandService>()
.AddSingleton<CommandHandlingService>()
.AddSingleton<HttpClient>()
.AddSingleton<PictureService>()
.BuildServiceProvider();
}
}
}
namespace-NoNetworcc
{
课程名称:ModuleBase
{
静态void Main(字符串[]参数)
=>新程序().MainAsync().GetAwaiter().GetResult();
公共异步任务mainsync()
{
使用(var services=ConfigureServices())
{
var client=services.GetRequiredService();
client.Log+=LogAsync;
client.MessageReceived+=MessageReceivedAsync;
services.GetRequiredService().Log+=LogAsync;
等待client.LoginAsync(TokenType.Bot,“NzEyNDA2MDAxMjAxOTcxMjcw.XsRF4A.8YENNInx3D4kqJyK9N8xjTU3mcs”);
等待client.StartAsync();
wait services.GetRequiredService().InitializeAsync();
等待任务。延迟(超时。无限);
}
}
专用任务日志异步(日志消息日志)
{
Console.WriteLine(log.ToString());
返回Task.CompletedTask;
}
专用异步任务消息ReceivedAsync(SocketMessage消息)
{
使用(BlacklistDatabaseContext lite=new BlacklistDatabaseContext())
{
var blacklistWords=lite.blacklistWords;
foreach(blacklistWords中的var单词)
{
if(message.Content.Contains(word.Blacklistword.ToString()))
{
ulong roleID=7565000113311616840;
var role=Context.Guild.GetRole(roleID);
wait((IGuildUser)Context.User).AddRoleAsync(角色);
wait message.Channel.SendMessageAsync($“{Context.User}因使用单词“{word}”而被软禁止);
}
}
}
}
专用服务提供商配置服务()
{
返回新的ServiceCollection()
.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddSingleton()
.BuildServiceProvider();
}
}
}
这是我的HandlingService代码:
namespace NoNetworcc.Services
{
public class CommandHandlingService
{
private readonly CommandService _commands;
private readonly DiscordSocketClient _discord;
private readonly IServiceProvider _services;
public CommandHandlingService(IServiceProvider services)
{
_commands = services.GetRequiredService<CommandService>();
_discord = services.GetRequiredService<DiscordSocketClient>();
_services = services;
_commands.CommandExecuted += CommandExecutedAsync;
_discord.MessageReceived += MessageReceivedAsync;
}
public async Task InitializeAsync()
{
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
}
public async Task MessageReceivedAsync(SocketMessage rawMessage)
{
if (!(rawMessage is SocketUserMessage message)) return;
if (message.Source != MessageSource.User) return;
var argPos = 0;
var context = new SocketCommandContext(_discord, message);
await _commands.ExecuteAsync(context, argPos, _services);
}
public async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandContext context, IResult result)
{
if (!command.IsSpecified)
return;
if (result.IsSuccess)
return;
await context.Channel.SendMessageAsync($"error: {result}");
}
}
}
namespace NoNetworcc.Services
{
公共类命令处理服务
{
专用只读命令服务(CommandService)命令;;
私有只读DiscordSocketClient _discord;
私人只读阅读器ViceProvider服务;
公共命令处理服务(IServiceProvider服务)
{
_commands=services.GetRequiredService();
_discord=services.GetRequiredService();
_服务=服务;
_commands.CommandExecuted+=CommandExecutedAsync;
_discord.MessageReceived+=MessageReceivedAsync;
}
公共异步任务InitializeAsync()
{
wait_commands.AddModulesAsync(Assembly.GetEntryAssembly(),_服务);
}
公共异步任务消息ReceivedAsync(SocketMessage rawMessage)
{
如果(!(rawMessage为SocketUserMessage))返回;
如果(message.Source!=MessageSource.User)返回;
var argPos=0;
var context=new SocketCommandContext(_discord,message);
wait_命令.ExecuteAsync(上下文、argPos、_服务);
}
公共异步任务命令ExecutedAsync(可选命令、ICommandContext上下文、IResult结果)
{
如果(!command.IsSpecified)
返回;
如果(结果。发布成功)
返回;
wait context.Channel.SendMessageAsync($“错误:{result}”);
}
}
}
如何解决此问题?专用任务消息ReceivedAsync(SocketMessage消息){
_=Task.Run(异步()=>{
使用(BlacklistDatabaseContext lite=new BlacklistDatabaseContext()){
var blacklistWords=lite.blacklistWords;
foreach(blacklistWords中的var单词){
if(message.Content.Contains(word.Blacklistword.ToString())){
ulong roleID=7565000113311616840;
var role=(message.Channel作为ITextChannel)?.Guild.GetRole(roleID);
if(角色!=null){
wait(message.Author as SocketGuildUser)?.AddRoleAsync(角色);
wait message.Channel.SendMessageAsync($“{message.Author}因使用单词“{word}”而被禁止);
}
}
}
}
});
返回Task.CompletedTask;
}
处理程序中的代码花费的时间太长。任何需要超过3秒钟才能执行的操作都会阻止记录该消息。任何长时间运行的代码都应该在另一个任务中执行(即不是网关任务)。@anu6例如,我该如何执行?你能在使用我的代码时给我看一下吗?只需将黑名单事件处理程序中的所有逻辑包装在Task.Run()
中即可。另外,您的程序.cs
不应继承自模块库
,上下文
在该类中将无效,因为它从未实际创建过。@Anu6is我仍然不明白它。你能在使用Task.Run()时重写我的代码吗?但是现在,我不能使用“using(BlacklistDatabaseContext lite=new BlacklistDatabaseContext()”,为什么不能使用它呢?它说的是“无效表达式”,与任务无关。Run()
检查你的代码语法代码语法与以前相同。我刚完成任务,绕过去