Skip to content

Commit f56c559

Browse files
author
pythonaian23
committed
basic functionality completed
1 parent b57dc9e commit f56c559

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

dish/bot.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import shlex
2+
import subprocess
13
import typing
24
import discord
35
import toml
@@ -16,29 +18,37 @@ async def _default_handler(_):
1618

1719
class Bot(discord.Client):
1820
def __init__(self, config: ConfigFile, **kwargs: typing.Any):
19-
super().__init__(**kwargs)
21+
super().__init__(
22+
intents=discord.Intents(messages=True, message_content=True), **kwargs
23+
)
2024
self.config: ConfigFile = config
21-
self.dishes: typing.List[DishFile]
25+
self.dishes: typing.Dict[str, DishFile]
2226
self._get_dishes()
23-
print(self.dishes)
2427

2528
def _get_dishes(self):
26-
self.dishes = []
29+
self.dishes: typing.Dict[str, DishFile] = {}
2730
for dish in self.config.get("dishes", []):
2831
if isinstance(dish, str):
29-
self.dishes.append(toml.load(dish, _dict=DishFile))
30-
else:
31-
self.dishes.append(dish)
32+
dish = toml.load(dish, _dict=DishFile)
33+
self.dishes[dish["command"]] = dish
34+
for alias in dish.get("aliases", []):
35+
self.dishes[alias] = dish
3236

3337
async def on_ready(self):
3438
await self.config.get("postinit", _default_postinit)(self)
3539

3640
async def on_message(self, message: discord.Message):
37-
if await self.config.get("handler", lambda _: False)(message):
41+
if await self.config.get("handler", _default_handler)(message):
3842
return
3943
if message.author == self.user:
4044
return
4145

46+
argv = shlex.split(message.content)
47+
if argv[0] in self.dishes.keys():
48+
argv = shlex.split(self.dishes[argv[0]]["run"]) + argv[1:]
49+
proc = subprocess.run(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
50+
await message.reply(proc.stdout.decode("utf-8"))
51+
4252
def run(self):
4353
self.config.get("preinit", lambda _: None)(self)
4454
super().run(self.config["token"])

dish/dishfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ class DishFile(typing.TypedDict):
55
command: str
66
aliases: typing.List[str]
77
description: str
8-
cmd: str
8+
run: str

0 commit comments

Comments
 (0)