擅长:python、mysql、java
<p>发生此错误的原因是,当bot发送消息时也会调用on_message事件,并且在您的情况下,bot是在dm中发送消息的,因此它应该是用户对象(成员对象仅限于帮会),并且用户对象没有权限,因为它们不在服务器内。它是一个ClientUser对象,而不是User,后者是一个特殊的类,类似于User,是bot所特有的。与User相比,这是一个单独的类,因为它有一些只有bot才能使用的特殊方法,比如编辑bot名称等
若要修复此问题,请忽略消息是否从bot发送,这样代码将</p>
<pre class="lang-py prettyprint-override"><code>@commands.Cog.listener()
async def on_message(self, ctx):
if message.author.bot:
return
if inviteChannel:
if ctx.author.guild_permissions.administrator:
return
if not ctx.content.startswith('!invite'):
await ctx.delete()
</code></pre>
<p>在这里,我添加了<code>if message.author.bot: return</code>,以便在作者是机器人时停止函数执行。我还删除了不必要的else语句</p>