From c982766c3a740a16bc47c01ad5e1b7b8332d6716 Mon Sep 17 00:00:00 2001 From: skyjoshua Date: Sat, 28 Mar 2026 03:26:51 +0000 Subject: [PATCH] messages and commands --- Guides/1.Login.md | 6 +- Guides/3.ConnectingAndSending.md | 2 + Guides/4.MessagesAndCommands.md | 148 +++++++++++++++++++++++++++++++ README.md | 1 + 4 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 Guides/4.MessagesAndCommands.md diff --git a/Guides/1.Login.md b/Guides/1.Login.md index 8a0b0c9..3e31eea 100644 --- a/Guides/1.Login.md +++ b/Guides/1.Login.md @@ -27,8 +27,8 @@ The below code will make a bot and login as that bot account you created. ```c# // Importing the Client SDK and DotEnv. using Valour.Sdk.Client; -using DotNetEnv; using Valour.Shared; +using DotNetEnv; // Creating the client that connects to Valour Servers. ValourClient client = new ValourClient("https://api.valour.gg/"); @@ -37,7 +37,7 @@ client.SetupHttpClient(); // Import the .env variables into the app. 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; // 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); ``` -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 dotnet run diff --git a/Guides/3.ConnectingAndSending.md b/Guides/3.ConnectingAndSending.md index d9ef999..cbfcfb9 100644 --- a/Guides/3.ConnectingAndSending.md +++ b/Guides/3.ConnectingAndSending.md @@ -131,3 +131,5 @@ await channel.SendMessageAsync("Hello from my bot!"); await Task.Delay(Timeout.Infinite); ``` +## +## [The Next step is receiving Messages and Commands](4.MessagesAndCommands.md) \ No newline at end of file diff --git a/Guides/4.MessagesAndCommands.md b/Guides/4.MessagesAndCommands.md new file mode 100644 index 0000000..88e03f9 --- /dev/null +++ b/Guides/4.MessagesAndCommands.md @@ -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. +

+### 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 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) \ No newline at end of file diff --git a/README.md b/README.md index 4c29fe9..ea7fbaf 100644 --- a/README.md +++ b/README.md @@ -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 2. [Joining a planet](Guides/2.JoinPlanet.md) ### Step 3. [Connecting and Sending a Message to a Planet](Guides/3.ConnectingAndSending.md) +### Step 4. [Reveiving Messages and Commands](4.MessagesAndCommands.md)