I have created an async function called
add_reaction_tasks
which maps specific function to dictionary keys. This function returns the dictionary to a variable calledtasks
which is located in the eventon_raw_reaction_add
.
The first issue is that tasks = await add_reaction_tasks()
raises the following error: line 343, in _run_event await coro(*args, **kwargs)
.
The second one is when I try to access the dictionary, denoted by tasks
, with the key which_trigger
and the argument payload
, the following error is raised:
'1' : await add_rules_messages_tasks, TypeError: object function can't be used in 'await' expression
.
I have made sure that await
is used where possible. Besides that, I am not sure what else I have to get done, to make this code work.
The code snippets:
1) the functions used in add_reaction_tasks
def display_messages_error(payload = discord.RawReactionActionEvent):
...
async def add_rules_messages_tasks(payload = discord.RawReactionActionEvent):
...
async def add_years_messages_tasks(payload = discord.RawReactionActionEvent):
...
async def add_programming_languages(payload = discord.RawReactionActionEvent):
...
2) add_reaction_tasks function
async def add_reaction_tasks():
add_tasks = {
'0' : display_messages_error,
'1' : await add_rules_messages_tasks,
'2' : await add_years_messages_tasks,
'3' : await add_programming_languages
}
return add_tasks
3) on_raw_reaction_add event
@bot.event
@commands.bot_has_permissions(manage_roles = True)
async def on_raw_reaction_add(payload : discord.RawReactionActionEvent):
role = None
guild = None
# if the guild is server_name
if payload.guild_id == server_id:
guild = find(lambda g : g.id == server_id, bot.guilds)
member = get(guild.members, id = payload.user_id)
if not guild: return
if not member: return
which_trigger = 0 # 0 stands for invalid message id (should trigger display_message_error function)
try:
which_trigger = str(all_role_reaction_ids[str(payload.message_id)]) # <= looks weird, but works
#print(f'\nwhich_trigger = {which_trigger}\n')
except KeyError as err:
print(f'KeyError: {err}')
print('Invalid role-reaction message id')
return
tasks = await add_reaction_tasks() # <= first error
await tasks[which_trigger](payload) # <= second error
return
Even though the last three functions in the dictionary are asychronous, removing the await
before them, makes them function as normal and no errors are raised.
However, how can they be asychronously run since the await
keyword is not used before them?
working code
async def add_reaction_tasks():
add_tasks = {
'0' : display_messages_error,
'1' : add_rules_messages_tasks,
'2' : add_years_messages_tasks,
'3' : add_programming_languages
}
return add_tasks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With