1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Discord.py logging in multiple times when restarted

Discussão em 'Python' iniciado por Stack, Novembro 30, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a project that has a Discord client running in a background thread to listen to incoming messages. I noticed each time the client is stopped & restarted, it logs in 1 extra time, as shown below.

    The session ID is the same for each group of logins but different between groups.

    [2025-11-30 13:39:56] [INFO ] discord.client: Logging in using static token.
    [2025-11-30 13:39:56] [INFO ] discord.http: Found user agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36, build number 472914.
    [2025-11-30 13:39:57] [INFO ] discord.gateway: Connected to Gateway (Session ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx).
    Ready. 4 sessions, 0 connections, 10 relationships
    Closed
    [2025-11-30 13:40:05] [INFO ] discord.client: Logging in using static token.
    [2025-11-30 13:40:05] [INFO ] discord.client: Logging in using static token.
    [2025-11-30 13:40:05] [INFO ] discord.http: Found user agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36, build number 472914.
    [2025-11-30 13:40:05] [INFO ] discord.http: Found user agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36, build number 472914.
    [2025-11-30 13:40:06] [INFO ] discord.gateway: Connected to Gateway (Session ID: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy).
    [2025-11-30 13:40:06] [INFO ] discord.gateway: Connected to Gateway (Session ID: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy).
    Ready. 4 sessions, 0 connections, 10 relationships
    Closed
    [2025-11-30 13:40:13] [INFO ] discord.client: Logging in using static token.
    [2025-11-30 13:40:13] [INFO ] discord.client: Logging in using static token.
    [2025-11-30 13:40:13] [INFO ] discord.client: Logging in using static token.
    [2025-11-30 13:40:13] [INFO ] discord.http: Found user agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36, build number 472914.
    [2025-11-30 13:40:13] [INFO ] discord.http: Found user agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36, build number 472914.
    [2025-11-30 13:40:13] [INFO ] discord.http: Found user agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36, build number 472914.
    [2025-11-30 13:40:14] [INFO ] discord.gateway: Connected to Gateway (Session ID: zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz).
    [2025-11-30 13:40:14] [INFO ] discord.gateway: Connected to Gateway (Session ID: zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz).
    [2025-11-30 13:40:14] [INFO ] discord.gateway: Connected to Gateway (Session ID: zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz).
    Ready. 4 sessions, 0 connections, 10 relationships
    Closed


    Below is basic code I wrote to test this bug and get the output above. I have both discord.py and discord.py-self installed.

    import discord
    from discord.ext import tasks
    import threading
    import time

    USER_TOKEN = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'

    class BgThread(threading.Thread):
    def run(self):
    self.client = self.DiscordClient()
    try:
    self.client.run(USER_TOKEN) # blocking
    except Exception as ex:
    print('The DiscordClient errored.')
    print(str(type(ex)))
    print(str(ex))
    print(str(traceback.format_exc()))
    print('Closed')

    class DiscordClient(discord.Client):
    def __init__(self):
    super().__init__()
    self.shouldStop = False

    async def on_ready(self):
    print('Ready. ' + str(len(self.sessions)) + ' sessions, ' + str(len(self.connections)) + ' connections, ' + \
    str(len(self.relationships)) + ' relationships')
    self.checkShouldStop.start()

    @tasks.loop(seconds=1.0)
    async def checkShouldStop(self):
    if self.shouldStop:
    await self.close()

    if __name__ == '__main__':
    while True:
    # Make and run a new background thread.
    bgThread = BgThread()
    bgThread.start()
    time.sleep(5)

    # Stop the background thread.
    bgThread.client.shouldStop = True
    while (bgThread.is_alive()):
    time.sleep(0.5)
    time.sleep(3)


    Here is a filtered Wireshark capture from running the code above. The filter is basically (ip.src==anyOfDiscordsIPs || ip.dst==anyOfDiscordsIps) && (tcp.connection.syn || tcp.connection.fin || tls.handshake.type==1 || tls.handshake.type==2). Discord has 10 IP addresses according to the DNS response I received, so the filter is decently long.

    The Wireshark capture appears the same for each of the 3 times the Discord client connected. That, plus the number of sessions not increasing in my program's "Ready" output each time, tells me this is most likely just a logging issue and not a real "logging in multiple times" issue. However, I am not confident in this conclusion.

    [​IMG]

    Anyone know why this bug is happening? I'm making a brand new BgThread object each time, so there's also a brand new discord.Client object each time.

    Continue reading...

Compartilhe esta Página