Discord.py:如何从用户输入的标签中获取角色



我正在制作一个Discord Bot,它有两个主要功能-为只有他们才能访问的用户创建一个频道,并允许他们邀请人们进入他们的频道。现在,第一部分有效,但邀请部分无效。

首先,这里有一个代码,让用户给自己一个角色,它工作得很好,并添加了角色:

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

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

没错,我已经想明白了。我需要先从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)

最新更新