-2

I'm currently working on a project and I want it to change my variable, x, to something different from the list every couple of seconds. I already have the list and random command set up, but right now every time I start the code it will choose 1 option from the list and keep it until I stop the code. How do I make it so that it continues to randomly choose, even after the first initial choice, every couple of seconds?

from discord.ext import commands
from discord import FFmpegPCMAudio
import random

Audio=['AUDIO_1.mp3','AUDIO_2.mp3','AUDIO_3.mp3']

x=random.choice(Audio)


client = commands.Bot(command_prefix = '!')

@client.event
async def on_ready():
    print("The bot is now ready for use.")
    print(".............................")


@client.command(pass_context=True)
async def join(ctx):
    if (ctx.author.voice):
        channel = ctx.message.author.voice.channel
        voice = await channel.connect()
        source = FFmpegPCMAudio(x)
        player = voice.play(source)
    else:
        await ctx.send("User not in a voice channel, unable to connect.")
 
@client.command(pass_context=True)
async def leave(ctx):
    if (ctx.voice_client):
        await ctx.guild.voice_client.disconnect()
        await ctx.send("I have left the voice channel.")
    else:
         await ctx.send("I am not in a voice channel.")



client.run('TOKEN')```

1 Answers1

1

x is a specific value, not a placeholder for a new value each time you read its value. You need to call random.choice each time you want a new value.

@client.command(pass_context=True)
async def join(ctx):
    if (ctx.author.voice):
        channel = ctx.message.author.voice.channel
        voice = await channel.connect()
        source = FFmpegPCMAudio(random.choice(Audio))
        player = voice.play(source)
    else:
        await ctx.send("User not in a voice channel, unable to connect.")
chepner
  • 497,756
  • 71
  • 530
  • 681