Send images in embeds discord.py - python

I'm trying to send embeds in discord.py, but whenever i run the code nothing gets sent. No errors nothing. Here is my code:
from variables.variables import client, token
import discord
#client.command()
async def test(ctx):
imageURL = "https://discordapp.com/assets/e4923594e694a21542a489471ecffa50.svg"
embed = discord.Embed()
embed.set_image(url=imageURL)
await ctx.send(embed=embed)
client.run(token)
This is what the embed looks like using above code:
Thanks!

.svg is not supported. Here's the same image in .png format:
#client.command()
async def test(ctx):
imageURL = "https://cdn.discordapp.com/attachments/744631318578462740/755866713182044240/ezgif-4-6eba1fde99f2.png"
embed = discord.Embed()
embed.set_image(url=imageURL)
await ctx.send(embed=embed)

Related

Is there a way to embed this discord message discord bot python

i have been struggling to embed this message , its currently getting the images from the path of the python project and sending them , but i wanted to embed them like this:
Title being Image:
Description being Enjoy!
And the coloured side bar to be red
Example of image
current code :
#client.command()
async def image(channel):
file = random.choice(files)
await channel.send(file=discord.File(file))
You need to read more about embeds before simply asking what to do without any effort.
#client.command()
async def image(channel):
file = random.choice(files)
embed = discord.Embed(title="", description="Enjoy!", color=discord.Color.red())
embed.set_image(url=file)
await channel.send(embed=embed)

Embed an image for discord using embeds

I'm trying to make an embed image command, something like $embed (image link)
This is my code:
async def image(ctx, link):
await ctx.message.delete()
embd = discord.Embed("")
embed.set_image(url=link)
await ctx.channel.send(embed=embd)
But it doesn't work, how can I fix it?
You spelled your variables incorrectly. Try this out:
async def image(ctx, link):
await ctx.message.delete()
embed = discord.Embed("")
embed.set_image(url=link)
await ctx.send(embed=embed)

Changing the avatar of the bot in Discord

I am trying to replace the avatar image of the bot when a user runs a command. How would I do this? I have managed to replace the nickname but not the image.
await ctx.guild.me.edit(nick=nick)
^ Replaces the nickname.
I tried (nick=nick, avatar=avatar) but it did not work.
EDIT:
#client.command()
async def test(ctx):
if ctx.guild.id == second_server:
await ctx.guild.me.edit(nick=nick, avatar_url=avatar)
pfp_path = "Profile-Picture.png"
with open(pfp_path, "rb") as pfp:
await client.user.edit(password=None, avatar=pfp.read())
print("profile picture changed")
According to the official docs, the following should work:
import discord
client = discord.Client()
token = '...'
#client.event
async def on_ready():
pfp_path = "file.jpg"
with open(pfp_path, "rb") as pfp:
await client.user.edit(password=None, avatar=pfp.read())
print("profile picture changed")
client.run(token)
You cannot directly give a URL to the desired profile picture.
Note: the only formats supported are JPEG and PNG

How to make my custom discord bot sends message

Im trying to make my custom bot sends a custom message using python
Me: ~repeat Hi
My Message deleted
custom-Bot: Hi
whenever I try using this I get error problems with this code specifically "client"
await client.delete_message(ctx.message)
return await client.say(mesg)
from discord.ext import commands
client = commands.Bot(command_prefix = '~') #sets prefix
#client.command(pass_context = True)
async def repeat(ctx, *args):
mesg = ' '.join(args)
await client.delete_message(ctx.message)
return await client.say(mesg)
client.run('Token')
client does not have an attribute called delete_message, to delete the author's message use ctx.message.delete. To send a message in the rewrite branch of discord.py, you use await ctx.send()
#client.command()
async def repeat(ctx, *args):
await ctx.message.delete()
await ctx.send(' '.join(args))

How can I send an embed via my Discord bot, w/python?

I've been working a new Discord bot.
I've learnt a few stuff,and, now, I'd like to make the things a little more custom.
I've been trying to make the bot send embeds, instead, of a common message.
embed=discord.Embed(title="Tile", description="Desc", color=0x00ff00)
embed.add_field(name="Fiel1", value="hi", inline=False)
embed.add_field(name="Field2", value="hi2", inline=False)
await self.bot.say(embed=embed)
When executing this code, I get the error that 'Embed' is not a valid member of the module 'discord'. All websites, show me this code, and I have no idea of any other way to send a embed.
To get it to work I changed your send_message line to
await message.channel.send(embed=embed)
Here is a full example bit of code to show how it all fits:
#client.event
async def on_message(message):
if message.content.startswith('!hello'):
embedVar = discord.Embed(title="Title", description="Desc", color=0x00ff00)
embedVar.add_field(name="Field1", value="hi", inline=False)
embedVar.add_field(name="Field2", value="hi2", inline=False)
await message.channel.send(embed=embedVar)
I used the discord.py docs to help find this.
https://discordpy.readthedocs.io/en/latest/api.html#discord.TextChannel.send for the layout of the send method.
https://discordpy.readthedocs.io/en/latest/api.html#embed for the Embed class.
Before version 1.0: If you're using a version before 1.0, use the method await client.send_message(message.channel, embed=embed) instead.
When executing this code, I get the error that 'Embed' is not a valid member of the module 'discord'. All websites, show me this code, and I have no idea of any other way to send a embed.
This means you're out of date. Use pip to update your version of the library.
pip install --upgrade discord.py
#bot.command()
async def displayembed(ctx):
embed = discord.Embed(title="Your title here", description="Your desc here") #,color=Hex code
embed.add_field(name="Name", value="you can make as much as fields you like to")
embed.set_footer(name="footer") #if you like to
await ctx.send(embed=embed)
how about put #client.event instead of the #bot.command() it fixed everything when I put #client.event... #bot.command() does not work you can type
#client.event
async def displayembed(ctx):
embed = discord.Embed(title="Your title here", description="Your desc here") #,color=Hex code
embed.add_field(name="Name", value="you can make as much as fields you like to")
embed.set_footer(name="footer") #if you like to
await ctx.send(embed=embed)
For anyone coming across this in 2022:
how about put #client.event instead of the #bot.command() it fixed everything when I put #client.event... #bot.command() does not work you can type
To this ^, I don't recommend using #client.event / #bot.event as you'd want to register your commands as a command.
If you want to simply make a command with an embed in your main.py file, make sure you have something like:
import discord
from discord.ext import commands
intents = discord.Itents.default()
bot = commands.Bot(command_prefix='YOURPREFIX', description='description', intents=intents)
#bot.command(name="embed")
async def embed(ctx):
embed = discord.Embed(title='Title', description='Desc', color=discord.Color.random())
embed.add_field(name="Name", value="Value", inline=False)
await ctx.send(embed=embed)
However, I personally like separating my commands into a /commands folder and with separate files for all of them as it's good practice for neater code.
For that, I use cogs.
/commands/embed.py
from discord.ext import commands
import discord
class EmbedCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
#commands.command(name="embed")
async def embed(self, ctx):
embed = discord.Embed(title='Title', description='Desc', color=discord.Color.random())
embed.add_field(name="Name", value="Value", inline=False)
await ctx.send(embed=embed)
Then import it all into your main.py file:
from commands.embed import EmbedCog
bot.add_cog(EmbedCog(bot))
Before using embed
embed (Embed) – The rich embed for the content to send. This cannot be mixed with embeds parameter.
embeds (List[Embed]) – A list of embeds to send with the content. Maximum of 10. This cannot be mixed with the embed
TypeError – You specified both embed and embeds or file and files or thread and thread_name.
#client.event
async def on_message(message):
if message.content.startswith('!hi'):
embed = discord.Embed(title="This is title of embedded element", description="Desc", color=0x00ff00)
embed.add_field(name="Like header in HTML", value="Text of field 1", inline=False)
embed.add_field(name="Like header in html", value="text of field 2", inline=False)
await message.channel.send(embed=embed)
Reference

Categories

Resources