使 Discord.py 机器人仅在特定渠道中工作
Make Discord.py Bot Only Work In Specific Channels
我正在使用 discord.py 为我的朋友制作一个机器人
我想让它只在包含单词 ticket 的频道中工作,由另一个名为 的机器人制作门票通行费
我该怎么做?
只允许机器人访问您希望其工作的阅读频道。
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
我正在使用 discord.py 为我的朋友制作一个机器人 我想让它只在包含单词 ticket 的频道中工作,由另一个名为 的机器人制作门票通行费
我该怎么做?
只允许机器人访问您希望其工作的阅读频道。
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