GOODBOIXD
GOODBOIXD

Reputation: 13

Random.choice inside an embed in discord.py not working

I did the same with client.command and it worked. However, I wanted to use ?rate me instead of ?rateme and hence decided to using on_message. I don't see anything wrong with my code, and its executing without any errors. But when I type ?rate me in discord, nothing fires. Would be grateful if someone could help me figure out what's wrong.

async def on_message(message):
    if message.content.startswith('?rate me'):
        async with message.channel.typing():
            variable_list =[
                '1',
                '2',
                '3',
                '4'
            ]

            embed = discord.Embed(colour=0xc81f9f,
                title="Rating",
                description=f"{message.author.mention} is a {(random.choice(variable_list))}"
            )
            embed.set_footer(text=f"{message.guild.name}")
            embed.timestamp = datetime.datetime.utcnow()

            await message.channel.send(embed=embed) 

Upvotes: 0

Views: 417

Answers (1)

Dominik
Dominik

Reputation: 3592

It seems that you have not marked your function as an event. Please use @client.event @bot.event or @commands.Cog.listener. Without this definition it will not work. I would also recommend you to rebuild the code a bit to detect the error faster. My code looks like this:

import discord
import random

@client.event / @bot.event / @commands.Cog.listener # Make it as an event that fires if the conditions are True
async def on_message(message):
    if message.content.startswith("?rate me"):
        async with message.channel.typing():
            variable_list = ['1', '2', '3', '4']

            embed = discord.Embed(color=0xc81f9f,
                                  title="Rating")
            embed.description = f"{message.author.mention} is a {(random.choice(variable_list))}" # Set description
            embed.set_footer(text=f"{message.guild.name}")
            embed.timestamp = datetime.datetime.utcnow()

            await message.channel.send(embed=embed)

If you already have an existing on_message event simply put the code without the first part into the existing event and then you have:

    if message.content.startswith("?rate me"):
        async with message.channel.typing():
            variable_list = ['1', '2', '3', '4']

            embed = discord.Embed(color=0xc81f9f,
                                  title="Rating")
            embed.description = f"{message.author.mention} is a {(random.choice(variable_list))}" # Set description
            embed.set_footer(text=f"{message.guild.name}")
            embed.timestamp = datetime.datetime.utcnow()

            await message.channel.send(embed=embed)

(This is because we can't have more than one on_message-event here.)

Upvotes: 1

Related Questions