Browse Source

python bot.

tobby48 3 years ago
parent
commit
d56c07a8c8

+ 137
- 0
src/main/python/kr/co/swh/lecture/opensource/project/discord/vote-language.py View File

@@ -0,0 +1,137 @@
1
+# https://blog.yonghyeon.com/54
2
+
3
+import discord
4
+from discord.http import Route
5
+
6
+bot = discord.Client()
7
+voted_data = {}
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": "Kotlin",
24
+                "style": 2,
25
+                "custom_id": "kotlin",
26
+                "emoji": {
27
+                    "id": "847876848662216714",
28
+                    "name": "kotlin"
29
+                }
30
+            }, {
31
+                "type": 2,
32
+                "label": "C언어",
33
+                "style": 2,
34
+                "custom_id": "c"
35
+            }, {
36
+                "type": 2,
37
+                "label": "C++",
38
+                "style": 2,
39
+                "custom_id": "cpp",
40
+                "emoji": {
41
+                    "id": "847876987778629722",
42
+                    "name": "cpp"
43
+                }
44
+            }, {
45
+                "type": 2,
46
+                "label": "Java",
47
+                "style": 2,
48
+                "custom_id": "java",
49
+                "emoji": {
50
+                    "id": "847876915619954708",
51
+                    "name": "java"
52
+                }
53
+            }
54
+        ]
55
+
56
+    }
57
+]
58
+http = bot.http
59
+
60
+
61
+@bot.event
62
+async def on_ready():
63
+    print("On Ready")
64
+
65
+
66
+@bot.event
67
+async def on_message(msg: discord.Message):
68
+    if msg.content == "!프로그래밍":
69
+        embed = discord.Embed(
70
+            title="최고의 프로그래밍 언어",
71
+            description="""<:python:847876880257908757> Python: 0표
72
+                <:kotlin:847876848662216714> Kotlin: 0표
73
+                C언어: 0표
74
+                <:cpp:847876987778629722> C++: 0표
75
+                <:java:847876915619954708> Java: 0표""",
76
+            colour=0x0080ff
77
+        )
78
+
79
+        component1 = {
80
+            "embed": embed.to_dict(),
81
+                "components": default_components
82
+        }
83
+
84
+        response = await http.request(
85
+            Route('POST', '/channels/{channel_id}/messages', channel_id=msg.channel.id), json=component1
86
+        )
87
+        print(response)
88
+        voted_data[response.get("id")] = {
89
+            "python": 0, "kotlin": 0, "java": 0, "cpp": 0, "c": 0
90
+        }
91
+
92
+
93
+@bot.event
94
+async def on_socket_response(payload: dict):
95
+    print(payload)
96
+    if payload.get("t", "") == "INTERACTION_CREATE":
97
+        d = payload.get("d", {})
98
+        message = d.get("message", {})
99
+
100
+        custom_id = d.get("data", {}).get("custom_id")
101
+
102
+        voted_data[message.get("id", 0)][custom_id] += 1
103
+
104
+        embed = discord.Embed(
105
+            title="최고의 프로그래밍 언어",
106
+            description="""<:python:847876880257908757> Python: {}표
107
+                        <:kotlin:847876848662216714> Kotlin: {}표
108
+                        C언어: {}표
109
+                        <:cpp:847876987778629722> C++: {}표
110
+                        <:java:847876915619954708> Java: {}표""".format(
111
+                voted_data[message.get("id", 0)]['python'], voted_data[message.get("id", 0)]['kotlin'],
112
+                voted_data[message.get("id", 0)]['c'], voted_data[message.get("id", 0)]['cpp'],
113
+                voted_data[message.get("id", 0)]['java']),
114
+            colour=0x0080ff
115
+        )
116
+
117
+        component2 = {
118
+            "embed": embed.to_dict(),
119
+            "components": default_components
120
+        }
121
+
122
+        await http.request(
123
+            Route('PATCH', '/channels/{channel_id}/messages/{message_id}',
124
+                  channel_id=message.get("channel_id"), message_id=message.get('id')), json=component2
125
+        )
126
+
127
+        interaction_id = d.get("id")
128
+        interaction_token = d.get("token")
129
+        await bot.http.request(
130
+            Route("POST", f"/interactions/{interaction_id}/{interaction_token}/callback"),
131
+            json={"type": 4, "data": {
132
+                "content": "당신은 {}를 고르셨군요!".format(custom_id),
133
+                "flags": 64
134
+            }},
135
+        )
136
+
137
+bot.run('NjYzMjgzODYxMTUyNTk1OTkz.XhGROw.Ps3JJIKlEXoE1eah_5OKHwViThY')