Error: 'Bot' object has no attribute 'get_user_info' - python

I'm currently programming my own discord bot.
Now i'm trying, to do that, when the bot startet it sends a dm to me.
This is my code
#bot.event
async def on_ready():
owner = await bot.get_user_info(my Userid)
await bot.send_message(owner, "Ready", tts=True)
but i'm always getting the error:
AttributeError: 'Bot' object has no attribute 'get_user_info'
and i have done some research but i found literally nothing
i hope you guys can help me:)
i appreciate it

get_user_info is not a valid attribute of Bot. The correct use would be get_user in order to get a User from a valid user ID.
Also, in order to send a message, you need to have a message channel object. Since you're working with DMs, you'll need to create one using the User.create_dm() function discord.py offers. Afterwhich you can then send a message.
A lot of what I referenced can be found in the discord.py documentation.
Below is how I solved this issue:
from discord.ext import commands
import discord
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="$", intents = intents)
#bot.event
async def on_ready():
owner = bot.get_user(my Userid) # Assuming 'my Userid' is an integer based user id like 278688959905660928
dm_channel = await owner.create_dm()
await dm_channel.send("Ready", tts=True)

Related

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!
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
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}" 😌')

How do I send a message to a specific discord channel while in a task loop?

import discord
from discord.ext import commands, tasks
from discord_webhook import DiscordWebhook
client = discord.Client()
bot = commands.Bot(command_prefix="$")
#tasks.loop(seconds=15.0)
async def getAlert():
#do work here
channel = bot.get_channel(channel_id_as_int)
await channel.send("TESTING")
getAlert.start()
bot.run(token)
When I print "channel", I am getting "None" and the program crashes saying "AttributeError: 'NoneType' object has no attribute 'send' ".
My guess is that I am getting the channel before it is available, but I am unsure. Anybody know how I can get this to send a message to a specific channel?
Your bot cannot get the channel straight away, especially if it's not in the bot's cache. Instead, I would recommend to get the server id, make the bot get the server from the id, then get the channel from that server. Do view the revised code below.
#tasks.loop(seconds=15.0)
async def getAlert():
#do work here
guild = bot.get_guild(server_id_as_int)
channel = guild.get_channel(channel_id_as_int)
await channel.send("TESTING")
(Edit: Including answer from comments so others may refer to this)
You should also ensure that your getAlert.start() is in an on_ready() event, as the bot needs to start and enter discord before it would be able to access any guilds or channels.
#bot.event
async def on_ready():
getAlert.start()
print("Ready!")
Helpful links:
discord.ext.tasks.loop - discord.py docs
on_ready event - discord.py docs
bot.get_guild(id) - discord.py docs
guild.get_channel(id) - discord.py docs
"I can't get certain guild with discord.py" - Stackoverflow

Python Discord Bot Error 'NoneType' object has no attribute 'send'

I'm currently programming my own discord bot. Now i'm trying, to do that, when the bot startet it sends a dm to me.
This is my code
#bot.event
async def on_ready():
owner = bot.get_user(520430044007628800)
dm_channel = await owner.create_dm()
await owner.send("Wir sind eingeloggt als User {}".format(bot.user.name), tts=True)
but i'm always getting the error:
AttributeError: 'NoneType' object has no attribute 'send'
and i have done some research but i found literally nothing i hope you guys can help me:) i appreciate it
Make sure you have the correct intents needed in order to send DM messages, in this case you will need default and members intents enabled. Here's the docs about discord.py intents.
Also, the discord bot must also share a server with the account that you're trying to create DM with. That could be another reason why you're getting this error if it isn't because of intents.
Below is the code needed to make this work, assuming that you have intents enabled in your developer portal and the bot shares a server with the user ID sent into get_user.
from discord.ext import commands
import discord
TOKEN = "your discord code here"
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=".", intents = intents)
#bot.event
async def on_ready():
owner = bot.get_user(520430044007628800)
await owner.send("Wir sind eingeloggt als User {}".format(bot.user.name), tts=True)
bot.run(TOKEN)
Check if you have default and members intents enabled.
After that try using fetch_user instead of get_user
Like this:
owner = bot.fetch_userI520430044007628800)
await owner.send("Wir sind eingeloggt als User {}".format(bot.user.name), tts=True)

Intents wont work with Discord.py enabled both trough website and code

So I am making discord bot that removes certain role if u remove certain emoji reaction from message. I enabled my intents both trough discord developers website and code and I still get error 'NoneType' object has no attribute 'remove_roles', this is my code
import discord
from discord.utils import get
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents = intents)
#client.event
async def on_raw_reaction_remove(payload):
guild = await client.fetch_guild(payload.guild_id)
if payload.emoji.name == "πŸŸ₯":
member = guild.get_member(payload.user_id)
role = get(guild.roles, id = 861745493049868319)
await member.remove_roles(role)
#client.event
async def on_ready():
print("Bot is online")
client.run()
anyone knows what might be the issue?
The issue doesn't have to do with intents. Its because you are using fetch_guild which does not populate with members, thus your .get_member call is returning None.
Always try to avoid using the fetch methods and try to use the get_x equal.
Also, this behavior is noted on fetch_guild

Sending message in discord using bot

I was working on discord.py and I got issue with error AttributeError: 'NoneType' object has no attribute 'send'
Here is the code
import discord
from discord.ext import commands
pybot=commands.Bot(command_prefix="#", description="I love it",case_insensitive=True)
log_channel_id=674175630916583445
#pybot.event
async def on_ready():
print(f"Logged in as{pybot.user}")
channel = pybot.get_user(log_channel_id)
await channel.send('πŸ‘€')
pybot.run(TOKEN, bot=True, reconnect=True)
You want to get a channel but you're using the get_user function. Since the bot can't find a user with the channel's ID, it returns None.
Replace
channel = pybot.get_user(log_channel_id)
with
channel = pybot.get_channel(log_channel_id)
#pybot.event
async def on_ready():
print(f"Logged in as{pybot.user}")
channel = pybot.get_channel(674175630916583445)
await channel.send('πŸ‘€')
You can have more information about get_channel in the official Discord.py docs
get_channel(id)

Categories