Skip to main content

library for interacting with discord slash commands via an independently hosted server. Powered by FastAPI

Project description

dispike


codecov Test Dispike PyPi Link


an extremely-extremely early WIP library for easily creating REST-based webhook bots for discord using the new Slash Commands feature.

Powered by FastAPI.

Example (a simple bot)

# Incoming models use this.
from dispike.models.incoming import IncomingDiscordInteraction # For Type Hinting

# Main Dispike Object
from dispike import Dispike

# For creating commands.
from dispike.register.models import DiscordCommand, CommandOption, CommandChoice, CommandTypes

# For responding to commands.
from dispike.response import DiscordResponse


bot = Dispike(
  client_public_key="< PublicKey >"
  bot_token="< Bot Token >",
  application_id="< AppID >"
)



# Let's register a command.
# /wave <person>
# Learn more about registering commands in the Discord Docs.

command_to_be_created = DiscordCommand(
    name="wave" # this is the main command name.,
    description="Send a wave to a nice person! 👋 ",
    options=[
        CommandOption(
            name="person" # this is the attribute assigned to the value passed.,
            description="person to target" # this describes the value to pass,
          	required=True,
            type=CommandTypes.USER
        )
    ]
)

# Nice, we created a command. /wave <person to ping>. Let's register it.

bot.register(
	command_to_be_created
)


# We've registered, now it's time to build the bot! 
# Arguments that you pass to your function are 
# the same arguments you defined/registered with discord..
# Don't forget to have an argument for a ctx (context). 

@bot.interaction.on("wave")
async def handle_send_wave(person: int, ctx: IncomingDiscordInteraction):
  logger.info("Recieved a wave!")
  

  # this is what we will be returning. Let's edit it.
  # Discord will pass the user id..
  _response = DiscordResponse()
  _response.content f"👋 Hi @<{person}>."
  
  return _response
  
# Run your bot.
# Powered by uvicorn.

bot.run(port=5000)
    

Embeds are also available (Copied from Discord.py)

from dispike.helper import Embed

_response = DiscordResponse()
_response.embed = Embed() # your embed created.

Getting, Deleting and Editing Commands

from dispike import Dispike

# For creating commands.
from dispike.register.models import DiscordCommand, CommandOption, CommandChoice, CommandTypes


bot = Dispike(
	client_public_key="< PublicKey >"
  bot_token="< Bot Token >",
  application_id="< AppID >"
)

all_commands = bot.get_commands()

for command in all_commands:
  print(f"{command.name} -> {command.id}")
 
target_command = all_commands[0]

command_to_be_created = DiscordCommand(
    name="salute" # this is the main command name.,
    description="Send a salute to a nice person! ",
    options=[
        CommandOption(
            name="person" # this is the attribute assigned to the value passed.,
            description="person to target" # this describes the value to pass,
          	required=True,
            type=CommandTypes.USER
        )
    ]
)

bot.edit_command(target_command.id, command_to_be_created)
bot.delete_command(all_commands[1].id)

When configuring your endpoint on discord settings, be sure to append /interactions to your domain.

Website

Caveats

  • Does not handle registring new commands.
  • Does not handle anything other then string responses. (However you are free to return any valid dict in your handler.)
  • Not on PyPi
  • Does not speak over the discord gateway. You'll need a server to handle requests and responses.
  • Python 3.6+
  • Does not support the following endpoints
  • Handling followup messages.

Development

Help is wanted in mantaining this library. Please try to direct PRs to the dev branch, and use black formatting (if possible).

Test Dispike

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dispike-0.4.5a0.tar.gz (16.4 kB view hashes)

Uploaded Source

Built Distribution

dispike-0.4.5a0-py3-none-any.whl (20.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page