【问题标题】:Discord.py: How do I get a role from a tag a user inputs?Discord.py:如何从用户输入的标签中获取角色?
【发布时间】:2020-03-31 23:10:13
【问题描述】:

我正在制作一个 Discord Bot,它有 2 个主要功能 - 为只有他们可以访问的用户创建一个频道,并让他们邀请人们进入他们的频道。目前,第一部分有效,但邀请部分无效。

首先,这是让用户给自己一个角色的代码,它可以正常工作并添加角色:

if message.content.startswith('!!start'):
    await message.channel.send('Hello {0.author.mention}! Welcome to The Hangout. We are about to setup your account here! Hang on..'.format(message))

    print(message.author)

    overwrites = {
        message.guild.default_role: discord.PermissionOverwrite(read_messages=False),
        message.guild.me: discord.PermissionOverwrite(read_messages=True),
        message.author: discord.PermissionOverwrite(read_messages=True, send_messages=True)
    }

    await message.channel.send('What would you like your apartment to be called?')

    msg = await client.wait_for('message')

    print(msg.content)

    apartments = discord.utils.get(message.guild.categories, name='Apartments')
    print(apartments)

    channel = await message.guild.create_text_channel(str(msg.content), overwrites=overwrites, category=apartments)

    await message.channel.send('Done! Next, we will setup a role which people can use to invite you to their apartment by.')

    await message.channel.send('What would you like your role to be called? (please be sensible, people will use this to invite you.)')

    msg = await client.wait_for('message')

    await message.guild.create_role(name=str(msg.content))
    role = discord.utils.get(message.guild.roles, name=str(msg.content))
    await message.author.add_roles(role)

    await message.channel.send('Done! You are now setup. In #other, type in !!help to learn how to invite people to your apartment.')

这是处理邀请的代码:

if message.content.startswith('!!invite'):
    await message.channel.send('Inviting ' + message.content[8:] + "...")

    role = discord.utils.get(message.guild.roles, name=message.content[8:])
    await message.channel.set_permissions(role, send_messages=True, read_messages=True)

    inviteChannel = client.get_channel(694547722899816498)

    await inviteChannel.send('Hello {1}! {0.author.mention} has invited you to their apartment! Join them in {0.channel}!'.format(message, message.content[8:]))

    await message.channel.send('Invited ' + message.content[8:] + '.')

代码应该接受来自用户(角色)的标签,并允许他们访问用户的频道。

当我尝试设置权限时,它不起作用。输出:

Traceback (most recent call last):
  File "/home/toffee/.local/lib/python3.6/site-packages/discord/client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "theBot.py", line 52, in on_message
    await message.channel.set_permissions(role, send_messages=True, read_messages=True)
  File "/home/toffee/.local/lib/python3.6/site-packages/discord/abc.py", line 618, in set_permissions
    raise InvalidArgument('target parameter must be either Member or Role')
discord.errors.InvalidArgument: target parameter must be either Member or Role

我将如何从给定的标签中获取所有用户的角色,然后过滤除自定义角色之外的任何其他角色?

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    是的,我想通了。我需要先从 ID 中获取角色,所以我使用这段代码来获取角色,这确实有效:

            await message.channel.send('Inviting ' + message.content[8:] + "...")
    
            print(message.content[12:-1])
    
            role = discord.utils.get(message.guild.roles, id=int(message.content[12:-1]))
            print(role)
            await message.channel.set_permissions(role, send_messages=True, read_messages=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 2022-01-15
      • 2021-06-01
      • 1970-01-01
      相关资源
      最近更新 更多