new commands
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
# Receiving messages and Making commands
|
||||
# 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!
|
||||
Now that we can send messages, it's time to receive them from users so 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**
|
||||
> **Note:** Due to a bug in the Valour SDK the built-in channel cache is not working right now, so we keep using the workaround from the previous step.
|
||||
|
||||
### Opening every channel
|
||||
|
||||
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:
|
||||
In the previous step we only opened one planet's channels. Now we loop through every planet the bot has joined and open all of their chat channels, so the bot can see messages everywhere.
|
||||
|
||||
```c#
|
||||
// Fetch all the planets instead of just the first
|
||||
// Loop through every planet the bot has joined
|
||||
foreach (Planet planet in client.PlanetService.JoinedPlanets)
|
||||
{
|
||||
await planet.EnsureReadyAsync();
|
||||
@@ -16,31 +17,34 @@ foreach (Planet planet in client.PlanetService.JoinedPlanets)
|
||||
|
||||
foreach (Channel chan in planet.Channels)
|
||||
{
|
||||
// Add the channel to the cache
|
||||
channelCache[chan.Id] = chan;
|
||||
|
||||
// If the channel is a chat channel then open that channel to the bot to see.
|
||||
// Open each chat channel so the bot can receive messages from it
|
||||
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.
|
||||
### Listening for messages
|
||||
|
||||
To receive messages we subscribe to the `MessageReceived` event on the message service.
|
||||
|
||||
```c#
|
||||
// Get all messages that are send into text channels
|
||||
// Subscribe to incoming messages
|
||||
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)
|
||||
We define the handler as a separate function rather than inline so we can use `await` inside it without restrictions.
|
||||
|
||||
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!
|
||||
### Creating a command
|
||||
|
||||
Inside `onMessageReceived` we check the message content against a prefix and respond to commands. Here's a simple `ping` command that replies with `Pong!`.
|
||||
|
||||
```c#
|
||||
// Function for what to do with the received messages
|
||||
async Task onMessageReceived(Message message)
|
||||
{
|
||||
// Prefix.. duh..
|
||||
@@ -83,7 +87,7 @@ string token = Environment.GetEnvironmentVariable("TOKEN") ?? string.Empty;
|
||||
|
||||
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;
|
||||
@@ -97,7 +101,7 @@ if (!loginResult.Success)
|
||||
|
||||
Console.WriteLine($"Logged in as {client.Me.Name} (ID: {client.Me.Id})");
|
||||
|
||||
// Fetch all the planets instead of just the first
|
||||
// Loop through every planet the bot has joined
|
||||
foreach (Planet planet in client.PlanetService.JoinedPlanets)
|
||||
{
|
||||
await planet.EnsureReadyAsync();
|
||||
@@ -105,21 +109,20 @@ foreach (Planet planet in client.PlanetService.JoinedPlanets)
|
||||
|
||||
foreach (Channel chan in planet.Channels)
|
||||
{
|
||||
// Add the channel to the cache
|
||||
channelCache[chan.Id] = chan;
|
||||
|
||||
// If the channel is a chat channel then open that channel to the bot to see.
|
||||
// Open each chat channel so the bot can receive messages from it
|
||||
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
|
||||
// Subscribe to incoming messages
|
||||
client.MessageService.MessageReceived += onMessageReceived;
|
||||
|
||||
// Function for what to do with the received messages
|
||||
async Task onMessageReceived(Message message)
|
||||
{
|
||||
// Prefix.. duh..
|
||||
@@ -144,5 +147,5 @@ async Task onMessageReceived(Message message)
|
||||
await Task.Delay(Timeout.Infinite);
|
||||
```
|
||||
|
||||
##
|
||||
## End of Bot Guide (for now)
|
||||
---
|
||||
## [The Next step is Command Arguments](5.CommandArguments.md)
|
||||
|
||||
Reference in New Issue
Block a user