152 lines
4.3 KiB
Markdown
152 lines
4.3 KiB
Markdown
# Receiving Messages and Making Commands
|
|
|
|
Now that we can send messages, it's time to receive them from users so we can make commands!
|
|
|
|
> **Note:** Due to a bug in the Valour SDK the built-in channel cache is not working right now, so we keep using the workaround from the previous step.
|
|
|
|
### Opening every channel
|
|
|
|
In the previous step we only opened one planet's channels. Now we loop through every planet the bot has joined and open all of their chat channels, so the bot can see messages everywhere.
|
|
|
|
```c#
|
|
// Loop through every planet the bot has joined
|
|
foreach (Planet planet in client.PlanetService.JoinedPlanets)
|
|
{
|
|
await planet.EnsureReadyAsync();
|
|
await planet.FetchInitialDataAsync();
|
|
|
|
foreach (Channel chan in planet.Channels)
|
|
{
|
|
// Add the channel to the cache
|
|
channelCache[chan.Id] = chan;
|
|
|
|
// Open each chat channel so the bot can receive messages from it
|
|
if (chan.ChannelType == ChannelTypeEnum.PlanetChat)
|
|
{
|
|
await chan.OpenWithResult("YourBotName");
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
### Listening for messages
|
|
|
|
To receive messages we subscribe to the `MessageReceived` event on the message service.
|
|
|
|
```c#
|
|
// Subscribe to incoming messages
|
|
client.MessageService.MessageReceived += onMessageReceived;
|
|
```
|
|
|
|
We define the handler as a separate function rather than inline so we can use `await` inside it without restrictions.
|
|
|
|
### Creating a command
|
|
|
|
Inside `onMessageReceived` we check the message content against a prefix and respond to commands. Here's a simple `ping` command that replies with `Pong!`.
|
|
|
|
```c#
|
|
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})");
|
|
|
|
// Loop through every planet the bot has joined
|
|
foreach (Planet planet in client.PlanetService.JoinedPlanets)
|
|
{
|
|
await planet.EnsureReadyAsync();
|
|
await planet.FetchInitialDataAsync();
|
|
|
|
foreach (Channel chan in planet.Channels)
|
|
{
|
|
// Add the channel to the cache
|
|
channelCache[chan.Id] = chan;
|
|
|
|
// Open each chat channel so the bot can receive messages from it
|
|
if (chan.ChannelType == ChannelTypeEnum.PlanetChat)
|
|
{
|
|
await chan.OpenWithResult("YourBotName");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Subscribe to incoming messages
|
|
client.MessageService.MessageReceived += onMessageReceived;
|
|
|
|
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);
|
|
```
|
|
|
|
---
|
|
## [The Next step is Command Arguments](5.CommandArguments.md)
|