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!
Bot made by BLACKSTORM[BM] Contact Us - t.me/BLACKSTORM18

ᴡʜᴀᴛ ɪs ᴊᴀʀᴠɪs sᴇᴄᴜʀɪᴛʏ ʙᴏᴛ ᴊᴀʀᴠɪs ʙᴏᴛ ɪs ᴛᴇʟᴇɢʀᴀᴍ ɢʀᴏᴜᴘ ᴍᴀɴᴀɢᴇʀ ʙᴏᴛ ᴡɪᴛʜ ᴍᴀɴʏ ғᴇᴀᴛᴜʀᴇs. ᴛʜɪs ʙᴏᴛ ʜᴇʟᴘs ʏᴏᴜ ᴛᴏ ᴍᴀɴᴀɢᴇ ʏᴏᴜʀ ɢʀᴏᴜᴘs ᴇᴀsɪʟʏ. ᴏʀɪɢɪɴᴀʟʟʏ ᴀ

1 Dec 11, 2021
Image captioning service for healthcare domains in Vietnamese using VLP

Image captioning service for healthcare domains in Vietnamese using VLP This service is a web service that provides image captioning services for heal

CS-UIT AI Club 2 Nov 04, 2021
Python implementation for PetitPotam

PetitPotam Coerce NTLM authentication from Windows hosts Installtion $ pip3 install impacket Usage usage: petitpotam.py [-h] [-debug] [-port [destinat

Oliver Lyak 137 Dec 28, 2022
WBMS automates sending of message to multiple numbers via WhatsApp Web

WhatsApp Bulk Message Sender - WBMS WBMS automates sending of message to multiple numbers via WhatsApp Web. Report Bug · Request Feature Love the proj

Akshay Parakh 3 Jun 26, 2022
A bot to display per user data from the Twitch Leak

twitch-leak-bot-discord A bot to display per user data from the Twitch Leak by username Where's the data? I can't and don't want to supply the .csv's

SSSEAL-C 0 Nov 08, 2022
Ini adalah UserBot Telegram dengan banyak modul keren. Ditulis dengan Python dengan Telethon dan Py-Tgcalls.

Okaeri-Userbot Okaeri-Userbot = userbot telegram modular yang berjalan di python3 dengan database sqlalchemy. Disclaimer Saya tidak bertanggung jawab

Wahyu 1 Dec 15, 2021
A Discord bot to combat phishing links for Steam trades and Discord gifts.

delink-bot A Discord bot to combat phishing links for Steam trades and Discord gifts. Requirement python3 -m pip install -U discord.py python3 -m pip

hugonun 15 Dec 09, 2022
A simple Python wrapper for the archive.is capturing service

archiveis A simple Python wrapper for the archive.is capturing service. Installation pipenv install archiveis Python Usage Import it. import archi

Ben Welsh 157 Dec 28, 2022
Dados Públicos de CNPJ disponibilizados pela Receita Federal do Brasil

Dados Públicos CNPJ Fonte oficial da Receita Federal do Brasil, aqui. Layout dos arquivos, aqui. A Receita Federal do Brasil disponibiliza bases com o

Aphonso Henrique do Amaral Rafael 102 Dec 28, 2022
Rotten Tomatoes API for Python

rottentomatoes.py rottentomatoes offers an easy-to-use Python wrapper to interact with the Rotten Tomatoes API. Before you try and use the API, make s

Zach Williams 88 Dec 15, 2022
Request based Python module(s) to help with the Newegg raffle.

Newegg Shuffle Python module(s) to help you with the Newegg raffle How to use $ git clone https://github.com/Matthew17-21/Newegg-Shuffle $ cd Newegg-S

Matthew 45 Dec 01, 2022
TrollWare 🤡 is the most advanced Discord Malware & RAT

TrollWare 🤡 TrollWare is the most advanced Discord Malware, with a built-in RAT which can be controlled through a Discord Bot Pinned Note: Please giv

doop 74 Jun 09, 2022
Hack WhatsApp Account Easily(Android)

X-Whatsapp Hack WhatsApp Account Easily(Android) HOW TO RUN 👇 (Termux) pkg update && pkg upgrade pkg install python pkg install git git clone https:/

KiLL3R_xRO 72 Dec 21, 2022
Watches your earnings on EarnApp and notifies you when you earned balance or received an payout.

EarnApp-Earning-Monitor Watches your earnings on EarnApp and notifies you when you earned balance or received an payout. Installation Install Python3

Yariya 21 Oct 17, 2022
MAASTA is a wrapper to create an Ansible inventory for MAAS instances that are provisioned by Terraform.

MAASTA is a wrapper to create an Ansible inventory for MAAS instances that are provisioned by Terraform.

Saeid Bostandoust 144 Dec 16, 2022
Mark Sullivan 66 Dec 13, 2022
Framework to make using Bottle less time-consuming and easier

A class for the Bottle API to reduce clutter and difficulty while creating a website.

Tygzy 0 Dec 26, 2022
Fetch the details of assets hosted on AWS.

onaws onaws is a simple tool to check if an IP/hostname belongs to the AWS IP space or not. It uses the AWS IP address ranges data published by AWS to

Amal Murali 80 Dec 29, 2022
Webservice that notifies users on Slack when a change in GitLab concern them.

Gitlab Slack Notifier Webservice that notifies users on Slack when a change in GitLab concern them. Setup Slack Create a Slack app, go to "OAuth & Per

Heuritech 2 Nov 04, 2021
Maestral is an open-source Dropbox client written in Python.

Maestral - A light-weight and open-source Dropbox client for macOS and Linux

2.6k Jan 03, 2023