Telethon bot sends message with wrong reply header - python

I'm creating a Telethon bot that receives a message in SOURCE channel and redirects the message to the TARGET channel. Everything is working fine except for this particular issue:
When the message on the SOURCE channel is a reply to another message and the bot sends it to the TARGET channel, the message on the TARGET channel ends up replying to the wrong message.
#bot.on(events.NewMessage(source_channel_id))
async def start_handler(event: events.NewMessage.Event):
log(f"Received signal: {event.text}")
reply_message = await event.get_reply_message()
await bot.send_message(target_channel_id, message=event.text, reply_to=reply_message)
log(f"Redirected signal: {event.raw_text}")
Although I've passed the object reply_message to the reply_to param on bot.send_message, the message redirected to the TARGET channel takes the message_id atribute from my reply_message object and replies to the message with the message_id from the TARGET channel and not from the SOURCE channel as I would expect. See images below:
FROM SOURCE CHANNEL:
FROM TARGET CHANNEL:
I just can't find a way to fix this strange behavior. What am I missing here?
Thank you for your time.

The problem is probably because the reply to parameter in the bot.send message method refers to the message in the TARGET channel, not the SOURCE channel right? if that's the case then
You might need to to keep track of the message IDs in both channels and use the corresponding message ID in the reply to parameter.
Can you try this improvised script, maybe it would sort it out for you?
#bot.on(events.NewMessage(source_channel_id))
async def start_handler(event: events.NewMessage.Event):
log(f"Received signal: {event.text}")
reply_message = await event.get_reply_message()
target_message = await bot.send_message(target_channel_id, message=event.text)
if reply_message:
await bot.send_message(target_channel_id, reply_to=target_message.id)
log(f"Redirected signal: {event.raw_text}")

Related

Why am I author of message send by byt - python discord

I am trying to send discord message to channel using python bot, but when I print it's author, it's me and not the bot. So later I can't edit it because of the author.
How can I send message as the bot?
My function:
#bot.command(name="send")
async def send(ctx: Context) -> None:
message = "message"
await ctx.channel.send(message)
print(ctx.message.author)
Because ctx.message is the message that invoked the command, not the message sent by the bot... So ctx.message.author is the person that invoked the command, which is you.
print(ctx.message.author) # <- "print the author of the message that invoked this command"
When you use Messageable.send(), it returns the message it sent. Thus, ctx.channel.send() returns the message that it sends. The .author of that message is your bot, and that is the message you can edit.
message = await ctx.channel.send("something")
I should mention I'm using a wrapper called pycord which I highly recommend.
member.id (Shows the specified members id)
member.name (Shows the specified members name and #)
member.mention (Mentions the specified member)
Doing ctx.send will show only the bot's message but will show a error code at the same time, So use ctx.respond("whatever") instead.
ctx.channel.send will send to a channel is
channel.send will send to a specified channel

The bot don't send message in specific channel

I'm trying to make a partnership command and to do that I need the bot to send a message in a specific channel. This is the code:
bot.command()
async def force(ctx, *, message=None):
channel = bot.get_channel(854633281589084221)
await ctx.channel.send(message)
(Sorry for the bad translation)
In the line of code:
await ctx.channel.send(message)
If I put ctx before channel it works, but it sends the message in the channel in which the command was executed and the programme tells me that the local variable channel isn't used, while if I put it after it no longer tells me that channel isn't used but it does not work
channel already has the value of the channel you want to send the message in.
So, you just need to run await channel.send(message) or await bot.get_channel(<id>).send(message) for your bot to work.
(adding on to #TheSj)
When a command is ran, the bot already replies in that given channel.
If you want to make it so it can only reply in that channel, you can use discord permissions using roles and remove the role from each channel you don't want it in.
Hope this helps and have a great day.:)

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

Send message to dms discord.py

I want to send message to dms using my discord bot and because the api has changed from client.send_message(user, message) to channel.send(message) I have no idea how to do that, also I dont want to make it dependent on the on_message(message) event. Any ideas?
If you have the user ID you can do:
user = client.get_user(user_id)
await user.send('Hello')
However If you already have the User object, you can do the following:
await message.author.send('Hey')
Where message is a Message object received from a on_message() event.
As for whether you can send private messages without first receiving an event unfortunately that is not be possible due to obvious spam-related issues.

How to copy message from channel to channel in discord?

I'm trying to program a bot that can copy a message from one channel to another from any channel in discord, but the code that I typed in was not working even though I'm sure I followed the documentations correctly (hopefully).
#bot.command() #Moves a message from channel to channel
async def copymessage(ctx, message_id, channel_id):
"""
Copy a message from channel to channel
"""
guild = ctx.guild
channel = guild.get_channel(int(channel_id))
message = guild.fetch_message(int(message_id))
print(f'Copying {message_id} to {channel_id}')
await channel.send(message)
I have tried using a similar code (shown below) to test if I followed correctly. This code ran successfully and did what it was supposed to do.
channel = guild.get_channel(623681100778176513)
await channel.send("Sparkle is online!")
Maybe there's someone that can help me figure out the problem in my code?
fetch_message is an attribute of Messageables (guilds are not messageable), so you need to use the channel.fetch_message. It also must be awaited.
message = await channel.fetch_message(int(message_id))

Categories

Resources