How do I add a description to an argument in a Discord slash command (Python) - discord

I'm the discord.ext library, and using the #bot.tree.command decorator. I've found that if I just use the #bot.command decorator then the command doesn't sync on_ready. I've added a description to the command itself, but I wanted to add a description to the optional argument it accepts. Code is below.
#client.tree.command(name="command", description="test command")
async def scores(interaction: discord.Interaction, date: str=datetime.now(tz).strftime('%Y-%m-%d')):
await interaction.response.send_message(str("\n".join(testcommand.getinfo(date))))
I saw this post that shows how to do it, but it only works with the #bot.command decorator. Trying it with #bot.tree.command fails.

I've figured out my own problem! There is a decorator to do this.
#discord.app_commands.describe(date = "Date in YYYY-MM-DD format")

Related

discord.ext.commands.errors.CommandNotFound: Command is not found [solved]

I finished a bot after a couple of hours and while trying to run it I get this error message 'discord.ext.commands.errors.CommandNotFound: Command "play" is not found' I don't know why/ how to fix this could anyone help?
The chunk of code with the error will be below this text:
#commands.command()
async def play(self,ctx,url):
ctx.voice_client.stop()
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
YDL_OPTIONS = {'format':"bestaudio"}
vc = ctx.voice_client
ask me if you need more of the code/more of the terminal output
I tried running the bot multiple times and I've looked around overflow for an answer and found nothing :/
(Comments mentioned you didn't load your cogs)
discord.py can't just "know" that it has to load your cogs, you have to do this yourself. This works through the process of extensions.
Extensions are loaded using load_extension. The best place to do this is setup_hook (don't use something like on_ready!). An extension is a file with a setup() method, which is invoked by the library when you call load_extension(). You can use this function to attach the cog to your bot.
Loading the extension in your bot's file:
class MyBot(commands.Bot):
...
async def setup_hook(self):
# This is typically done by looping over a directory of Cog files
# instead of 1 by 1, but this is just an example
await self.load_extension("path.to.your.extension")
Attaching the Cog in your extension's file:
class MyCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
async def setup(bot):
await bot.add_cog(MyCog(bot))

DIscord Slash Commands Interaction Failed on discord py

async def _ping(ctx): # Defines a new "context" (ctx) command called "ping."
async def command_hi(ctx):
await ctx.defer()
await ctx.send("Pong!")
pass
I was making slash commands and first time it worked but secondtime, it does not work.
I tried await ctx.defer() but it's not working either
Hey #Justnoob I recommend you to use Pycord, It has inbuilt Buttons, Slash, Dropdowns and much more but for now the discord api with application commands is a bit complicated we usually use ctx.send but in app commands it is ctx.respond so here is the code:-
async def ping(ctx): #Don't use _ping too.
await ctx.respond("Pong!")
And I still don't understand why you created two async def in one command. There should be only one and you already specified it.

Discordbot with discord.py doesn`t deletes messages with command ctx.channel.purge(*amount*)

I got the problem, that when I use the purge command in my Bot using discord.py rewriting method, it doesn't work. What I mean by that is that when I run the code, and then write 'clear' in the discord channel, it simply doesn't deletes the given amount of messages, and it also doesn't raises an error. I've also tried to put print('test') in the definition, but then it only prints test...
That is the Code I used to do this:
#client.command
async def clear(ctx, amount=5):
await ctx.channel.purge(limit=amount)
the proper use of #client.command is #client.command(). See if that fixes it.

Getting the URL of an attachment in Discord.py

I am trying to get the URL of the file that the user attaches so I can use it later on in my code however I'm not too sure on how to go about doing so.
#client.command(pass_context=True)
async def readURL(ctx, url):
# Do something
References:
discord.ext.commands
Attachment.url
If I'm understanding the question correctly, it would go like this:
#client.command() # context is automatically passed in rewrite
async def readURL(ctx):
attachment = ctx.message.attachments[0] # gets first attachment that user
# sent along with command
print(attachment.url)
References:
Attachment.url
Message.attachments

Discord.py - Tag a random user

I recently started working with Discord.py.
My question is: How can I tag a random user, for example if you write !tag in the chat? I haven't found an answer yet.
if message.content.startswith('+best'):
userid = '<# # A RANDOM ID #>'
yield from client.send_message(message.channel, ' : %s is the best ' % userid)
ThankĀ“s
Here's how I'd do it:
Generate a list of users in your server
Use random.choice to select a random user from the list
Mention that user using the API (or if you'd like, do it manually) along with your message
Here's the implementation:
from random import choice
if message.content.startswith('+best'):
user = choice(message.channel.guild.members)
yield from client.send_message(message.channel, ' : %s is the best ' % user.mention)
Elaborating on aeshthetic's answer, you can do something like this:
import random
if message.content.startswith('+best'):
channel = message.channel
randomMember = random.choice(channel.guild.members)
await channel.send(f'{randomMember.mention} is the best')
Please note that the code is in the rewrite version of discord.py and not the async version - if you're using the async version, I'd recommend you migrate to rewrite as support for the async version of discord.py has ceased. To learn more about that, refer to the migrating documentation found here.
Let me know if you need help - happy coding!
I was playing around a bit and got this to work, you can try it.
#client.command(pass_context=True)
async def hug(ctx):
user = choice(ctx.message.channel.guild.members)
await ctx.send(f'{ctx.message.author.mention} hugged {user.mention}')
First of all, I suggest you install discord.py-rewrite, as it is more advanced.
Then, I suggest you create bot commands using the #client.command() decorator, like this:
#client.command()
async def testcommand(ctx):
pass
Now that you have done both things, there are several ways to do it. For example, if you don't want the bot to mention the command invoker or other bots, you can write:
from random import choice
from discord.ext import commands
#client.command()
#commands.guild_only()
async def tag(ctx):
try:
await ctx.send(choice(tuple(member.mention for member in ctx.guild.members if not member.bot and member!=ctx.author)))
except IndexError:
await ctx.send("You are the only human member on it!")
If you don't want the bot to mention other bots, but it can mention the command invoker, use:
from random import choice
from discord.ext import commands
#client.command()
#commands.guild_only()
async def tag(ctx):
await ctx.send(choice(tuple(member.mention for member in ctx.guild.members if not member.bot)))
If you want the bot to mention any guild member, human or bot, use:
from random import choice
from discord.ext import commands
#client.command()
#commands.guild_only()
async def tag(ctx):
await ctx.send(choice(tuple(member.mention for member in ctx.guild.members)))

Resources