discord-net / Discord.Net

An unofficial .Net wrapper for the Discord API (https://discord.com/)
https://discordnet.dev
MIT License
3.29k stars 742 forks source link

[Bug]: Can't use Bearer token Login after v3.1.0 #2382

Closed MihalicMarko closed 1 year ago

MihalicMarko commented 2 years ago

Check The Docs

Verify Issue Source

Check your intents

Description

After upgrading from v3.1.0. I've started receiving Exception when using the Bearer token to Login.

Version

3.7.2

Working Version

3.1.0

Logs

Gateway: System.Exception: Processing READY failed
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Discord.WebSocket.DiscordSocketClient.ProcessMessageAsync(GatewayOpCode opCode, Nullable`1 seq, String type, Object payload)
   --- End of inner exception stack trace ---
   at Discord.ConnectionManager.WaitAsync()
   at Discord.WebSocket.DiscordSocketClient.OnConnectingAsync()
   at Discord.ConnectionManager.ConnectAsync(CancellationTokenSource reconnectCancelToken)
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()

Sample

using Discord;
using Discord.Commands;
using Discord.WebSocket;

internal class Startup
{
    private readonly String dateFormat = @"yyyy-MM-dd";
    private readonly String timeFormat = @"hh:mm:ss.fffffff";

    private readonly String _logDirectory;
    private readonly String _logFile;

    private String[] args;

    public Startup(String[] args)
    {
        this.args = args;

        _logDirectory = Path.Combine(AppContext.BaseDirectory, "logs");
        _logFile = Path.Combine(_logDirectory, $"{DateTime.UtcNow.ToString(dateFormat)}.txt");
    }

    internal async Task RunAsync()
    {
        var discordSocketClient = new DiscordSocketClient(new DiscordSocketConfig
        {
            LogLevel = LogSeverity.Verbose,
            MessageCacheSize = 1000
        });

        var commandService = new CommandService(new CommandServiceConfig
        {
            LogLevel = LogSeverity.Verbose,
            DefaultRunMode = RunMode.Async
        });

        discordSocketClient.MessageReceived += OnMessageReceivedAsync;

        discordSocketClient.Log += OnLogAsync;
        commandService.Log += OnLogAsync;

        await discordSocketClient.LoginAsync(TokenType.Bearer, @"XXX");
        await discordSocketClient.StartAsync();

        // Keep the application alive
        await Task.Delay(-1);
    }

    private Task OnLogAsync(LogMessage msg)
    {
        if (!Directory.Exists(_logDirectory))     // Create the log directory if it doesn't exist
            Directory.CreateDirectory(_logDirectory);
        if (!File.Exists(_logFile))               // Create today's log file if it doesn't exist
            File.Create(_logFile).Dispose();

        string logText = $"{DateTime.UtcNow.ToString(timeFormat)} [{msg.Severity}] {msg.Source}: {msg.Exception?.ToString() ?? msg.Message}";
        File.AppendAllText(_logFile, logText + "\n");     // Write the log text to a file

        return Console.Out.WriteLineAsync(logText);       // Write the log text to the console
    }

    private async Task OnMessageReceivedAsync(SocketMessage s)
    {

    }
}

Packages

Discord.Net V3.7.2

SylveonDeko commented 2 years ago

We dont support selfbots.

csmir commented 2 years ago

To the above, bearer does not imply self-botting, this is an irrelevant comment.

Could you go further into your use case, so I can attempt to repro?

csmir commented 2 years ago

May be related to #2381

csmir commented 2 years ago

Please follow up on the question above.

MihalicMarko commented 2 years ago

Hi, sorry, for the late reply, I was on vacation and forgot about it...

Regarding use-case: I was using it as an alternative to SignalR and developing the additional application (e.g. Guest comes to a Restaurant, joins Discord channel over QR code provided on that table, and calls waiter to his table...waiter can easily reply and owner can have some statistics regarding efficiency, favorite orders etc.) In the meantime, I've switched to TokenType.Bot and managed to solve my problem. I've used Bearer out of laziness (It's Standard and I didn't give too much thought about it + I didn't want to create and Setup a Bot Application) Didn't even know about selfbots before @Pusheon mentioned it so thx for the heads-up!

Regarding #2381 : It could be related, but mine was failing at Login, not at Calling other methods afterward (I'm not calling any except StartAsync) You should be able to reproduce the Bug with the Sample code I've provided above (You can remove logging to the file, shouldn't make any difference).

As I've mentioned v3.1.0 was working fine (except UserMessage didn't contain Content which was the reason why I've updated to v3.7.2), but all later versions were throwing the exception.

Hope that helps, be free to ask if you need any more information.

SarpedonTD commented 2 years ago

Ran into this as well. My use case is to register as applications rather than a bot and read from the gateway. Logging in as an application is only possible using a bearer token.