【问题标题】:How would I make my bot made in discord.py create a text message with custom permissions?如何让我在 discord.py 中制作的机器人创建具有自定义权限的短信?
【发布时间】:2018-06-14 06:13:50
【问题描述】:

所以我有一个用 python3.6 制作的不和谐机器人,我希望他创建一个文本频道,但仅限于管理员。

我知道如何使用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.

【问题讨论】:

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


    【解决方案1】:

    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!")
    

    【讨论】:

    • 我知道这听起来可能很愚蠢,但是,一旦创建了频道,我将如何通过它发送消息?使用await client.send_message("Phantom", "hello")?
    • @TehAlex11 查看我的编辑。您可以捕获create_channel 的返回值并将其用作send_message 的目标
    【解决方案2】:

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

    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")
    

    我不是专家,也没有测试过,所以告诉我它是否不起作用。

    【讨论】:

    • 这对我也有很大帮助,但唯一的问题是我希望我的机器人在每台服务器上都运行,所以我还需要查看 Admin 角色 ID 是什么。
    • 那么你应该检查权限而不是检查角色。而不是if 340682622932090890" in [role.id for role in message.author.roles]:if ctx.message.author.server_permissions.administrator:
    猜你喜欢
    • 1970-01-01
    • 2019-01-29
    • 2021-03-25
    • 2019-03-06
    • 1970-01-01
    • 2021-04-30
    • 2021-09-01
    • 2021-10-07
    • 2020-09-01
    相关资源
    最近更新 更多