Reaction on the message - python-3.x

My code
#bot.event
async def on_message(message):
emoji = '🎨'
message = message.id
await bot.add_reaction(message, emoji)
Error in console
Ignoring exception in on_message
Traceback (most recent call last):
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/discord/client.py", line 270, in _run_event
await coro(*args, **kwargs)
File "main.py", line 31, in on_message
AttributeError: 'Bot' object has no attribute 'add_reaction'
How to fix this error?

As the python interpreter helpfully points out, your Bot object doesn't have the function add_reaction(). Instead, it's available to the Message object:
#bot.event
async def on_message(message):
emoji = '🎨'
await message.add_reaction(emoji)
You can look at their FAQ for more help: "How do I add a reaction to a message?"

Related

How to edit embed with message id discord bot py in on_ready event

I want to edit a embed in on_ready event this is the only way i can edit can do on_message or is there a way to keep a function running until the program ends?
from discord.ext import commands
import discord
from discord.utils import get
#bot.event
async def on_ready():
msg = bot.get_message(892788147287654471)
em = discord.Embed(title="lik",description="kjk")
await msg.edit(embed=em)
bot.run(os.environ['token'])
error code:
Ignoring exception in on_ready
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 56, in on_ready
msg = bot.get_message(892788147287654471)
AttributeError: 'Bot' object has no attribute 'get_message'
Well as the error says, bot has no get_message attribute. Instead what you can do is get the channel, and then get the message or partial message within.
https://discordpy.readthedocs.io/en/master/api.html?highlight=partial%20message#discord.PartialMessage
Example:
message = bot.get_channel(1234567890).get_partial_message(1234567890)
await message.edit(content="test")

Facing error when using buttons from discord components in cogs Discord.py

import discord
from discord.ext import commands
bot = discord.ext.commands.Bot(command_prefix = ";");
import random
from asyncio import TimeoutError
import subprocess
import sys
from discord_components import *
class GameCommands(commands.Cog):
def __init__(self, bot):
self.bot=bot
#commands.Cog.listener()
async def on_ready(self):
DiscordComponents(bot)
print('ready')
#commands.command()
async def rps(self,ctx):
choices=['rock','paper','scissors']
bot_choise=random.choice(choices)
player_choise=""
yet= discord.Embed(title=f"{ctx.author.display_name}'s Rock Paper Scissors Game", description="Click On A Button", color=discord.Color.from_rgb(0, 208, 255))
won= discord.Embed(title=f"You Won! Le Hurray!", description=f"You chose {player_choise} and the Bot chose {bot_choise}", color=discord.Color.from_rgb(255, 213, 0))
lost= discord.Embed(title=f"You Lost! Sadge :(", description=f"You chose {player_choise} and the Bot chose {bot_choise}", color=discord.Color.from_rgb(102, 61, 69))
tie= discord.Embed(title=f"Hmm, A tie!", description=f"You and the bot both chose {bot_choise}", color=discord.Color.from_rgb(137, 49, 181))
out= discord.Embed(title=f"Timeout ", description=f"You didn't choose any option in time, Bruh!", color=discord.Color.from_rgb(43, 194, 146))
m = await ctx.send(embed=yet,components=[[Button(style=1 ,label="Rock"),Button(style=3 ,label="Paper"),Button(style=4 ,label="Scissors")]])
def check(res):
return ctx.author==res.user and res.channel==ctx.channel
try:
res= await self.bot.wait_for("button_click", check=check, timeout=15)
player_choise= res.component.label
if player_choise==bot_choise:
await m.edit(embed=tie, components=[])
if player_choise=="Paper" and bot_choise=="Rock":
await m.edit(embed=won, components=[])
if player_choise=="Scissors" and bot_choise=="Paper":
await m.edit(embed=won, components=[])
if player_choise=="Rock" and bot_choise=="Scissors":
await m.edit(embed=won, components=[])
if player_choise=="Rock" and bot_choise=="Paper":
await m.edit(embed=lost, components=[])
if player_choise=="Paper" and bot_choise=="Scissor":
await m.edit(embed=lost, components=[])
if player_choise=="Scissor" and bot_choise=="Rock":
await m.edit(embed=lost, components=[])
except TimeoutError:
await m.edit(embed=out, components=[])
def setup(bot):
bot.add_cog(GameCommands(bot))
I have discord-components installed, whenever i run the command i get this error:
Ignoring exception in command rps:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/home/runner/TheDuck/commands/games.py", line 37, in rps
m = await ctx.send(embed=yet,components=[[Button(style=1 ,label="Rock"),Button(style=3 ,label="Paper"),Button(style=4 ,label="Scissors")]])
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord_components/client.py", line 46, in send_component_msg_prop
return await self.send_component_msg(ctxorchannel.channel, *args, **kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord_components/client.py", line 177, in send_component_msg
data = await self.bot.http.request(
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 192, in request
async with self.__session.request(method, url, **kwargs) as r:
AttributeError: 'NoneType' object has no attribute 'request'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'request'
I don't know what is causing this error. I tried finding about it online but couldn't.
I would Like to know what possible mistake i might be making or is it a bug.
I saw almost the same code being run on the main file without any problems. Is it a problem with cogs or my code is wrong?
While I am unsure if that is a bug or not, I think it would be better to use Discord.py 2.0 (Currently in Alpha) for buttons instead of third party libs. Keep in mind it might be unstable so is your choice.

Green to python, having issues with Discord Embed

So keep getting a Type error stating send() got an unexpected keyword argument 'Embed'
I'm not sure what I did wrong. Any help would be awesome.
client = discord.Client()
#client.event
async def on_message(message):
if message.content == 'what is the version':
dev_channel = client.get_channel(Deleted for obs reasons)
myEmbed = discord.Embed(title="Current Version", description="The bot
is in Version 1.0", color=0x00ff00)
myEmbed.add_field(name="Version Code:", value="v1.0.0", inline=False)
myEmbed.add_field(name="Date Released:", value="November 25th, 2020",
inline=False)
myEmbed.set_footer(text="End of message")
await dev_channel.send(Embed=myEmbed)
Visual Studios gives following text in terminal when I type what is the version in the discord text channel.
Ignoring exception in on_message
Traceback (most recent call last):
", line 333, in _run_event
File "C:\Users\lazy5\AppData\Local\Programs\Python\Python39\lib\site-
packages\discord\client.pyy", line 333, in _run_event
await coro(*args, **kwargs)
File "c:\Users\lazy5\Desktop\worth.py", line 30, in on_message
await dev_channel.send(Embed=myEmbed)
TypeError: send() got an unexpected keyword argument 'Embed'
On your send line
await dev_channel.send(Embed=myEmbed)
'Embed=' should be lowercase like so:
await dev_channel.send(embed=myEmbed)

Callback for join command is missing "ctx" parameter

I'm trying to make a music cog for my bot. I'm currently making commands for the bot to join and leave a voice channel. But everytime i try to launch both of these commands, the same error occurs.
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 663, in _parse_arguments
next(iterator)
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 930, in on_message
await self.process_commands(message)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 927, in process_commands
await self.invoke(ctx)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 790, in invoke
await self.prepare(ctx)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 751, in prepare
await self._parse_arguments(ctx)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 666, in _parse_arguments
raise discord.ClientException(fmt.format(self))
discord.errors.ClientException: Callback for join/leave command is missing "ctx" parameter.
Here's the code i have:
import discord
from discord.ext import commands
import youtube_dl
class Music(commands.Cog):
def __init__(self, client):
self.client = client
#commands.command()
async def join(ctx):
channel = ctx.message.author.voice.channel
await channel.connect()
#commands.command()
async def leave(ctx):
await ctx.voice_client.diconnect()
def setup(client):
client.add_cog(Music(client))
Anyone know how i can fix this ?
You need to put self in your function async def join as well as your leave function:
#commands.command()
async def join(self, ctx):
channel = ctx.message.author.voice.channel
await channel.connect()
You're using a cog so you have a class with self that must be passed to classes not decorated with #staticmethod or #classmethod. With discord.py I recommend you do not do either of those for command/event functions you have above.
See: https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html
For cogs in discord.py

How to fix "discord.errors.ClientException: Command kick is already registered." error?

I'm making a discord bot in discord.py that kicks any member that sends a certain string, but I get the error "discord.errors.ClientException: Command kick is already registered."
bot = commands.Bot(command_prefix=',')
#client.event
async def on_message(message):
if message.author == client.user:
return
if "kick me"in message.content:
#bot.command(name="kick", pass_context=True)
#has_permissions(kick_members=True)
async def _kick(ctx, member: Member):
await bot.kick(member)
Instead of kicking the member, I get this lovely traceback:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Jason\AppData\Local\Programs\Python\Python35\lib\site-packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:\Users\Jason\AppData\Local\Programs\Python\Python35\PrawnBot.py", line 66, in on_message
async def _kick(ctx, member: Member):
File "C:\Users\Jason\AppData\Local\Programs\Python\Python35\lib\site-packages\discord\ext\commands\core.py", line 574, in decorator
self.add_command(result)
File "C:\Users\Jason\AppData\Local\Programs\Python\Python35\lib\site-packages\discord\ext\commands\core.py", line 487, in add_command
raise discord.ClientException('Command {0.name} is already registered.'.format(command))
discord.errors.ClientException: Command kick is already registered.
Whenever the message !kick me is sent, you're re-registering the command. The command should be at the top level of your script or cog, not recreated every time the event is called.
bot = commands.Bot(command_prefix=',')
#bot.event
async def on_message(message)
...
await bot.process_commands(mesage)
#bot.command(name="kick", pass_context=True)
#has_permissions(kick_members=True)
async def _kick(ctx, member: Member):
await bot.kick(member)

Resources