tobby48 3 years ago
parent
commit
028b1ee3e4

+ 76
- 0
src/main/python/kr/co/swh/lecture/opensource/project/discord/callback.py View File

@@ -0,0 +1,76 @@
1
+# https://blog.yonghyeon.com/54
2
+
3
+import discord
4
+from discord.http import Route
5
+
6
+bot = discord.Client()
7
+
8
+default_components = [
9
+    {
10
+        "type": 1,
11
+        "components": [
12
+            {
13
+                "type": 2,
14
+                "label": "Python",
15
+                "style": 2,
16
+                "custom_id": "python",
17
+                "emoji": {
18
+                    "id": "847876880257908757",
19
+                    "name": "python"
20
+                }
21
+            }, {
22
+                "type": 2,
23
+                "label": "Java",
24
+                "style": 2,
25
+                "custom_id": "java",
26
+                "emoji": {
27
+                    "id": "847876915619954708",
28
+                    "name": "java"
29
+                }
30
+            }
31
+        ]
32
+
33
+    }
34
+]
35
+http = bot.http
36
+
37
+
38
+@bot.event
39
+async def on_ready():
40
+    print("On Ready")
41
+
42
+
43
+@bot.event
44
+async def on_message(msg: discord.Message):
45
+    if msg.content == "!프로그래밍":
46
+        embed = discord.Embed(
47
+            title="최고의 프로그래밍 언어",
48
+            description="""Python: 0표
49
+                Java: 0표""",
50
+            colour=0x0080ff
51
+        )
52
+
53
+        response = await http.request(
54
+            Route('POST', '/channels/{channel_id}/messages', channel_id=msg.channel.id), 
55
+                json={"embed": embed.to_dict(), "components": default_components}
56
+        )
57
+        print(response)
58
+
59
+@bot.event
60
+async def on_socket_response(payload: dict):
61
+    print(payload)
62
+    if payload.get("t", "") == "INTERACTION_CREATE":
63
+        d = payload.get("d", {})
64
+        custom_id = d.get("data", {}).get("custom_id")
65
+
66
+        interaction_id = d.get("id")
67
+        interaction_token = d.get("token")
68
+        await bot.http.request(
69
+            Route("POST", f"/interactions/{interaction_id}/{interaction_token}/callback"),
70
+            json={"type": 4, "data": {
71
+                "content": "당신은 {}를 고르셨군요!".format(custom_id),
72
+                "flags": 64
73
+            }},
74
+        )
75
+
76
+bot.run('NjYzMjgzODYxMTUyNTk1OTkz.XhGROw.Ps3JJIKlEXoE1eah_5OKHwViThY')