当我在DM上运行此代码时,我想获得服务器上的用户角色



我知道我需要使用服务器ID,但我不知道如何使用代码

if message.content.startswith("!role"):
await message.channel.send(random.choice(ruleth))
def check(msg):
return msg.author == message.author and msg.channel == msg.channel
try:
msg = await client.wait_for("message", timeout=10, check=check)
except:
await message.channel.send("time out")
return

if random.choice(ruleth) == "text 1":
if msg.content == "text!":
await message.channel.send("ok!")
member = message.author
role = get(member.guild.roles, name="user")
await member.add_roles(role)

你不可以添加一个角色通过dm服务器机器人如果这就是你想实现由于不和api的工作方式。你需要在服务器本身做检查你可以运行像

这样的检查
if msg.guild.id == yourServerID:
await msg.author.add_role(role)
else:
do stuff

最新更新