Hey there, fellow dev! Ready to dive into the world of Discord bots? You're in for a treat. We'll be using Discord.Net, a powerful library that makes bot creation a breeze. Before we jump in, make sure you've got Visual Studio installed, a basic grasp of C#, and a Discord account. Let's get this party started!
First things first, fire up Visual Studio and create a new C# console application. Name it something cool - maybe "AwesomeDiscordBot"? Once that's done, it's time to bring in the big guns. Head over to the NuGet package manager and search for Discord.Net. Install it, and boom! You're locked and loaded.
Now for the fun part - creating your bot's digital identity:
Time to breathe life into your bot. Add this to your Program.cs
:
using Discord; using Discord.WebSocket; class Program { private DiscordSocketClient _client; static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult(); public async Task MainAsync() { _client = new DiscordSocketClient(); _client.Log += Log; await _client.LoginAsync(TokenType.Bot, "YOUR_BOT_TOKEN_HERE"); await _client.StartAsync(); // Block this task until the program is closed. await Task.Delay(-1); } private Task Log(LogMessage msg) { Console.WriteLine(msg.ToString()); return Task.CompletedTask; } }
Replace "YOUR_BOT_TOKEN_HERE" with your actual bot token. Run this, and voila! Your bot is online.
Let's make your bot do something cool. We'll add a simple command handler:
_client.MessageReceived += HandleCommandAsync; private async Task HandleCommandAsync(SocketMessage messageParam) { var message = messageParam as SocketUserMessage; if (message == null) return; int argPos = 0; if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos)) || message.Author.IsBot) return; if (message.Content.ToLower().StartsWith("!hello")) { await message.Channel.SendMessageAsync("Hello there!"); } }
Now your bot will respond with "Hello there!" when someone types "!hello". Pretty neat, huh?
Want to level up? Let's add a slash command:
_client.Ready += Client_Ready; private async Task Client_Ready() { var globalCommand = new SlashCommandBuilder() .WithName("greet") .WithDescription("Sends a greeting"); try { await _client.CreateGlobalApplicationCommandAsync(globalCommand.Build()); } catch (HttpException exception) { var json = JsonConvert.SerializeObject(exception.Errors, Formatting.Indented); Console.WriteLine(json); } } _client.SlashCommandExecuted += SlashCommandHandler; private async Task SlashCommandHandler(SocketSlashCommand command) { if (command.Data.Name == "greet") { await command.RespondAsync($"Greetings, {command.User.Username}!"); } }
Ready to unleash your creation? You've got options:
Remember, to keep your bot online 24/7, you'll need to host it on a machine that's always running.
As your bot grows, keep these tips in mind:
Stuck? Don't sweat it. Common hiccups include:
You've done it! You've created a Discord bot from scratch. But why stop here? The Discord.Net library has tons more features to explore. How about adding some games, moderation tools, or even AI integration?
Remember, the best way to learn is by doing. So keep coding, keep experimenting, and most importantly, have fun! Happy botting!