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

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);
```