Skip to content

Commit

Permalink
Prepare for stable release
Browse files Browse the repository at this point in the history
Updated the example code in the readme file.

Added installation instructions in the readme file.

Fixed a few last minute bugs in the source code.
  • Loading branch information
viral32111 committed Mar 19, 2021
1 parent 7813116 commit ab0394f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,66 @@

This is a wrapper around the [Discord Slash Commands API](https://discord.com/developers/docs/interactions/slash-commands), to be used with [discord.py](https://github.com/Rapptz/discord.py).

## Installation

The best way to install this is by running `pip install git+https://github.com/viral32111/slashcommands` (you must use the GitHub link because this package is not published on PyPI).

Alternatively, you can manually install this by [downloading the latest release](https://github.com/viral32111/slashcommands/releases/latest) (or cloning this repository) then running `pip install .` while in the directory.

## Example

```python
# Import modules
import discord, slashcommands, asyncio

# Create the client
client = discord.Client()
print( "Loading..." )

# Create a test command
@slashcommands.new( "A simple test command to check if everything works." )
async def test( interaction ):
message = await interaction.respond( f"This is a test, { interaction.user.username }!" )
await message.followup( "Do you like my reply @everyone?", mentions = discord.AllowedMentions.none() )

@slashcommands.new( "Another test command for deferred responses." )
# Create a sleep command
@slashcommands.new( "Another test command for deferred responses.", options = [ slashcommands.option(
type = slashcommands.option.type.number,
name = "time",
description = "How long to sleep for.",
required = True
) ] )
async def sleep( interaction ):
message = await interaction.think()
await asyncio.sleep( 5 )
await asyncio.sleep( int( interaction.arguments[ "time" ] ) )
await message.edit( "Finished!" )

# Create a warn command
@slashcommands.new( "Warn a member with a provided reason.", options = [ slashcommands.option(
type = slashcommands.option.type.user,
name = "member",
description = "The member to warn.",
required = True
), slashcommands.option(
type = slashcommands.option.type.string,
name = "reason",
description = "Why you are warning this member.",
required = True
) ] )
async def warn( interaction ):
await interaction.respond( f"You warned <@{ interaction.arguments[ 'member' ] }> for { interaction.arguments[ 'reason' ] }.", hidden = True )

# Runs when we're ready
@client.event
async def on_ready():
print( "Ready!" )

# Runs when we receive a gateway event
@client.event
async def on_socket_response( payload ):
await slashcommands.run( payload, client )

# Start the client
client.run( "BOT-TOKEN-HERE" )
```

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
description = "A wrapper for the Discord Slash Commands API, to be used with discord.py.",
keywords = "discord slashcommands api wrapper library module development viral32111",

version = "0.2.0",
version = "1.0.0",
license = "AGPL-3.0-only",
url = "https://github.com/viral32111/slashcommands",

Expand All @@ -17,7 +17,7 @@
install_requires = [ "discord.py", "requests", "deepdiff" ],

classifiers = [
"Development Status :: 3 - Alpha",
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Topic :: Internet",
"Topic :: Software Development :: Libraries :: Python Modules",
Expand Down
13 changes: 7 additions & 6 deletions slashcommands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ def __init__( self, payload, client ):
if self.__data:
self.data = interaction.data( self.__data )

for option in self.data.options:
self.arguments = { option.name: option.value for option in self.data.options }
if self.data.options:
for option in self.data.options:
self.arguments = { option.name: option.value for option in self.data.options }

if self.__member:
self.member = member( self.__member )
Expand Down Expand Up @@ -237,7 +238,7 @@ class original:
def __init__( self, token ):
self.__interactionToken = token

async def edit( self, content, **optional ):
async def edit( self, *arguments, **optional ):
discordEmbeds = optional.get( "embeds", None )
jsonDiscordEmbeds = [ embed.to_dict() for embed in discordEmbeds ] if discordEmbeds else None

Expand All @@ -250,7 +251,7 @@ async def edit( self, content, **optional ):
jsonAllowedMentions = None

await _request( "webhooks/" + str( _applicationID ) + "/" + self.__interactionToken + "/messages/@original", method = "PATCH", data = {
"content": content,
"content": arguments[ 0 ] if len( arguments ) > 0 else None,
"embeds": jsonDiscordEmbeds,
"allowed_mentions": jsonAllowedMentions
} )
Expand Down Expand Up @@ -284,7 +285,7 @@ def __init__( self, token, id ):
self.__interactionToken = token
self.__messageID = id

async def edit( self, content, **optional ):
async def edit( self, *arguments, **optional ):
discordEmbeds = optional.get( "embeds", None )
jsonDiscordEmbeds = [ embed.to_dict() for embed in discordEmbeds ] if discordEmbeds else None

Expand All @@ -297,7 +298,7 @@ async def edit( self, content, **optional ):
jsonAllowedMentions = None

await _request( "webhooks/" + str( _applicationID ) + "/" + self.__interactionToken + "/messages/" + str( self.__messageID ), method = "PATCH", data = {
"content": content,
"content": arguments[ 0 ] if len( arguments ) > 0 else None,
"embeds": jsonDiscordEmbeds,
"allowed_mentions": jsonAllowedMentions
} )
Expand Down

0 comments on commit ab0394f

Please sign in to comment.