检查已加入频道的特定用户

时间:2020-05-13 22:42:14

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

我正在尝试将特定用户移至afk语音频道时播放mp3文件。

在discord.py中重写:

async def on_voice_state_update(member, before, after):
user = bot.get_user("my user id")
if after.afk and user == True:
    channel = bot.get_channel("the id of the channel I want the bot to connect to")
    voice = await channel.connect()
    voice.play(discord.FFmpegPCMAudio('directory of mp3 file'))
    await asyncio.sleep(7)
    await voice.disconnect()

问题

一切正常,直到我尝试在if语句中指定用户为止: user == True

但是,一旦我包含此要求,它将停止工作。

我尝试过的事情

我试图给用户对象一个属性,例如user.connect == Trueuser.joined == True等,但是没有运气。

目标

以完善的形式,我不仅可以指定需要加入afk频道的用户才能使活动正常运行,还可以指定其他用户的频道ID,以便漫游器可以连接到该频道:

channel = bot.get_channel("the id the channel that a specified user is in")

编辑:

根据Diggy的回答,我将if语句更改为如下形式:

async def on_voice_state_update(member, prev, cur):
    if member.id == 'my user id' and cur.afk:
            channel = bot.get_channel('the channel id for the bot to connect to')
            voice = await channel.connect()
            voice.play(discord.FFmpegPCMAudio('dir of mp3 file'))
            await asyncio.sleep(7)
            await voice.disconnect()
    else:
            pass

但是,一旦我离开频道,它也会播放音频。

我尝试指定使用if member.id == 'my user id' and cur.afk and not prev.afk离开频道后将不会播放,但是离开时仍会播放。

实验解决方案:

好吧,所以我想到了让机器人只加入上一个频道,所以一旦我离开afk频道,它就会以没有问题的方式播放它。这使它可以在afk之前的频道中工作和播放,虽然很棒,但并不完美。它不需要加入afk频道:

async def on_voice_state_update(member, prev, cur):
    if member.id == 'my user id' and cur.afk:
            prevchannel = prev.channel.id
            channel = bot.get_channel(prevchannel)
            voice = await channel.connect()
            voice.play(discord.FFmpegPCMAudio('dir to mp3 file'))
            await asyncio.sleep('duration of mp3 file')
            await voice.disconnect()
    else:
            pass

我认为Elif可能会起作用,例如:

elif prev.afk is not None and cur.afk is None:
    pass

但它几乎忽略了它。

-prevcur的参数有时也不起作用,因此我不得不切换回beforeafter

2 个答案:

答案 0 :(得分:1)

执行此操作的一种方法如下:

async def on_voice_state_update(member, prev, cur):
    if member.id == your_user_id and cur.afk and not prev.afk:
        voice = await member.voice.channel.connect()
        voice.play(discord.FFmpegPCMAudio('directory of mp3 file'))
        await asyncio.sleep(7)
        await voice.disconnect()

答案 1 :(得分:0)

在不一致的情况下,您可以检查频道中的用户并获得返回给您的列表。收到该列表后,您就可以检查相关用户是否在用户列表中,因此类似下面的代码是可能您正在寻找的

def user_check(userid, channelid):
    user = bot.get_user(userid)
    channel = bot.get_channel(channelid)
    if user in channel.members:
        return true