I’m in the process of creating a Discord bot, I can’t seem to get it working. Whenever I run main.py it returns with the error message [2024-07-02 17:20:46] [INFO ] discord.client: logging in using static token
. My bot token seems up to date in my .env file and it matches with the discord bot token in the Developer portal. This is the code I have in main.py
from typing import Final
import os
from dotenv import load_dotenv
from discord import Intents, Client, Message
from responses import get_response
load_dotenv()
TOKEN: Final[str] = os.getenv('DISCORD_TOKEN')
intents: Intents = Intents.default()
intents.message_content = True
client: Client = Client(intents=intents)
async def send_message(message: Message, user_message: str) -> None:
if not user_message:
print('Message empty because intents were not enabled probably.')
return
is_private = user_message[0] == '?'
if is_private:
user_message = user_message[1:]
try:
response: str = get_response(user_message)
if is_private:
await message.author.send(response)
else:
await message.channel.send(response)
except Exception as e:
print(f'Error sending message: {e}')
@client.event
async def on_ready() -> None:
print(f'{client.user} is now running!')
@client.event
async def on_message(message: Message) -> None:
if message.author == client.user:
return
username: str = str(message.author)
user_message: str = message.content
channel: str = str(message.channel)
print(f'[{channel}] {username}: "{user_message}"')
await send_message(message, user_message)
def main() -> None:
client.run(TOKEN)
if __name__ == '__main__':
main()
None of the posts I have found helped me on this, and I was expecting an easier time with this and having it work. I followed a tutorial as I am relatively new in this topic.
Here is my error message in full:
[2024-07-02 17:25:00] [INFO ] discord.client: logging in using static token
Traceback (most recent call last):
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/aiohttp/connector.py", line 1025, in _wrap_create_connection
return await self._loop.create_connection(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py", line 1126, in create_connection
transport, protocol = await self._create_connection_transport(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py", line 1159, in _create_connection_transport
await waiter
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/sslproto.py", line 575, in _on_handshake_complete
raise handshake_exc
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/sslproto.py", line 557, in _do_handshake
self._sslobj.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ssl.py", line 917, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/siddharthn/Python/DiscordBot/main.py", line 52, in <module>
main()
File "/Users/siddharthn/Python/DiscordBot/main.py", line 49, in main
client.run(TOKEN)
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/discord/client.py", line 869, in run
asyncio.run(runner())
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 194, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py", line 664, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/discord/client.py", line 858, in runner
await self.start(token, reconnect=reconnect)
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/discord/client.py", line 786, in start
await self.login(token)
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/discord/client.py", line 620, in login
data = await self.http.static_login(token)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/discord/http.py", line 816, in static_login
data = await self.request(Route('GET', '/users/@me'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/discord/http.py", line 638, in request
async with self.__session.request(method, url, **kwargs) as response:
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/aiohttp/client.py", line 1197, in __aenter__
self._resp = await self._coro
^^^^^^^^^^^^^^^^
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/aiohttp/client.py", line 581, in _request
conn = await self._connector.connect(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/aiohttp/connector.py", line 544, in connect
proto = await self._create_connection(req, traces, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/aiohttp/connector.py", line 944, in _create_connection
_, proto = await self._create_direct_connection(req, traces, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/aiohttp/connector.py", line 1257, in _create_direct_connection
raise last_exc
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/aiohttp/connector.py", line 1226, in _create_direct_connection
transp, proto = await self._wrap_create_connection(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/siddharthn/Python/DiscordBot/.venv/lib/python3.12/site-packages/aiohttp/connector.py", line 1027, in _wrap_create_connection
raise ClientConnectorCertificateError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorCertificateError: Cannot connect to host discord.com:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)')]
user25953155 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.