【问题标题】:How to make random.choice constantly repeat [duplicate]如何使random.choice不断重复[重复]
【发布时间】:2021-07-28 04:15:38
【问题描述】:

我目前正在处理一个项目,我希望它每隔几秒钟将我的变量 x 更改为与列表不同的值。我已经设置了列表和随机命令,但是现在每次我启动代码时,它都会从列表中选择 1 个选项并保留它直到我停止代码。如何让它继续随机选择,即使在第一个初始选择之后,每隔几秒钟?

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')```

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    x 是一个特定值,而不是每次读取新值时的占位符。每次需要新值时都需要调用random.choice

    @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.")

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多