Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查消息频道是否属于该类别_Python_Discord.py - Fatal编程技术网

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

Python 检查消息频道是否属于该类别,python,discord.py,Python,Discord.py,有人能帮我吗?我正在尝试制作一个modmail,一切正常,用户发送一个DM,bot在“category”下创建一个通道,如果有人在“category”内发送消息,它将通过DM发送给用户。然而,每当有人在该“类别”或用户DMs中回复bot时,我就会遇到这个恼人的错误。我试图做一个类别检查,只做一些事情,如果它是国防部邮件类别。提前谢谢你 这是我的密码: async def on_message(self, message): if isinstance(message.channel, disc

有人能帮我吗?我正在尝试制作一个modmail,一切正常,用户发送一个DM,bot在“category”下创建一个通道,如果有人在“category”内发送消息,它将通过DM发送给用户。然而,每当有人在该“类别”或用户DMs中回复bot时,我就会遇到这个恼人的错误。我试图做一个类别检查,只做一些事情,如果它是国防部邮件类别。提前谢谢你

这是我的密码:

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. #
一切都正常,但每次有人在textchannel“category”或用户DMs机器人中回复时,我都会遇到这个错误

错误:

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

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

async def on_消息(消息):
#检查频道是dm频道还是文本频道
如果isinstance(message.channel、discord.DMChannel):
#做你的事
elif isinstance(message.channel、discord.TextChannel):
如果message.channel.category不是无:
如果message.channel.category.id==您的\u id:
#做你的事

您应该正确设置代码格式

您还可以检查
message.author.id
是您的机器人id
client.user.id
还是用户


有关更多信息,请阅读手册。

Edited:它正在工作。message.category没有属性,因此我将message.category更改为message.channel.category_I您不需要#来关闭评论。你只需要在评论的开头加一条,其余的部分就是评论。是的,我只是为了风格才这么做的。哈哈问题已经解决,谢谢提醒!