-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhow-to-build-a-discordbot-with-python.html
224 lines (210 loc) · 13.1 KB
/
how-to-build-a-discordbot-with-python.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags always come first -->
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>How to build a Discordbot with Python | SilMon's Blog
</title>
<link rel="canonical" href="https://silmon.github.io/how-to-build-a-discordbot-with-python.html">
<link rel="alternate" type="application/atom+xml" href="https://silmon.github.io/feeds/all.atom.xml" title="Full Atom Feed">
<link rel="alternate" type="application/atom+xml" href="https://silmon.github.io/feeds/{slug}.atom.xml" title="Categories Atom Feed">
<link rel="apple-touch-icon" href="https://silmon.github.io/apple-touch-icon.png" sizes="180x180">
<link rel="icon" type="image/png" href="https://silmon.github.io/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="https://silmon.github.io/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="https://silmon.github.io/manifest.json">
<meta name="theme-color" content="#333333">
<link rel="stylesheet" href="https://silmon.github.io/theme/css/bootstrap.min.css">
<link rel="stylesheet" href="https://silmon.github.io/theme/css/font-awesome.min.css">
<link rel="stylesheet" href="https://silmon.github.io/theme/css/pygments/default.min.css">
<link rel="stylesheet" href="https://silmon.github.io/theme/css/style.css">
<meta name="description" content="Have you ever wondered how to build a bot for discord? Wonder no longer, here's the answer!">
</head>
<body>
<header class="header">
<div class="container">
<div class="row">
<div class="col-sm-4">
<a href="https://silmon.github.io">
<img class="img-fluid" src=https://silmon.github.io/images/logo.png width=200 height=200 alt="SilMon's Blog">
</a>
</div>
<div class="col-sm-8">
<h1 class="title"><a href="https://silmon.github.io">SilMon's Blog</a></h1>
<p class="text-muted">Personal blog about image processing, programming and machine learning</p>
<ul class="list-inline">
<li class="list-inline-item"><a href="http://getpelican.com/" target="_blank">Pelican</a></li>
<li class="list-inline-item"><a href="http://python.org/" target="_blank">Python.org</a></li>
<li class="list-inline-item text-muted">|</li>
<li class="list-inline-item"><a href="https://silmon.github.io/pages/about-this-website.html">About this website</a></li>
<li class=" list-inline-item text-muted">|</li>
<li class="list-inline-item"><a class="fa fa-feed" href="https://silmon.github.io/feeds/all.atom.xml" target="_blank"></a></li>
<li class="list-inline-item"><a class="fa fa-github" href="https://github.com/SilMon" target="_blank"></a></li>
</ul>
</div>
</div>
</div>
</header>
<div class="main">
<div class="container">
<h1>How to build a Discordbot with Python
</h1>
<hr>
<article class="article">
<header>
<ul class="list-inline">
<li class="list-inline-item text-muted" title="2019-06-19T00:00:00+02:00">
<i class="fa fa-clock-o"></i>
Mi 19 Juni 2019
</li>
<li class="list-inline-item">
<i class="fa fa-folder-open-o"></i>
<a href="https://silmon.github.io/category/programming.html">Programming</a>
</li>
<li class="list-inline-item">
<i class="fa fa-files-o"></i>
<a href="https://silmon.github.io/tag/programming.html">#programming</a>, <a href="https://silmon.github.io/tag/discord.html">#discord</a>, <a href="https://silmon.github.io/tag/python.html">#python</a>, <a href="https://silmon.github.io/tag/bot.html">#bot</a>, <a href="https://silmon.github.io/tag/automation.html">#automation</a> </li>
</ul>
</header>
<div class="content">
<h2>1 Setup the bot</h2>
<p>
The first that needs to be done is to create a bot is to log in into the <a href="https://discordapp.com/">
discord page</a>. If you have no account, create one. Then switch to the <a href="https://discordapp.com/developers/applications/">
discord developer page</a> and create a new application: <img src="images/discordbot%231.png" style="display: block" class="img-fluid" vspace="15">
To get you running you simply need to enter a name for your application: <img class="img-fluid" src="images/discordbot%232.PNG" style="display: block" vspace="15">
After
that's done, you need to change to your created application (simply click on it) and then to bot: <img class="img-fluid" src="images/discordbot%233.PNG" style="display: block" vspace="15"> There you need to create a bot for your
application
(Discord will warn you that
this action is irreversible, but that's fine, we want this application to have a bot):<img class="img-fluid" src="images/discordbot%234.PNG" style="display: block" vspace="15"> You can then choose a "profil" name for the
bot, which
will be displayed
int the member list as well as an icon for the bot:<img class="img-fluid" src="images/discordbot%235.PNG" style="display: block" vspace="15"> The
next thing we need is the authorization token, which allows to control the bots actions via the discord API:<img class="img-fluid" src="images/discordbot%236.PNG" style="display: block" vspace="15">
Do not share this with anyone as it will allow them to control your bot!</p>
<h2>2 Make the bot interactive</h2>
<p>
Because i am lazy and this is a block about programming, we will not control the bot directly via Discords REST API,
but rather use a prebuild python library (conveniently also named discord. Simply install it via pip and the fun can
begin:
<img class="img-fluid" src="images/discordbot%237.PNG" style="display: block" vspace="15">
Then fire up your favorite Python IDE (or if you are daring a plain text editor) and begin typing. The first thing
we
need to do is to import the discord library and define the used client, which defines our connection to the discord
servers:
<br>
<pre style="color:#000020;background:#f6f8ff;"><span style="color:#200080; font-weight:bold; ">import</span> discord
client <span style="color:#308080; ">=</span> discord<span style="color:#308080; ">.</span>Client<span style="color:#308080; ">(</span><span style="color:#308080; ">)</span>
</pre>
<!--Created using ToHtml.com on 2019-06-19 19:48:53 UTC -->
After that we will override two very important functions, namely the <code>on_ready()</code> and the <code>on_message(message)</code>
function. <code>on_ready()</code> functions as a constructor/initializer for the bot. Here you can do various things,
such as changing the "played game" of the bot via:
<pre style="color:#000020;background:#f6f8ff;"><span style="color:#308080; ">@</span>client<span style="color:#308080; ">.</span>event
async <span style="color:#200080; font-weight:bold; ">def</span> on_ready<span style="color:#308080; ">(</span><span style="color:#308080; ">)</span><span style="color:#308080; ">:</span>
await client<span style="color:#308080; ">.</span>change_presence<span style="color:#308080; ">(</span>activity<span style="color:#308080; ">=</span>discord<span style="color:#308080; ">.</span>Game<span style="color:#308080; ">(</span>name<span style="color:#308080; ">=</span><span style="color:#1060b6; ">"Awesome bot game"</span><span style="color:#308080; ">)</span><span style="color:#308080; ">)</span>
</pre>
The <code>on_message(message)</code> function serves as an event listener, which will be called every time a server
member
sends a message. This will allow us to interact with server members. Imagine we would want to add a "thumbs up" to every
new message that occurs on the server.
This, of course, would be really annoying behaviour, but since people seem to crave for likes, this may be what you want
😉.
<pre style="color:#000020;background:#f6f8ff;"><span style="color:#308080; ">@</span>client<span style="color:#308080; ">.</span>event
async <span style="color:#200080; font-weight:bold; ">def</span> on_message<span style="color:#308080; ">(</span>message<span style="color:#308080; ">)</span><span style="color:#308080; ">:</span>
<span style="color:#595979; "># Checks if the message was written by the bot. If so, abort</span>
<span style="color:#200080; font-weight:bold; ">if</span> message<span style="color:#308080; ">.</span>author <span style="color:#44aadd; ">==</span> client<span style="color:#308080; ">.</span>user<span style="color:#308080; ">:</span>
<span style="color:#200080; font-weight:bold; ">return</span>
<span style="color:#595979; "># Add a thumbs up to the new message</span>
await message<span style="color:#308080; ">.</span>add_reaction<span style="color:#308080; ">(</span><span style="color:#1060b6; ">"👍"</span><span style="color:#308080; ">)</span>
</pre>
<!--Created using ToHtml.com on 2019-06-19 20:19:05 UTC -->
To make the bot script actually runnable, we need to call the run method with the token of our bot:
<pre style="color:#000020;background:#f6f8ff;">client<span style="color:#308080; ">.</span>run<span style="color:#308080; ">(</span><span style="color:#1060b6; ">"NTkwOTUwODQ0NTM5NjAwOTA5.XQqZKw.MCwgCKXPWtzagw2BMVHsLv1PISU"</span><span style="color:#308080; ">)</span>
</pre>
The only thing left to do is to run the script with Python.
<!--Created using ToHtml.com on 2019-06-19 20:22:52 UTC -->
<table>
<tr>
<th>Function</th>
<th>Called when</th>
<th>Parameters</th>
</tr>
<tr>
<td>on_typing</td>
<td>someone is typing</td>
<td>channel - The channel the typing occurs on<br>user - The typing user<br>when - When the typing started as a naive datetime in UTC<br></td>
</tr>
<tr>
<td>on_message_delete</td>
<td>someone deletes a message</td>
<td>message - The deleted message</td>
</tr>
<tr>
<td>on_message_edit</td>
<td>someone edits a message</td>
<td><span style="font-style:italic">before</span> - The message before editing<br>after - The message after editing<br></td>
</tr>
<tr>
<td>on_reaction_add</td>
<td>a reaction is added to a message</td>
<td><span style="font-style:italic">reaction</span>- The added reaction<br>message - The message the reaction was added to</td>
</tr>
<tr>
<td>on_reaction_remove</td>
<td>a reaction was removed from a message</td>
<td><span style="font-style:italic">reaction</span> - The removed reaction<br><span style="font-style:italic">message</span> - The message the reaction was removed from</td>
</tr>
<tr>
<td>on_member_join</td>
<td>a user joins the server</td>
<td><span style="font-style:italic">member</span> - The member that is joining the server</td>
</tr>
<tr>
<td>on_member_leave</td>
<td>a user leaves the server</td>
<td><span style="font-style:italic">member </span>- The member that is leaving the server</td>
</tr>
<tr>
<td>on_user_update</td>
<td>a user updates his/her avatar/username/discriminator etc.</td>
<td><span style="font-style:italic">before</span> - The status before the edit<br><span style="font-style:italic">after</span> - The status after the edit<br></td>
</tr>
</table>
</p>
<p>
<h2>3 Add the bot to a server</h2>
To actually add the bot to the server, we first need to create an invite link people can use. To do so, we change to the
OATH2 tab:
<img class="img-fluid" src="images/discordbot%238.PNG" style="display: block" vspace="15">
There, we generate the link by selecting <em>bot</em> under the scopes tab and copy the generated link. Because our bot
should be able to add reactions to messages, we need to set the permissions integer to 64.
<img class="img-fluid" src="images/discordbot%239.PNG" style="display: block" vspace="15">
<strong>Note: The permission integer can be calculated under Bot/Bot Permissions</strong><br>
Then the link can be simply used to invite the bot to a server you have admin rights:<br>
<code>https://discordapp.com/api/oauth2/authorize?client_id=590950844539600909&permissions;=64&scope;=bot</code>
<h2>4 Enjoy 😊</h2>
<img class="img-fluid" src="images/discordbot%2310.gif" style="display: block" vspace="15">
</p>
</div>
</article>
</div>
</div>
<footer class="footer">
<div class="container">
<div class="row">
<ul class="col-sm-6 list-inline">
<li class="list-inline-item"><a href="https://silmon.github.io/archives.html">Archives</a></li>
<li class="list-inline-item"><a href="https://silmon.github.io/categories.html">Categories</a></li>
<li class="list-inline-item"><a href="https://silmon.github.io/tags.html">Tags</a></li>
</ul>
<p class="col-sm-6 text-sm-right text-muted">
Generated by <a href="https://github.com/getpelican/pelican" target="_blank">Pelican</a> / <a href="https://github.com/nairobilug/pelican-alchemy" target="_blank">✨</a>
</p>
</div>
</div>
</footer>
</body>
</html>