Discord.py How to download the server icon? - discord.py

I want to make my bot download the server icon, but using that guild.icon I only get a string of numbers and letters and not a URL. Can someone help?

In the documentation you can see that you can use guild.icon_url to get an url to the servers icon. So if you want to get the server icon from the guild a message is from.
You can use ctx.guild.icon_url to get the url to the icon:
#commands.command()
async def get_server_icon_url(self, ctx):
icon_url = ctx.guild.icon_url
await ctx.send(f"The icon url is: {icon_url}")
The reason that guild.icon does not work as expected. Is that guild.icon returns the hash of the icon instead of an url.

ctx.guild.icon_url doesn't work, try ctx.guild.icon.url instead.
#commands.command()
async def get_server_icon_url(self, ctx):
icon_url = ctx.guild.icon.url
await ctx.send(f"The icon url is: {icon_url}")

Related

discord.py - message.content returns null

I am currently trying to program my own Discord Bot with Python. I have now made an "on_message" event, but "message.content" is not returning anything. No message, nothing! Can you help me?
#client.event
async def on_message(message):
if message.content.startswith("$test"):
await message.channel.send("test")
You don't have the message_content intent, so you can't read messages.
Docs: https://discordpy.readthedocs.io/en/latest/intents.html
Don't forget to enable them on your dev portal as well.
Also, instead of manually parsing messages with prefixes, consider using the built-in Commands framework that does all this for you. Docs: https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html
PS "null" is not a thing in Python, it's None. And the message content isn't None/"null", it's an empty string ("").

How do I make my discord bot copy and paste someone's message in python?

I am trying to make a command that has an interaction like this:
User: p.poesia (text) #user
Bot: (text copied)
#user, 2021
Please consider reading the discord.py documentation.
This is the basic code I made for you to run it.
#client.command()
async def poesia(ctx, text):
await ctx.send(text + str(ctx.author))

How do you create a discord bot that creates embeds which contain previews of videos

I'm creating a bot that allows users to create embedded messages. Here is a command that I am working with:
#bot.command()
async def embed(ctx,title_str,text_str,url_str):
embed=discord.Embed(title=title_str, url=url_str, description=text_str, color=0xFF5733)
await ctx.send(embed=embed)
This works fine, but if a video is linked I'd like it to add a preview image - in the same way it does if you post a Youtube link I tried adding image = video_url to the discord.Embed command, but this did not help.
Clearly discord is able to do this because when you type in a url to a video you get a preview in an embed automatically generated.
Per the Discord API docs: "For the embed object, you can set every field except type (it will be rich regardless of if you try to set it), provider, video, and any height, width, or proxy_url values for images."
Therefore, you cannot assign a video to Embeds via the API.
See: https://discord.com/developers/docs/resources/channel#create-message

Command not found error after changing virtual machine

Okay so I have a music bot code that was working before I switched to another virtual machine provider. All the requirements are exactly the same as in my previous virtual machine because I copy and pasted everything including the requirements.txt. The bot runs normally with 0 errors until I try to run any of the commands. It gave me this error:
discord.ext.commands.errors.CommandNotFound: Command "play" is not found
I've tried rolling back to the rewrite version I started the project on,
changed #client.command to #bot.command after assigning bot = commands.Bot(command_prefix='prefix')
#I've assigned client = discord.ext.commands
#client.command(name='play', aliases=['sing'])
async def play(self, ctx, *, search: str):
#then some code
update 1: Ran it as a cog and raised:
discord.ext.commands.errors.ExtensionFailed: Extension 'music' raised an error: TypeError: cogs must derive from Cog
update 2: No idea why rolling back the rewrite version didn't work though. Perhaps I didn't do it correctly.
Just simply run it as a cog.
Note that the way cogs work has been updated recently:
https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html
If you still want to run it as a standalone bot,
your bot should looks like something along the lines of:
from discord.ext.commands import Bot
bot = Bot("!")
#bot.command(name='play', aliases=['sing'])
async def play(ctx, *, search: str): # Note no self
#then some code
bot.run("token")
It's important that the bot that you run is the same bot that you register the commands with. You're also passing self to your bot even though it's not in a cog, which doesn't make sense.
Okay so I found the problem.
The bot doesnt work when I try to run it as a standalone bot.
The reason using it as a cog don't work the first time was because.
the way cogs work has been changed in discord.py rewrite.
These are the changes I made:
#in cogs/music.py
class Music:
#Code
#bot.event
async def on_ready():
print('Music bot online')
to
#in cogs/music.py
class Music(commands.Cog):
#Code
#commands.Cog.listener()
async def on_ready():
print('Music bot online')
Thank you to the legendary #PatrickHaugh for helping me figure this out.

discord.py delete author message after executing command

I need the bot to delete the message from the command author, and leave the bot message. Any help will be appreciated! thank you.
I have already tried looking for a answer on google but nothing has worked
I'm kinda late here. I tried implementing this with the new 1.0 terms but couldn't make it work. If anyone has an updated version, please do tell
Edit: I found that the new best way of doing it is to just add this at the end of the function:
await ctx.message.delete()
No need for a separate delete function anymore.
You can obtain the message that called the command by passing the context with the command using the pass_context option. You can use the Client.delete_message coroutine to delete messages.
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command(pass_context=True)
async def deletethis(ctx):
await bot.say('Command received')
await bot.delete_message(ctx.message)
await bot.say('Message deleted')
bot.run('token')

Resources