Search code examples
pythonpycord

I'm trying to find out how to generate a new random.choice() each time my function is called when my function calls for a positional argument


Using Pycord.

I am trying to get a new random choice EACH time the function is called, but I'm unable to do so because I have to define a URL in the field eDuel.set_image. Currently, only the "Duel Samples" are randomized ONCE and the chosen list object is used on each successive call of the function.

Code where random is initiated:

DuelSamples = ["https://pbs.twimg.com/media/COd5G_EUwAAtABd.jpg", "https://media.tenor.com/XoYV9sqXITkAAAAC/yu-gi-oh-time-to-duel.gif"]

eDuel = discord.Embed(
    title="I challenge you to a duel!",
    description="",
    color=discord.Colour.brand_red()
)
eDuel.set_image(url = random.choice(DuelSamples))

Function Code:

@client.event #Message Watcher
async def on_message(message):
    user = message.author

    if message.author == client.user:
        return    

    async for message in message.channel.history(after=datetime.now() - timedelta(seconds = 2), before=message.created_at):
        if user == message.author:
            return

    else:
        if not (message.channel.id == 922678396486553685): 
            return
        else: 
            if random.randint(1,1) == 1:
                await message.channel.send(f"<@{user.id}> \n ** **",embed = eDuel, view = DuelButton())
                player.insert(1, user.id)

            elif random.randint(1,50) == 1:
                await message.channel.send(f"<@{user.id}> \n ** **",embed = eGlitcherFetch, view = GFetch())
                player.insert(1, user.id)

If I attempt to scrap url = from eDuel.set_image and input that field into my list objects, I get a return: "Requires 1 positional Argument" (understandably so)

I also attempted to completely pass eDuel.set_image into the list and throw random.choice(DuelSamples) underneath the eDuel embed, but it could not detect that set_image was being requested so there was no image naturally


Solution

  • You are defining the image only once and then calling that defined image each time, seeing your code the simplest way yo achieve what you want would be something like:

    if random.randint(1,1) == 1:
        eDuel.set_image(url = random.choice(DuelSamples))
        await message.channel.send(f"<@{user.id}> \n ** **",embed = eDuel, view = DuelButton())
       player.insert(1, user.id)
    

    That way each time it will run the choice picker.