#starting the bot properly with .start()

1 messages · Page 1 of 1 (latest)

formal loom
#

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()"
#

@neat dawn I am done editing, in case you wanted to see the post.

#

@midnight viper (ping as requested)

midnight viper
#

sup

#

soooo, there are few ways of "best practice" a bot.
guess send u an example before, but got a simpler example of how u work with commands and extensions if u need them.
its recommended to split your bot up into different files, not mandatory.
https://github.com/MuffinAmor/timmy
the main file presents the bot itself.
everything in the "command" dictionary contains the commands and events in the bot.

some things may have changed since i wrote this thing, but the mandatory should still work for you.

#

also this fixes your slash command problems.
how u alter the files in your own way is your choice.
as long as it works, its up to you how u do that

formal loom
midnight viper
#

👍

placid compass
#

you could use new_event_loop instead of get

#

or try/except get_running_loop and go for new_event_loop if it raises a runtimerror

#

you should also be passing loop to client so it uses the same one you've already made

formal loom