所以,我正在为我的 Discord 服务器制作一个 modmail 机器人。但是当我运行时,bot 可以连接,但是当我在 bot DM 中发送测试消息时,我收到此错误:AttributeError: 'NoneType' object has no attribute 'send'。 我在 Arch Linux 上使用 python 3.9.1。这是我的代码:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix="!")
@client.event
async def on_ready():
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='for moderation mail'))
print("Thunderbird is ready")
@client.event
async def on_message(message):
empty_array = []
modmail_channel = discord.utils.get(client.get_all_channels(), name="modmail-box")
if message.author == client.user:
return
if str(message.channel.type) == "private":
if message.attachments != empty_array:
files = message.attachments
await modmail_channel.send("<@" + message.author.id + ">: ")
for file in files:
await modmail_channel.send(file.url)
else:
await modmail_channel.send("<@" + str(message.author.id) + ">: " + message.content)
elif str(message.channel) == "modmail" and message.content.startswith("<"):
member_object = message.mentions[0]
if message.attachments != empty_array:
files = message.attachments
await member_object.send("**[MOD]** " + "**" + message.author.display_name + "**: ")
for file in files:
await member_object.send(file.url)
else:
index = message.content.index(" ")
string = message.content
mod_message = string[index:]
await member_object.send("**[MOD]** " + "**" + message.author.display_name + "**: " + mod_message)
client.run('No leaking here')
答案 0 :(得分:0)
我假设错误在这一行:
await member_object.send("**[MOD]** " + "**" + message.author.display_name + "**: ")
并且 member_object 来自这个块:
elif str(message.channel) == "modmail" and message.content.startswith("<"):
member_object = message.mentions[0]
if message.attachments != empty_array:
files = message.attachments
await member_object.send("**[MOD]** " + "**" + message.author.display_name + "**: ")
for file in files:
await member_object.send(file.url)
您将 member_object
定义为 message.mentions
数组中的第一个元素,但是 NoneType has no attribute
的错误表明该数组中没有元素,因此 member_object
是实际上等于 None
而不是具有 send
方法的类的实例。
您需要检查对该函数的输入,并确保当您的代码到达该块时,它确实具有您期望的属性