Skip to content

Commit

Permalink
feat: Add slash commands and other interactions
Browse files Browse the repository at this point in the history
- Basic tests for slash and other interactions, including buttons
- Create slash command for >p command
  • Loading branch information
Seniru committed Jul 16, 2021
1 parent bab32bb commit f511384
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 3 deletions.
139 changes: 138 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,141 @@ discordia.log
gateway.json
init.sh
.env
*.pyc

__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "discordslashcommands"]
path = discordslashcommands
url = https://github.com/mactul/discordslashcommands
1 change: 1 addition & 0 deletions discordslashcommands
Submodule discordslashcommands added at 94412b
34 changes: 34 additions & 0 deletions src/bots/Discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from datetime import datetime, timedelta

import discord
import discordslashcommands as slash
import requests
import utils
from data import data
from discord_components import Button, DiscordComponents, Select, SelectOption

from bots.cmd_handler import commands

Expand Down Expand Up @@ -43,6 +45,9 @@ async def on_ready(self):
self.mod_data = await self.data_channel.fetch_message(data["data"]["mod"])
self.mod_data = json.loads(self.mod_data.content[7:-3])

self.slash = slash.Manager(self)
DiscordComponents(self)

await self.start_period_tasks()

async def on_message(self, message):
Expand Down Expand Up @@ -114,6 +119,35 @@ async def on_message(self, message):
"color": 0x2987ba
}))

async def on_interaction(self, member, interaction):
await interaction.end(content = "** **")
cmd_name = interaction.command.name
if cmd_name in commands and commands[cmd_name]["discord"]:
cmd = commands[cmd_name]
interaction.author = self.main_guild.get_member(interaction._member_data["user"]["id"])
interaction.member = interaction.author
if cmd["allowed_roles"]:
for role in cmd["allowed_roles"]:
if self.main_guild.get_role(role) in interaction.member.roles:
break
else:
return await interaction.channel.send(embed = discord.Embed.from_dict({
"title": ":x: Missing permissions",
"description": "You need 1 of the following roles to use this command: \n{}".format(
", ".join(list(map(lambda role: "<@&{}>".format(role), cmd["allowed_roles"])))
),
"color": 0xcc0000
}))
interaction.reply = self.main_guild.get_channel(interaction.channel.id).send
interaction.send = self.main_guild.get_channel(interaction.channel.id).send
interaction.options = list(map(lambda o: o.value, interaction.command.options))
interaction.mentions = list(
map(
lambda m: self.main_guild.get_member(int(re.match(r".*?(\d+).*", m)[1])),
filter(lambda o: re.match(r"^<@!?(\d+)>$", o), interaction.options)
))
await cmd["f"](interaction.options, interaction, self)

async def on_member_join(self, member):
error = False
try:
Expand Down
13 changes: 11 additions & 2 deletions src/bots/cmd_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ async def stats(args, msg, client):
> :person_running: **Rounds: ** `{}`
> <:cheese:691158951563362314> **Cheese:** `{}`
> <:p7:836550194380275742> **Firsts:** `{}`
> <:bootcamp:836550195683917834> **Bootcamp:** `{}`
> <:bootcamp:836550195683917834> **Bootcamp:** `{}`
>
> <:shaman:836550192387850251> **Gathered cheese/Normal/Hard/Divine: [** `{}`/`{}`/`{}`/`{}` **]**
""".format(
Expand All @@ -345,5 +345,14 @@ async def stats(args, msg, client):
res["stats"]["shaman"]["saves_divine"]
))

@command(discord=True)
async def test(args, msg, client):
from discord_components import DiscordComponents, Button, Select, SelectOption

await msg.reply(content="Hello", components=[Button(label="hello")])
interaction = await client.wait_for("button_click")
print(interaction)
#await interaction.
await interaction.respond(content="ur mom")



2 changes: 2 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import sys
import asyncio

from bots.Transformice import Transformice
sys.path.append("discordslashcommands")
from bots.Discord import Discord

loop = asyncio.get_event_loop()
Expand Down

0 comments on commit f511384

Please sign in to comment.