feat(bot): add support for Discord message context commands (#513)
This commit is contained in:
4
scripts/app-commands/.gitignore
vendored
Normal file
4
scripts/app-commands/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/commands.json
|
||||
|
||||
*.pyc
|
||||
__pycache__/
|
23
scripts/app-commands/README.md
Normal file
23
scripts/app-commands/README.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# PluralKit "application command" helpers
|
||||
|
||||
## Adding new commands
|
||||
|
||||
Edit the `COMMAND_LIST` global in `commands.py`, making sure that any
|
||||
command names that are specified in that file match up with the
|
||||
command names used in the bot code (which will generally be in the list
|
||||
in `PluralKit.Bot/ApplicationCommandMeta/ApplicationCommandList.cs`).
|
||||
|
||||
TODO: add helpers for slash commands to this
|
||||
|
||||
## Dumping application command JSON
|
||||
|
||||
Run `python3 commands.py` to get a JSON dump of the available application
|
||||
commands - this is in a format that can be sent to Discord as a `PUT` to
|
||||
`/applications/{clientId}/commands`.
|
||||
|
||||
## Updating Discord's list of application commands
|
||||
|
||||
From the root of the repository (where your `pluralkit.conf` resides),
|
||||
run `python3 ./scripts/app-commands/update.py`. This will **REPLACE**
|
||||
any existing application commands that Discord knows about, with the
|
||||
updated list.
|
10
scripts/app-commands/commands.py
Normal file
10
scripts/app-commands/commands.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from common import *
|
||||
|
||||
COMMAND_LIST = [
|
||||
MessageCommand("\U00002753 Message info"),
|
||||
MessageCommand("\U0000274c Delete message"),
|
||||
MessageCommand("\U0001f514 Ping author"),
|
||||
]
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(__import__('json').dumps(COMMAND_LIST))
|
1
scripts/app-commands/common/__init__.py
Normal file
1
scripts/app-commands/common/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .types import MessageCommand
|
7
scripts/app-commands/common/types.py
Normal file
7
scripts/app-commands/common/types.py
Normal file
@@ -0,0 +1,7 @@
|
||||
class MessageCommand(dict):
|
||||
COMMAND_TYPE = 3
|
||||
|
||||
def __init__(self, name):
|
||||
super().__init__()
|
||||
self["type"] = self.__class__.COMMAND_TYPE
|
||||
self["name"] = name
|
70
scripts/app-commands/update.py
Normal file
70
scripts/app-commands/update.py
Normal file
@@ -0,0 +1,70 @@
|
||||
from common import *
|
||||
from commands import COMMAND_LIST
|
||||
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
|
||||
from pathlib import Path
|
||||
from urllib import request
|
||||
from urllib.error import URLError
|
||||
|
||||
DISCORD_API_BASE = "https://discord.com/api/v10"
|
||||
|
||||
def get_config():
|
||||
data = {}
|
||||
|
||||
# prefer token from environment if present
|
||||
envbase = ["PluralKit", "Bot"]
|
||||
for var in ["Token", "ClientId"]:
|
||||
for sep in [':', '__']:
|
||||
envvar = sep.join(envbase + [var])
|
||||
if envvar in os.environ:
|
||||
data[var] = os.environ[envvar]
|
||||
|
||||
if "Token" in data and "ClientId" in data:
|
||||
return data
|
||||
|
||||
# else fall back to config
|
||||
cfg_path = Path(os.getcwd()) / "pluralkit.conf"
|
||||
if cfg_path.exists():
|
||||
cfg = {}
|
||||
with open(str(cfg_path), 'r') as fh:
|
||||
cfg = json.load(fh)
|
||||
|
||||
if 'PluralKit' in cfg and 'Bot' in cfg['PluralKit']:
|
||||
return cfg['PluralKit']['Bot']
|
||||
|
||||
return None
|
||||
|
||||
def main():
|
||||
config = get_config()
|
||||
if config is None:
|
||||
raise ArgumentError("config was not loaded")
|
||||
if 'Token' not in config or 'ClientId' not in config:
|
||||
raise ArgumentError("config is missing 'Token' or 'ClientId'")
|
||||
|
||||
data = json.dumps(COMMAND_LIST)
|
||||
url = DISCORD_API_BASE + f"/applications/{config['ClientId']}/commands"
|
||||
req = request.Request(url, method='PUT', data=data.encode('utf-8'))
|
||||
req.add_header("Content-Type", "application/json")
|
||||
req.add_header("Authorization", f"Bot {config['Token']}")
|
||||
req.add_header("User-Agent", "PluralKit (app-commands updater; https://pluralkit.me)")
|
||||
|
||||
try:
|
||||
with request.urlopen(req) as resp:
|
||||
if resp.status == 200:
|
||||
print("Update successful!")
|
||||
return 0
|
||||
|
||||
except URLError as resp:
|
||||
print(f"[!!!] Update not successful: status {resp.status}", file=sys.stderr)
|
||||
print(f"[!!!] Response body below:\n", file=sys.stderr)
|
||||
print(resp.read(), file=sys.stderr)
|
||||
sys.stderr.flush()
|
||||
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
Reference in New Issue
Block a user