messages and commands
This commit is contained in:
@@ -27,8 +27,8 @@ The below code will make a bot and login as that bot account you created.
|
|||||||
```c#
|
```c#
|
||||||
// Importing the Client SDK and DotEnv.
|
// Importing the Client SDK and DotEnv.
|
||||||
using Valour.Sdk.Client;
|
using Valour.Sdk.Client;
|
||||||
using DotNetEnv;
|
|
||||||
using Valour.Shared;
|
using Valour.Shared;
|
||||||
|
using DotNetEnv;
|
||||||
|
|
||||||
// Creating the client that connects to Valour Servers.
|
// Creating the client that connects to Valour Servers.
|
||||||
ValourClient client = new ValourClient("https://api.valour.gg/");
|
ValourClient client = new ValourClient("https://api.valour.gg/");
|
||||||
@@ -37,7 +37,7 @@ client.SetupHttpClient();
|
|||||||
// Import the .env variables into the app.
|
// Import the .env variables into the app.
|
||||||
Env.Load();
|
Env.Load();
|
||||||
|
|
||||||
// Store the token into a variable inside of the client.
|
// Store the token into a variable inside of the script.
|
||||||
string token = Environment.GetEnvironmentVariable("TOKEN") ?? string.Empty;
|
string token = Environment.GetEnvironmentVariable("TOKEN") ?? string.Empty;
|
||||||
|
|
||||||
// Check if the token is valid.
|
// Check if the token is valid.
|
||||||
@@ -62,7 +62,7 @@ Console.WriteLine($"Logged in as {client.Me.Name} (ID: {client.Me.Id})");
|
|||||||
await Task.Delay(Timeout.Infinite);
|
await Task.Delay(Timeout.Infinite);
|
||||||
```
|
```
|
||||||
|
|
||||||
Now to run the bot we need to make sure ew are inside the root directory of the app and then run:
|
Now to run the bot we need to make sure we are inside the root directory of the app and then run:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
dotnet run
|
dotnet run
|
||||||
|
|||||||
@@ -131,3 +131,5 @@ await channel.SendMessageAsync("Hello from my bot!");
|
|||||||
await Task.Delay(Timeout.Infinite);
|
await Task.Delay(Timeout.Infinite);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
##
|
||||||
|
## [The Next step is receiving Messages and Commands](4.MessagesAndCommands.md)
|
||||||
148
Guides/4.MessagesAndCommands.md
Normal file
148
Guides/4.MessagesAndCommands.md
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
# Receiving messages and Making commands
|
||||||
|
|
||||||
|
Now that we can send messages to a channel using the bot its time to receive messages from users so that we can make commands!
|
||||||
|
|
||||||
|
**Unfortunately due to a bug in the Valour SDK at this moment the caching of channels isnt working and so we have to do a workaround**
|
||||||
|
|
||||||
|
|
||||||
|
First lets start with making it so the bot can see every channel for every planet its in. We can do this by editing the code just a little bit and looping through each planet and then opening every channel:
|
||||||
|
|
||||||
|
```c#
|
||||||
|
// Fetch all the planets instead of just the first
|
||||||
|
foreach (Planet planet in client.PlanetService.JoinedPlanets)
|
||||||
|
{
|
||||||
|
await planet.EnsureReadyAsync();
|
||||||
|
await planet.FetchInitialDataAsync();
|
||||||
|
|
||||||
|
foreach (Channel chan in planet.Channels)
|
||||||
|
{
|
||||||
|
channelCache[chan.Id] = chan;
|
||||||
|
|
||||||
|
// If the channel is a chat channel then open that channel to the bot to see.
|
||||||
|
if (chan.ChannelType == ChannelTypeEnum.PlanetChat)
|
||||||
|
{
|
||||||
|
// Open the channel with your bots name!
|
||||||
|
await chan.OpenWithResult("YourBotName");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
After every channel is open we have to make the bot listen for incoming messages.
|
||||||
|
|
||||||
|
```c#
|
||||||
|
// Get all messages that are send into text channels
|
||||||
|
client.MessageService.MessageReceived += onMessageReceived;
|
||||||
|
```
|
||||||
|
|
||||||
|
In order to use this listener we have to make a Function called onMessageReceived (or whatever you named it above). We can do this directly on the listener but some things are restricted by doing this (idk dont ask :P)
|
||||||
|
|
||||||
|
Inside this listener is where we execute what we want to do with the incoming message. In this example we have a simple Ping command that replys with Pong!
|
||||||
|
|
||||||
|
```c#
|
||||||
|
// Function for what to do with the received messages
|
||||||
|
async Task onMessageReceived(Message message)
|
||||||
|
{
|
||||||
|
// Prefix.. duh..
|
||||||
|
string prefix = "bg/";
|
||||||
|
// What the message says
|
||||||
|
string content = message.Content;
|
||||||
|
|
||||||
|
// Get the channel that the message was sent in
|
||||||
|
if (!channelCache.TryGetValue(message.ChannelId, out var channel)) return;
|
||||||
|
|
||||||
|
// If the message doesnt start with the prefix then just ignore it
|
||||||
|
if (!content.StartsWith(prefix)) return;
|
||||||
|
|
||||||
|
// Check if the message is the prefix + ping
|
||||||
|
if (content.StartsWith($"{prefix}ping"))
|
||||||
|
{
|
||||||
|
// Sends the message `Pong!` to the channel
|
||||||
|
await channel.SendMessageAsync("Pong!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This is the basics of how to get commands working with a bot on Valour.
|
||||||
|
<br><br>
|
||||||
|
### Full code below
|
||||||
|
|
||||||
|
```c#
|
||||||
|
using Valour.Sdk.Client;
|
||||||
|
using Valour.Shared;
|
||||||
|
using DotNetEnv;
|
||||||
|
using Valour.Sdk.Models;
|
||||||
|
using Valour.Shared.Models;
|
||||||
|
|
||||||
|
ValourClient client = new ValourClient("https://api.valour.gg/");
|
||||||
|
client.SetupHttpClient();
|
||||||
|
|
||||||
|
Env.Load();
|
||||||
|
|
||||||
|
string token = Environment.GetEnvironmentVariable("TOKEN") ?? string.Empty;
|
||||||
|
|
||||||
|
Dictionary<long, Channel> channelCache = new();
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(token))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Token invalid. Please make sure you set a valid token in your .env");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
TaskResult loginResult = await client.InitializeUser(token);
|
||||||
|
if (!loginResult.Success)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Login failed: {loginResult.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine($"Logged in as {client.Me.Name} (ID: {client.Me.Id})");
|
||||||
|
|
||||||
|
// Fetch all the planets instead of just the first
|
||||||
|
foreach (Planet planet in client.PlanetService.JoinedPlanets)
|
||||||
|
{
|
||||||
|
await planet.EnsureReadyAsync();
|
||||||
|
await planet.FetchInitialDataAsync();
|
||||||
|
|
||||||
|
foreach (Channel chan in planet.Channels)
|
||||||
|
{
|
||||||
|
channelCache[chan.Id] = chan;
|
||||||
|
|
||||||
|
// If the channel is a chat channel then open that channel to the bot to see.
|
||||||
|
if (chan.ChannelType == ChannelTypeEnum.PlanetChat)
|
||||||
|
{
|
||||||
|
// Open the channel with your bots name!
|
||||||
|
await chan.OpenWithResult("YourBotName");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all messages that are send into text channels
|
||||||
|
client.MessageService.MessageReceived += onMessageReceived;
|
||||||
|
|
||||||
|
// Function for what to do with the received messages
|
||||||
|
async Task onMessageReceived(Message message)
|
||||||
|
{
|
||||||
|
// Prefix.. duh..
|
||||||
|
string prefix = "bg/";
|
||||||
|
// What the message says
|
||||||
|
string content = message.Content;
|
||||||
|
|
||||||
|
// Get the channel that the message was sent in
|
||||||
|
if (!channelCache.TryGetValue(message.ChannelId, out var channel)) return;
|
||||||
|
|
||||||
|
// If the message doesnt start with the prefix then just ignore it
|
||||||
|
if (!content.StartsWith(prefix)) return;
|
||||||
|
|
||||||
|
// Check if the message is the prefix + ping
|
||||||
|
if (content.StartsWith($"{prefix}ping"))
|
||||||
|
{
|
||||||
|
// Sends the message `Pong!` to the channel
|
||||||
|
await channel.SendMessageAsync("Pong!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await Task.Delay(Timeout.Infinite);
|
||||||
|
```
|
||||||
|
|
||||||
|
##
|
||||||
|
## End of Bot Guide (for now)
|
||||||
@@ -14,4 +14,5 @@ This guide walks you through creating a bot on Valour.gg, connecting it with the
|
|||||||
### Step 1. [Logging in as the bot](Guides/1.Login.md)
|
### Step 1. [Logging in as the bot](Guides/1.Login.md)
|
||||||
### Step 2. [Joining a planet](Guides/2.JoinPlanet.md)
|
### Step 2. [Joining a planet](Guides/2.JoinPlanet.md)
|
||||||
### Step 3. [Connecting and Sending a Message to a Planet](Guides/3.ConnectingAndSending.md)
|
### Step 3. [Connecting and Sending a Message to a Planet](Guides/3.ConnectingAndSending.md)
|
||||||
|
### Step 4. [Reveiving Messages and Commands](4.MessagesAndCommands.md)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user