簡體   English   中英

如何在 Pycord 中修復“AttributeError:'TextChannel' 對象沒有屬性'news'”?

[英]How do I fix "AttributeError: 'TextChannel' object has no attribute 'news'" in Pycord?

我最近開始使用Pycord制作一個 Discord 機器人。 但是,我很快遇到了一個我不知道如何處理的錯誤。

這是我的代碼:

import discord

bot = discord.Bot(debug_guilds=[<my guild ID>])

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user}!')

@bot.event
async def on_message(message: discord.Message):
    print(message)

bot.run('token')

這是錯誤:

Ignoring exception in on_message
Traceback (most recent call last):
  File "c:\Users\Username\Desktop\DiscordBot\env\lib\site-packages\discord\client.py", line 382, in _run_event
    await coro(*args, **kwargs)
  File "c:/Users/Username/Desktop/DiscordBot/main.py", line 18, in on_message
    print(message)
  File "c:\Users\Username\Desktop\DiscordBot\env\lib\site-packages\discord\message.py", line 777, in __repr__
    return f"<{name} id={self.id} channel={self.channel!r} type={self.type!r} author={self.author!r} flags={self.flags!r}>"
  File "c:\Users\Username\Desktop\DiscordBot\env\lib\site-packages\discord\channel.py", line 190, in __repr__
    attrs = [(val, getattr(self, val)) for val in self._repr_attrs]
  File "c:\Users\Username\Desktop\DiscordBot\env\lib\site-packages\discord\channel.py", line 190, in <listcomp>
    attrs = [(val, getattr(self, val)) for val in self._repr_attrs]
AttributeError: 'TextChannel' object has no attribute 'news'

我不確定為什么會發生此錯誤,因為據我所知, TextChannel對象沒有屬性“ news ”。 我能找到的最接近的是is_news()

清單:

檢查重復的問題。
檢查了 on_message 的on_message API 參考
試圖只打印消息content而不是message對象。
通過確認在Discord Developer Portal上啟用了Message Intent來檢查意圖問題。

環境信息:

操作系統:Windows 7
Python 版本:3.8.10(停留在這個版本上,因為 Python 3.9 及更高版本不再支持 Windows 7)
Python 虛擬環境名稱:env
Pycord 版本:2.0.0rc1

我認為你應該將你的 pycord 升級到開發版本。

pip install -U git+https://github.com/Pycord-Development/pycord

似乎在這個commit中,他們添加了news屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM