Files
Valour-Bot-Guide/Guides/1.Login.md
2026-03-29 02:07:51 +01:00

80 lines
2.1 KiB
Markdown

# Logging in to the Bot
Before we start we need to create the .NET application and install the required packages.
### Setting up the project
Create a new .NET console app and add the Valour SDK and DotNetEnv:
```bash
dotnet new console -n BasicValourBot
cd BasicValourBot
dotnet add package Valour.SDK
dotnet add package DotNetEnv
```
### Creating the .env file
In the root of your Bot folder create a file called `.env`. This file will contain your bot token so **DO NOT** show it to anyone!
Inside of this file add the following line, replacing the placeholder with your actual token:
```
TOKEN=bot-your-bot-token-here
```
### Logging in
The code below creates a `ValourClient`, loads the token from your `.env` file, and logs in as your bot.
```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);
```
### Running the bot
Make sure you are inside the root directory of the app and run:
```bash
dotnet run
```
You should then see `Logged in as botname (ID: botid)` in the console.
---
## [The Next step is to Add the bot to a Planet.](2.JoinPlanet.md)