Sending message in discord using bot - python

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)

Related

How to send message to a specific discord channel

I am trying to send a message to a specific channel and I can run the code below just nothing show up on the channel and I have no idea why... I put the channel Id in the get_channel input, just putting a random number here.
import discord
client = discord.Client()
async def send_msg(msg):
channel = client.get_channel(123456456788)
await channel.send('hello')
Is the function even called somewhere? Otherwise there could also be an issue with the discord permissions or the Intents.
This is one of many ways to call the function and post 'hello' in some specific channel. No msg parameter is required.
#client.event
async def on_ready():
await send_msg()
async def send_msg():
channel = client.get_channel(123456456788)
await channel.send('hello')

Discord.py on_member_join() sending a message into a specific channel

So i was writing a simple discord bot where when an user join it would simply send a message in the main channel saying something like hello user. Everything was working, at first i printed that a user joined on the console and everything worked. Then i tried to do it in a channel by using channel = client.get_channel("my channel here") await channel.send("Hello") but it's not working as intended. So i was wondering if there was a way to do this.
(Btw this is my first stackoverflow question, so if there's anything i did wrong pls let me know)
Here is the Full code:
from discord.ext import commands
intents = discord.Intents()
intents.members = True
client = commands.Bot(command_prefix='!', intents = intents)
#client.event
async def on_ready():
print("The bot logged on")
print("-----------------")
#client.event
async def on_member_join(member):
print("Somebody joined")
channel = client.get_channel(my channel here)
await channel.send("Hello")
client.run("MY token")
And here is the error:
AttributeError: 'NoneType' object has no attribute 'send'```
You need to await fetch, because the cache might not have the channel in there when the member joins. Your coroutine error comes from not awaiting it. (An unexecuted coroutine object gets assigned to channel, and then you get an error when it tries to call that because it was "already called".)
#client.event
async def on_member_join(member):
print("Somebody joined")
channel = await client.fetch_channel(123456789)
await channel.send("Hello")

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

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)

'Member' has no attribute 'server'

In the given code below I was trying to give a role to the person who reacts to a message by using any emoji but this code throws me an error saying that 'Member' has no attribute 'server' what should I do now?
import discord
from discord.ext import commands
import random
client = discord.Client()
#client.event
async def on_ready():
print('ready')
#client.event
async def on_reaction_add(reaction, user):
channel = reaction.message.channel
await channel.send(f'{user.name} has reacted by using {reaction.emoji} emoji, his message was {reaction.message.content}')
role = discord.utils.get(user.server.roles, name = 'Bot')
await client.add_roles(user, role)
client.run('TOKEN')
First off take a look at the documentation. It'll tell you everything you need to know about what has what attributes. Also, server is called guild in discord.py. So use user.guild.roles instead.

On member join event message doesn't work - discord.py

I'm very new to discord.py, I want to learn python and make a simple bot for my server.
I want make a bot that sends a message when someone joins server. But the bot won't send any message. I tried so many solutions but none work.
No error in console. Just 'on_member_join' doesn't work; ping and console message methods work.
Here is my code:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='*')
TOKEN = '----my token here----'
#client.event
async def on_ready():
print('Bot is ready')
#client.event
async def on_member_join(member):
channel = client.get_channel(764960656553934879)
await channel.send('Hello')
#client.command()
async def ping(ctx):
await ctx.send('Pong!')
client.run(TOKEN)
For discord.py 1.5.0, reference the introduction of intents.
Your bot will need the members intent - which happens to be a priveleged intent - in order for the on_member_join event to be triggered properly.

Categories