简体   繁体   中英

Discord.py AttributeError: 'NoneType' object has no attribute 'send'

i am trying to make my bot send a message in my log channel whenever someone leave or join the server but i am getting the error AttributeError: 'NoneType' object has no attribute 'send' in my console. I have already tried many solutions i found on StackOverflow and other websites but nothing seems to work in my case.

Here is my code:

@bot.event
async def on_member_remove(member):
   logChannel = bot.get_channel(id=myLogChannelIdHere)
   embed = discord.Embed(
      title= "**🚪 Un membre est parti**",
      description = f"${member} a quitté le serveur",
      color=0xda291c,
      set_thumbnail = member.avatar_url,
      )
   embed.timestamp = datetime.datetime.utcnow()
   await logChannel.send(embed=embed)

The thing i don't understand is that i have the exact same thing in other event like for my on_ready event (When my bot come online it send a message in my log channel) and it work perfectly

@bot.event
async def on_ready():
   logChannel = bot.get_channel(id=myLogChannelIdHere)
   await bot.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name=""))
   await logChannel.send("Le bot est en ligne")
   print(bot.user.name + " est en ligne")

Thanks!

It seems like your logChannel = bot.get_channel(id=myLogChannelIdHere) isn't actually catching the channel, this could happen for a couple of reasons.

The value stored in myLogChannelIdHere is either not a valid channel id that the bot can see, or it is not an Integer. If it's a string, wrapping int() around it will solve it.

One other point may or may not be relevant, when I use get_channel I don't specify the id= parameter, I just pop the channel id straight in.

import discord
from discord import Member
from discord.utils import get
@client.event
async def on_member_join(member:Member):
    if member.bot:
        guild:discord.Guild = member.guild
        role = guild.get_role(config.botRoleID)
        channel:discord.TextChannel = guild.get_channel(YOUR_CHANNEL_ID)
        await member.add_roles(role)
        await channel.send(f'Set Bot role to "{member.display_name}" 😌')

Try to first print out what get_channel returns because if get_channel can't find a channel it will return None.

Try it out by hardcoding an int in bot.get_channel()

and see what it returns

Also check so the Bot actually can see that channel and write to it.

I tried it out on my own bot and it works fine for me.

Code

Copy the text channel ID

Result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM