首先,这可能是一个奇怪的句子,因为我用翻译写下来,但请理解。我第一次使用discord.py开发了一个个人服务器机器人。我的问题是,当用户通过移动设备加入服务器时,我想分配一个角色,但我不确定如何分配。这可能是一个很愚蠢的故事,但我很感激你的帮助。我想知道与discord.Member.is_on_mobile.
的区别这是我期望的代码。我真的很感激你的帮助。
async def shutup_mobile(message : discord.Message, member : discord.Member) :
if discord.Member.mobile_status.online :
# When someone joins voice_channel on mobile
# Assign role A
# After assigning a role, I want to send a sentence to a specific text_channel
await message.channel.send(f'{discord.Member.name} is don't want playing game with us')
关于is_on_mobile
的文档
我觉得你的整个功能都是错误的——如果一个用户正在加入一个语音频道,为什么你在那里有一个消息参数?您应该使用on_voice_update
客户机事件。显然,您可以从那里调用您提供的函数—但是message
对象似乎是错误的。
@client.event
async def on_voice_update(member: discord.Member, before: discord.VoiceState, after: discord.VoiceState):
# before and after are voice state objects - can be used to check if a user has joined/left a vc
if before.channel:
# user is already in a channel
return
# user has joined a voice channel
if member.is_on_mobile():
# user is on mobile - do our thing here
guild = after.channel.guild # get the guild object
role_to_apply = guild.get_role(THE_ROLE_ID_YOU_WANT_GIVE)
await member.add_roles(role_to_apply)
the_text_channel = guild.get_channel(THE_CHANNEL_ID_YOU_WANT_TO_SEND_MESSAGE_TO)
await the_text_channel.send(f"{member.name} is don't want playing game with us")
添加角色—在discord.Member
上使用add_roles
方法。
你似乎还在试图调用尚未实例化的类的方法。使用discord.Member.name
将失败,因为它不是该类的实例-你已经有了它的实例,member
在函数参数中,所以你应该直接使用member.name
。我已经修复了这个问题,并在我的例子中出现了类似的情况。或许可以考虑读一读Python中的类。
希望这足以让你走下去。还可以扩展为在用户离开时删除角色等。
discord.Member.mobile_status
属性返回discord.Status
。如果用户不在移动设备上或离线,则返回discord.Status.offline
。如果他们在移动设备上,它返回用户的当前状态。
根据文档,discord.Member.is_on_mobile
返回一个布尔值。
除了ESloman指出的问题,discord.Member.mobile_status.online
没有意义。相反,
if member.mobile_status == discord.Status.online:
#do something
只有当成员在移动设备上并且状态为discord.Status.online
时才会这样做您应该使用member
而不是discord.Member
,因为您将member
作为参数,而discord.Member
只是一个类。
如果你想检查用户是否在手机上,
if member.is_on_mobile():
#do something
discord.Member.mobile_status,discord.Member.is_on_mobile