74 lines
2.0 KiB
Markdown
74 lines
2.0 KiB
Markdown
# 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 Valour.Shared;
|
|
using DotNetEnv;
|
|
|
|
// 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 script.
|
|
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 we 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) |