如何让基于Python的Discord Bot随机播放不同的声音



我正在用Python编写Discord Bot。我想让它加入一个语音频道,然后从列表中随机播放不同的声音。它可以每10秒播放一个特定的音频文件。

其他一切都很好,机器人连接到一个频道等,但我不知道如何留在语音频道上随机说话。

现在看起来是这样的:

arvaus = [ *list of my files here* ]
@client.command()
async def arvaa(ctx):
if not ctx.author.voice:
return await ctx.send(ctx.message.author.mention + 'You need to connect to a voice channel')

guild = ctx.guild
voice_client: discord.VoiceClient = discord.utils.get(client.voice_clients, guild=guild)
audio_source =(discord.FFmpegPCMAudio(executable="F:/ffmpeg/bin/ffmpeg.exe", source=random.choice(arvaus)))
voice_client.play(audio_source, after=None)

所以基本上现在它播放一个音频文件,直到我再次键入命令来触发另一个。

async def play(self, ctx, *, query):
"""Plays a file from the local filesystem"""
source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(query))
ctx.voice_client.play(source, after=lambda e: print(f'Player error: {e}') if e else None)
await ctx.send(f'Now playing: {query}')

这是一个例子discord_bot_voice myaybe中的一段代码,试图在你的代码中实现这一点,比如说在bot连接后从你有的文件中播放随机声音

附加:如果你不发出任何声音,机器人会在一段时间后自动断开连接,所以可能只是让机器人播放没有声音的文件,直到随机时间

也许

arvaus = [ *list of my files here* ]
@client.command()
async def arvaa(ctx):
if not ctx.author.voice:
return await ctx.send(ctx.message.author.mention + 'You need to connect to a voice 
channel')
guild = ctx.guild
voice_client: discord.VoiceClient = discord.utils.get(client.voice_clients, guild=guild)
for i in range(len(arvaus)):
audio_source = (discord.FFmpegPCMAudio(executable="F:/ffmpeg/bin/ffmpeg.exe", 
source=random.choice(arvaus)))
randomtime = random.randint(20,200)
await asyncio.sleep(ransomint)
voice_client.play(audio_source, after=None)

最新更新