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?