Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve getting started guide #61

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions Documentation/guides/getting-started/coding.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
# Coding!
# Setting Up Your First C# Discord Bot with NetCord

> [!Note]
> This section assumes that you have a project with NetCord installed and you have a bot created. If not, go back!
> [!NOTE]
> This guide assumes you have already created a project with NetCord installed and a bot set up in the [Discord Developer Portal](https://discord.com/developers/applications). If you haven't, please return to the previous guides for setup instructions.

Before we start, you need a token of your bot... so you need to go to the [Discord Developer Portal](https://discord.com/developers/applications) and get it.

- Go to `Bot` section and click `Reset Token`.
![Go to 'Bot' section and click 'Reset Token'](../../images/coding_Token_1.png)

- Then click `Copy` to copy the token to the clipboard.
![Click 'Copy' to copy the token to the clipboard](../../images/coding_Token_2.png)

> [!IMPORTANT]
> You should never give your token to anybody.
This guide will walk you through setting up your first C# Discord bot using NetCord. It will show you how to create a simple bot that connects to Discord. Nothing too fancy, just the basics to get you started. Let's dive in!

## [Generic Host](#tab/generic-host)

> [!NOTE]
> The generic host way requires the following packages:
> - [Microsoft.Extensions.Hosting](https://www.nuget.org/packages/Microsoft.Extensions.Hosting)
> - [NetCord.Hosting](https://www.nuget.org/packages/NetCord.Hosting)

With the generic host, you can just use @NetCord.Hosting.Gateway.GatewayClientServiceCollectionExtensions.AddDiscordGateway(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add your bot to the host. Quite easy, right?
[!code-cs[Program.cs](CodingHosting/Program.cs)]

Also note that the token needs to be stored in the configuration. You can for example use `appsettings.json` file. It should look like this:
Also note that the token should be stored in the configuration. You can for example use `appsettings.json` file. It should look like this:
[!code-json[appsettings.json](CodingHosting/appsettings.json)]

Now, when you run the code, your bot should be online!
![Bot being online](../../images/coding_BotOnline.png)

## [Bare Bones](#tab/bare-bones)

Add the following lines to file `Program.cs`.
Add the following lines to the `Program.cs` file to create the @NetCord.Gateway.GatewayClient.
[!code-cs[Program.cs](Coding/Program.cs#L1-L4)]

Logging is important, because of it you know what is your bot doing. To add it, add the following lines to your code.
Then add logging, to know what your bot is doing.
[!code-cs[Program.cs](Coding/Program.cs#L6-L10)]

Now it's time to finally... make the bot online! To do it, add the following lines to your code.
[!code-cs[Program.cs](Coding/Program.cs#L12-L13)]

Now, when you run the code, your bot should be online!
![Bot being online](../../images/coding_BotOnline.png)

### The Final Product
[!code-cs[Program.cs](Coding/Program.cs)]

***

Now, when you run the code, your bot should be online!
![Bot being online](../../images/coding_BotOnline.png)
15 changes: 0 additions & 15 deletions Documentation/guides/getting-started/creating-a-first-bot.md

This file was deleted.

15 changes: 14 additions & 1 deletion Documentation/guides/getting-started/installation.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# Installing NetCord

This guide will walk you through the process of installing NetCord packages using various tools like the .NET CLI, Visual Studio and JetBrains Rider.

> [!NOTE]
> NetCord requires [.NET 8](https://dotnet.microsoft.com/download/dotnet/8.0) or higher. Older versions are not supported.

## [.NET CLI](#tab/dotnet-cli)

To install the NetCord package using the .NET CLI, run the following command:
```bash
dotnet add package NetCord --prerelease
```

## [Visual Studio](#tab/visual-studio)

To install the NetCord package using the Visual Studio UI, follow these steps:

1. Create a new project.

2. Click `Manage NuGet Packages`.
Expand All @@ -20,6 +31,8 @@

## [JetBrains Rider](#tab/rider)

To install the NetCord package using JetBrains Rider UI, follow these steps:

1. Create a new project.

2. Click `Manage NuGet Packages`.
Expand All @@ -35,7 +48,7 @@

## [Other](#tab/other)

Follow instructions on [NuGet](https://www.nuget.org/packages/NetCord).
To install the NetCord package using other tools, see the instructions on [NuGet](https://www.nuget.org/packages/NetCord).

***

Expand Down
44 changes: 44 additions & 0 deletions Documentation/guides/getting-started/prerequisites.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Prerequisites for Writing a Discord Bot

Before you can start writing your awesome Discord bot in C#, there are a few key steps to follow, such as creating a bot, adding it to a server, and retrieving its token. This guide will walk you through each of these steps.

To begin, navigate to the [Discord Developer Portal](https://discord.com/developers/applications).

## 1. Creating a Discord Bot

1. In the **Applications** section, click on **New Application**.
![Click 'New Application' in the 'Applications' section](../../images/prerequisites_CreateApplication_1.png)

2. Fill in the name of your bot, check the checkbox, and then click **Create**.
![Fill in the name, check the box, and click 'Create'](../../images/prerequisites_CreateApplication_2.png)

That's it! You have successfully created your Discord bot.

## 2. Adding Your Discord Bot to a Server

Now that your bot is created, let's add it to a server:

1. In your bot's settings, go to the **OAuth2** section and generate an invite link by selecting the **bot** scope. Then copy the generated link and paste it into your browser.
![Generate a bot invite link in 'OAuth2 > URL Generator' by selecting 'bot' scope](../../images/prerequisites_AddBotToServer_1.png)

2. Select the server you want to add your bot to, and click **Authorize**.
![Select the server and click 'Authorise'](../../images/prerequisites_AddBotToServer_2.png)

Done! Your bot is now part of the server.

## 3. Getting Your Discord Bot Token

> [!IMPORTANT]
> Never share your bot token with anyone! It grants full control over your bot.

The bot token is crucial for connecting your C# Discord bot with the Discord API. Follow these steps to get the token:

1. Go to the **Bot** section in your application's settings and click **Reset Token**.
![Go to 'Bot' section and click 'Reset Token'](../../images/prerequisites_Token_1.png)

2. After resetting, click **Copy** to copy the token to your clipboard.
![Click 'Copy' to copy the bot token to the clipboard](../../images/prerequisites_Token_2.png)

***

Now you have everything you need to start coding your Discord bot in C#. Let's get started!
6 changes: 3 additions & 3 deletions Documentation/guides/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
items:
- name: Installation
href: getting-started/installation.md
- name: Creating a First Bot
href: getting-started/creating-a-first-bot.md
- name: Coding Time
- name: Prerequisites
href: getting-started/prerequisites.md
- name: Setting Up Your Discord Bot
href: getting-started/coding.md
- name: Working with Events
items:
Expand Down
Binary file removed Documentation/image_sources/coding_Token_1.pdn
Binary file not shown.
Binary file removed Documentation/image_sources/coding_Token_2.pdn
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed Documentation/images/coding_Token_1.png
Binary file not shown.
Binary file removed Documentation/images/coding_Token_2.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Documentation/images/prerequisites_Token_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Documentation/images/prerequisites_Token_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.