Your First Bot
Note
This article assumes the following:
- You have created a bot account and have a token.
- You have JetBrains Rider or another IDE installed on your computer.
Create a Project
Open up Rider and click on New Solution
at the top of the window.
Select .NET / .NET Core > Console Application
Next, you'll give your project a name. For this example, we'll name it MyFirstBot
. If you'd like, you can also change
the directory that your project will be created in.
Enter your desired project name, then click on the Create
button.
Install Package
Now that you have a project created, you'll want to get KSharpPlus installed.
Locate the Explorer towards the upper left, then right click on Dependencies
and select Manage NuGet Packages
from the context menu.
You'll then be greeted by the NuGet package manager.
Select the Packages
tab, then type KSharpPlus
into the search text box.
The first result should be the KSharpPlus package.
Package | Description |
---|---|
KSharpPlus |
Main package; Kuracord API client. |
We'll only need the KSharpPlus
package for the basic bot we'll be writing in this article. Select it from the list
then click the Install
button to the right (after verifing that you will be installing the latest version).
You're now ready to write some code!
First Lines of Code
KSharpPlus implements Task-based Asynchronous Pattern.
Because of this, the majority of KSharpPlus methods must be executed in a method marked as async
so they can be properly await
ed.
Head back to your Program.cs tab and delete all in it.
Then add the Main
method:
namespace MyFirstBot;
internal static class Program {
public static async Task Main() {
}
}
We'll now create a new KuracordClient
instance in our brand new asynchronous method.
Create a new variable and assign it a new KuracordClient instance, then pass an instance of KuracordConfiguration to its constructor. Create an object initializer for KuracordConfiguration and populate the Token property with your token, then set the TokenType property to User.
KuracordClient client = new(new KuracordConfiguration {
Token = "My First Token",
TokenType = TokenType.User
});
Warning
We hard-code the token in the above snippet to keep things simple and easy to understand.
Hard-coding your token is not a smart idea, especially if you plan on distributing your source code. Instead you should store your token in an external medium, such as a configuration file or environment variable, and read that into your program to be used with KSharpPlus.
Follow that up with ConnectAsync to connect and login to Kuracord, and await Task.Delay(-1);
at the end of the method to prevent the console window from closing prematurely.
KuracordClient client = new();
await client.ConnectAsync();
await Task.Delay(-1);
ReSharper will have auto generated the needed using
directive for you if you typed this in by hand. If
you've copied the snippet, be sure to apply the recommended suggestion to insert the required directive.
If you hit F5
on your keyboard to compile and run your program, you'll be greeted by a happy little console with a
single log message from KSharpPlus. Woo hoo!
Spicing Up Your Bot
Right now our bot doesn't do a whole lot. It's not even on any server! Let's bring it to life by adding it to the server and having it respond to a message!
Login to your Kuracord account and select the server you want to invite your bot to. Then open any channel and copy Vanity URL for your server.
Hook the Ready event fired by KuracordClient with a lambda.
Mark it as async
and give it two parameters: s
and e
.
Then, add an if
statement into the body of your event lambda that will check is the bot is in any server:
client.Ready += async (s, e) => {
if (client.Guilds.Count > 0) return;
};
Cool! Next we'll add an JoinGuildAsync method and pass Vanity URL as parameters to it.
Also we'll add an confirmation message output to the console.
client.Ready += async (s, e) => {
if (client.Guilds.Count > 0) return;
KuracordGuild guild = await client.JoinGuildAsync("My First Invite");
Console.WriteLine($"I joined the {guild.Name}!");
};
Now we need to add some functionality to the bot.
Subscribe to the MessageCreated event as before.
client.MessageCreated += async (s, e) => {
};
Add an if
statement that will check if Content starts with your
desired trigger word and respond with a message using SendMessageAsync if it does.
For this example, we'll have the bot to respond with pong! for each message that starts with ping.
client.MessageCreated += async (s, e) => {
if (e.Message.Content.ToLower().StartsWith("ping"))
await e.Channel.SendMessageAsync("pong!");
};
The Finished Product
Your entire program should now look like this:
using KSharpPlus;
using KSharpPlus.Clients;
using KSharpPlus.Entities.Guild;
using KSharpPlus.Enums;
namespace MyFirstBot;
internal static class Program {
public static async Task Main() {
KuracordClient client = new(new KuracordConfiguration {
Token = "My First Token",
TokenType = TokenType.User
});
client.Ready += async (s, e) => {
if (client.Guilds.Count > 0) return;
KuracordGuild guild = await client.JoinGuildAsync("My First Invite");
Console.WriteLine($"I joined the {guild.Name}!");
};
client.MessageCreated += async (s, e) => {
if (e.Message.Content.ToLower().StartsWith("ping"))
await e.Channel.SendMessageAsync("pong!");
};
await client.ConnectAsync();
await Task.Delay(-1);
}
}
Hit F5
to run your bot, then send ping in any channel your bot account has access to. Your bot should respond with
pong! for each ping you send.
Congrats, your bot now does something!
Further Reading
Now that you have a basic bot up and running, you should take a look at the following: