How do you get a message from a channel? - python

channel = bot.get_channel(id_of_the_channel)
message = await channel.fetch_message(id_of_the_message)
I tried to use this code so that a bot could edit its own message, but there's an error on the line with "fetch_message".
File "main.py", line 280, in on_message
message = await channel.fetch_message(messageid)
AttributeError: 'NoneType' object has no attribute 'fetch_message'

So get_channel only returns the channel object if it's in the cache - if it's returning None then it's not. It is returning None as per the error message. To fix:
channel = await bot.fetch_channel(id_of_the_channel)
message = await channel.fetch_message(id_of_the_message)
This should resolve that issue.

Related

AttributeError: 'NoneType' object has no attribute 'send' when sending message

so I checked all over in stack overflow for answers and nothing.
Im trying to make it that when the bot fires up it will send a message in a channel, I have alr checked if the bot can see and talk in the channel and they can.
My code:
import discord
from discord.ext import commands
from datetime import datetime
intents = discord.Intents().all()
bot = commands.Bot(command_prefix="!", intents=intents, case_insensitive=True)
client = discord.Client(intents=intents)
#bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
current_time = datetime.now().strftime("%H:%M:%S")
channel = bot.get_channel('1069075658979954700')
embed = discord.Embed(title="GAH Verification",
description="I have awoken",
color=discord.Color.green())
embed.set_footer(text=current_time)
await channel.send('Hi')
bot.run(
"MTA2OTI2NDQ0MTQwMjcyODU3MQ.GV353u.XXagXFsp3Ax4vRxvt3y5s6P9FPPDrOeMh2M7jI")
Error:
Traceback (most recent call last):
File "/home/runner/GAH-Bot/venv/lib/python3.10/site-packages/discord/client.py", line 409, in _run_event
await coro(*args, **kwargs)
File "main.py", line 19, in on_ready
await channel.send('Hi')
AttributeError: 'NoneType' object has no attribute 'send'
currently bot.get_channel() returns None,
either because it dont get it because 1069075658979954700 is in quotes
you should try using:
bot.get_channel(1069075658979954700)
and check if the bot has access to the channel
if this won't work you can always try to get the channel via the server this should work 100% because maybe the channel is not loaded to the cache directly idk
server = bot.get_guild(1234) # server id here
channel = await server.fetch_channel(12345) # channel id here

How can I fix, AttributeError: 'NoneType' object has no attribute 'send'

Aim of this is to try get a local image saved on your computer sent to a discord channel via a bot
# Create an Intents object with the `messages` attribute set to True
intents = discord.Intents(messages=True)
# Create the client with the specified intents
client = discord.Client(intents=intents)
#client.event
async def on_ready():
# When the bot is ready, send the image to the specified channel
channel = client.get_channel(CHANNEL_ID)
with open(r"file path", 'rb') as f:
file = discord.File(f)
await channel.send(file=file)
client.run(TOKEN)
Traceback (most recent call last):
File "C:\Python\Python39\lib\site-packages\discord\client.py", line 409, in _run_event
await coro(*args, **kwargs)
File "path", line 39, in on_ready
await channel.send(file=file)
AttributeError: 'NoneType' object has no attribute 'send'
The offical documentation for Discord.py says
Parameters:
id (int) – The ID to search for.
Returns:
The returned channel or None if not found.
This means that the ID supplied does not match any server, so most likely you mistyped it or accidentally modified it in code somewhere
await channel.send(file=file)
AttributeError: 'NoneType' object has no attribute 'send'
So channel is None, so the error was caused by the following.
channel = client.get_channel(CHANNEL_ID)
I guess 99% that CHANNEL_ID is a string and not an integer...

How to send a embed message discord.py

I am trying to send this embed message to a channel, I will keep trying and changing things around and it does not want to work, the problem is that it does not recognize the ".send", because it can not be called with channel. I would really appreciate it if someone would explain this to me. MY ERROR:
Traceback (most recent call last):
File "C:\Users\jaspe\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\jaspe\OneDrive\Desktop\DISCORD_BOT\zdiscord bot.py", line 19, in on_message_edit
await channel.send(embed=embed)
AttributeError: 'NoneType' object has no attribute 'send'
My code:
#bot.event
async def on_message_edit(message_before, message_after):
embed = discord.Embed(title="{} edited a message".format(message_before.author.name),description="", color=0xFF0000)
embed.add_field(name=message_before.content, value="This is the message before any edit",inline=True)
embed.add_field(name=message_after.content, value="This is the message after the edit",inline=True)
channel = bot.get_channel("My channel id")
await channel.send(embed=embed)
Change this part:
channel = bot.get_channel("My channel id")
To:
channel = discord.utils.get(message_before.guild.text_channels, id=put_id_here)
The way you are getting the channel is probably returning None, which of course has no method named send
The channel ID you are trying to provide must be an INTEGER, but you provided a STRING.
#bot.event
async def on_message_edit(message_before, message_after):
embed = discord.Embed(title="{} edited a message".format(message_before.author.name),description="", color=0xFF0000)
embed.add_field(name=message_before.content, value="This is the message before any edit",inline=True)
embed.add_field(name=message_after.content, value="This is the message after the edit",inline=True)
channel = bot.get_channel(1234567890)
await channel.send(embed=embed)```

Ping members with discord.py

I am making a command for my bot that will DM members when the command is used +ping #Member . Here is my code:
if message.content.startswith('+ping'):
ping = message.content.replace("+ping ","")
dm_member = ping
pinger = message.author
await dm_member.send('You got pinged by:')
await dm_member.send(pinger)
But in return I get:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 95, in on_message
await dm_member.send('You got pinged by:')
AttributeError: 'str' object has no attribute 'send'
How do I fix this AttributeError?
When I replace dm_member with message.author (therefore pinging the author) it works.
I think it doesn't work because str objects don't have discord attributes. But how can I fix this?
if message.content.startswith('+ping'):
ping = message.content.replace("+ping ","")
user_id = int(ping[2:-1]) # convert to int
dm_member = await message.guild.get_member(user_id) # this function accepts an int
pinger = message.author
await dm_member.send('You got pinged by:')
await dm_member.send(pinger)
You set dm_member equal to ping which is a string, a Python built-in. pinger is the user object of the author. To get the pinged user, extract the user id from ping and use it to find the user object using await message.guild.get_member(user_id).

discord.py 'coroutine' object has no attribute 'reactions' error

#bot.command()
async def voto(ctx, id, num_people):
msg = ctx.fetch_message(id)
winners = []
total_users = []
reactions = msg.reactions
for reaction in reactions:
users = await reaction.users().flatten()
for user in users:
total_users.append(user)
for i in range(num_people):
winners.append(i.name)
winners_msg = '\n'.join(winners)
await ctx.send(f"{winners_msg}\nHas won")
I get the below error in the above code
File ".\Testing.py", line 24, in voto
reactions = msg.reactions
AttributeError: 'coroutine' object has no attribute 'reactions'
The above exception was the direct cause of the following exception:
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
I want to fix But I get an error
Please help me
I used a translator
In your code ctx.fetch_message(id) is a coroutine, so it should be awaited, that is the error you are getting.
Replace that line with:
msg = await ctx.fetch_message(id)

Categories