Skip to content

Commit be55234

Browse files
committed
Hotfix syncing issue for context menus, make slash commands work.
1 parent a952453 commit be55234

File tree

5 files changed

+112
-30
lines changed

5 files changed

+112
-30
lines changed

bot.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import logging
2+
from discord_slash import SlashCommand, SlashContext, MenuContext
3+
from discord_slash.model import ContextMenuType
4+
from discord import Intents
5+
from discord.ext.commands import Bot
6+
7+
bot = Bot(
8+
command_prefix="/",
9+
help_command=None,
10+
intents=Intents.default()
11+
)
12+
slash = SlashCommand(
13+
bot,
14+
sync_commands=True
15+
)
16+
log = logging.Logger(name="errors.log", level=logging.DEBUG)
17+
18+
bot.load_extension("cog")
19+
20+
@bot.event
21+
async def on_ready():
22+
print("we're live!")
23+
24+
@bot.event
25+
async def on_slash_command_error(ctx, ex):
26+
log.debug(ctx)
27+
log.debug(ex)
28+
29+
@slash.slash(name="testcmd", guild_ids=[852402668294766612])
30+
async def testcmd(ctx: SlashContext):
31+
await ctx.send("test2!")
32+
33+
@slash.context_menu(ContextMenuType.USER, name="testuser", guild_ids=[852402668294766612])
34+
async def testuser(ctx: MenuContext):
35+
await ctx.send("test!")
36+
37+
@slash.context_menu(ContextMenuType.MESSAGE, name="Testing Name Space", guild_ids=[852402668294766612])
38+
async def testmsg(ctx: MenuContext):
39+
await ctx.send("test!")
40+
41+
for cmd in slash.commands["context"]:
42+
print(slash.commands["context"][cmd]._type)
43+
44+
bot.run("Mzc5MzQzMzIyNTQ1NzgyNzg0.WgiY_w.uVRHvtT5KmGFuZ3zOiH_Y3MoGfc", bot=True, reconnect=True)

cog.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from discord_slash.context import MenuContext
2+
from discord.ext.commands import Cog
3+
from discord.ext.commands.context import Context
4+
from discord_slash import cog_ext
5+
from discord_slash.model import ContextMenuType
6+
7+
class Cog(Cog):
8+
def __init__(self, bot):
9+
self.bot = bot
10+
11+
@cog_ext.cog_context_menu(ContextMenuType.MESSAGE, name="cog testing", guild_ids=[852402668294766612])
12+
async def cog_testing(self, ctx: MenuContext):
13+
await ctx.send("test!")
14+
15+
def setup(bot):
16+
bot.add_cog(Cog(bot))

discord_slash/client.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ async def to_dict(self):
296296
"options": selected.options or [],
297297
"default_permission": selected.default_permission,
298298
"permissions": {},
299-
"type": selected.type,
299+
"type": selected._type,
300300
}
301301
if y in selected.permissions:
302302
command_dict["permissions"][y] = selected.permissions[y]
@@ -679,7 +679,10 @@ def add_context_menu(self, cmd, name: str, _type: int, guild_ids: list = None):
679679
"has_subcommands": False,
680680
"api_permissions": {},
681681
}
682-
obj = model.BaseCommandObject(name, cmd=_cmd, type=_type)
682+
683+
print("add_context_menu".upper(), ": ", _cmd)
684+
685+
obj = model.BaseCommandObject(name, cmd=_cmd, _type=_type)
683686
self.commands["context"][name] = obj
684687
self.logger.debug(f"Added context command `{name}`")
685688
return obj
@@ -1371,14 +1374,17 @@ async def on_socket_response(self, msg):
13711374
if msg["t"] != "INTERACTION_CREATE":
13721375
return
13731376

1377+
print("on_socket_response".upper(), ": ", msg)
13741378
to_use = msg["d"]
13751379
interaction_type = to_use["type"]
1376-
if interaction_type == 1:
1377-
return await self._on_slash(to_use)
1378-
if interaction_type in (2, 3):
1379-
return await self._on_context_menu(to_use)
1380+
if interaction_type in (2, 3) or msg["s"] == 5:
1381+
await self._on_slash(to_use)
1382+
await self._on_context_menu(to_use)
1383+
if interaction_type >= 4: # what the fuck did Discord make components!?
1384+
return await self._on_component(to_use)
13801385

1381-
raise NotImplementedError
1386+
return
1387+
# raise NotImplementedError
13821388

13831389
async def _on_component(self, to_use):
13841390
ctx = context.ComponentContext(self.req, to_use, self._discord, self.logger)
@@ -1439,11 +1445,11 @@ async def _on_slash(self, to_use):
14391445

14401446
async def _on_context_menu(self, to_use):
14411447
if to_use["data"]["name"] in self.commands["context"]:
1442-
ctx = context.SlashContext(self.req, to_use, self._discord, self.logger)
1448+
ctx = context.MenuContext(self.req, to_use, self._discord, self.logger)
14431449
cmd_name = to_use["data"]["name"]
14441450

14451451
if cmd_name not in self.commands["context"] and cmd_name in self.subcommands:
1446-
return await self.handle_subcommand(ctx, to_use)
1452+
return # menus don't have subcommands you smooth brain
14471453

14481454
selected_cmd = self.commands["context"][cmd_name]
14491455

discord_slash/context.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(
4848
):
4949
self._token = _json["token"]
5050
self.message = None
51+
self.menu_messages = None
5152
self.data = _json["data"]
5253
self.interaction_id = _json["id"]
5354
self._http = _http
@@ -64,12 +65,20 @@ def __init__(
6465
self.channel_id = int(_json["channel_id"])
6566
if self.guild:
6667
self.author = discord.Member(
67-
data=_json["member"], state=self.bot._connection, guild=self.guild
68+
data=_json["member"],
69+
state=self.bot._connection,
70+
guild=self.guild
6871
)
6972
elif self.guild_id:
70-
self.author = discord.Member(data=_json["member"]["user"], state=self.bot._connection)
73+
self.author = discord.User(
74+
data=_json["member"]["user"],
75+
state=self.bot._connection
76+
)
7177
else:
72-
self.author = discord.User(data=_json["user"], state=self.bot._connection)
78+
self.author = discord.User(
79+
data=_json["user"],
80+
state=self.bot._connection
81+
)
7382
self.created_at: datetime.datetime = snowflake_time(int(self.interaction_id))
7483

7584
@property
@@ -669,8 +678,8 @@ def __init__(
669678
logger,
670679
):
671680
super().__init__(_http=_http, _json=_json, _discord=_discord, logger=logger)
672-
self.target_id = super().data["target_id"]
673-
self.context_type = super()._json["type"]
681+
self.target_id = self.data["target_id"]
682+
self.context_type = _json["type"]
674683

675684
try:
676685
self.menu_authors = (
@@ -686,12 +695,17 @@ def __init__(
686695
[user for user in self.menu_authors][0] if self.menu_authors is not None else []
687696
)
688697

689-
if super().guild and self.author:
690-
self.context_author = discord.Member(data=self.author, state=self.bot._connection)
698+
if self.guild and self.context_author:
699+
print(self.context_author)
700+
self.context_author = discord.Member(
701+
data=self.context_author,
702+
state=self.bot._connection,
703+
guild=self.guild
704+
)
691705

692706
try:
693-
if super().menu_messages is not None:
694-
super().menu_messages = model.SlashMessage(
707+
if self.menu_messages is not None:
708+
self.menu_messages = model.SlashMessage(
695709
state=self.bot._connection,
696710
channel=_discord.get_channel(self.channel_id),
697711
data=self.context_message,

discord_slash/model.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,14 @@ class CommandObject(CallbackObject):
309309
:ivar connector: Kwargs connector of the command.
310310
"""
311311

312-
def __init__(self, name, cmd, type=1): # Let's reuse old command formatting.
312+
def __init__(self, name, cmd, _type=1): # Let's reuse old command formatting.
313313
super().__init__(cmd["func"])
314314
self.name = name.lower()
315315
self.description = cmd["description"]
316316
self.allowed_guild_ids = cmd["guild_ids"] or []
317317
self.options = cmd["api_options"] or []
318318
self.connector = cmd["connector"] or {}
319-
self.type = type
319+
self._type = _type
320320

321321

322322
class BaseCommandObject(CommandObject):
@@ -334,8 +334,8 @@ class BaseCommandObject(CommandObject):
334334
:ivar permissions: Permissions to restrict use of this command.
335335
"""
336336

337-
def __init__(self, name, cmd, type=1): # Let's reuse old command formatting.
338-
super().__init__(name, cmd, type)
337+
def __init__(self, name, cmd, _type=1): # Let's reuse old command formatting.
338+
super().__init__(name, cmd, _type)
339339
self.has_subcommands = cmd["has_subcommands"]
340340
self.default_permission = cmd["default_permission"]
341341
self.permissions = cmd["api_permissions"] or {}
@@ -377,8 +377,10 @@ def __init__(self, *args):
377377
# this is a really bad way to add context menu support
378378
# but i cannot be bothered anymore to make it better until
379379
# v4.0 is out for rewrite. sorry!
380-
args[2] = 1 if not args[2] else args[2]
381-
super().__init__(*args)
380+
_arg = 1 if not args[0] else args[0]
381+
values = [arg for arg in args]
382+
values[0] = _arg
383+
super().__init__(*values)
382384
self.cog = None # Manually set this later.
383385

384386

@@ -709,9 +711,9 @@ class ContextMenuType(IntEnum):
709711
USER = 2
710712
MESSAGE = 3
711713

712-
@classmethod
713-
def from_type(cls, t: type):
714-
if isinstance(t, discord.Member) or issubclass(t, discord.abc.User):
715-
return cls.USER
716-
if issubclass(t, discord.abc.Messageable):
717-
return cls.MESSAGE
714+
# @classmethod
715+
# def from_type(cls, t: type):
716+
# if isinstance(t, discord.Member) or issubclass(t, discord.abc.User):
717+
# return cls.USER
718+
# if issubclass(t, discord.abc.Messageable):
719+
# return cls.MESSAGE

0 commit comments

Comments
 (0)