Getting Started: Powering up the bot

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 Quick Overview: Supported features document.

Message creation event

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.

C#
File: Program.cs
// ... Where we subscribe to Connected and Prepared eventsclient.MessageCreated    .Subscribe(msgCreated => Console.WriteLine("Received message with content: {0}", msgCreated.Content));

Now anytime any user, bot or webhook posts a message, our client will write Received message with content: message here in the console. We can now reply to the message using ReplyAsync(string) method:

C#
client.MessageCreated    .Subscribe(async msgCreated => await msgCreated.ReplyAsync("Pong!").ConfigureAwait(false));

Write Hi! in your server and… the bot replies with Pong! and then spams it while replying to itself.

So, what went wrong?

The bot saw a message with Hi! which we wrote and replied to it with Pong!, but then it saw a message it itself created Pong!, so it replied to itself and started spamming. It can be seen in the reply to which message it reacted this way.

As such, we should check whether it is !ping and not any other message:

C#
using System.Reactive.Linq;// ... Where we subscribed to Connected and Prepared eventsclient.MessageCreated    .Where(msgCreated => msgCreated.Content == "!ping")    .Subscribe(async msgCreated => await msgCreated.ReplyAsync("Pong!").ConfigureAwait(false));

Writing Hi! should give you no response from your bot. Now write !ping and the bot should respond with Pong!. We have now set up the most basic command we could make.

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 CreatedByWebhook.

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.

Commands

While you can build your own, it’s recommended to use Guilded.NET’s command system. This will be covered in this document.

Other events

There is more events than message created event. The list includes MessageUpdated, MessageDeleted and other events. It is recommend to check them out and see what you can do.