【问题标题】:Make Discord.py Bot Only Work In Specific Channels使 Discord.py Bot 仅在特定渠道中工作
【发布时间】:2021-12-15 04:06:24
【问题描述】:

我正在使用 discord.py 为我的朋友制作一个机器人 我想让它只在包含单词 ticket 的频道中工作,由另一个名为 Ticket Toll 的机器人制作p>

我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    Relevant docs on text channels

    不幸的是,Discord 的 API 没有跟踪谁创建了频道(这就是为什么没有 channel.author 这样的东西)。

    一种解决方案是让 Ticket Toll 在一个类别中创建频道,并且只授予您的机器人查看该类别的权限。

    但是,如果频道名称中没有 "ticket",您可以通过检查 channel.name 轻松让机器人忽略消息。以下是 on_message 事件的示例:

    @client.event
    async def on_message(message):
        if 'ticket' not in message.channel.name: return
        # stuff to execute if message was sent in a channel with ticket in its name
    

    或者作为命令:

    @client.command()
    async def something(ctx, arg):
        if "ticket" not in ctx.message.channel.name: return
        # stuff to execute if the command was sent in a channel with ticket in its name
    

    【讨论】:

      【解决方案2】:

      仅授予机器人访问您希望其工作的读取频道的权限。

      【讨论】:

        猜你喜欢
        • 2021-07-02
        • 2017-05-28
        • 1970-01-01
        • 2020-12-27
        • 2019-02-25
        • 2022-01-21
        • 2018-10-04
        • 2020-09-27
        • 1970-01-01
        相关资源
        最近更新 更多