检查消息频道是否属于该类别



有人能帮我吗?我正在尝试制作一个modmail,一切都很好,用户发送DM,机器人在"下创建一个频道;类别";如果有人在里面留言";类别";它将通过DM发送给用户。然而,每次有人在里面回复";类别";或者用户DM这个机器人。我正在尝试进行类别检查,只在它是mod邮件类别的情况下做一些事情。提前谢谢!

这是我的代码:

async def on_message(self, message):
if isinstance(message.channel, discord.DMChannel):
# User DM the bot and the bot will make a textchannel under "category" and will send his DMs there. #
if message.channel.category_id == 123456789101112:
if isinstance(message.channel, discord.TextChannel):
# Message the user who DMed the bot using the textchannel under "category", your replies will be sent to the user by the bot via DM. #

一切都正常,但每次有人在短信频道内回复时,我都会收到这个错误;类别";或者用户DM机器人

错误:

if message.channel.category_id == 123456789101112:
AttributeError: 'DMChannel' object has no attribute 'category_id'

您的if语句没有多大意义,您应该首先检查通道的类型,然后检查其类别并进行比较。

async def on_message(message):
# Checking if the channel is a dm or a text one
if isinstance(message.channel, discord.DMChannel):
# do your thing
elif isinstance(message.channel, discord.TextChannel):
if message.channel.category is not None:
if message.channel.category.id == your_id:
# do your thing

您应该正确格式化代码。

此外,您还可以检查message.author.id是您的机器人idclient.user.id还是用户。

阅读文档以了解更多信息。

最新更新