Skip to content

Commit 1bc941a

Browse files
committed
Experiments.
1 parent eb676e1 commit 1bc941a

File tree

4 files changed

+102
-12
lines changed

4 files changed

+102
-12
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
.cache/
66
_build/
77
build/
8-
dist/
8+
dist/
9+
10+
# dev test
11+
.TOKEN

bot.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from discord_slash import SlashCommand, SlashContext
2+
from discord_slash.model import ContextMenuType
3+
from discord import Intents
4+
from discord.ext.commands import Bot
5+
6+
bot = Bot(
7+
command_prefix="/",
8+
help_command=None,
9+
intents=Intents.default()
10+
)
11+
slash = SlashCommand(
12+
bot,
13+
sync_commands=False
14+
)
15+
16+
@bot.event
17+
async def on_ready():
18+
print("we're live!")
19+
20+
@slash.slash(name="testcmd", guild_ids=[852402668294766612])
21+
async def testcmd(ctx: SlashContext):
22+
print(ctx)
23+
await ctx.send("test!")
24+
25+
@slash.context_menu(ContextMenuType.MESSAGE, name="testname", guild_ids=[852402668294766612])
26+
async def testname(ctx: SlashContext):
27+
print(1 + 1)
28+
print(ctx)
29+
print(ctx.message_menus)
30+
await ctx.send("test!")
31+
32+
bot.run(open(".TOKEN", "r").read(), reconnect=True, bot=True)

discord_slash/client.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,15 @@ def add_context_menu(self, cmd, name: str, _type: int, guild_ids: list = None):
659659
raise error.IncorrectGuildIDType(
660660
f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name}' will be deactivated and broken until fixed."
661661
)
662+
663+
if name in self.commands["context"]:
664+
tgt = self.commands["context"][name]
665+
if not tgt.has_subcommands:
666+
raise error.DuplicateCommand(name)
667+
has_subcommands = tgt.has_subcommands
668+
for x in tgt.allowed_guild_ids:
669+
if x not in guild_ids:
670+
guild_ids.append(x)
662671

663672
_cmd = {
664673
"default_permission": None,
@@ -866,11 +875,8 @@ async def _pick(ctx, choice1, choice2): # Command with 1 or more args.
866875

867876
def wrapper(cmd):
868877
decorator_permissions = getattr(cmd, "__permissions__", None)
869-
decorator_context = getattr(cmd, "__context_menu__", None)
870878
if decorator_permissions:
871879
permissions.update(decorator_permissions)
872-
if decorator_context:
873-
context.update(decorator_context)
874880

875881
obj = self.add_slash_command(
876882
cmd,
@@ -1439,6 +1445,51 @@ async def _on_slash(self, to_use):
14391445

14401446
await self.invoke_command(selected_cmd, ctx, args)
14411447

1448+
async def _on_context_menu(self, to_use):
1449+
if to_use["data"]["name"] in self.commands["context"]:
1450+
ctx = context.SlashContext(self.req, to_use, self._discord, self.logger)
1451+
cmd_name = to_use["data"]["name"]
1452+
1453+
if cmd_name not in self.commands["context"] and cmd_name in self.subcommands:
1454+
return await self.handle_subcommand(ctx, to_use)
1455+
1456+
selected_cmd = self.commands["context"][cmd_name]
1457+
1458+
if (
1459+
selected_cmd.allowed_guild_ids
1460+
and ctx.guild_id not in selected_cmd.allowed_guild_ids
1461+
):
1462+
return
1463+
1464+
if selected_cmd.has_subcommands and not selected_cmd.func:
1465+
return await self.handle_subcommand(ctx, to_use)
1466+
1467+
if "options" in to_use["data"]:
1468+
for x in to_use["data"]["options"]:
1469+
if "value" not in x:
1470+
return await self.handle_subcommand(ctx, to_use)
1471+
1472+
# This is to temporarily fix Issue #97, that on Android device
1473+
# does not give option type from API.
1474+
temporary_auto_convert = {}
1475+
for x in selected_cmd.options:
1476+
temporary_auto_convert[x["name"].lower()] = x["type"]
1477+
1478+
args = (
1479+
await self.process_options(
1480+
ctx.guild,
1481+
to_use["data"]["options"],
1482+
selected_cmd.connector,
1483+
temporary_auto_convert,
1484+
)
1485+
if "options" in to_use["data"]
1486+
else {}
1487+
)
1488+
1489+
self._discord.dispatch("context_menu", ctx)
1490+
1491+
await self.invoke_command(selected_cmd, ctx, args)
1492+
14421493
async def handle_subcommand(self, ctx: context.SlashContext, data: dict):
14431494
"""
14441495
Coroutine for handling subcommand.

discord_slash/context.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(
5252
self.data = _json["data"]
5353
self._message_menu_id = self.data["resolved"]["messages"] if "resolved" in self.data.keys() else None # Should be set later.
5454
self._author_menus_id = self.data["resolved"]["members"] if "resolved" in self.data.keys() else None
55-
self.interaction_id = self.data["id"] if "id" in self.data.keys() else _json["id"]
55+
self.interaction_id = self.data["id"] if "resolved" in self.data.keys() else _json["id"]
5656
self._http = _http
5757
self.bot = _discord
5858
self._logger = logger
@@ -78,13 +78,17 @@ def __init__(
7878
self.message_menus = None
7979
try:
8080
if self._message_menu_id:
81-
self.message_menus = model.SlashMessage(
82-
state=self.bot._connection,
83-
channel=_discord.get_channel(self.channel_id),
84-
data=_json["data"]["resolved"]["messages"][self._message_menu_id],
85-
_http=_http,
86-
interaction_token=self._token,
87-
)
81+
self.message_menus = []
82+
for message in self._message_menu_id:
83+
self.message_menus.append(
84+
model.SlashMessage(
85+
state=self.bot._connection,
86+
channel=_discord.get_channel(self.channel_id),
87+
data=self._message_menu_id[message],
88+
_http=_http,
89+
interaction_token=self._token,
90+
)
91+
)
8892
else:
8993
raise KeyError
9094
except KeyError as err:

0 commit comments

Comments
 (0)