1
votes

When I try to run the command I always get this error message: Discord.py 'NoneType' object has no attribute 'send'

import os
import json
import discord
from discord.ext import commands

def get_channel(client, message):
    with open('./data/welcome.json', 'r') as f:
        tchann = json.load(f)
    return tchann[str(message.guild.id)]

class welcome(commands.Cog):

    def __init__(self, client):
        self.client = client

    # Events
    @commands.Cog.listener()
    async def on_ready(self):
        print('Welcome script loading.')

    @commands.Cog.listener()
    async def on_member_join(self, member):
        embed=discord.Embed(title="Yoshino",
            url="http://animeex.nhely.hu/",
            description=f"asd1.",
            color=0x29FF94)

        channel = self.client.get_channel(id=get_channel)

        await channel.send(embed=embed)

    @commands.command()
    async def welcome(self, ctx, channel):
        with open('./data/welcome.json', 'r') as f:
            tchann = json.load(f)

        tchann[str(ctx.guild.id)] = channel

        with open('./data/welcome.json', 'w') as f:
            json.dump(tchann, f, indent=4)

        await ctx.send(f'Channel set: {channel}')

def setup(client):
    client.add_cog(welcome(client))

Error code:

File "C:\Users\NexaHn\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs) File "K:\Discord BOT\PythonX\cogs\welcome.py", line 30, in on_member_join await channel.send(embed=embed) AttributeError: 'NoneType' object has no attribute 'send'

2

2 Answers

1
votes
def get_channel(client, message):
    with open('./data/welcome.json', 'r') as f:
        tchann = json.load(f)
    return tchann[str(message.guild.id)]

channel = self.client.get_channel(id=get_channel)

get_channel is a method. It is expecting an int. See:

https://discordpy.readthedocs.io/en/stable/api.html?highlight=wait_for#discord.Client.get_channel

channel = self.client.get_channel(id=123456789) would have been valid (given the id 123456789 exists).

0
votes

The error means that variable channel is of NoneType or None. This probably means that there was no channel with the id of get_channel. You might have wanted to run the function get_channel instead of putting that as the id.