[Python] Why does it say "Improper token has been passed" when trying to use discord.py to...

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    Whenever I try to run my code, it tells me it is logging in, then that it found a user agent, after which it offers me an error. Whenever I tried using the exact same code on a normal discord account it worked perfectly fine, but I cannot get it to work while using an official discord application that I made.

    Below is my code


    import discord
    from discord.ext import commands
    # in player.py I have commented out import audioop in line 28


    prefix = ["Yombot, ", "YomBot, ", "Yb, ", "YB, "]


    token = "token from bot tab in discord developer portal"
    client = commands.Bot(command_prefix=prefix, bot=True)


    @client.event
    async def on_ready():
    print(f'Yombot has connected to Discord!')


    client.run(token)


    above is what I tried


    What I expected: For the bot to log in and be operational


    What ended up happening: I got the error(s)

    E:\yombot\venv\Scripts\python.exe E:\yombot\main.py
    2025-02-10 00:23:55 INFO discord.client Logging in using static token.
    2025-02-10 00:23:56 INFO discord.http Found user agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36, build number 366955.
    Traceback (most recent call last):
    File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\discord\http.py", line 994, in static_login
    data = await self.get_me()
    ^^^^^^^^^^^^^^^^^^^
    File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\discord\http.py", line 838, in request
    raise HTTPException(response, data)
    discord.errors.HTTPException: 401 Unauthorized (error code: 0): 401: Unauthorized

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
    File "E:\yombot\main.py", line 19, in <module>
    client.run(token)
    ~~~~~~~~~~^^^^^^^
    File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\discord\client.py", line 938, in run
    asyncio.run(runner())
    ~~~~~~~~~~~^^^^^^^^^^
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.13_3.13.752.0_x64__qbz5n2kfra8p0\Lib\asyncio\runners.py", line 195, in run
    return runner.run(main)
    ~~~~~~~~~~^^^^^^
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.13_3.13.752.0_x64__qbz5n2kfra8p0\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.13_3.13.752.0_x64__qbz5n2kfra8p0\Lib\asyncio\base_events.py", line 725, in run_until_complete
    return future.result()
    ~~~~~~~~~~~~~^^
    File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\discord\client.py", line 927, in runner
    await self.start(token, reconnect=reconnect)
    File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\discord\client.py", line 857, in start
    await self.login(token)
    File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\discord\client.py", line 698, in login
    data = await state.http.static_login(token.strip())
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\me\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\discord\http.py", line 998, in static_login
    raise LoginFailure('Improper token has been passed') from exc
    discord.errors.LoginFailure: Improper token has been passed

    Continue reading...

Compartilhe esta Página