Skip to content

Latest commit

 

History

History
132 lines (91 loc) · 5.06 KB

File metadata and controls

132 lines (91 loc) · 5.06 KB
name Developing bots
icon play
priority 1
category Getting Started
tags
Getting Started
Docs
C#

Getting Started: Developing the bot

NOTE: Guilded.NET offers project templates that can create a skeleton of a bot. HOWEVER(!!!), if you are getting started with Guilded.NET, it's recommended to continue reading this tutorial, as you may not understand parts of template's code. If you want to use templates, check out Getting Started: .NET templates document.

Installing Guilded.NET

First of all, we need to create a .NET project for the bot. The preferred .NET project type is console, however, it's not necessarily restricted to any .NET project type, so you can choose any that you prefer.

cd "/path/to/the/dotnet/project"
dotnet new console

When we have a new .NET project, we can then add Guilded.NET, which in itself is one simple command:

dotnet add package Guilded

While this adds Guilded.NET to the project, it doesn't connect Guilded, let alone create a Guilded bot. Which means we need to create and configure a bot for it.

Configuring your bot

Now we'll need to define prefix and an authentication token for your bot. You can use either constants or configuration files for that, but we'll use a configuration file.

The name of the directory, the file and even properties in the file can be whatever you want to name it, but make sure it is referenced in your code. We'll use config/config.json in this documentation.

In your config file, create a property auth and property prefix, where your authentication token and prefix will be stored:

{
    "auth": "authentication_credentials_here",
    "prefix": "/"
}

NOTE: If you don't have an auth token, Getting token document covers how to get one.

Be sure to make MSBuild not ignore your config folder in your project file:

<!-- ... Other stuff ... -->
<ItemGroup>
    <Content Include="config/*">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>
<!-- ... Other stuff ... -->

We now have a configuration file that we can safely reference in our code and use properties in it. You can use either System.Text.Json or Newtonsoft.Json to deserialize our JSON and get its values. For this tutorial, we are going to use Newtonsoft.Json, since Guilded.NET has Newtonsoft.Json as a dependency. Newtonsoft.Json will be removed as a dependency in the future, so if you'll still want to use Newtonsoft.Json, it is recommended to add Newtonsoft.Json as a dependency in your project.

We are now going to get the configuration and use it in our bot:

// At the very top of the file
using System.IO;

using Guilded;

using Newtonsoft.Json.Linq;
JObject config = JObject.Parse(File.ReadAllText("./config/config.json"));

string auth   = config.Value<string>("auth"),
       prefix = config.Value<string>("prefix");

await using var client = new GuildedBotClient(auth);

You can now run your project, but as we can see, it doesn't really do anything. But that's because we are only creating our bot, not connecting to Guilded yet, which we are going to do in the next section.

Connecting to Guilded

Method ConnectAsync is used to connect the client to Guilded. But once you use it, the bot will connect to Guilded and program will close seeing that it's done. One of the ways to stop it from closing is to use await Task.Delay(-1):

using System.IO;
using System.Threading.Tasks;

using Guilded;

using Newtonsoft.Json;
await client.ConnectAsync();

// Stop program from closing
await Task.Delay(-1);

CAUTION: Make sure that everything you put goes above await Task.Delay(-1);, as anything below it may never be run at all.

To see if connection between Guilded and the client are initiated, we can subscribe to Prepared or Connected events:

using System;
using System.IO;
using System.Threading.Tasks;

using Guilded;

using Newtonsoft.Json;
// Below `await using GuildedBotClient client = ...;`
client.Prepared
      .Subscribe(me =>
          Console.WriteLine("The bot is prepared.\nLogged in as \"{0}\" with the ID \"{1}\"", me.Name, me.Id)
      );

The main difference is that Connected gets connected once the bot is online and can be functional, while Prepared is called once the bot is connected and gets additional information, such as client.Me. If Connected gets called before Prepared, the client.Me property will always be null.

Once the bot successfully connects and does everything it needs, you will be able to see The bot is prepared. in the console.

The bot won't do anything apart from connecting to Guilded. In Powering up the bot document, we are going to give functionality to our bot with text commands.