Is it possible in python-telegram-bot to get update when somebody puts a "reaction" (like/dislike/other emojis) on message? - python-telegram-bot

The following code using python-telegram-bot (for the bot added as admin in some group) captures all kind of messages - posting messages, replies, edits. But when somebody puts like (or other emojis) on existing message, a handler function is not invoked.
from telegram import Update
from telegram.ext import Updater, MessageHandler, Filters, CallbackContext
updater = Updater(token='TOKEN', use_context=True)
dispatcher = updater.dispatcher
def handler(update: Update, context: CallbackContext):
print("here")
echo_handler = MessageHandler(Filters.all, handler)
dispatcher.add_handler(echo_handler)
updater.start_polling()
Is it possible in python-telegram-bot to get update in a handler function when somebody puts a "reaction" (like/dislike/other emojis) on message?

Related

Cogs Not Working and not detected In Discord.py

My bot program can't detected cogs file
I have tried every way to solve it, but still nothing has changed
main.py
import discord
import os
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents)
async def load():
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
bot.run('MTA2NzQ1MDMxMTAzMzIzMzUxOA.GSdg-i.CW3X5T82VYGYjPqEPWZ8YQ6oDZni33T-Bi1Ovo')
lab.py
import discord
from discord.ext import commands
class lab(commands.Cog):
def __init__(self,bot):
self.bot = bot
#commands.Cog.listener()
async def on_ready(self):
print('lab.py is running')
#commands.command()
async def holla(ctx):
await ctx.reply('hai')
async def setup(bot):
await bot.add_cog(lab(bot))
eror
I need someone to help me solve my problem, thanks in advance
You're never calling that load() function... It's cool that you made it, but if you're not using it then it's not very surprising that your cogs aren't being loaded.
The recommended place to do this in is setup_hook. Subclass Client (or Bot depending on what you want) and override it.

Discord.py automatic message handler

this code (discord.py's Cog) should simply send a "Hello World" message.What im trying to do is a exercise. It would be also useful to know how to make it work on a loop(eg. every 2hrs). What im trying to achive, is that, when the bot has started, it will automatic send Hello World in channel (ID). anyone know how i can solve this?
I tried using an event listener, with different guides from discord.py repository but nothing seems to be working.
import discord
from discord.ext import commands
from discord.ext.commands import Context
from helpers import checks
class Tickets2(commands.Cog, name="ticket2"):
def __init__(self, bot):
self.bot = bot
#commands.hybrid_command(
name="ticket2",
description="Hello Wolrd."
)
#checks.not_blacklisted()
async def ticket2(self, context: Context) -> None:
"""
Stampa a video hello
"""
channel = self.bot.get_channel(1066374083803107518)
await channel.reply('hello')
async def setup(bot):
await bot.add_cog(Tickets2(bot))

Discord.py 2.0 - Button showing before message finishes editing view

I'm hosting my bot online and sometimes messages take time to edit their own View components which is fine. The problem is when i modify a view by calling
await message.edit(view=...)
, the new button/select components are displayed instantly but their callbacks are not operational because the message editing is taking some time to complete. Thus, unknown interaction error tends to occur when clicking on the button a little too early, the callbacks are not being called, and I need to wait to re-click.
My question is : Is it possible to wait for a message.edit() to fully complete before showing the buttons, or is there another way to solve this issue?
Code sample :
async def throw_dice(self,ctx):
try :
superself = self
async def action(superself):
...
if isinstance(self.current,PlayerDiscord) :
class myButton(ui.Button):
def __init__(self,label,style,row=None):
super().__init__(label=label,style=style,row=row)
async def callback(self,interaction):
await interaction.response.defer()
nonlocal superself
if interaction.user.id==superself.current.member.id:
self.view.stop()
await superself.msg_play.edit(view=None)
await action(superself)
self.view = FRPGame.myView(ctx,self) #Create new view
self.view.add_item(myButton("\U0001F3B2",style=discord.ButtonStyle.primary))
#self.msg_play stores the message
await self.msg_play.edit(content=self.content,view=self.view) #<-- problem is this single line
else :
...
except BaseException :
traceback.print_exc()
Please send your entire code so we can see what could be done
#code here
asyncio.sleep(10) #Will wait 10 seconds before sending
You can use the wait_for method to wait for the message to be edited. You can then use the message.edit method to edit the message and add the buttons.
import discord
from discord.ext import commands
from discord_components import DiscordComponents, Button, ButtonStyle
bot = commands.Bot(command_prefix="!")
DiscordComponents(bot)
#bot.command()
async def test(ctx):
message = await ctx.send("Hello")
await message.edit(content="Hello World")
await message.edit(components=[
Button(style=ButtonStyle.red, label="Red"),
Button(style=ButtonStyle.green, label="Green"),
Button(style=ButtonStyle.blue, label="Blue")
])
bot.run("token")
This uses discord-components, which you can get here:
https://devkiki7000.gitbook.io/discord-components

Discord.py bot not able to send embed messages?

Trying to implement embedded messages for my discord bot using interactions. The following is the code with the error message under it.
import interactions
import discord
bot = interactions.Client(token="<REDACTED>")
#bot.command(
name="test",
description="Tests"
)
async def test(ctx: interactions.CommandContext):
embed = interactions.Embed(
title="testing",
description="test purposes"
)
await ctx.send(embed = embed)
Error Message:
payload = await super().send(content, **kwargs)
TypeError: _Context.send() got an unexpected keyword argument 'embed'
interactions.py documentation said This is quite simple: The argument embed got deprecated by Discord. The new naming is embeds.
Changed embed into embeds and it works now.
#bot.command(
name="test",
description="Tests"
)
async def test(ctx: interactions.CommandContext):
embeds = interactions.Embed(
title="testing",
description="test purposes"
)
await ctx.send(embeds = embeds)

How do I send a message with components (buttons) in discord_components? (discord.py)

I can't figure out how to send a message without using ctx in the discord_components module, which extends discord.py for using buttons and other components. The fact is that in some guides uses ctx, but this does not suit me. I want to send a message according to the following principle: message.channel.send(embed=embed, components = components), but in this module (discord_components) I can't figure out why this doesn't work for me, and why I encounter the error "TypeError: send() got an unexpected keyword argument 'components'".
I also tried using "send_component_msg" instead of "send", but I got the error " Attribute error: the "TextChannel" object does not have the "send_component_msg " attribute".
My code:
import discord
from discord_components import DiscordComponents, Button, Select, SelectOption
async def info(msg):
embed = my_embed
...
components = [Button(label = "🔸"), Button(label = "🔶"), Button(label = "🔺"), Button(label = "🔻")]
try:
await message.edit(embed = embed, components = components)
except Exception:
message = await msg.channel.send_component_msg(embed=embed, components = components)
While I haven't used discord-components, I've used discord-py-slash-commands (https://discord-py-slash-command.readthedocs.io/en/latest/index.html), which has support for slash commands, buttons, and dropdown menus.
If you want to create a button using discord-py-slash-commands, it is pretty simple, here's the code:
buttons = [
create_button(style=ButtonStyle.green, label="🔸"),
create_button(style=ButtonStyle.green, label="🔺"),
create_button(style=ButtonStyle.green, label="🔻")
]
action_row = create_actionrow(*buttons)
message = await ctx.send("hello there")
await message.edit(components=[action_row])
While this doesn't use discord-components, hopefully this helps.

Resources