在on_voice_state_update过程之前,检查机器人是否在频道中

时间:2020-05-30 13:04:20

标签: python discord discord.py discord.py-rewrite

我之前曾问过这个问题的实际过程,但我确实做到了,但是只有一个错误。

async def on_voice_state_update(self, member, before, after):
    def is_connected(member):
        voice_client = get(self.bot.voice_clients, guild=member.guild)
        return voice_client and voice_client.is_connected()

    if is_connected(member):
        await asyncio.sleep(9)
        if member.id == 'id of member' and after.afk and not before.afk:
            prevchannel = int(before.channel.id)
            channel = self.bot.get_channel(prevchannel)
            voice = await channel.connect()
            voice.play(discord.FFmpegPCMAudio('dir of mp3 file'))
            await asyncio.sleep(5)
            await voice.disconnect()
        else:
            pass
    else:
        if member.id == 'id of member' and after.afk and not before.afk:
            prevchannel = int(before.channel.id)
            channel = self.bot.get_channel(prevchannel)
            voice = await channel.connect()
            voice.play(discord.FFmpegPCMAudio('dir of mp3 file'))
            await asyncio.sleep(5)
            await voice.disconnect()
        else:
            pass

我正在尝试检查漫游器是否已连接到语音通道,以便在同一时间(例如2秒后)有多个人加入afk,它会等到执行程序的下一次迭代: await asyncio.sleep(10)

问题

尽管我相信我朝着正确的方向前进,但我似乎无法使is_connected函数正常工作。我曾想过尝试获取机器人的用户ID,并检查它是否已连接到语音通道。会是这样的:

def is_botconnected(member):
    botid = self.bot.get_user('id of bot')
    channel = (all the channels)
if botid in channel.members:
    await asyncio.sleep(9)
    #execute program
else:
    #execute program

1 个答案:

答案 0 :(得分:0)

解决方案

我设法使用bot.voice_clients使它正常工作。

async def on_voice_state_update(self, member, before, after):
    def is_connected(self):
        bot_client = get(self.bot.voice_clients)
        return bot_client and bot_client.is_connected()

    if is_connected(self):
        await asyncio.sleep(9)
        #execute program
    else:
        #execute program

因为它是齿轮,所以使用了自己。