Skip to content

Commit 90a8ee4

Browse files
committed
Initial Commit
0 parents  commit 90a8ee4

File tree

7 files changed

+193
-0
lines changed

7 files changed

+193
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
*/__pycache__
3+
test.py

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 eunwoo1104
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# discord-py-slash-command
2+
그냥 심심해서 만들어봤어요
3+
4+
## 예제
5+
```py
6+
import discord
7+
from discord.ext import commands
8+
from discord_slash import SlashCommand
9+
from discord_slash.model import SlashContext
10+
11+
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
12+
slash = SlashCommand(bot)
13+
14+
15+
@slash.slash(name="test")
16+
async def _test(ctx: SlashContext):
17+
embed = discord.Embed(title="임베드")
18+
await ctx.send(text="테스트", embeds=[embed])
19+
20+
21+
bot.run("디스코드 토큰")
22+
```
23+
24+
## 설치
25+
그냥 알아서 클론해서 `discord_slash` 파일 갖다 쓰세요
26+
~~어차피 조금만 있으면 디코파이가 지원하겠죠~~

discord_slash/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
MIT License
3+
4+
Copyright (c) 2020 eunwoo1104
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
"""
24+
25+
from .client import SlashCommand
26+
27+
__version__ = "0.1.0"

discord_slash/client.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import typing
2+
import discord
3+
from discord.ext import commands
4+
from . import http
5+
from . import model
6+
7+
8+
class SlashCommand:
9+
def __init__(self,
10+
client: typing.Union[discord.Client,
11+
commands.Bot]
12+
):
13+
if isinstance(client, discord.Client) and not isinstance(client, commands.Bot):
14+
raise Exception("Currently only ext.Bot is supported.")
15+
self._discord = client
16+
self.commands = {}
17+
self.http = http.SlashCommandRequest()
18+
self._discord.add_listener(self.on_socket_response)
19+
20+
def slash(self, name=None):
21+
def wrapper(cmd):
22+
self.commands[cmd.__name__ if not name else name] = cmd
23+
return wrapper
24+
25+
async def on_socket_response(self, msg):
26+
if not msg["t"] == "INTERACTION_CREATE":
27+
return
28+
to_use = msg["d"]
29+
if to_use["data"]["name"] in self.commands.keys():
30+
ctx = model.SlashContext(self.http, to_use, self._discord)
31+
await self.commands[to_use["data"]["name"]](ctx)

discord_slash/http.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import aiohttp
2+
3+
4+
class SlashCommandRequest:
5+
def __init__(self):
6+
pass
7+
8+
async def post(self, _resp, _id, token):
9+
req_url = f"https://discord.com/api/v8/interactions/{_id}/{token}/callback"
10+
async with aiohttp.ClientSession() as session:
11+
async with session.post(req_url, json=_resp) as resp:
12+
if not 200 <= resp.status < 300:
13+
raise Exception(f"Request failed with resp: {resp.status} | {await resp.text()}")
14+
return True

discord_slash/model.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import typing
2+
import discord
3+
from discord.ext import commands
4+
from . import http
5+
6+
7+
class SlashContext:
8+
def __init__(self,
9+
_http: http.SlashCommandRequest,
10+
_json: dict,
11+
_discord: commands.Bot):
12+
self.__token = _json["token"]
13+
self.name = _json["data"]["name"]
14+
self.__id = _json["id"]
15+
self.id = _json["data"]["id"]
16+
self._http = _http
17+
self.guild: discord.Guild = _discord.get_guild(int(_json["guild_id"]))
18+
self.author: discord.Member = self.guild.get_member(int(_json["member"]["user"]["id"]))
19+
20+
async def send(self,
21+
send_type: int = 4,
22+
text: str = "",
23+
embeds: typing.List[discord.Embed] = None,
24+
tts: bool = False):
25+
base = {
26+
"type": send_type,
27+
"data": {
28+
"tts": tts,
29+
"content": text,
30+
"embeds": [x.to_dict() for x in embeds] if embeds else [],
31+
"allowed_mentions": []
32+
}
33+
}
34+
await self._http.post(base, self.__id, self.__token)
35+
36+
37+
"""
38+
{
39+
"type": 2,
40+
"token": "A_UNIQUE_TOKEN",
41+
"member": {
42+
"user": {
43+
"id": 53908232506183680,
44+
"username": "Mason",
45+
"avatar": "a_d5efa99b3eeaa7dd43acca82f5692432",
46+
"discriminator": "1337",
47+
"public_flags": 131141
48+
},
49+
"roles": [539082325061836999],
50+
"premium_since": null,
51+
"permissions": "2147483647",
52+
"pending": false,
53+
"nick": null,
54+
"mute": false,
55+
"joined_at": "2017-03-13T19:19:14.040000+00:00",
56+
"is_pending": false,
57+
"deaf": false
58+
},
59+
"id": "786008729715212338",
60+
"guild_id": "290926798626357999",
61+
"data": {
62+
"options": [{
63+
"name": "cardname",
64+
"value": "The Gitrog Monster"
65+
}],
66+
"name": "cardsearch",
67+
"id": "771825006014889984"
68+
},
69+
"channel_id": "645027906669510667"
70+
}
71+
"""

0 commit comments

Comments
 (0)