【发布时间】:2021-02-20 02:15:16
【问题描述】:
我正在为 Discord 开发一个机器人,它一直运行良好,直到最近该机器人停止调用事件 UserJoined 和 UserLeft。 我已将断点放在 UserJoined 的方法中,但它不再被调用。该程序不去那里。也没有例外,只是没有任何反应。 我一直在使用 Discord.NET 的 2.1.1 版本,现在尝试解决我尝试将项目升级到 2.2.0 的问题。该方法仍未被调用。
有什么想法吗?
class Program
{
static void Main(string[] args) => new
Program().RunBotAsync().GetAwaiter().GetResult();
private DiscordSocketClient _client;
private CommandService _commands;
private IServiceProvider _services;
private ISWAIService _shAiService;
public async Task RunBotAsync()
{
var config = new DiscordSocketConfig { MessageCacheSize = 100 };
_client = new DiscordSocketClient(config);
_commands = new CommandService();
_shAiService = new SWAIService();
_services = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commands)
.BuildServiceProvider();
_client.Log += _client_Log;
await RegisterCommandsAsync();
await _client.LoginAsync(TokenType.Bot, StaticSettingsService.GetTOKEN());
await _client.StartAsync();
await Task.Delay(-1);
}
private async Task RegisterCommandsAsync()
{
_client.MessageReceived += HandleCommandAsync;
_client.UserJoined += HandleUserJoinAsync;
_client.UserLeft += HandleUserLeftAsync;
_client.MessageDeleted += HandleMsgDeletedAsync;
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
}
private async Task HandleUserJoinAsync(SocketGuildUser arg)
{
var newUserRoleId = StaticSettingsService.GetNewUserRoleId();
var newUserRole = arg.Guild.GetRole((ulong)newUserRoleId);
if(newUserRole != null)
{
await arg.AddRoleAsync(newUserRole);
}
var modsChannel = _client.GetChannel(StaticSettingsService.GetModsSpamChannelId()) as SocketTextChannel;
await modsChannel.SendMessageAsync("User: username: " + arg.Username + " joined.");
}
}
MessageRecieved 和 MessageDeleted 工作正常。
【问题讨论】:
-
您需要在开发者门户中为您的机器人启用意图
-
@Anu6is 谢谢,它成功了!随意发布它作为答案,我会接受:)
标签: c# .net discord discord.net