Discord.py bot not able to send embed messages? - discord.py

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)

Related

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))

Can't figure out how to use discord.py ctx

I have tried to convert my discord bot's commands to using ctx instead of interactions but the code I have tried below does not work.
#bot.command(description="Says test")
async def test(ctx):
await ctx.send("Test")
My issue is that the command never loads.
Updated code but still broken
import discord
from discord import ui
from discord.ext import commands
bot = commands.Bot(command_prefix="/", intents = discord.Intents.all())
# Run code
#bot.event
async def on_ready():
print('The bot has sucuessfully logged in: {0.user}'.format(bot))
await bot.change_presence(activity=discord.Activity(type = discord.ActivityType.listening, name = f"Chilling Music - {len(bot.guilds)}"))
# Commands
#bot.command(description="Says Hello")
async def hello(ctx):
await ctx.send(f"Hello {ctx.author.mention}!")
#bot.command(description="Plays music")
async def play(ctx, song_name : str):
await ctx.author.voice.channel.connect()
await ctx.send(f"Now playing: {song_name}")
#bot.command(description="The amount of servers the bot is in")
async def servercount(ctx):
await ctx.send(f"Chill Bot is in {len(bot.guilds)} discord servers!")
#bot.command(description="The bots ping")
async def ping(ctx):
await ctx.send(f"πŸ“Pong\nChill Bot's ping is\n{round(bot.latency * 1000)} ms")
#bot.command(description="Announcement command")
async def announce(ctx, message : str):
await ctx.send(message)
#bot.command(description="Support Invite")
async def support(ctx):
await ctx.send(f"{ctx.author.mention} Please join our server for support! [invite]", ephemeral=True)
#bot.command(description = "Syncs up commands")
async def sync(ctx):
await ctx.send("Synced commands!", ephemeral=True)
bot.run('')
Your commands don't work because you incorrectly created an on_message event handler.
By overwriting it and not calling Bot.process_commands(), the library no longer tries to parse incoming messages into commands.
The docs also show this in the Frequently Asked Questions (https://discordpy.readthedocs.io/en/stable/faq.html#why-does-on-message-make-my-commands-stop-working)
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message.
Also, don't auto-sync (you're doing it in on_ready). Make a message command and call it manually whenever you want to sync.

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.

Discord.py react to bot own messages

I am trying to react to my bot own messages. But it does not work.
if args[0] == "solido":
reaction = "πŸ‘"
sent = await ctx.message.channel.send(client.get_channel("795387716967333889"), embed=embed)
# await message.add_reaction(sent, emoji = "\U0001F44D")
await ctx.message.add_reaction(reaction)
if I use this type of code, the bot will react to MY message commands. But if i dont use the ctx method the bot will give me this error: NameError: name 'message' is not defined
How could I fix?
using await sent.add_reaction(reaction) instead of await ctx.message.add_reaction(reaction) should work

Discord.py sending an embed without the use of add fields

I am trying to get a message from a user, and then sending it to a specific text channel. The message shall be in an embed. But I don't like how it looks when you have a bunch of fields, I would like the description in the discord.Embed() to hold the text content. But it is giving me an error
TypeError: Object of type Message is not JSON serializable
This is my code:
class Changelog(commands.Cog):
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_ready(self):
print('Changelog is loaded')
#commands.command()
async def changelog(self, ctx):
changelog_channel = self.client.get_channel(759547196433104956)
await ctx.send("`Message: `")
message = await self.client.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
embed = discord.Embed(title="Changelog", description=message, color=0)
await changelog_channel.send(embed=embed)
def setup(client):
client.add_cog(Changelog(client))
You're not getting this error because you're not adding fields, but because you're trying to put a message instance in the description, while you should be putting that message's content in there instead. The description can only take strings as it's value.
embed = discord.Embed(title="Changelog", description=message.content, color=0)
wait_for("message") returns a discord.Message instance. A Message holds that message's content, id, author, channel, and way more inside. If you only want to get the text the user sent, you need the content attribute.
More info on what a Message can do can be found in the API docs.

Resources