An example of using discordpy 2.0.0a to create a bot that supports slash commands

Overview

DpySlashBotExample

An example of using discordpy 2.0.0a to create a bot that supports slash commands. This is not a fully complete bot, just an example of a kind of structure and syntax that can be used to keep discord.ext.commands-like syntax while still supporting slash commands. This approach comes with many limitations, but it is relatively easy to integrate into an existing bot.

Requirements

  • Python 3.8
  • discordpy 2.0.0a
  • A discord bot that has been added to a server

Instructions

  1. Download and extract the repo.
  2. Edit slashbot/core/bot and replace "your_token_here" with your bot's token. Change the text command prefix if you would like to now.
  3. Run the bot with python -m slashbot.
  4. Once "Ready" appears in console, you should be able to use the text commands to interact with the bot. Run ff-load example to load the example cog.
  5. Run ff-setup to register the slash commands in your server.
  6. Assuming the example cog is loaded, and you have registered the slash commands, you should now be able to run /command and /group command.

Explanation

This bot overrides the add_cog, remove_cog, and on_interaction methods of commands.AutoShardedBot in order to integrate slash commands in a non-intrusive way. When a cog is loaded, slash commands are searched for similar to how text commands are searched for, and are added to a dictionary stored in the bot class. When a cog is unloaded, any slash commands are removed from this dictionary. When a command interaction happens, the bot searches for a registered slash command with the same "path". If one is found, the function is executed, with arguments prepared from the raw data given by the interaction. If one is not found, a simple error message is sent.

slashbot/core/slash.py does most of the heavy lifting. SlashContext is intended to be a drop in replacement for the context normally returned by a command. It does not contain everything that context normally does, partially because I chose not to add everything, and partially because everything is not able to be obtained. The send method is defined to respond to the interaction if it has not yet been responded to, otherwise it just calls channel.send as normal. prepare_args and recursive_options are internal functions which take in an interaction and output that interaction's path and formatted arguments. Slash commands cannot have more than 1 optional arg as this system does not currently differentiate args outside of the order they are received from the API, however it is possible to modify this method to support that. If your bot has the members intent, it may be a good idea to replace the SlashMember mock class with a call to guild.get_member.

slashbot/core/example.py is an example slash cog. It appears very similar to a normal cog, with a few key differences. @commands.command() is replaced with @slash.command(), using the decorator provided by slashbot/core/slash.py. This prepares slash commands to be found by the bot when the cog is loaded. Slash command function names follow a syntax of command_subcommand, however the optional path=("command", subcommand) argument can be provided to the decorator to override the default name handling. Currently existing checks cannot be used with slash commands, although it may be possible to modify the SlashCommand class to support them. Cog methods such as cog_check, before_invoke, etc will not be run on slash commands.

Registering Commands

This code will not register slash commands dynamically, though it is possible to modify add_cog to do so. If you want to register a command (other than the example commands), you will need to do so semi-manually. A slash command payload must be created, see the discord api docs for an explanation on how to create one. Once you have a payload, you can run the following code to register a command:

payload = {
    "name": "command",
    "type": 1,
    "description": "An example command payload.",
}
# For a guild-specific command
await bot.http.upsert_guild_command(bot.user.id, guild.id, payload)
# For a global command
await bot.http.upsert_global_command(bot.user.id, payload)

Integration into your bot

Be sure to respect the license of this code if you integrate any or all of it into your bot. If you want to integrate a similar system into your bot, you will probably need to do the following things:

  1. Add the add_cog, remove_cog, and on_interaction overrides to your bot class.
  2. Add slash.py to your bot.
  3. Replace all instances of @commands.command() with @slash.command().
  4. Manually register all of the commands you require.
  5. Deal with any and all issues that arise as a result of incompatibilities with this system.

Support/Contact

I will not be providing support for this code, nor any attempts to integrate this code into your bot. This repo is purely for demonstration of one system that can be used to create a bot which supports slash commands using only discordpy 2.0.0a.

You might also like...
Pincer-ext-commands - A simple, lightweight package for pincer prefixed commands

pincer.ext.commands A reimagining of pincer's command system and bot system. Ins

Example-bot-discord - Example bot discord xD

example-python-bot-discord Clone this repository Grab a token on Discord's devel

A discord bot wrapper for python have slash command

A discord bot wrapper for python have slash command

Transcript-Extractor-Bot - Yet another Telegram Voice Recognition bot but using vosk and supports 20+ languages

transcript extractor Yet another Telegram Voice Recognition bot but using vosk a

A drop-in vanilla discord.py cog to add slash command support with little to no code modifications

discord.py /slash cog A drop-in vanilla discord.py cog that acts as a translation layer to add slash command support with little to no code modificati

Slash util - A simple script to add application command support to discord.py v2.0
Slash util - A simple script to add application command support to discord.py v2.0

slash_util is a simple wrapper around slash commands for discord.py This is writ

Terminal Bot which will Execute your Commands From telegram bot!

Terminal-Bot see this bot alive: https://t.me/HerokuTerminal_Bot With this bot you can execute system commands on your server. how to config? clone or

Grocy-create-product - A script supports the batch creation of new products in Grocy

grocy-create-product This script supports the batch creation of new products in

This is a simple bot that can be used to upload images to a third-party cloud (image hosting). Currently, only the imgbb.com website supports the bot. I Will do future updates

TGImageHosting This is a simple bot that can be used to upload images to a third party cloud (image hosting). Currently, only the imgbb.com website su

Owner
Howdy!
null
Aqui está disponível GRATUITAMENTE, um bot de discord feito em python, saiba que, terá que criar seu bot como aplicação, e utilizar seu próprio token, e lembrando, é um bot básico, não se utiliza Cogs nem slash commands nele!

BotDiscordPython Aqui está disponível GRATUITAMENTE, um bot de discord feito em python, saiba que, terá que criar seu bot como aplicação, e utilizar s

Matheus Muguet 4 Feb 5, 2022
Attempting to create a framework for Discord Slash commands... yes

discord_slash.py Attempting to create a framework for Discord Slash commands... yes Installation pip install slashpy Documentation Coming soon™ Why is

AlexFlipnote 11 Mar 24, 2021
Discord music bot using discord.py, slash commands, and yt-dlp.

bop Discord music bot using discord.py, slash commands, and yt-dlp. Features Play music from YouTube videos and playlists Queue system with shuffle Sk

Hizkia Felix 3 Aug 11, 2022
A Slash Commands Discord Bot created using Pycord!

Hey, I am Slash Bot. A Bot which works with Slash Commands! Prerequisites Python 3+ Check out. the requirements.txt and install all the pakages. Insta

Saumya Patel 18 Nov 15, 2022
Your custom slash commands Discord bot!

Slashy - Your custom slash-commands bot Hey, I'm Slashy - your friendly neighborhood custom-command bot! The code for this bot exists because I'm like

Omar Zunic 8 Dec 20, 2022
Your custom slash commands Discord bot!

Slashy - Your custom slash-commands bot Hey, I'm Slashy - your friendly neighborhood custom-command bot! The code for this bot exists because I'm like

Omar Zunic 3 Jan 8, 2022
A python library for creating Slack slash commands using AWS Lambda Functions

slashbot Slashbot makes it easy to create slash commands using AWS Lambda functions. These can be handy for creating a secure way to execute automated

Eric Brassell 17 Oct 21, 2022
A Python wrapper for discord slash-commands, designed to extend discord.py.

dislash.py An extending library for discord.py that allows to build awesome slash-commands. ⭐

null 173 Dec 19, 2022
A discord Server Bot made with Python, This bot helps people feel better by inspiring them with motivational quotes or by responding with a great message, also the users of the server can create custom messages by telling the bot with Commands.

A discord Server Bot made with Python, This bot helps people feel better by inspiring them with motivational quotes or by responding with a great message, also the users of the server can create custom messages by telling the bot with Commands.

Aran 1 Oct 13, 2021
A Telegram Bot to Play Audio in Voice Chats With Youtube and Deezer support. Supports Live streaming from youtube Supports Mega Radio Fm Streamings

Bot To Stream Musics on PyTGcalls with Channel Support. A Telegram Bot to Play Audio in Voice Chats With Supports Live streaming from youtube and Mega

Shamil Habeeb 37 Dec 15, 2022