I needed to change my bots start up behavior so database calls are part of the bot class.
Now there is 2 problems with this code. one is that slash commands don't show up anymore, the other is a small error message due to my lack of knowledge about asyncio.
Looking forward to your insight and ideas about it!
Here is the code:
from discord.ext import commands
import asyncio
import discord
import aiomysql
import database_access as dba
class Boot(discord.Bot):
"""
overwrites bot init
"""
def __init__(self, pool: aiomysql.Pool):
intents = discord.Intents.default()
intents.members = True # noqa
# Includes/uses the initial bot init
super().__init__(intents=intents, activity=discord.Game(name="SQL tests"))
# Makes Database-access part of the bot
self.db: dba.Database = dba.Database()
self.pool = pool
async def on_ready(self):
"""
define what the bot does at start up
"""
print(f'Logged on as {self.user}!')
# 103676.. is the id of my test server
@commands.slash_command(name="bla", description="does nothing", guild_ids=[1036765308498739230])
async def bla(self, ctx: discord.ApplicationContext):
await ctx.respond("Hey there!")
async def start_up(token, loop):
# EventLoop starten und `task` ausführen
pool = await aiomysql.create_pool(
host='localhost', port=3306, user='root',
password='some_password', db='some_table_name', loop=loop
)
booty = Boot(pool)
await booty.start(token)
if __name__ == "__main__":
with open("token.txt", "r") as file:
key = file.read()
loop_one = asyncio.get_event_loop()
loop_one.run_until_complete(start_up(token=key, loop=loop_one))
Here is the error or rather warning:
DeprecationWarning: There is no current event loop
loop_one = asyncio.get_event_loop()
EDIT:
The warning points to this line:
"loop_one = asyncio.get_event_loop()"