我正在制作一个具有2个主要功能的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
我该如何从给定的标签中获取所有用户角色,然后过滤掉除自定义角色以外的其他所有角色?
答案 0 :(得分:0)
对,我知道了。我首先需要从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)