将bot分配给单通道discord.py

问题描述 投票:0回答:1

我想确保我的机器人只响应命令/消息并仅在1个特定通道中响应这是否可能我尝试了多种变化而没有成功。如果我可以为任何事件定义它,那就更好了。有人有任何想法吗?

discord.py
1个回答
2
投票

你可以在message.channel事件中检查on_message,如果它符合你的标准,在这种情况下是一个特定的通道,那么就做process_commands

下面是一个例子,其中!ping命令仅在channel.name为“general”时才有效。

from discord.ext import commands

client = commands.Bot(command_prefix='!')

@client.command()
async def ping():
    await client.say('Pong')

@client.event
async def on_message(message):
    if message.channel.name == 'general':
        await client.process_commands(message)

client.run('token')
© www.soinside.com 2019 - 2024. All rights reserved.