Discord py - edit webhook message embed in 'on_message' - python

i have a small problem that i dont know how to fix it. I want to edit a messages embed, that was send by a discord webhook directly if it gets posted. So that's why i use the 'on_message' event for this. In the normal way, you can just fetch the message and than use msg.edit but that doesnt work for webhook messages.
How can i edit the message from the webhook directly when it gets posted? I tried to use the api "discord-webhooks" but i couldn't find a solution for that what i want.
I tried this:
#bot.event
async def on_message(message):
##### Netflix-News edit ######
if message.channel.id == 804484025166856262:
embed = discord.Embed(title=f"{title}",
url=f"{lonk}",
description=f"{descfix}",
color=0xe74c3c)
embed.set_thumbnail(url=f"https://i.imgur.com/4np2bdK.png")
embed.set_image(url=f"{Thumbnail}")
embed.set_footer(text=f"{footer}", icon_url="https://i.imgur.com/4np2bdK.png")
webhook = DiscordWebhook(url='LINKHERE')
webhook.embed = embed
await webhook.edit(sent_webhook)

Webhook.edit edits the webhook itself, not a message. To edit a message use Webhook.edit_message
await webhook.edit_message(message_id, embed=embed)
PS: You didn't define sent_webhook
Reference:
Webhook.edit_message

Related

How to get a telegram bot work in a channel?

I can't get it to work through properties like command or just content_types. No errors show up.
#dp.message_handler(commands=["start"])
async def start(msg:Message):
if msg.chat.type == ChatType.CHANNEL:
await msg.answer(msg)
I made so bot sent a message to the channel. Still nothing.
P.S It works with getUpdates
In the Dispatcher class there is a decorator called "channel_post_handler".

How to quit a discord bot without using 'except' python

need quick help this is the code that simply posts to a discord channel I am using it as a subpart of my project all I want from this to post and shuts (without killing the program) so I can't use 'exit', I have used while loops and all that but nothing is working.
def post_discord():
print('POSTING NEW OFFER TO DISCORD')
token = 'token here'
while True:
bot = commands.Bot(command_prefix='!')
#bot.command()
async def on_ready():
await bot.wait_until_ready()
channel = bot.get_channel(12345678) # replace with channel ID that you want to send to
# await channel.send()
await channel.send(text,file =discord.File('promo.png'))
await asyncio.sleep(2)
# exit()
# await bot.close()
bot.loop.create_task(on_ready())
bot.run(token)
time.sleep(3)
If you really want to just stop the bot from running after it finishes doing whatever you want it to do, what you're looking for is discord.Client.logout(). In your scenario, the usage would be bot.logout()
Although, as someone who replied to your post said, a webhook may be more suitable for your use case. A simple webhook for sending a message using requests would look something like this:
import requests
data = {"content": "Message Content"}
requests.post("Webhook URL", data=data)
Discord's documentation for this can be found here, notable limitations for this route are that you're unable to attach files, but if you're just needing to send an image along with some text, a .png link may suffice as Discord will hide the link in the message.

Listening to reaction or message after sending embed in discord.py

I have a command, that should setup the verification process, i want that my bot, after sending an embed, listens for the next message, sent in chat, and storing that message in a variable. I know that on_message() exists, but how do to that without it?
The command should look something like this:
#client.command()
async def verificationSetup(ctx, channel_id, message):
if channel_id is not None:
embed=discord.Embed(title="Send the emoji you want to set!", description=" ")
await ctx.channel.send(embed=embed)
emoji = listen_to_message()
Solved
What I was looking for was wait_for()
Solved thanks to a guy on discord.py's discord

Python - Discord bot on ready send message to channel

I have problem creating simple python script which sends message to default channel when executed from terminal.
import discord
#bot.event
async def on_ready(ctx):
print('Online')
await ctx.send('Message sent!')
bot.run('MYTOKEN')
with this example I keep getting "ctx" is not defined
The issue here is that the on_ready event should not receive any parameters. See the Minimal Bot documentation here and the on_ready documentation in the Event Reference here. If you want to send a message when the bot connects. You must first get the channel object and then use the send method. You can find an example of getting a channel and sending a message in the FAQ section of the docs here

Can't send private messages from my discord bot to user?

I want to send a private message to a specific user with my discord bot. I am using discord==1.0.1
discord.py==1.3.1. I already tried using the docs (https://discordpy.readthedocs.io/en/latest/api.html?highlight=private%20message#discord.DMChannel) but i dont understand that. I tried following code, which didnt worked:
#client.command()
async def cmds(ctx):
embed = discord.Embed(title='Discord Server Befehle',
description='Description',
color=0x374883)
[...]
msg = await ctx.send(embed=embed)
await ctx.author.create_dm('Hi')
print(f'{command_prefix}help ausgeführt von {ctx.author}')
Try using the send function instead of the create_dm. create_dm only creates the channel between two users which discord does automatically as far as i know.
according to documentation
This should be rarely called, as this is done transparently for most people.
so it should be
ctx.author.send('Hi')

Categories

Resources