Upto Connecting and Sending a message to a planet

This commit is contained in:
2026-03-28 00:56:48 +00:00
parent 6be27bc381
commit 7154f3ff48
6 changed files with 984 additions and 1 deletions

74
Guides/1.Login.md Normal file
View File

@@ -0,0 +1,74 @@
# Logging in to the Bot
Before we start we need to create the .NET application area.
Create a new .NET console app and add the Valour SDK + DotEnv compatability:
```bash
dotnet new console -n BasicValourBot
cd BasicValourBot
dotnet add package Valour.SDK
dotnet add package DotNetEnv
```
### Firstly we need to create another file!
In the root of your Bot folder create another file called ```.env``` this file will contain your bot token so **DO NOT** show it to anyone!
<br>
Inside of this file we will input the following:
<br>
```
TOKEN=bot-your-bot-token-here
```
### Next we can start coding the bot!
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;
// Creating the client that connects to Valour Servers.
ValourClient client = new ValourClient("https://api.valour.gg/");
client.SetupHttpClient();
// Import the .env variables into the app.
Env.Load();
// Store the token into a variable inside of the client.
string token = Environment.GetEnvironmentVariable("TOKEN") ?? string.Empty;
// Check if the token is valid.
if (String.IsNullOrWhiteSpace(token))
{
// The token in your .env file is missing or invalid, make sure you set a valid token!
Console.WriteLine($"Token invalid. Please make sure you set a valid token in your .env");
return;
}
// Login to the client using the token for your bot.
TaskResult loginResult = await client.InitializeUser(token);
if (!loginResult.Success)
{
Console.WriteLine($"Login failed: {loginResult.Message}");
}
// Shows that the bot logged in and who it logged in as.
Console.WriteLine($"Logged in as {client.Me.Name} (ID: {client.Me.Id})");
// Keep the bot alive.
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:
```bash
dotnet run
```
The bot should then start up and you should see `Logged in as botname (ID: botid)`
##
## [The Next step is to Add the bot to a Planet.](2.JoinPlanet.md)

70
Guides/2.JoinPlanet.md Normal file
View File

@@ -0,0 +1,70 @@
# Add the bot to a Planet
Bots join planets the same way users do - through invite links or the Discovery page.
<br>
There are a few way of doing this. I will be going through **two** of them in this guide.
## 1. Using my website
The easiest way to add a bot to your Planet is to use my website [here](https://skyjoshua.xyz/planetjoiner)
<br>
You should see something like this below:
<br>
![Planet Joiner Site](https://sxsc.xyz/files/images/.PlanetJoiner.png)
<br>
Fill in the boxes with the required information and hit **Join Planet** and the bot should join the desired planet.
<br>
*Note: Sometimes the Invite Cote is not required if the Planet you are trying to join is Discoverable*
## 2. Using code
To make a Bot join your Planet using code we can add 1 line to the previous code.
```c#
// If the planet is open to discovery only the planet ID is required.
await client.PlanetService.JoinPlanetAsync(planetId);
```
or
```c#
// If the planet is not open to discovery an the last part of an invite (the invite code) is also required.
await client.PlanetService.JoinPlanetAsync(planetId, "INVITE_CODE");
```
Below is the full code required to add the bot to a planet. (Comments are new parts added.)
```c#
using Valour.Sdk.Client;
using Valour.Shared;
using DotNetEnv;
ValourClient client = new ValourClient("https://api.valour.gg/");
client.SetupHttpClient();
Env.Load();
string token = Environment.GetEnvironmentVariable("TOKEN") ?? string.Empty;
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})");
// Join a planet
await client.PlanetService.JoinPlanetAsync(00000000000000000, "ABCDEF");
await Task.Delay(Timeout.Infinite);
```
##
## [The Next step is to Connecting and Sending a message to a Planet](3.ConnectingAndSending.md)

View File

@@ -0,0 +1,144 @@
# Connecting and Sending a message to a Planet
After logging in and Joining a planet, you will need to open a real time connection before you can interact with it. This sets up the SignalR connection that delivers live events.
**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**
Firstly we have to create a Channel Cache (as said above the current one is broken) we can do this like so:
```c#
// Make your own Channel Cache
Dictionary<long, Channel> channelCache = new();
```
Next we have to get the first planet the bot has joined.
```c#
// Get the first planet the bot joined.
Planet planet = client.PlanetService.JoinedPlanets.First();
```
Now that we have the first planet that the bot has joined we can use that to get all the channels that the planet has. We can do this with this code:
```c#
// Load the planet's data.
await planet.EnsureReadyAsync();
await planet.FetchInitialDataAsync();
// Fetch each channel.
foreach (Channel chan in planet.Channels)
{
// Add the channel to the cache using its id to call back to it.
channelCache[chan.Id] = chan;
}
```
Now that we have all the channels and they have been added to our channel cache we can use that cache to find the default channel for that planet. We can do that with the code below:
```c#
// Fetch the default chat channel
if (!channelCache.TryGetValue(planet.PrimaryChatChannel.Id, out var channel)) return;
// Fall back to the first channel
if (channel is null)
{
channel = planet.Channels.FirstOrDefault(
c => c.ChannelType == ChannelTypeEnum.PlanetChat
);
}
// One final check if the channel is still null. (very rare!)
if (channel is null) {
Console.WriteLine("Unable to find the Default or First channel");
}
```
Now that we have the first channel we can use that channel to send a message to it.
```c#
// Send message to the channel!
await channel.SendMessageAsync("Hello from my bot!");
```
Great now your bot has sent its first message in your Planet!
<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;
// Make your own Channel Cache
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})");
// Get the first planet the bot joined.
Planet planet = client.PlanetService.JoinedPlanets.First();
// Load the planet's data.
await planet.EnsureReadyAsync();
await planet.FetchInitialDataAsync();
// Fetch each channel.
foreach (Channel chan in planet.Channels)
{
// Add the channel to the cache using its id to call back to it.
channelCache[chan.Id] = chan;
}
// Fetch the default chat channel
if (!channelCache.TryGetValue(planet.PrimaryChatChannel.Id, out var channel)) return;
// Fall back to the first channel
if (channel is null)
{
channel = planet.Channels.FirstOrDefault(
c => c.ChannelType == ChannelTypeEnum.PlanetChat
);
}
// One final check if the channel is still null. (very rare!)
if (channel is null) {
Console.WriteLine("Unable to find the Default or First channel")
}
// Send message to the channel!
await channel.SendMessageAsync("Hello from my bot!");
await Task.Delay(Timeout.Infinite);
```