【问题标题】:discord.py | check for users joining in a voice channel不和谐.py |检查加入语音频道的用户
【发布时间】:2021-02-10 14:45:17
【问题描述】:

我正在尝试制作一个支持机器人,当用户连接支持频道时加入语音支持频道。

@client.command(aliases=['sup', 's'])
async def support(ctx):
    log = client.get_channel(id=701765882417774663)
    channels = ['bot-befehle']
    vc = client.get_channel(id=702412635127152686)
    global player

    if str(ctx.channel) in channels:

        try:
            player = await vc.connect()
        except:
            return

        player.play(discord.FFmpegPCMAudio('support.mp3'))
        player.source = discord.PCMVolumeTransformer(player.source)
        player.source.volume = 1.00
        await asyncio.sleep(30)
        player.stop()
        await player.disconnect()
        await log.send('Habe den Befehl "!support" erfolgreich ausgeführt.')
        print('Habe den Befehl "Support" erfolgreich ausgeführt.')

我不知道如何在语音频道中检查用户。有人可以帮忙吗?

【问题讨论】:

  • 你到底想做什么?你的解释有点难以理解。是否要检查是否有人在它加入的语音频道中?
  • 是的,就是这样。当有人在频道中时,机器人应该加入这个频道。

标签: python-3.x discord discord.py


【解决方案1】:

你的解释有点重复。我将假设您不希望机器人自动加入,但仅在使用此命令之后(否则该命令将什么都不做)。

要检查是否有人在 VoiceChannel 中,您可以使用它的 members 属性,该属性将返回频道中所有成员的列表。您可以简单地检查此列表是否为空。

@client.command(aliases=['sup', 's'])
async def support(ctx):
    log = client.get_channel(id=701765882417774663)
    channels = ['bot-befehle']
    vc = client.get_channel(id=702412635127152686)

    if not vc.members:
        return

    # ... rest of your code

这样,如果频道没有任何人连接,命令将停止执行。

【讨论】:

    【解决方案2】:

    导入不和谐

    from discord.ext import commands
    
    @commands.Cog.listener()
    
    async def on_voice_state_update(self, member, before, after):
    
          if after.channel.id == channel_id and not member.bot:
    
             voice_client = await channel.connect()
    
             #do whatever you want here
    
           `  
    

    【讨论】:

    • channel_id 是支持频道 ID,只需将其复制并粘贴到那里即可。如果它不起作用,请告诉我。干杯。
    猜你喜欢
    • 1970-01-01
    • 2021-09-27
    • 2023-04-07
    • 2021-01-18
    • 1970-01-01
    • 2021-04-06
    • 2020-10-15
    • 1970-01-01
    • 2019-12-08
    相关资源
    最近更新 更多