Discord.py How can get attachment in message.content - python

I'm trying to make a bot. It's an archive bot. If some message getting some emoji and some amount, it's moving message to a specific channel that user chooses. I have a problem: it's working well but it's not getting attachment in message.content. Just getting strings, not attachment. How can I fix this problem?
Here is my code:
#Bot.event
async def on_raw_reaction_add(payload):
user = Bot.get_user(payload.user_id)
guild = Bot.get_guild(payload.guild_id)
channel =
guild.get_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
#some files is opening in these lines...
if #some check points..
else:
await guild.get_channel(to_list[sıra]).send("Written by {} :\n\n {}".format(message.author.mention,message.content))
await guild.get_channel(from_list[sıra]).send("{} your message move to this channel --> {}".format(message.author.mention,Bot.get_channel(to_list[sıra]).mention))
await message.add_reaction("✔️")
How can i get attachment in message.content in on_raw_reaction_add function ?

Try using files in send() method. Example:
await channel.send(
message.content, files=[await f.to_file() for f in message.attachments]
)
In your case it will probably look like that:
await guild.get_channel(to_list[sıra]).send(
"Written by {} :\n\n {}".format(message.author.mention, message.content),
files=[await f.to_file() for f in message.attachments],
)
Reference:
discord.Message.attachments
discord.Attachment.to_file
List Comprehension

Related

Discord.py Translate message contents from Channel to Specific User ID's DMS

I am trying to create a bot that if it detects "$" before a message, to then take that entire messages content, and send it to a specific users dms. (user id removed for security)
async def on_message(message):
if message.content.startswith('$ '):
CONTENT = message.content
async def sendDm():
user = await client.fetch_user("530916313972080652")
await user.send(CONTENT)
However, upon doing this, the bot does not respond to any message period.
I have tried multiple send.message functions, all to no avail, most saying not defined.
I got assistance from the Discord.py Discord.
#client.event
async def on_message(message):
if message.content and message.content.startswith("$") and not message.author.bot:
user = await client.fetch_user(user_id)
await user.send(message.content)

is there a way to send a message after a message has been deleted?

So, when a user sends a message I want my bot to send a message to that channel with an emoji
so far I've been able to send it to a specific channel, but after that I haven't been able to send a message to any channel that a message was deleted in.
maybe I just don't know the attributes.
thanks for all the help in advance!
#bot.event
async def on_message_delete(ctx):
if ctx.author.id != 835676293625282601:
author = ctx.message.author
del_msg = await channel.send(":eyes:")
await author.send(del_msg)
await asynciaito.sleep(10)
await del_msg.delete()
Events never take ctx as an argument, if you take a look at the docs you can see that on_message_delete takes message as the only argument
#bot.event
async def on_message_delete(message):
if message.author.id != 835676293625282601:
author = message.author
del_msg = await message.channel.send(":eyes:")
await author.send(del_msg)
await asynciaito.sleep(10)
await del_msg.delete()
Also you don't have to learn all the attributes from all the discord objects, just read the documentation

How to send messages that are sniped to another channel? (discord.py)

Whenever a message is deleted, I want that message to be sent into another channel sort of like a log. Basically whenever the message is deleted within a certain channel, I want that sniped/deleted message to be sent into another channel.
Example: If a message gets deleted in the channel X, I want the message content to go to channel Y.
Code
import discord
from discord.ext import commands
from tokens import token, CHANNEL_ID
client = commands.Bot(command_prefix='!')
client.sniped_message = None
#client.event
async def on_ready():
print("Your bot is ready.")
#client.event
async def on_message(message):
if message.channel.id == CHANNEL_ID and message.author != client.user:
print(f'Fetched message: {message}')
client.sniped_message = message
#client.command()
async def snipe(ctx):
if ctx.channel.id != CHANNEL_ID:
return
if client.sniped_message is None:
await ctx.channel.send("Couldn't find a message to fetch!")
return
message = client.sniped_message
embed = discord.Embed(
description=message.content,
color=discord.Color.purple(),
timestamp=message.created_at
)
embed.set_author(
name=f"{message.author.name}#{message.author.discriminator}",
icon_url=message.author.avatar_url
)
embed.set_footer(text=f"Message sent in: #{message.channel.name}")
await ctx.channel.send(embed=embed)
# assume I would be sending the embed to another channel but not sure how to tackle that
client.run(token)
Help is very appreciated!
PS: I am very new to Python and it is very different from JS...
Get the Channel Y like this first:
channely = client.get_channel(channel_id)
Then you can do
await channely.send(client.sniped_message)

Retrieving a message from on_raw_reaction_add discord.py

I've been trying to make it so the bot removes the reaction using discord.Message.remove_reaction(), but I can't find a way to actually retrieve and store the message in a variable. Does anybody know how to do this?
Here is the code I have so far:
#client.event
async def on_raw_reaction_add(self):
if self.message_id == 805179023641542726:
channel = client.get_channel(self.channel_id)
message = ???
user = client.get_user(self.user_id)
await message.remove_reaction(self.emoji,user)
on_raw_reaction_add returns a RawReactionActionEvent which has a message_id attribute. You can pass it to discord.abc.Messageable.fetch_message to retreive the message :
#client.event
async def on_raw_reaction_add(payload):
channel = client.get_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
user = client.get_user(payload.user_id)
await message.remove_reaction(payload.emoji,user)
Reference : discord.py documentation

Discord.py trying to get the message author in my embed

I have been getting into coding my very own discord bot using Discord.py in Thonny, using python version 3.7.6. I want to have an embed when a certain command is typed (!submit) to have the users name as the title and the content of the message as the description. I am fine having !submit ' ' in my embed but if there is any way of taking that out and only having the content of the message minus the !submit i would highly appreciate it. Right now with my code i am getting two errors, one is that the client.user.name is the name of the bot (submit bot) and not the author (old code), and i am getting this message 'Command raised an exception: TypeError: Object of type Member is not JSON serializable' with my new code(below), if anyone could offer insight please reply with the appropriate fixes!
client = commands.Bot(command_prefix = '!')
channel = client.get_channel(707110628254285835)
#client.event
async def on_ready():
print ("bot online")
#client.command()
async def submit(message):
embed = discord.Embed(
title = message.message.author,
description = message.message.content,
colour = discord.Colour.dark_purple()
)
channel = client.get_channel(707110628254285835)
await channel.send(embed=embed)
client.run('TOKEN')
client = commands.Bot(command_prefix = '!')
#got rid of the old channel variable here
#client.event
async def on_ready():
print ("bot online")
#client.command()
async def submit(ctx, *, message): #the `*` makes it so that the message can include spaces
embed = discord.Embed(
title = ctx.author, #author is an instance of the context class
description = message, #No need to get the content of a message object
colour = discord.Colour.dark_purple()
)
channel = client.get_channel(707110628254285835)
await channel.send(embed=embed)
client.run('TOKEN')
Problems:
1. channel is defined twice,
2. function commands in discord.py take an implicit context argument, usually called ctx.
Overall, it looks like you aren't understand the underlying OOP concepts that discord.py has to offer. It might be helpful to refresh your memory with an online class or article.
You're pretty close...
The following items should help:
Working with commands you generally define the context of the command as "ctx", which is the first and sometime only argument. "message" is generally used on message events like async def on_message(message):.
To break out the message from the command itself you add arguments to the function definition.
To get the name of the user you need to cast to a string to avoid the TypeError.
To send the message back in the same channel as the !submit was entered, you can use await ctx.send(embed=embed)
Try:
#client.command()
async def submit(ctx, *, extra=None):
embed = discord.Embed(
title=str(ctx.author.display_name),
description=extra,
colour=discord.Colour.dark_purple()
)
await ctx.send(embed=embed)
Result:

Categories

Resources