-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowering-bot.html
More file actions
89 lines (82 loc) · 12.5 KB
/
Copy pathpowering-bot.html
File metadata and controls
89 lines (82 loc) · 12.5 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="../favicon.png" />
<link rel="stylesheet" href="../fontawesome.css" />
<meta name="viewport" content="width=device-width" />
<meta http-equiv="content-security-policy" content="">
<link href="../app/immutable/assets/_layout.3a6a7fd4.css" rel="stylesheet">
<link href="../app/immutable/assets/_page.9068b0fb.css" rel="stylesheet">
<link href="../app/immutable/assets/DocList.312ef88c.css" rel="stylesheet">
<link href="../app/immutable/assets/MarkdownContent.09881ec7.css" rel="stylesheet">
<link href="../app/immutable/assets/IndexWrapper.b786c468.css" rel="stylesheet">
<link href="../app/immutable/assets/embeds.1b9cc348.css" rel="stylesheet">
</head>
<body>
<main class="layout wrapper svelte-77ehm7"><nav class="nav container svelte-1e9pbzu"><div class="nav display svelte-1e9pbzu"><a class="nav branding svelte-1e9pbzu" href="/"><svg class="logo container nav svg svelte-1h116gd" xmlns="http://www.w3.org/2000/svg" width="271.115mm" height="271.115mm" viewBox="0 0 1024 1024"><defs><mask id="gnet-mask"><circle id="gnet-mask-base" cx="512" cy="512" r="440" fill="white"></circle><circle id="gnet-dot-mask" cx="192" cy="512" r="192" fill="black"></circle><path id="gnet-guilded" fill="black" d="M 644.27,704.52
C 600.00,688.00 424.00,622.39 424.00,320.00
424.00,320.00 864.00,320.00 864.00,320.00
864.00,320.00 864.00,376.00 857.26,407.01
857.26,407.01 522.00,408.00 522.00,408.00
522.01,424.05 536.00,560.00 644.00,609.84
664.00,608.00 712.00,568.00 724.00,543.00
724.00,543.00 637.00,542.40 637.00,542.40
608.00,520.00 596.08,483.25 591.00,455.00
591.00,455.00 852.00,455.00 852.00,455.00
824.00,584.00 744.00,672.00 645.00,704.79
644.76,704.71 644.52,704.62 644.27,704.52 Z"></path></mask></defs><g id="gnet-main"><circle id="gnet-base" cx="512" cy="512" r="440" mask="url(#gnet-mask)"></circle><circle id="gnet-dot" cx="192" cy="512" r="132"></circle></g></svg></a>
<ul class="nav button-list svelte-1e9pbzu"><li class="nav button-item svelte-1e9pbzu"><a class="nav button svelte-1e9pbzu" title="Docs" href="/docs">Docs</a></li>
<li class="nav button-item svelte-1e9pbzu"><a class="nav button svelte-1e9pbzu" title="API References" href="/references">API</a></li>
</ul></div>
</nav>
<div class="layout container svelte-77ehm7">
<div class="layout document svelte-77ehm7"><aside class="entry-list container svelte-1eetiev"><div class="entry-list search-field"><div class="entry-list search-wrapper svelte-1eetiev"><i class="entry-list search-icon fas fa-search svelte-1eetiev"></i>
<input id="entry-list-search" class="entry-list search svelte-1eetiev" placeholder="Search docs" value=""></div></div>
<div class="entry-list items-wrapper svelte-1eetiev"><ul class="entry-list items svelte-1eetiev"><li class="entry-category container svelte-flts2q"><header class="entry-category header svelte-flts2q"><h5 class="entry-category header-text svelte-flts2q">Getting Started</h5></header>
<ul class="entry-category body svelte-flts2q"><li class="entry-item container svelte-1bnzhps"><a class="entry-item link svelte-1bnzhps" href="/docs/develop-bot"><i class="container fa-solid fa-play entry-item icon"></i>
<span class="entry-item name">Developing bots</span></a>
</li><li class="entry-item container active svelte-1bnzhps"><a class="entry-item link svelte-1bnzhps" href="/docs/powering-bot"><i class="container fa-solid fa-bolt entry-item icon"></i>
<span class="entry-item name">Powering up bot</span></a>
</li><li class="entry-item container svelte-1bnzhps"><a class="entry-item link svelte-1bnzhps" href="/docs/commands"><i class="container fa-solid fa-bolt entry-item icon"></i>
<span class="entry-item name">Commands</span></a>
</li></ul>
</li><li class="entry-category container svelte-flts2q"><header class="entry-category header svelte-flts2q"><h5 class="entry-category header-text svelte-flts2q">How to Use</h5></header>
<ul class="entry-category body svelte-flts2q"><li class="entry-item container svelte-1bnzhps"><a class="entry-item link svelte-1bnzhps" href="/docs/embeds"><i class="container fa-solid fa-play-circle entry-item icon"></i>
<span class="entry-item name">Embeds</span></a>
</li></ul>
</li><li class="entry-category container svelte-flts2q"><header class="entry-category header svelte-flts2q"><h5 class="entry-category header-text svelte-flts2q">Quick Overview</h5></header>
<ul class="entry-category body svelte-flts2q"><li class="entry-item container svelte-1bnzhps"><a class="entry-item link svelte-1bnzhps" href="/docs/supported"><i class="container fa-solid fa-check-double entry-item icon"></i>
<span class="entry-item name">Supported Features</span></a>
</li></ul>
</li><li class="entry-category container svelte-flts2q"><header class="entry-category header svelte-flts2q"><h5 class="entry-category header-text svelte-flts2q">Resources</h5></header>
<ul class="entry-category body svelte-flts2q"><li class="entry-item container svelte-1bnzhps"><a class="entry-item link svelte-1bnzhps" href="/docs/message"><i class="container fa-solid fa-pencil entry-item icon"></i>
<span class="entry-item name">Message builder</span></a>
</li></ul>
</li></ul></div>
</aside>
<div class="content-wrapper container svelte-1y4vd51"><article class="content-wrapper content svelte-1y4vd51"><div class="markdown-content container svelte-lhtcnm"><h1>Getting Started: Powering up the bot</h1>
<p>Well, to no surprise, Guilded bots can do more than connect to Guilded. They can create, update, delete messages, forum threads, etc. and receive various events that Guilded API exposes. Guilded.NET should have majority of these features implemented, but if you want to check out which indeed are supported, go over to the <a href="/docs/supported">Quick Overview: Supported features</a> document.</p>
<h2>Message creation event</h2>
<p>One of the supported events in Guilded API and Guilded.NET is message creation event. It can have plenty of uses, but most notable one is text commands. But to understand text commands, we need to start with a simple response bot.</p>
<div class="code-block container"><header class="code-block header"><div class="code-block lang">C#</div><section class="code-block meta"><div class="code-block-meta-prop container"><span class="code-block-meta-prop key">File: </span><span class="code-block-meta-prop value">Program.cs</span></div></section></header><pre class="code-block code-wrapper language-csharp csharp"><code class="code-block code-container language-csharp csharp"><codeline class="code-block line"><span class="token comment">// ... Where we subscribe to Connected and Prepared events</span></codeline><codeline class="code-block line">client<span class="token punctuation">.</span>MessageCreated</codeline><codeline class="code-block line"> <span class="token punctuation">.</span><span class="token function">Subscribe</span><span class="token punctuation">(</span>msgCreated <span class="token operator">=></span> Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token string">"Received message with content: {0}"</span><span class="token punctuation">,</span> msgCreated<span class="token punctuation">.</span>Content<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></codeline></code></pre></div>
<p>Now anytime any user, bot or webhook posts a message, our client will write <q>Received message with content: message here</q> in the console. We can now reply to the message using <a href="/"><code>ReplyAsync(string)</code></a> method:</p>
<div class="code-block container"><header class="code-block header"><div class="code-block lang">C#</div></header><pre class="code-block code-wrapper language-csharp csharp"><code class="code-block code-container language-csharp csharp"><codeline class="code-block line">client<span class="token punctuation">.</span>MessageCreated</codeline><codeline class="code-block line"> <span class="token punctuation">.</span><span class="token function">Subscribe</span><span class="token punctuation">(</span><span class="token keyword">async</span> msgCreated <span class="token operator">=></span> <span class="token keyword">await</span> msgCreated<span class="token punctuation">.</span><span class="token function">ReplyAsync</span><span class="token punctuation">(</span><span class="token string">"Pong!"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">ConfigureAwait</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></codeline></code></pre></div>
<p>Write <q>Hi!</q> in your server and… the bot replies with <q>Pong!</q> and then spams it while replying to itself.</p>
<p><strong>So, what went wrong?</strong></p>
<p>The bot saw a message with <q>Hi!</q> which we wrote and replied to it with <q>Pong!</q>, but then it saw a message it itself created <q>Pong!</q>, so it replied to itself and started spamming. It can be seen in the reply to which message it reacted this way.</p>
<p>As such, we should check whether it is <q>!ping</q> and not any other message:</p>
<div class="code-block container"><header class="code-block header"><div class="code-block lang">C#</div></header><pre class="code-block code-wrapper language-csharp csharp"><code class="code-block code-container language-csharp csharp"><codeline class="code-block line"><span class="token keyword">using</span> <span class="token namespace">System<span class="token punctuation">.</span>Reactive<span class="token punctuation">.</span>Linq</span><span class="token punctuation">;</span></codeline><codeline class="code-block line"></codeline><codeline class="code-block line"><span class="token comment">// ... Where we subscribed to Connected and Prepared events</span></codeline><codeline class="code-block line">client<span class="token punctuation">.</span>MessageCreated</codeline><codeline class="code-block line"> <span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span>msgCreated <span class="token operator">=></span> msgCreated<span class="token punctuation">.</span>Content <span class="token operator">==</span> <span class="token string">"!ping"</span><span class="token punctuation">)</span></codeline><codeline class="code-block line"> <span class="token punctuation">.</span><span class="token function">Subscribe</span><span class="token punctuation">(</span><span class="token keyword">async</span> msgCreated <span class="token operator">=></span> <span class="token keyword">await</span> msgCreated<span class="token punctuation">.</span><span class="token function">ReplyAsync</span><span class="token punctuation">(</span><span class="token string">"Pong!"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">ConfigureAwait</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></codeline></code></pre></div>
<p>Writing <q>Hi!</q> should give you no response from your bot. Now write <q>!ping</q> and the bot should respond with <q>Pong!</q>. We have now set up the most basic command we could make.</p>
<p>At this time, there is no way to detect whether the author of the message is a bot. You can, however, it’s possible to check if they are a webhook by checking if <a href="/">CreatedByWebhook</a>.</p>
<p>This only allows us to use ping command though, but we definitely want to have more than a ping command. Time to make more commands.</p>
<h2>Commands</h2>
<p>While you can build your own, it’s recommended to use Guilded.NET’s command system. This will be covered <a href="/docs/commands">in this document</a>.</p>
<h2>Other events</h2>
<p>There is more events than message created event. The list includes <a href="/">MessageUpdated</a>, <a href="/">MessageDeleted</a> and other events. It is recommend to check them out and see what you can do.</p>
</div></article>
</div>
</div></div>
</main>
</body>
</html>