Search code examples
c#.netdiscorddiscord.net

UserJoined event is not being called


I'm developing a bot for Discord and it has worked quite fine until recently when the bot stopped calling the event UserJoined and UserLeft. I've put the breakpoint inside the method of UserJoined but it's not called anymore. The program doesn't go there. Also no exceptions, just nothing happens. I've been using 2.1.1 version of the Discord.NET, now to try and resolve the issue i tried to upgrade the project to 2.2.0. The method is still not called.

Any ideas what's wrong?

  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 and MessageDeleted work fine.


Solution

  • At the end of October, Discord enforced Privileged Intents, one of their previously announced changes for Bots. TO enable them you'd need to visit the Discord Developer Portal, select your bot, click the Bot tab and navigate to the Privileged Intents section to enable the intents.

    See this previous post which is another side effect of missing intents.