new commands
This commit is contained in:
@@ -1,44 +1,46 @@
|
||||
# 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.
|
||||
After logging in and joining a planet, you 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**
|
||||
> **Note:** Due to a bug in the Valour SDK the built-in channel cache is not working right now, so we use our own dictionary as a workaround.
|
||||
|
||||
### Creating a channel cache
|
||||
|
||||
We store each channel by its ID so we can look them up quickly later.
|
||||
|
||||
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
|
||||
// Make your own channel cache using the channel's ID as the key
|
||||
Dictionary<long, Channel> channelCache = new();
|
||||
```
|
||||
|
||||
Next we have to get the first planet the bot has joined.
|
||||
### Loading planet data
|
||||
|
||||
Before we can read a planet's channels we need to fetch its data. We also loop through every channel and add it to the cache.
|
||||
|
||||
```c#
|
||||
// Get the first planet the bot joined.
|
||||
// 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.
|
||||
// Load the planet's data
|
||||
await planet.EnsureReadyAsync();
|
||||
await planet.FetchInitialDataAsync();
|
||||
|
||||
// Fetch each channel.
|
||||
// Add every channel to the cache
|
||||
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:
|
||||
### Finding the default channel
|
||||
|
||||
Now that the cache is filled we can grab the planet's primary chat channel to send messages to.
|
||||
|
||||
```c#
|
||||
// Fetch the default chat channel
|
||||
if (!channelCache.TryGetValue(planet.PrimaryChatChannel.Id, out var channel)) return;
|
||||
|
||||
// Fall back to the first channel
|
||||
// Fall back to the first chat channel if the default wasn't found
|
||||
if (channel is null)
|
||||
{
|
||||
channel = planet.Channels.FirstOrDefault(
|
||||
@@ -46,20 +48,23 @@ if (channel is null)
|
||||
);
|
||||
}
|
||||
|
||||
// One final check if the channel is still null. (very rare!)
|
||||
if (channel is null) {
|
||||
// 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.
|
||||
### Sending a message
|
||||
|
||||
With a channel in hand, sending a message is just one line.
|
||||
|
||||
```c#
|
||||
// Send message to the channel!
|
||||
// Send a message to the channel
|
||||
await channel.SendMessageAsync("Hello from my bot!");
|
||||
```
|
||||
|
||||
Great now your bot has sent its first message in your Planet!
|
||||
Great, your bot has sent its first message in your Planet!
|
||||
<br><br>
|
||||
### Full code below
|
||||
|
||||
@@ -77,10 +82,10 @@ Env.Load();
|
||||
|
||||
string token = Environment.GetEnvironmentVariable("TOKEN") ?? string.Empty;
|
||||
|
||||
// Make your own Channel Cache
|
||||
// Make your own channel cache using the channel's ID as the key
|
||||
Dictionary<long, Channel> channelCache = new();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(token))
|
||||
if (string.IsNullOrWhiteSpace(token))
|
||||
{
|
||||
Console.WriteLine($"Token invalid. Please make sure you set a valid token in your .env");
|
||||
return;
|
||||
@@ -94,24 +99,23 @@ if (!loginResult.Success)
|
||||
|
||||
Console.WriteLine($"Logged in as {client.Me.Name} (ID: {client.Me.Id})");
|
||||
|
||||
// Get the first planet the bot joined.
|
||||
// Get the first planet the bot joined
|
||||
Planet planet = client.PlanetService.JoinedPlanets.First();
|
||||
|
||||
// Load the planet's data.
|
||||
// Load the planet's data
|
||||
await planet.EnsureReadyAsync();
|
||||
await planet.FetchInitialDataAsync();
|
||||
|
||||
// Fetch each channel.
|
||||
// Add every channel to the cache
|
||||
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
|
||||
// Fall back to the first chat channel if the default wasn't found
|
||||
if (channel is null)
|
||||
{
|
||||
channel = planet.Channels.FirstOrDefault(
|
||||
@@ -119,17 +123,17 @@ if (channel is null)
|
||||
);
|
||||
}
|
||||
|
||||
// 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")
|
||||
// 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!
|
||||
// Send a message to the channel
|
||||
await channel.SendMessageAsync("Hello from my bot!");
|
||||
|
||||
|
||||
await Task.Delay(Timeout.Infinite);
|
||||
```
|
||||
|
||||
##
|
||||
## [The Next step is receiving Messages and Commands](4.MessagesAndCommands.md)
|
||||
---
|
||||
## [The Next step is Receiving Messages and Commands](4.MessagesAndCommands.md)
|
||||
|
||||
Reference in New Issue
Block a user