Skip to content

Commit a04f630

Browse files
committed
Added dico-interaction support
1 parent 4b4081c commit a04f630

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
22
dico
3+
dico_interaction
34
test
45
__pycache__

dico_command/addon.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import typing
22
from .command import Command
3+
try:
4+
from dico_interaction import InteractionCommand, ComponentCallback
5+
except ImportError:
6+
InteractionCommand = None
7+
ComponentCallback = None
38

49
if typing.TYPE_CHECKING:
510
from .bot import Bot
@@ -26,6 +31,9 @@ def wrap(func):
2631
return wrap
2732

2833

34+
on_ = on
35+
36+
2937
class Addon:
3038
name: str
3139

@@ -37,6 +45,8 @@ def __init__(self, bot: "Bot"):
3745
resp = [getattr(self, x) for x in dir(self)]
3846
self.commands: typing.List[Command] = [x for x in resp if isinstance(x, Command)]
3947
self.listeners: typing.List[Listener] = [x for x in resp if isinstance(x, Listener)]
48+
self.interactions: typing.List["InteractionCommand"] = [x for x in resp if InteractionCommand is not None and isinstance(x, InteractionCommand)]
49+
self.callbacks: typing.List["ComponentCallback"] = [x for x in resp if ComponentCallback is not None and isinstance(x, ComponentCallback)]
4050

4151
def __str__(self):
4252
return self.name

dico_command/bot.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,18 @@ def load_addons(self, *addons: typing.Type["Addon"]):
9999
for e in loaded.listeners:
100100
e.register_addon(loaded)
101101
self.on_(e.event, e.func)
102+
if hasattr(self, "interaction"):
103+
for t in loaded.interactions:
104+
t.register_self_or_cls(loaded)
105+
self.interaction.add_command(t)
106+
for cc in loaded.callbacks:
107+
cc.register_self_or_cls(loaded)
108+
self.interaction.add_callback(cc)
102109

103110
def unload_addons(self, *addons: typing.Union[str, typing.Type["Addon"]]):
104111
for x in addons:
112+
tgt = x if isinstance(x, str) else x.name
105113
for i, n in enumerate(self.addon_names):
106-
tgt = x if isinstance(x, str) else x.name
107114
if n == tgt:
108115
del self.addon_names[i]
109116
addon = self.addons.pop(i)
@@ -113,6 +120,11 @@ def unload_addons(self, *addons: typing.Union[str, typing.Type["Addon"]]):
113120
event_name = e.event.upper().lstrip("ON_")
114121
if self.events.get(event_name):
115122
self.events.remove(event_name, e.func)
123+
if hasattr(self, "interaction"):
124+
for t in addon.interactions:
125+
self.interaction.remove_command(t)
126+
for cc in addon.callbacks:
127+
self.interaction.remove_callback(cc)
116128

117129
def load_module(self, import_path: str):
118130
try:
@@ -129,7 +141,6 @@ def load_module(self, import_path: str):
129141
def unload_module(self, import_path: str):
130142
try:
131143
module = importlib.import_module(import_path)
132-
importlib.reload(module)
133144
if module.__name__ in self.modules:
134145
if hasattr(module, "unload"):
135146
module.unload(self)

0 commit comments

Comments
 (0)