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.

Owner
Howdy!
StudyLion is a Discord bot that tracks members' study and work time while offering members to view their statistics and use productivity tools such as: To-do lists, Pomodoro timers, reminders, and much more.

StudyLion - Discord Productivity Bot StudyLion is a Discord bot that tracks members' study and work time while offering members the ability to view th

45 Dec 26, 2022
troposphere - Python library to create AWS CloudFormation descriptions

troposphere - Python library to create AWS CloudFormation descriptions

4.8k Jan 06, 2023
veez music bot is a telegram music bot project, allow you to play music on voice chat group telegram.

🎶 VEEZ MUSIC BOT Veez Music is a telegram bot project that's allow you to play music on telegram voice chat group. Requirements 📝 FFmpeg NodeJS node

levina 143 Jun 19, 2022
Python Telegram bot api.

pyTelegramBotAPI A simple, but extensible Python implementation for the Telegram Bot API. Getting started. Writing your first bot Prerequisites A simp

FrankWang 6.4k Jan 09, 2023
A Telegram Bot Written In Python

TelegraphUploader A Telegram Bot Written In Python DEPLOY Local Machine Clone the repository Install requirements: pip3 install -r requirements.txt e

Wahyusaputra 2 Dec 29, 2021
Telegram Group Calls Streaming bot with some useful features, written in Python with Pyrogram and Py-Tgcalls. Supporting platforms like Youtube, Spotify, Resso, AppleMusic, Soundcloud and M3u8 Links.

Yukki Music Bot Yukki Music Bot is a Powerful Telegram Music+Video Bot written in Python using Pyrogram and Py-Tgcalls by which you can stream songs,

Team Yukki 996 Dec 28, 2022
This repository contains the best Data Science free hand-picked resources to equip you with all the industry-driven skills and interview preparation kit.

Best Data Science Resources Hey, Data Enthusiasts out there! Finally, after lots of requests from the community I finally came up with the best free D

Mohit Kumar 415 Dec 31, 2022
Lazy airdrop based on private temporary ids

LobsterDAO This uses a modified MerkleDistributor, which allows to issue a lazy airdrop using temporary IDs. In this example it uses Telegram chat_id

41 Sep 10, 2022
Python library for the DeepL language translation API.

The DeepL API is a language translation API that allows other computer programs to send texts and documents to DeepL's servers and receive high-quality translations. This opens a whole universe of op

DeepL 535 Jan 04, 2023
A simple Telegram bot that can add caption to any media on your channel

Channel Auto Caption This bot can add a caption for any media/document sent to a channel. Just deploy bot and add bot as admin to a channel. Deploy to

22 Nov 14, 2022
❤️ DaisyX 2.0 ❤️ A Powerful, Smart And Simple Group Manager ... Written with AioGram , Pyrogram and Telethon...

❤️ DaisyX 2.0 ❤️ A Powerful, Smart And Simple Group Manager ... Written with AioGram , Pyrogram and Telethon... ⭐️ Thanks to everyone who starred Dais

TeamOfDaisyX 44 Oct 06, 2022
Huggingface transformers for discord

disformers Huggingface transformers for discord base source butyr/huggingface-transformer-chatbots install pip install -U disformers example see examp

SpaceDEVofficial 1 Nov 09, 2021
Pancakeswap Sniper Bot GUI Uniswap Matic 2022 (WINDOWS LINUX MAC) AUTO BUY TOKEN ON LAUNCH AFTER ADD LIQUIDITY

Pancakeswap Sniper Bot GUI Uniswap Matic 2022 (WINDOWS LINUX MAC) ⭐️ AUTO BUY TOKEN ON LAUNCH AFTER ADD LIQUIDITY ⭐️ ⭐️ First GUI SNIPER BOT for WINDO

Crypto Trader 1 Jan 05, 2022
Simple Telegram Bot to extract various types of archives from a telegram file or a direct link

Unzipper Bot A Telegram Bot to Extract Various Types Of Archives Features Extract various types of archives like rar, zip, tar, 7z, tar.xz etc. Passwo

I'm Not A Bot #Left_TG 93 Dec 27, 2022
Balsam Python client API & SDK

balsam No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) This Python package is automatically

Darren Govoni 1 Oct 22, 2021
Python library to interact with a Z-Wave JS server.

zwave-js-server-python Python library for communicating with zwave-js-server. Goal for this library is to replicate the structure and the events of Z-

Home Assistant Libraries 54 Dec 18, 2022
Source code for Profile REST API

PROJECT PROFILE REST API Creating local development server: We will create a local development server that can run and test our API as we build it. We

1 Mar 29, 2022
qualysclient - a python SDK for interacting with the Qualys API

qualysclient - a python SDK for interacting with the Qualys API

5 Oct 28, 2022
A solution designed to extract, transform and load Chicago crime data from an RDS instance to other services in AWS.

This project is intended to implement a solution designed to extract, transform and load Chicago crime data from an RDS instance to other services in AWS.

Yesaswi Avula 1 Feb 04, 2022
Optimus Prime - A modular Telegram group management and drive clone bot running on Python with sqlalchemy database

Optimus Prime Bot . 🤖 A modular Telegram group management and drive clone bot r

9 Jun 01, 2022