#Bot thinks its connected but isn't...

1 messages · Page 1 of 1 (latest)

dusty breach
#

I am using a discord bot as a logger to for an ASP.NET api. Its running as a background process

i just noticed that my logger hasn't logged anything in a month. so i dug into the process and noticed something weird. here's my simplified logging code:

 if (_client.ConnectionState == ConnectionState.Connected) {
...
}
else {
  Console.WriteLine($"Attempted to send a log to discord but bot is {_client.ConnectionState}:" +
  $"\n\t{log.Message}");
}

the point of this part of the code is to send logs to discord if your connected, but otherwise to dump the message on the console.

here's the console when i start the API:

Attempted to send a log to discord but bot is Disconnected:
        Discord.Net v3.15.3 (API v10)
Attempted to send a log to discord but bot is Connecting:
        Connecting
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5154
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Users\<username>\Desktop\Ward Management\MyWardDataLayer\MyWardDataLayer
Attempted to send a log to discord but bot is Connecting:
        Connected

the last line is the head-scratcher. why does the system think its connected?!

#

Bot thinks its connected but isn't...

coral warren
#

Have you tried debugging? To see what _client.ConnectionState actually is when it’s trying, since it going to else.

dusty breach
coral warren
#

I assume that’s a no then

dusty breach
#

in the last case the system tryes to log the message "connected" and my system sees that the bot is not connnected and sends that to the console with the message:

Attempted to send a log to discord but bot is Connecting:
        Connected

so no i didnt step though the program for this output, but I've done the next best thing. if the variable and the debugger disagree ive got bigger problems

coral warren
#

Just put a breakpoint to find out. Thought it would be interesting to see what ConnectionState it is since it doesn’t think it’s Connected.

#

Also it’s asynchronously so it might have changed state before it sends log

young heron
#

Is there any console logs after that one or does it start logging to discord

dusty breach
#

that throws a wrench in things...

#

the issue is that i the system never sends the ready log

dusty breach
#

it briefly shows disconected before changing to connecting while i am in the breakpoint...

#

is this something weird with async? i don't really understand how async works... i am kind of just winging it on that.

young heron
dusty breach
#

when i look at it on the server, yea

#

so somehow the bot thinks its connecting to discords backend but its not

coral warren
#

You could add some listeners instead.

young heron
#

Try hooking into the ready event and just have it put a console log when its fired

coral warren
#
_client.Disconnected += async (ex) => Console.WriteLine($"Bot disconnected. Reason: {ex?.Message}");
_client.Ready += async () => Console.WriteLine("Bot is ready.");```
#

Idk how to format as code on phone lol

young heron
coral warren
#

Yeah don’t have that symbol on my keyboard lmao

young heron
#

Its under symbols

dusty breach
#

Or on iPhone

#

Just this: ‘

coral warren
#

Aaaah

#

There we go had to hold down the ‘ symbol to pick the other one

dusty breach
#

test

#

Look at that

young heron
dusty breach
#

ok. more logging implimented

#

didnt help... i am looping though a list with one element and the loop tries to loop though a second item which is, unsuprisingly, null.

young heron
#

What is the exception that you get on the ready event

dusty breach
#

system.nullreferanceexception

#
List<SocketGuild> guildList = _client.Guilds.ToList();
foreach (SocketGuild guild in guildList) {
    if (guild != null) {
        await CheckIfGuildAllowed(guild);
    }
    
}

system loops though the first (and only) guild in the list and then it jumps to in and throws the exception

#

i am looking though the values of the exception rn trying to find what object its refering to to be sure.

#

when i am stepping though the program the last step is in, but the message of the exception says int on the checkIfGuildAllowed() line. i assume this is some quirk of async.

coral warren
#

What does checkifguildallowed ?

#

What does it execute

dusty breach
#

its checking the server against a list of allowed servers and if its a member of a server thats not allowed it leaves.

#

HA! i applied the wrong token! its trying to run as a differant bot which is a member of servers not on the allow its

#

then it leaves the guild which makes the guild null which screws the loop

#

i do need to fix this, i need this system to not crash if its a member of a server that's not on the whitelist, but i know the problem now.

#

man async is weird...

coral warren
#

Basically, async marks a method as asynchronous, allowing it to perform non-blocking operations and return a Task or Task<T> that represents ongoing work. await pauses the execution of an async method until the awaited task completes, allowing the program to perform other work in the meantime.

dusty breach
#

yea, but it also screws the order of operations which makes debugging a nightmare. I see its nesisary and useful, but when i am trying to debug somthing i wish it would just do it the slow way

#

ok, ive clearly missunderstood the situation.

#

i redesigned they system to save a list of guilds to remove then remove them later, but i am still throwing an exception:

List<SocketGuild> guildList = _client.Guilds.ToList();
// make sure the bot is not in a guild that is not allowed
List<SocketGuild> guildsToLeave = new List<SocketGuild>();
foreach (SocketGuild guild in guildList) {
   if (!await CheckIfGuildAllowed(guild)) {
       guildsToLeave.Add(guild);
   }
}

foreach (SocketGuild guild in guildsToLeave) {
   await guild.LeaveAsync();
}

private static async Task<bool> CheckIfGuildAllowed(SocketGuild guild) {
if (_client.Guilds.Contains(guild)) {
   Debug.Assert(config != null, nameof(config) + " != null");
   if (!config.AllowedServers.Contains(guild.Id)) {
       await LogAsync(LogSeverity.Critical, $"errormsg");
       return false;
   }
}

return true;
}

whats up with this? why does the loop try and loop a second time? what is it pointing to thats null and why dont the stack trace and the debugger line up?

dusty breach
#

found it.