如何在discord.py中制作我的机器人创建具有自定义权限的短信?

时间:2018-06-14 06:13:50

标签: python-3.x discord discord.py

所以我有一个用python3.6制作的discord bot,我希望他创建一个文本频道,但仅限管理员。

我知道如何使用await client.create_channel()创建文字频道,但我不知道如何设置自定义权限以及如何向其发送消息。

谢谢!

我的代码(如果您需要):

@client.event
async def discord.on_server_join(server):
    await client.send_message(channel, "Thank you for using Phantom. From all the developers, we want to thank you, as we know there are thousands of other bots out there.")
    await client.send_message(channel, "You should see that a new channel was created and is called \"Phantom\". That is the phantom moderation channel and is used for administrating your phantom instance.")
    server = ctx.message.server
    await client.create_channel(server, "Phantom", type=discord.ChannelType.text)

@client.event
async def on_message(message):
# The code continues here, but I think you only need the on_server_join function.

2 个答案:

答案 0 :(得分:2)

create_channel的文档提供了类似的示例。下面,它会更新,以使具有管理员权限的任何角色都可以看到该频道。

def overwrites(server):
    invisible = discord.PermissionOverwrite(read_messages=False)
    visible = discord.PermissionOverwrite(read_messages=True)
    perms = [(server.default_role, invisible)]
    for role in server.roles:
        if role.permissions.administrator:
            perms.append((role, visible))
    return perms

@client.event
async def discord.on_server_join(server):
    await client.send_message(channel, "Thank you for using Phantom. From all the developers, we want to thank you, as we know there are thousands of other bots out there.")
    await client.send_message(channel, "You should see that a new channel was created and is called \"Phantom\". That is the phantom moderation channel and is used for administrating your phantom instance.")
    server = ctx.message.server
    channel = await client.create_channel(server, "Phantom", *overwrites(server), type=discord.ChannelType.text)
    await client.send_message(channel, "Hello!")

答案 1 :(得分:1)

最简单的方法是尝试检查玩家是否具有特定角色, 例如,检查玩家是否具有管理员角色。这是一个例子。

if message.content.lower().startswith('/admin'):
    role = discord.utils.get(message.server.roles,id='420576440111726592') #Replace the numbers with your role's id
    if "340682622932090890" in [role.id for role in message.author.roles]: #Do the same here
        await client.send_message(message.channel, "Aye you have admin!")
    else:
        await client.send_message(message.channel, "You do not have permissions to do this command")

我不是专家,也没有对此进行过测试,请告诉我它是否无效。