Im making a mini game in Discord where the bot will make a random react on a embed. I tried to use the code (random react:). I dont get any errors when trying to run the code but when I use the command the random react dont get reacted by the Discord bot.
Could someone help on how to make the bot send random react on a embed?
@bot.command(pass_context=True)
async def playbs(self, ctx):
embed = discord.Embed(title="**BEATSABER**",
colour=discord.Colour.dark_red())
embed.add_field(
name="*HOW TO PLAY:*",
value=
"Under this message is many reacted emojis. In the middle you have 4 arrows. 1 arrow that points up 1 that ponts down and 2 that points on left and right."
)
embed.set_footer(text="Made by: Jellie")
embed.set_thumbnail(
url=
"https://i.imgur.com/keLKx3H.png"
)
msg = await ctx.reply(embed=embed)
await msg.add_reaction("<:gray_cube1:1000731095437934672>")
await msg.add_reaction("<:up:959720228407894056>")
await msg.add_reaction("<:right:959720161156407308>")
await msg.add_reaction("<:left:959720190730436608>")
await msg.add_reaction("<:down:959720112808673370>")
await msg.add_reaction("<:gray_cube2:1000731117445464125>")
random react:
r = random.choice(["up", "left", "right", "down"])
if r == (["up"]):
await msg.add_reaction(":green_square:")
if r == (["left"]):
await msg.add_reaction(":green_square:")
if r == (["right"]):
await msg.add_reaction(":green_square:")
if r == (["down"]):
await msg.add_reaction(":green_square:")
>Solution :
Try printing the value.
f = random.choice(["A", "B"])
print(repr(f)) # repr gets a more detailed view of what a variable actually is.
# prints "A"
print(repr("A" == ["A"])) # prints False, they are not equal
print(repr("A" == "A")) # prints True, same type
So you can fix your if statements like this:
r = random.choice(["up", "left", "right", "down"])
if r == "up":
await msg.add_reaction(":green_square:")
elif r == "left":
await msg.add_reaction(":green_square:")
elif r == "right":
await msg.add_reaction(":green_square:")
elif r == "down":
await msg.add_reaction(":green_square:")
else:
# Cause a error if the emoji is not found, help prevent undefined behavior in case of a typo
raise ValueError(f"Unexpected value for r: {r}")
Alternate Cleaner Solutions
However, you can do this much cleaner. There is no need to use any if at all here.
await msg.add_reaction(
random.choice([
"<:gray_cube2:1000731117445464125>",
"<:red_cube>"
# add any more emojis here
])
)
If you do need to know the direction maybe for later in the code you can do this, using a dict instead:
emojis_for_directions = {
"left": ":green_square",
"right": ":red_square",
# add any more emojis here
}
r = random.choice(["left", "right"])
await msg.add_reaction(emojis_for_directions[r])