1

I've been working on a chatroom-based game within Discord, using bots. I want to use two bots on the same cog, because the functions within the cog are shared between those bots. However, a core function is still in the main bot file, because it's different for both bots. I've tried many search queries, but I can't find anything about calling functions from the main file, from a cog. Normally I would use await function(argument), but this gives me name 'function' is not defined.

The main file looks something like this:

async def gen():
    item = random.randint(10000000, 99999999)
    return item

@bot.command()
async def buy(ctx, itemname=None, user=None)
    dict = #List with items

    if itemname in dict:
        item = await gen()

    items = bot.get_cog('cog_items')
    economy = bot.get_cog('cog_economy')
    await items.prompt(ctx, item)
    if h = 4:
        ~ #Continue

The 'cog_items' file looks something like this:

class cog_items(commands.Cog):
    def __init__(self, bot=None):
        self.bot = bot

    async def prompt(self, ctx, item):
        emoji1 = "✅"
        emoji2 = ""
        embed = ~      #Show and describe item in embed, done by a function
        msg = await ctx.send(embed=embed)
        await msg.add_reaction(emoji1)
        await msg.add_reaction(emoji2)

        def check(reaction, user):
            return reaction.message.id == msg.id and (str(reaction.emoji) == emoji1 or emoji2) and user == ctx.message.author

        while 1:
            try:    #Wait for appropriate reaction
                reaction, user = await self.bot.wait_for('reaction_add', timeout=60, check=check)
                if str(reaction.emoji) == emoji2:
                    return h = 4 #The 4 triggers a response in the initial function.
                    break
                if str(reaction.emoji) == emoji1:
                    item = await gen()
                    #Create new item, embed and message, add reactions
                    continue
            except asyncio.TimeoutError:
                break

From the main file, a function in the 'cog_items' is called, and from this function, a function in the main file is called. This way, it results in a NameError: name 'function' is not defined

My solid question right now is: How do I call a function in the main file, within a function in a cog?

Dewesto
  • 21
  • 4
  • Can you edit your post to include your code? Otherwise, maybe you can move the function to its own file (a module) and call it in your main file and two cog files. – Benjin May 03 '21 at 10:04

1 Answers1

1

I have already found the answer myself:

I've tried importing the main file (import __main__), but it seems this resulted in a circular import. With the help of somebody else, I've stumbled upon questions about circular imports. The solution was to import the main file locally, within the function that requires the function from the main file. So I put the import __main__ right above the called function, and instead of await function(argument) I used await __main__.function(argument).

The links to the questions we found: https://stackoverflow.com/a/17845428 Local import statements in Python

Dewesto
  • 21
  • 4