0
votes

So, I'm making a modmail bot for my discord server. But when I run, the bot can connect but when I send a test messange in the bot DM, I receive this error: AttributeError: 'NoneType' object has no attribute 'send'. I'm using python 3.9.1 on Arch Linux. Here is my code:

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')
1
Which line is throwing that error? and what is the full error? basically what you are trying to call send on is not actually the class you think it is, you're calling send on something that is None - DBA108642
@DBA108642 the line of the error is line 28 - JimmyBlue
please include the full error message - DBA108642

1 Answers

0
votes

I am assuming that the error is on this line:

await member_object.send("**[MOD]** " + "**" + message.author.display_name + "**: ")

and member_object is coming from this block:

    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)

You are defining member_object as the first element in the message.mentions array, however your error of NoneType has no attribute suggests that there are no elements in that array and so member_object is actually equal to None not an instance of a class which has a send method.

You need to check your input to that function and ensure that when your code reaches that block it actually has the attributes that you are expecting