Skip to content

Instantly share code, notes, and snippets.

@BruceCodesGithub
Last active September 6, 2021 16:17
Show Gist options
  • Save BruceCodesGithub/f448844d32b4f648672fdb7c03f7b8d1 to your computer and use it in GitHub Desktop.
Save BruceCodesGithub/f448844d32b4f648672fdb7c03f7b8d1 to your computer and use it in GitHub Desktop.
Application Commands Examples 101

Context Menus

Examples

import discord
from discord.ext import commands
from discord.app import Option # alpha

bot = commands.Bot()

@bot.user_command(name="Wow these can have spaces", description="This created a global user command")
async def coolname(ctx, member:discord.Member):
	await ctx.send(f"You chose {member.mention}!")
	

@bot.message_command(name="Reapeat", description="This created a global message command")
async def coolname(ctx, message:discord.Message):
	await ctx.send(f"You said: {message.content}")

Slash Commands

Basic Slash Commands

import discord
from discord.ext import commands
from discord.app import Option # alpha

bot = commands.Bot()

bot.slash_command(name="hello", description="Say hello to a user", guild_ids=[...])
async def hello(ctx, text:Option(str, 'description'), user:Option(discord.user, 'Choose a user', required=False)):
	await ctx.send("Hello, {user.mention}!", ephemeral=True, allowed_mentions=discord.AllowedMentions.none())
	

Sub command group

import discord
from discord.ext import commands
from discord.app import Option # alpha

bot = commands.Bot()

greet = discord.command_group(name="greet", description="Greet related commands", guild_ids=[...])

greet.command(name="hello", description="Say hello to a user")
async def hello(ctx, text:Option(str, 'description'), user:Option(discord.user, 'Choose a user', required=False)):
	await ctx.send("Hello, {user.mention}!", ephemeral=True, allowed_mentions=discord.AllowedMentions.none())
@CodeWithSwastik
Copy link

bot.command_group not discord.command_group

@Dorukyum
Copy link

Dorukyum commented Sep 6, 2021

Also discord.user should be discord.User

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment