检查已加入频道的特定用户
Checking for specific user that has joined a channel
当特定用户移至 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 == True
和user.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
但它几乎忽略了它。
-prev
和 cur
args 有时也不起作用所以我必须切换回 before
和 after
在 discordpy 中,您可以查看频道中的用户并获得返回给您的列表。获得该列表后,您就可以检查相关用户是否在用户列表中,因此类似下面的代码的内容可能是您要查找的内容
def user_check(userid, channelid):
user = bot.get_user(userid)
channel = bot.get_channel(channelid)
if user in channel.members:
return true
一种方法如下:
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()
当特定用户移至 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 == True
和user.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
但它几乎忽略了它。
-prev
和 cur
args 有时也不起作用所以我必须切换回 before
和 after
在 discordpy 中,您可以查看频道中的用户并获得返回给您的列表。获得该列表后,您就可以检查相关用户是否在用户列表中,因此类似下面的代码的内容可能是您要查找的内容
def user_check(userid, channelid):
user = bot.get_user(userid)
channel = bot.get_channel(channelid)
if user in channel.members:
return true
一种方法如下:
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()