148 lines
4.5 KiB
Markdown
148 lines
4.5 KiB
Markdown
# 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) |