【问题标题】:UserJoined event is not being called未调用 UserJoined 事件
【发布时间】: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


【解决方案1】:

10 月底,Discord 强制执行 Privileged Intents,这是他们对 Bots 的 previously announced 更改之一。要启用它们,您需要访问 Discord Developer Portal,选择您的机器人,单击“机器人”选项卡并导航到“特权意图”部分以启用意图。

查看previous post,这是缺少意图的另一个副作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 2014-03-06
    • 2015-11-14
    • 2021-04-24
    相关资源
    最近更新 更多