Discord.py我如何从DM检查哪些角色在某个服务器中有用户



我需要一个机器人,它可以检测用户在Direct Message中的角色。我可以从服务器聊天中完成,但我也需要在直接消息中完成。我当前的代码简化了:

import discord
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
role1 = 'ROLE_ID'
@client.event
async def on_message(message):
#only user with role1 is able to send the command
if message.content.lower().startswith('can I?'):
for r in message.author.roles:
if str(r.id) in role1:
await message.channel.send('Yes you can!')
return
#everyone is able to send the command $hello
if message.content.startswith('Hello'):
await message.channel.send('Hi!')

client.run('TOKEN')```

不要混淆Member对象和User对象。User没有角色,因为它作为Member与服务器无关。您可以做的是将该用户的成员发送到所需的服务器:
import discord
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
role1_id = 000000000000000000000
guild1_id = 000000000000000000000
@client.event
async def on_message(message):
if message.content.lower().startswith('can I?'):
if message.guild:
member = ctx.author
else:  # private message
guild = client.get_guild(guild1_id)
member = await guild.fetch_member(ctx.author.id)
#  only user with role1 is able to send the command
for r in member.roles:
if r.id == role1_id:
await message.channel.send('Yes you can!')
return
#everyone is able to send the command $hello
if message.content.startswith('Hello'):
await message.channel.send('Hi!')

相关内容

最新更新