Discord PY On member Join 添加角色

时间:2021-01-10 06:21:20

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

我对 Discord PY 有点陌生,我正在寻找一种方法来添加角色,如果一个人 DM 是机器人的关键。 我研究并输入了此代码,但出现错误,请帮助 代码是

Mod = 789737793042776074


@Client.event
async def on_member_join(person):
    async for guild in Client.fetch_guilds(limit=150):
        channel = person
        await channel.send(f"Thanks for Joining {guild.name}")
        reply = await Client.wait_for('message', timeout=60)
        if reply.content == 'STron':
#         role = get(guild.roles, id = Mod)
          await channel.send('Sshh I Gave you Moderator')
          await person.add_roles(id = Mod)

我收到一个错误

Ignoring exception in on_member_join
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 28, in on_member_join
    await person.add_roles(id = Mod)
TypeError: add_roles() got an unexpected keyword argument 'id'

2 个答案:

答案 0 :(得分:1)

您需要获取角色然后添加

Mod = 789737793042776074

@Client.event
async def on_member_join(person):
    await person.send(f"Thanks for Joining {person.guild.name}")
    msg = await Client.wait_for("message", check=lambda m: not m.guild and m.author.id == person.id)
    if msg.content == "STron":
        await person.send("Sshh I gave you Moderator")
        await person.add_roles(person.guild.get_role(Mod))

答案 1 :(得分:0)

@client.event
async def on_member_join(user):
    await user.send(f"Message and variables")
    await user.add_roles(id) ##or await user.add_roles(ctx.guild.get_role(Name or Id))