Getting message id of Discord bot - python

I want to send a message with my Discord bot, but since the id of this message will help me in my next job, I need to send this id to another channel, how can I do it? or can i? thanks in advance for your answers
I tried this for catch the message but is not worked i tought bot is not a "author"
#Bot.listen()
async def on_message(ctx):
if ctx.message.author.id == "1006858614369177711":
x_id = ctx.message.id
await ctx.channel.send(f"currend id : {x_id}")

You can simply await to the message inside a variable.
message = await ctx.channel.send(...)
Then you can get any property you want from that variable, like its id...

Related

how to make a discord.py bot not accepts commands from dms

How do I make a discord.py bot not react to commands from the bot's DMs? I only want the bot to respond to messages if they are on a specific channel on a specific server.
If you wanted to only respond to messages on a specific channel and you know the name of the channel, you could do this:
channel = discord.utils.get(ctx.guild.channels, name="channel name")
channel_id = channel.id
Then you would check if the id matched the one channel you wanted it to be in. To get a channel or server's id, you need to enable discord developer mode. After than you could just right click on the server or channel and copy the id.
To get a server's id you need to add this piece of code as a command:
#client.command(pass_context=True)
async def getguild(ctx):
id = ctx.message.guild.id # the guild is the server
# do something with the id (print it out)
After you get the server id, you can delete the method.
And to check if a message is sent by a person or a bot, you could do this in the on_message method:
def on_message(self, message):
if (message.author.bot):
# is a bot
pass
You Can Use Simplest And Best Way
#bot.command()
async def check(ctx):
if not isinstance(ctx.channel, discord.channel.DMChannel):
Your Work...
So just to make the bot not respond to DMs, add this code after each command:
if message.guild:
# Message comes from a server.
else:
# Message comes from a DM.
This makes it better to separate DM from server messages. You just now have to move the "await message.channel.send" function.
I assume that you are asking for a bot that only listens to your commands. Well, in that case, you can create a check to see if the message is sent by you or not. It can be done using,
#client.event
async def on_message(message):
if message.author.id == <#your user id>:
await message.channel.send('message detected')
...#your code

Print channel id/name into console with discord py

So I need my bot to print in which channel it sents a message. This is my code:
#client.command()
async def test(ctx):
await ctx.reply('hello', mention_author=True)
channel = #what goes here????
print('i said hello in', (channel))
I've tried all of these:
user = bot.get_user(user_id)
message.author.username
username = client.get_user(user_id)
The things you tried are referencing user id. I have not used discord.py in a while, but you will have to reference the channel id and not the user id. Maybe try:
ctx.message.channel.id
I can't remember if this returns an object or a string so you may have to format further to get the string.

Check if the message channel is in that category

Can someone help me? I'm trying to make a modmail every thing is working fine, user sends a DM, bot creates a channel under "category" and if someone message inside that "category" it will be delivered to the user via DM. However I get this annoying error every time someone replies inside that "category" or user DMs the bot. I'm trying to make a category check to only do something if it's the mod mail category. Thank you in advance!
Here's my code:
async def on_message(self, message):
if isinstance(message.channel, discord.DMChannel):
# User DM the bot and the bot will make a textchannel under "category" and will send his DMs there. #
if message.channel.category_id == 123456789101112:
if isinstance(message.channel, discord.TextChannel):
# Message the user who DMed the bot using the textchannel under "category", your replies will be sent to the user by the bot via DM. #
Everything is working, but I'm getting this error every time someone replies inside that textchannel "category" or user DMs the bot
Error:
if message.channel.category_id == 123456789101112:
AttributeError: 'DMChannel' object has no attribute 'category_id'
Your if-statement doesn't make much sense, you should first check for the type of the channel and then for it's category and compare it.
async def on_message(message):
# Checking if the channel is a dm or a text one
if isinstance(message.channel, discord.DMChannel):
# do your thing
elif isinstance(message.channel, discord.TextChannel):
if message.channel.category is not None:
if message.channel.category.id == your_id:
# do your thing
You should format your code properly.
Also you can check whether message.author.id is your bots id client.user.id or a user.
Read the documentation for further information.

How do I get a server's ID with discord.py?

I'm not using discord.ext, just discord. I need to find the server ID that the bot is currently connected to. I've done some googling, but could only find 1 stack overflow question and the answer wasn't helpful.
I could really use this, thanks!
You didn't specify how you want to get the server ID,
so here is an example using the command, you get ID of server, where this command was used.
#client.command(pass_context=True)
async def getguild(ctx):
id = ctx.message.guild.id
You can check it and output with print for example print(id) will give output like this: Also you can just add ctx.send(id) and Discord Bot will send this ID to server chat.
Discord guild id have integer type. Now you got ID and can manipulate with it.
Read more about it here
Here's a way to get the guild (server) ID without using discord.ext, assuming you're using:
async def on_message(self, message: discord.Message) -> None:
...
to capture messages/commands in your bot.
message.guild.name
and
message.guild.id
Would hold the relevant information.
Guild Name: 'My Server Name'
Guild ID: '299299999999999991'
I believe you will have to use the ctx.message.guild.id or ctx.guild.id
Here is some example code
#client.command(pass_context = true)
async def getserverid(ctx):
serverId = ctx.message.guild.id
await ctx.send(serverId)
Without discord.ext:
#client.event
async def on_message(message):
if(message.author == client.user):
print(f"Guild id: {message.guild.id}")

How to make my discord.py rewrite bot send a Private Message to the message author

I have made a Discord bot for a game's Discord server. I'm using the discord.py rewrite version and I want to send a private message to the author of the message.
I've tried other codes on the internet which includes some "#bot" code, but it always comes up with the error
"Name 'bot' is not defined"
and if I try send_message it says
"Client object has no attribute 'send_message'"
My code:
#I've tried this...
#bot.command(pass_context=True)
async def poke(ctx, message):
await client.send_message(ctx.message.author, 'boop')
#but it comes up with the error "Name 'bot' is not defined" and stuff like that
For example, I want to create a command "!messageme", and if a user executes the command I expect the bot to private message the author of the message saying "Just messaged you!".
If Pierce#9255 executes the command in the server, the bot should private message him saying "Just messaged you!".
Did you define your bot variable? If not then do this:
bot = commands.Bot(command_prefix='!') # Just add your desired prefix there.
# sending dm
#bot.command()
async def poke(ctx):
await ctx.author.send('boop!')
Also, if you still confused then just give this yt tutorial a try: -NiByO6h7Ck
Firstly you have to define the Bot. The you will have to DM a user.
bot = commands.Bot(command_prefix='your_prefix')
#bot.command()
async def hello(ctx):
user = ctx.author
await user.send("Hello!")

Categories