Viewing entries in
Raspberry Pi

C# Discord Bot: Logging All The Things

C# Discord Bot: Logging All The Things

Welcome

No matter what you're using your bot for, more likely than not you'll want to know what the heck is happening at some point. Luckily, we can add a logging service to get this done.

In this post I will be going over how to use Serilog, along with Microsoft’s logging framework/interface to wrap it up for dependency injection. The logging done will be to the console, as well as to a file.

The starting point I will be using is from the prerequisites portion, if you want to follow along and build it out as you go.

Prerequisites

  • .NET Core 2.x

  • A fundamental understanding of the Discord.Net library, or following along step-by-step with (if you’re working on a local bot, just omit the Raspberry Pi steps!):

Adding Required Packages

The first thing we will want to do is add the packages we need to take care of logging.

Here is the list of packages we will need:

Microsoft.Extensions.Logging.Debug
Microsoft.Extensions.Logging.Console
Microsoft.Extensions.Logging
Serilog.Sinks.File
Serilog.Sinks.Console
Serilog.AspNetCore

We can use the dotnet add package command to get them added.

dotnet add package Microsoft.Extensions.Logging.Debug
dotnet add package Microsoft.Extensions.Logging.Console
dotnet add package Microsoft.Extensions.Logging
dotnet add package Serilog.Sinks.File
dotnet add package Serilog.Sinks.Console
dotnet add package Serilog.AspNetCore

**Note**
Make sure you are in the csharpi project’s or your own project’s root folder when doing this (the one with the .csproj file in it).

add packages.png

Now that we have the required packages added, we can move on to modifying Program.cs to add some logging goodness!

Modifying Program.cs

The next step will be to modify Program.cs to setup and use logging.
We will be:

  • Adding using statements to bring in the namespaces we need

  • Setting up the Serilog logger with a few options

  • Adding logging to the services provided by dependency injection

  • Invoke the logger

Using Statements

We will need to add the following using statements to Program.cs:

1
2
using Serilog;
using Microsoft.Extensions.Logging;

Modifying the main method to add logging options

Now we will want to add some code to the main method to configure the logger.

  • We will null out the args parameter so we can better take input

  • An if statement will be added to see if an argument was passed to set the logging level

  • The Serilog logger will be created with the options we specify

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
static void Main(string[] args = null)
{
    if (args.Count() != 0)
    {
        _logLevel = args[0];
    } 
    Log.Logger = new LoggerConfiguration()
         .WriteTo.File("logs/csharpi.log", rollingInterval: RollingInterval.Day)
         .WriteTo.Console()
         .CreateLogger();

    new Program().MainAsync().GetAwaiter().GetResult();
}

A note on Log.logger

The Log.Logger portion of the code is what sets up the logging configuration.
In the above example the following options are configured:

  • Write out to the console and to a file

  • The file will be stored in the logs directory (from the project root), and will create a new file named csharpi.log (feel free to change the directory/file name to suite your needs)

  • The files will roll every day, and have each day’s date as a timestamp (Serilog takes care of this magic)

Adding the logger to our service provider / dependency injection

Now we’ll want to change our ConfigureServices method to add logging.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
private ServiceProvider ConfigureServices()
{
    // this returns a ServiceProvider that is used later to call for those services
    // we can add types we have access to here, hence adding the new using statement:
    // using csharpi.Services;
    // the config we build is also added, which comes in handy for setting the command prefix!
    var services = new ServiceCollection()
        .AddSingleton(_config)
        .AddSingleton<DiscordSocketClient>()
        .AddSingleton<CommandService>()
        .AddSingleton<CommandHandler>()
        .AddSingleton<LoggingService>()
        .AddLogging(configure => configure.AddSerilog());

    if (!string.IsNullOrEmpty(_logLevel)) 
    {
        switch (_logLevel.ToLower())
        {
            case "info":
            {
                services.Configure<LoggerFilterOptions>(options => options.MinLevel = LogLevel.Information);
                break;
            }
            case "error":
            {
                services.Configure<LoggerFilterOptions>(options => options.MinLevel = LogLevel.Error);
                break;
            } 
            case "debug":
            {
                services.Configure<LoggerFilterOptions>(options => options.MinLevel = LogLevel.Debug);
                break;
            } 
            default: 
            {
                services.Configure<LoggerFilterOptions>(options => options.MinLevel = LogLevel.Error);
                break;
            }
        }
    }
    else
    {
        services.Configure<LoggerFilterOptions>(options => options.MinLevel = LogLevel.Information);
    }

    var serviceProvider = services.BuildServiceProvider();
    return serviceProvider;
}

The above method will build out the service provider, and handle some logic in regards to the logging level.

MainAsync method modification

We can now clean up the MainAsync method a bit, here are the new contents (note that the logging hooks are removed, and a simple service retrieval takes its place):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public async Task MainAsync()
{
    // call ConfigureServices to create the ServiceCollection/Provider for passing around the services
    using (var services = ConfigureServices())
    {
        // get the client and assign to client 
        // you get the services via GetRequiredService<T>
        var client = services.GetRequiredService<DiscordSocketClient>();
        _client = client;

        // setup logging and the ready event
        services.GetRequiredService<LoggingService>();

        // this is where we get the Token value from the configuration file, and start the bot
        await client.LoginAsync(TokenType.Bot, _config["Token"]);
        await client.StartAsync();

        // we get the CommandHandler class here and call the InitializeAsync method to start things up for the CommandHandler service
        await services.GetRequiredService<CommandHandler>().InitializeAsync();

        await Task.Delay(-1);
    }
}

You can always see the most updated version of this example’s Program.cs file, here: https://github.com/gngrninja/csharpi/blob/03-logging/Program.cs.

**Note**
There is a little chicken and egg here, where we are adding the LoggingService. We will be building that out next!

Adding the LoggingService

The logging service is where we will move the old logging hooks and methods to. This will allow us to remove the following from Program.cs, if it is there:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
client.Log += LogAsync;
client.Ready += ReadyAsync;
services.GetRequiredService<CommandService>().Log += LogAsync;
private Task LogAsync(LogMessage log)
{
    Console.WriteLine(log.ToString());
    return Task.CompletedTask;
}

private Task ReadyAsync()
{
    Console.WriteLine($"Connected as -> [] :)");
    return Task.CompletedTask;
}

To create the logging service, perform the following steps:

1. Create LoggingService.cs in the Services/ folder

2. Place the following content in the file (to see the most updated code, go here: https://github.com/gngrninja/csharpi/blob/03-logging/Services/LoggingService.cs). Be sure to change the namespace to yourprojectname.Services, if needed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace csharpi.Services
{
    public class LoggingService
    {

        // declare the fields used later in this class
        private readonly ILogger _logger;
        private readonly DiscordSocketClient _discord;
        private readonly CommandService _commands;

        public LoggingService(IServiceProvider services)
        { 
            // get the services we need via DI, and assign the fields declared above to them
            _discord = services.GetRequiredService<DiscordSocketClient>(); 
            _commands = services.GetRequiredService<CommandService>();
            _logger = services.GetRequiredService<ILogger<LoggingService>>();

            // hook into these events with the methods provided below
            _discord.Ready += OnReadyAsync;
            _discord.Log += OnLogAsync;
            _commands.Log += OnLogAsync;
        }

        // this method executes on the bot being connected/ready
        public Task OnReadyAsync()
        {
            _logger.LogInformation($"Connected as -> [{_discord.CurrentUser}] :)");
            _logger.LogInformation($"We are on [{_discord.Guilds.Count}] servers");
            return Task.CompletedTask;
        }

        // this method switches out the severity level from Discord.Net's API, and logs appropriately
        public Task OnLogAsync(LogMessage msg)
        { 
            string logText = $"{msg.Source}: {msg.Exception?.ToString() ?? msg.Message}";
            switch (msg.Severity.ToString())
            {
                case "Critical":
                {
                    _logger.LogCritical(logText);
                    break;
                }
                case "Warning":
                {
                    _logger.LogWarning(logText);
                    break;
                }
                case "Info":
                {
                    _logger.LogInformation(logText);
                    break;
                }
                case "Verbose":
                {
                    _logger.LogInformation(logText);
                    break;
                } 
                case "Debug":
                {
                    _logger.LogDebug(logText);
                    break;
                } 
                case "Error":
                {
                    _logger.LogError(logText);
                    break;
                } 
            }

            return Task.CompletedTask; 

        }
    }
}

What LoggingService does

  • The fields in this class are _logger, _discord, and _commands

  • The constructor is what handles the assignment of those fields, and when using Microsoft’s logging interface, we want to assign the _logger field as as ILogger<TypeName>, so in this case ILogger<LoggingService>.

    • This is a nice way to do it, because it abstracts the logging away from implemented framework, which in this case is Serilog. You could change that to a different framework, and this code would stay the same

    • After assigning the fields we hook into the OnReadyAsync and OnLogAsync events, and assign them to the appropriate methods

Now let’s move on the adding logging to an existing service, CommandHandler!

Adding Logging to CommandHandler

This next part here will demonstrate how to add logging to an existing class via dependency injection.

To add logging to the CommandHandler service, we will need to add the following using statement:

using Microsoft.Extensions.Logging;

We’ll then want to create the following field:

private readonly Microsoft.Extensions.Logging.ILogger _logger;

In the constructor, we can assign the logger as we did in the LoggingService, using the type name of this class:

_logger = services.GetRequiredService<ILogger<CommandHandler>>();

Now we can start using _logger, as such:

_logger.LogError($"Command failed to execute for [{context.User.Username}] <-> [{result.ErrorReason}]!");

Much better than strictly using System.Console.WriteLine!

Here is the full code for the updated CommandHandler.cs file (to see the most updated code, go here https://github.com/gngrninja/csharpi/blob/03-logging/Services/CommandHandler.cs):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace csharpi.Services
{
    public class CommandHandler
    {
        // setup fields to be set later in the constructor
        private readonly IConfiguration _config;
        private readonly CommandService _commands;
        private readonly DiscordSocketClient _client;
        private readonly IServiceProvider _services;
        private readonly Microsoft.Extensions.Logging.ILogger _logger;

        public CommandHandler(IServiceProvider services)
        {
            // juice up the fields with these services
            // since we passed the services in, we can use GetRequiredService to pass them into the fields set earlier
            _config = services.GetRequiredService<IConfiguration>();
            _commands = services.GetRequiredService<CommandService>();
            _client = services.GetRequiredService<DiscordSocketClient>();
            _logger = services.GetRequiredService<ILogger<CommandHandler>>();
            _services = services;
            
            // take action when we execute a command
            _commands.CommandExecuted += CommandExecutedAsync;

            // take action when we receive a message (so we can process it, and see if it is a valid command)
            _client.MessageReceived += MessageReceivedAsync;
        }

        public async Task InitializeAsync()
        {
            // register modules that are public and inherit ModuleBase<T>.
            await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
        }

        // this class is where the magic starts, and takes actions upon receiving messages
        public async Task MessageReceivedAsync(SocketMessage rawMessage)
        {
            // ensures we don't process system/other bot messages
            if (!(rawMessage is SocketUserMessage message)) 
            {
                return;
            }
            
            if (message.Source != MessageSource.User) 
            {
                return;
            }

            // sets the argument position away from the prefix we set
            var argPos = 0;

            // get prefix from the configuration file
            char prefix = Char.Parse(_config["Prefix"]);

            // determine if the message has a valid prefix, and adjust argPos based on prefix
            if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) || message.HasCharPrefix(prefix, ref argPos))) 
            {
                return;
            }
           
            var context = new SocketCommandContext(_client, message);

            // execute command if one is found that matches
            await _commands.ExecuteAsync(context, argPos, _services); 
        }

        public async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandContext context, IResult result)
        {
            // if a command isn't found, log that info to console and exit this method
            if (!command.IsSpecified)
            {
                _logger.LogError($"Command failed to execute for [{context.User.Username}] <-> [{result.ErrorReason}]!");
                return;
            }
                

            // log success to the console and exit this method
            if (result.IsSuccess)
            {
                _logger.LogInformation($"Command [{command.Value.Name}] executed for [{context.User.Username}] on [{context.Guild.Name}]");
                return;
            }
            
            // failure scenario, let's let the user know
            await context.Channel.SendMessageAsync($"Sorry, {context.User.Username}... something went wrong -> [{result}]!");
        }        
    }
}

Testing it all out

Now its time for the fun part, seeing if it all works!

You can either debug it in VS Code (F5), or from a terminal (ensure you are in the project’s folder) and use:

dotnet run
Screen Shot 2019-07-21 at 12.32.51 PM.png

The logging to the console looks good! On the left it shows the log level next to the timestamp, in this case INF means info. Now let’s look in the logs folder for a log file and see its contents.

Screen Shot 2019-07-21 at 12.36.49 PM.png

Awesome! Since we hooked into the CommandHandler, and use the logger now, let’s test a command and see what happens.

hello.png
hello_logged.png

Looks good, logged as expected. Now let’s check something that would log as an error (in this case an unknown command):

not_cmd_error.png
cmd_error.png

Changing the log level

The log level can be changed by running the bot with an argument (that is the log level). To change the log level to error (thus not logging anything under the error level such as INF/Info, use:

dotnet run error

error.png

Notice we don’t have the information messages anymore. However, if we used an unknown command, it should show an error:

not_cmd_error.png
just_error.png

Conclusion

And that’s that. Now we have some logging in place for use with our Discord Bot!

Be sure to add the /logs folder to your .gitignore file, as you likely don’t want those uploaded to GitHub.

Feel free to change things around and see what you can make happen (or break and fix… break + fix = learn, right?).

In the next part of this series, we will add a database to our project.


If you have any questions or comments, leave them below!

[top]


C# Discord Bot: Command Handling

C# Discord Bot: Command Handling

This post will go over command handling with Discord.Net. Whether you are following along from the Raspberry Pi series, or are just curious about how to do command handling with Discord.Net, welcome!

Prerequisites

  • .NET Core 2.x

  • …and we will be building upon the framework laid out here:

If you want to follow along with the finished code, go here: https://github.com/gngrninja/csharpi/tree/02-command-basics.

If you would like the starter code, and want to try building off of it yourself, go here: https://github.com/gngrninja/csharpi/tree/intro.

Adding the Command Handler

The first thing we’ll want to do is add the command handling service.

This service will be responsible for:

  • Hooking into the MessageReceivedAsync and CommandExecutedAsync events to process messages as they come in (to see if they are a valid command), and handle command execution (success/failure/not found actions)

  • Utilizing Dependency Injection in .NET Core to setup and pass through services/configurations

  • Loading all the command modules that inherit from ModuleBase

Command Handler Service Creation

The first thing we want to do here is create the command handling service.

  1. Create a folder named Services, and under that folder a file named CommandHandler.cs.

services_folder.png
CommandHandler.png

2. Here is the code for the command handling service, with comments to help understand what is happening (don’t worry too much about not understanding what is happening, yet!):

You can always view the most updated code, here: https://github.com/gngrninja/csharpi/blob/02-command-basics/Services/CommandHandler.cs.

using System;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;

namespace csharpi.Services
{
    public class CommandHandler
    {
        // setup fields to be set later in the constructor
        private readonly IConfiguration _config;
        private readonly CommandService _commands;
        private readonly DiscordSocketClient _client;
        private readonly IServiceProvider _services;

        public CommandHandler(IServiceProvider services)
        {
            // juice up the fields with these services
            // since we passed the services in, we can use GetRequiredService to pass them into the fields set earlier
            _config = services.GetRequiredService<IConfiguration>();
            _commands = services.GetRequiredService<CommandService>();
            _client = services.GetRequiredService<DiscordSocketClient>();
            _services = services;
            
            // take action when we execute a command
            _commands.CommandExecuted += CommandExecutedAsync;

            // take action when we receive a message (so we can process it, and see if it is a valid command)
            _client.MessageReceived += MessageReceivedAsync;
        }

        public async Task InitializeAsync()
        {
            // register modules that are public and inherit ModuleBase<T>.
            await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
        }

        // this class is where the magic starts, and takes actions upon receiving messages
        public async Task MessageReceivedAsync(SocketMessage rawMessage)
        {
            // ensures we don't process system/other bot messages
            if (!(rawMessage is SocketUserMessage message)) 
            {
                return;
            }
            
            if (message.Source != MessageSource.User) 
            {
                return;
            }

            // sets the argument position away from the prefix we set
            var argPos = 0;

            // get prefix from the configuration file
            char prefix = Char.Parse(_config["Prefix"]);

            // determine if the message has a valid prefix, and adjust argPos based on prefix
            if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) || message.HasCharPrefix(prefix, ref argPos))) 
            {
                return;
            }
           
            var context = new SocketCommandContext(_client, message);

            // execute command if one is found that matches
            await _commands.ExecuteAsync(context, argPos, _services); 
        }

        public async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandContext context, IResult result)
        {
            // if a command isn't found, log that info to console and exit this method
            if (!command.IsSpecified)
            {
                System.Console.WriteLine($"Command failed to execute for [] <-> []!");
                return;
            }
                

            // log success to the console and exit this method
            if (result.IsSuccess)
            {
                System.Console.WriteLine($"Command [] executed for -> []");
                return;
            }
                

            // failure scenario, let's let the user know
            await context.Channel.SendMessageAsync($"Sorry, ... something went wrong -> []!");
        }        
    }
}

[top]

Adding Command Prefix to config.json

The above code will parse out the command prefix from the configuration file config.json.

Remember :

  • When debugging you want the config.json file in /bin/Debug/netcoreapp2.x/

  • When running the bot normally via dotnet projectname.dll, you want the config.json file in the root folder (same folder as the .dll/executable)

To add the prefix, your config.json should look like this:

{
    "Token":  "ThIsIsNtMyToKeN",
    "Prefix": ";"
}

You can replace the “;” with whatever one-character prefix you’d like to use.

Now what we have created our CommandHandler service, let’s wire up Program.cs to enable dependency injection and use it.

[top]

Adding Dependency Injection

Next we’ll want to modify Program.cs to add dependency injection.

What we need to do in Program.cs:

  • Add a using statement to gain access to the service we created
    - For this example that’s using chsarpi.Services;

  • Create a method that will construct the dependency injection model / ServicesProvider for the model we can consume later (ConfigureServices)

  • Call upon and use the ConfigureServices method, and work with the bot code via dependency injection

Below is the sample code to achieve what I’ve gone over above:

You can always view the most updated code, here: https://github.com/gngrninja/csharpi/blob/02-command-basics/Program.cs

using System;
using Discord;
using Discord.Net;
using Discord.Commands;
using Discord.WebSocket;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.DependencyInjection;
using csharpi.Services;

namespace csharpi
{
    class Program
    {
        // setup our fields we assign later
        private readonly IConfiguration _config;
        private DiscordSocketClient _client;

        static void Main(string[] args)
        {
            new Program().MainAsync().GetAwaiter().GetResult();
        }

        public Program()
        {
            // create the configuration
            var _builder = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory)
                .AddJsonFile(path: "config.json");  

            // build the configuration and assign to _config          
            _config = _builder.Build();
        }

        public async Task MainAsync()
        {
            // call ConfigureServices to create the ServiceCollection/Provider for passing around the services
            using (var services = ConfigureServices())
            {
                // get the client and assign to client 
                // you get the services via GetRequiredService<T>
                var client = services.GetRequiredService<DiscordSocketClient>();
                _client = client;

                // setup logging and the ready event
                client.Log += LogAsync;
                client.Ready += ReadyAsync;
                services.GetRequiredService<CommandService>().Log += LogAsync;

                // this is where we get the Token value from the configuration file, and start the bot
                await client.LoginAsync(TokenType.Bot, _config["Token"]);
                await client.StartAsync();

                // we get the CommandHandler class here and call the InitializeAsync method to start things up for the CommandHandler service
                await services.GetRequiredService<CommandHandler>().InitializeAsync();

                await Task.Delay(-1);
            }
        }

        private Task LogAsync(LogMessage log)
        {
            Console.WriteLine(log.ToString());
            return Task.CompletedTask;
        }

        private Task ReadyAsync()
        {
            Console.WriteLine($"Connected as -> [] :)");
            return Task.CompletedTask;
        }

        // this method handles the ServiceCollection creation/configuration, and builds out the service provider we can call on later
        private ServiceProvider ConfigureServices()
        {
            // this returns a ServiceProvider that is used later to call for those services
            // we can add types we have access to here, hence adding the new using statement:
            // using csharpi.Services;
            // the config we build is also added, which comes in handy for setting the command prefix!
            return new ServiceCollection()
                .AddSingleton(_config)
                .AddSingleton<DiscordSocketClient>()
                .AddSingleton<CommandService>()
                .AddSingleton<CommandHandler>()
                .BuildServiceProvider();
        }
    }
}

Now that we’re wired up to use dependency injection, and have our service created to handle commands, let’s create our first set of commands (the right way!).

[top]

Writing Robust Commands

In this section we will be writing our first real, robust commands. Writing commands this way gives us access to Discord.Net’s command writing goodness.

1. Create a folder named Modules, and under that folder a file named ExampleCommands.cs.

module_folder.png
example_commands_file.png

ExampleCommands will contain our first commands using this framework!

Below I have some code that will get us started with two commands:

You can always find the most up to date code, here: https://github.com/gngrninja/csharpi/blob/02-command-basics/Modules/ExampleCommands.cs

using Discord;
using Discord.Net;
using Discord.WebSocket;
using Discord.Commands;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;

namespace csharpi.Modules
{
    // for commands to be available, and have the Context passed to them, we must inherit ModuleBase
    public class ExampleCommands : ModuleBase
    {
        [Command("hello")]
        public async Task HelloCommand()
        {
            // initialize empty string builder for reply
            var sb = new StringBuilder();

            // get user info from the Context
            var user = Context.User;
            
            // build out the reply
            sb.AppendLine($"You are -> []");
            sb.AppendLine("I must now say, World!");

            // send simple string reply
            await ReplyAsync(sb.ToString());
        }

        [Command("8ball")]
        [Alias("ask")]
        [RequireUserPermission(GuildPermission.KickMembers)]
        public async Task AskEightBall([Remainder]string args = null)
        {
            // I like using StringBuilder to build out the reply
            var sb = new StringBuilder();
            // let's use an embed for this one!
            var embed = new EmbedBuilder();

            // now to create a list of possible replies
            var replies = new List<string>();

            // add our possible replies
            replies.Add("yes");
            replies.Add("no");
            replies.Add("maybe");
            replies.Add("hazzzzy....");

            // time to add some options to the embed (like color and title)
            embed.WithColor(new Color(0, 255,0));
            embed.Title = "Welcome to the 8-ball!";
            
            // we can get lots of information from the Context that is passed into the commands
            // here I'm setting up the preface with the user's name and a comma
            sb.AppendLine($",");
            sb.AppendLine();

            // let's make sure the supplied question isn't null 
            if (args == null)
            {
                // if no question is asked (args are null), reply with the below text
                sb.AppendLine("Sorry, can't answer a question you didn't ask!");
            }
            else 
            {
                // if we have a question, let's give an answer!
                // get a random number to index our list with (arrays start at zero so we subtract 1 from the count)
                var answer = replies[new Random().Next(replies.Count - 1)];
                
                // build out our reply with the handy StringBuilder
                sb.AppendLine($"You asked: [****]...");
                sb.AppendLine();
                sb.AppendLine($"...your answer is [****]");

                // bonus - let's switch out the reply and change the color based on it
                switch (answer) 
                {
                    case "yes":
                    {
                        embed.WithColor(new Color(0, 255, 0));
                        break;
                    }
                    case "no":
                    {
                        embed.WithColor(new Color(255, 0, 0));
                        break;
                    }
                    case "maybe":
                    {
                        embed.WithColor(new Color(255,255,0));
                        break;
                    }
                    case "hazzzzy....":
                    {
                        embed.WithColor(new Color(255,0,255));
                        break;
                    }
                }
            }

            // now we can assign the description of the embed to the contents of the StringBuilder we created
            embed.Description = sb.ToString();

            // this will reply with the embed
            await ReplyAsync(null, false, embed.Build());
        }
    }
}

That’s it! Now we’re ready to debug/test to see if it is all working!

Remember it is F5 in VS Code, or you can use [Debug] -> [Start Debugging] from the menu bar.

Hello Command

This command is a simple example of command execution. The command prefix I chose is “;”, and I’m debugging, so let’s see what happens in the server:

hello_command.png
hello_executed.png

Behind the scenes

The first thing we do when creating commands in a module file is to add the [Command] attribute. For the above command the syntax is:

[Command("hello")]

The text in quotes is what the command name is and what the user will use to trigger the method.
There are other attributes that can be added, more on those in the 8-ball example.

Now the method that performs the action can be declared:

        public async Task HelloCommand()
        {
            // initialize empty string builder for reply
            var sb = new StringBuilder();

            // get user info from the Context
            var user = Context.User;
            
            // build out the reply
            sb.AppendLine($"You are -> []");
            sb.AppendLine("I must now say, World!");

            // send simple string reply
            await ReplyAsync(sb.ToString());
        }

The code above is what executes when the bot code finds a match for the “hello” command.

8-Ball Command

Now let’s move on to the 8-ball command. I’m debugging and ready to ask the 8-ball a question…

willthiswork.png
executed.png

Notice that with this command I was able to use ask, and not 8ball to trigger it. 8ball would also work, but with the attribute [Alias] we’re able to add an alias for the command. Here are the attributes we have setup for the 8ball command:

        [Command("8ball")]
        [Alias("ask")]
        [RequireUserPermission(GuildPermission.KickMembers)]

Hmm what’s this one all about? ([RequireUserPermission(GuildPermission.KickMembers)]). Let’s try to use the 8ball as a normal user on the server that cannot kick people:

needkick.png

The command framework allows you to specify the minimum permission needed to use the command. To remove that requirement, simply remove the attribute decorator:

[RequireUserPermission(GuildPermission.KickMembers)]

Behind the scenes

In the 8ball command we get a little more advanced by:

  • Requiring a minimum permission level

  • Adding a command alias

  • Replying with an embed

        public async Task AskEightBall([Remainder]string args = null)
        {
            // I like using StringBuilder to build out the reply
            var sb = new StringBuilder();
            // let's use an embed for this one!
            var embed = new EmbedBuilder();

            // now to create a list of possible replies
            var replies = new List<string>();

            // add our possible replies
            replies.Add("yes");
            replies.Add("no");
            replies.Add("maybe");
            replies.Add("hazzzzy....");

            // time to add some options to the embed (like color and title)
            embed.WithColor(new Color(0, 255,0));
            embed.Title = "Welcome to the 8-ball!";
            
            // we can get lots of information from the Context that is passed into the commands
            // here I'm setting up the preface with the user's name and a comma
            sb.AppendLine($",");
            sb.AppendLine();

            // let's make sure the supplied question isn't null 
            if (args == null)
            {
                // if no question is asked (args are null), reply with the below text
                sb.AppendLine("Sorry, can't answer a question you didn't ask!");
            }
            else 
            {
                // if we have a question, let's give an answer!
                // get a random number to index our list with (arrays start at zero so we subtract 1 from the count)
                var answer = replies[new Random().Next(replies.Count - 1)];
                
                // build out our reply with the handy StringBuilder
                sb.AppendLine($"You asked: [****]...");
                sb.AppendLine();
                sb.AppendLine($"...your answer is [****]");

                // bonus - let's switch out the reply and change the color based on it
                switch (answer) 
                {
                    case "yes":
                    {
                        embed.WithColor(new Color(0, 255, 0));
                        break;
                    }
                    case "no":
                    {
                        embed.WithColor(new Color(255, 0, 0));
                        break;
                    }
                    case "maybe":
                    {
                        embed.WithColor(new Color(255,255,0));
                        break;
                    }
                    case "hazzzzy....":
                    {
                        embed.WithColor(new Color(255,0,255));
                        break;
                    }
                }
            }

            // now we can assign the description of the embed to the contents of the StringBuilder we created
            embed.Description = sb.ToString();

            // this will reply with the embed
            await ReplyAsync(null, false, embed.Build());
        }

[top]

Getting it Working on the Pi

To get this working on our Raspberry Pi we will simply need to push the updated code to the Github repo, and pull it down to the Pi. We will need to update our config.json on the Pi, and copy it to the debug folder, as well as the bot folder (after we’ve added the Prefix line). If you’d like more information on getting the initial setup done with the Raspberry Pi, visit the below post and check out it’s prerequisites as well!

The below steps all assume we are sshed into our Pi.

ssh_in_pi.png

Switching Branches on Git

If you are following along, and want to use my Github repo as reference, you must ensure you’re working with the proper branch for this post.

First you’ll want to get into the directory:

cd csharpi

Then, you want to run:

git checkout 02-command-basics
git pull
You can see here we are in the intro branch, and we need to switch it up!

You can see here we are in the intro branch, and we need to switch it up!

checkpull.png

Pi config.json Editing

Now that we have the latest code on the Pi (refresher here if you’re using your own repo), let’s edit the config.json file to add the command prefix we want to use.

You’ll want to be in the cloned git repo’s directory, and assuming you have the base config created there from the linked post above, run (if you don’t, create it by using touch config.json):

nano config.json
nanotoeditconfig.png

In this file we want to ensure the Token and Prefix are there as such:

{
    "Token":  "ThIsIsNtMyToKeN",
    "Prefix": ";"
}
pi_config.png

Use the following sequence to save the contents:

CTRL+O
[Enter]
CTRL+X

Now let’s copy that to the debug folder so we can test/debug the code, and to the bot’s published location:

cp config.json bin/Debug/netcoreapp2.2/
cp config.json ~/bot
copyconfig.png

Let’s test the code from the Git repo…

Test Code From Repo

Now that we copied the new config.json file over to the Debug folder, we can test things out. Ensure you’re in the repo’s folder and run (remember, this method of running the bot takes a while to start):

dotnet run
piruns.png

Now to run a command in Discord just to be sure…

worksonpi.png
piexecuted.png

Success! Now to get it published and run it properly.

Running Published Code on Pi

Let’s get things published and running smoothly! To start out, ensure you are in the git repo’s folder and run:

dotnet publish -o ~/bot

Then you’ll want to get into the bot’s folder:

cd ~/bot
publishcd.png

And finally, you can run:

dotnet csharpi.dll
dotnet csharpi.png

Looks good, but one more test with Discord to really be sure!

8ballpub.png
pubrunning.png

Looks like we’re all set.

accuracy of 8-ball is not guaranteed

[top]

Conclusion

In this post we added some proper command handling to our Discord bot.
Feel free to change things around and see what you can make happen (or break and fix… break + fix = learn, right?).

In the next part of this series, we will add proper logging to the bot.
If you have any questions or comments, leave them below!

C# Discord Bot on Raspberry Pi: Simple Bot With Config File

C# Discord Bot on Raspberry Pi: Simple Bot With Config File

CSharPi: Simple Bot With Config File

RPi-Logo-Reg-SCREEN.png

Now let’s get a Discord bot working on our Pi!

Creating the Discord App

We will need a bot token to authenticate the bot and get things going.

1. Login to the developer portal for Discord, here: https://discordapp.com/developers/applications/.

2. Once logged in, click “New Application”, and give it a name.

newapp.png

3. Now click “Bot” on the menu to the left.

Click bot.png

4. Click “Add Bot”, and confirm.

Add Bot.png
botconfirm.png

5. Now you should see an option to reveal the bot’s token. We will need to get this information shortly.

token.png

6. Let’s invite the bot to a Discord server. You will need to be an administrator on the server you want to invite the bot to. To do this, go to the “General Information” option on the menu to the left, and copy the Client ID.

clientid.png

My bot’s client ID is 554549670953615380.

7. Go to https://discordapi.com/permissions.html#3198016, and input your client ID towards the bottom. I’ve added the basic permissions that will be good for this example bot. Feel free to add more if you’d like.

invitepage.png

8. The invite URL is the one you see towards the bottom. Go to that link in your browser. For my bot, it is: https://discordapp.com/oauth2/authorize?client_id=554549670953615380&scope=bot&permissions=3198016.

9. Confirm the server you are adding the bot to, and click “Authorize”.

confirmauth.png

10. You should now see an (offline) bot join your server.

joined.png

Token in Configuration File

Local Machine

From here on out we will be relying upon the groundwork we laid in this post:

If you haven’t done the setup in that post, but are an advanced user familiar with C# / .NET Core, carry on. Otherwise, I strongly recommend going through it.

1. Open the csharpi folder in Visual Studio Code on your local machine.

2. Create a new file in the root named config.json

createfile.png

3. Give it the following content:

    {
        "Token":  ""
    }

4. Move the file to the /bin/Debug/netcoreapp2.2 folder
(note) the only reason we are creating this copy is for testing, and when we debug the app it uses this folder as its root folder. We will be creating a separate config.json on the Pi in a moment.

movefile.png
movedfiled.png

5. Navigate back to https://discordapp.com/developers/applications/, and click the application we made earlier.

6. Click “Bot from the menu on the left, and then click “Reveal Token

bottokencopy.png

7. Copy the token and drop it inbetween the quotes in the config.json file.

droptokeninconffil.png

(note) it is very important you keep your token a secret, and regenerate it if you think anyone else knows it.

Raspberry Pi

1. SSH into your Raspberry Pi

2. Navigate to the published folder we created in part one:

        cd /home/pi/bot

3. Create the configuration file here:

        touch config.json

4. Copy the contents of the file from the one created on your local machine.

copycontents.png

5. Open the config.json file in /home/pi/bot on the Pi in nano:

        nano config.json
cdtouchnano.png

6. Once the editor is opened, paste in the contents.

pastenano.png

7. Use the following sequence to write the contents and exit the editor:

        CTRL+O
        [Enter]
        CTRL+X

8. Verify the file contents via the more command:

        more config.json
Screen Shot 2019-03-11 at 12.18.17 AM.png

Add Required Packages

Now it is time to add required packages to our project. We will need to add the Discord.Net package, and a couple Microsoft configuration packages to help us read and store the configuration file.

1. Open your favorite console / terminal, and navigate to the csharpi project folder on your local machine.

2. Run the following commands:

dotnet add package Discord.Net --version 2.0.1
dotnet add package Microsoft.Extensions.Configuration --version 3.0.0-preview3.19153.1   
dotnet add package Microsoft.Extensions.Configuration.Json --version 3.0.0-preview3.19153.1

Add Simple Bot Code

Now we’re going to replace the contents of Program.cs with the simple bot code. This code will be the basis for future posts to come.

1. Open the csharpi folder on your local machine in Visual Studio Code.

2. Replace the contents of Program.cs with the following:
(note) If you did not name your project csharpi, be mindful of the namespace declaration and update yours to match your project name!

using System;
using Discord;
using Discord.Net;
using Discord.Commands;
using Discord.WebSocket;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;

namespace csharpi
{
    class Program
    {
        private readonly DiscordSocketClient _client;
        private readonly IConfiguration _config;

        static void Main(string[] args)
        {
            new Program().MainAsync().GetAwaiter().GetResult();
        }

        public Program()
        {
            _client = new DiscordSocketClient();

            //Hook into log event and write it out to the console
            _client.Log += LogAsync;

            //Hook into the client ready event
            _client.Ready += ReadyAsync;

            //Hook into the message received event, this is how we handle the hello world example
            _client.MessageReceived += MessageReceivedAsync;

            //Create the configuration
            var _builder = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory)
                .AddJsonFile(path: "config.json");            
            _config = _builder.Build();
        }

        public async Task MainAsync()
        {
            //This is where we get the Token value from the configuration file
            await _client.LoginAsync(TokenType.Bot, _config["Token"]);
            await _client.StartAsync();

            // Block the program until it is closed.
            await Task.Delay(-1);
        }

        private Task LogAsync(LogMessage log)
        {
            Console.WriteLine(log.ToString());
            return Task.CompletedTask;
        }

        private Task ReadyAsync()
        {
            Console.WriteLine($"Connected as -> [{_client.CurrentUser}] :)");
            return Task.CompletedTask;
        }

        //I wonder if there's a better way to handle commands (spoiler: there is :))
        private async Task MessageReceivedAsync(SocketMessage message)
        {
            //This ensures we don't loop things by responding to ourselves (as the bot)
            if (message.Author.Id == _client.CurrentUser.Id)
                return;

            if (message.Content == ".hello")
            {
                await message.Channel.SendMessageAsync("world!");
            }  
        }
    }
}
programcseditor.png

3. Now that we have the new code for Program.cs, let’s test it by debugging (F5).

debugconnected.png

4. Now that we verified it is connected and debugging, try sending .hello to the discord server your bot is in, and see what happens (the bot should appear online now!)

hellofromlocal.png

Awesome! It works locally. Now let’s work on getting it to the Pi.

Commit New Code to Github

Let’s get our new code committed to Github!

1. Open up the csharpi folder on your local machine with VS Code.

2. The Git icon should show that 2 files have been modified. These are the .csproj file with the required packages we added, and the Program.cs file we modified.

gitmod.png

3. Since VS Code is Git-aware, let’s push these changes up to our Github repository.

4. Add a commit message and click the checkmark icon to stage/commit the files.

commitmessage.png

5. Now click the three dots “”, and select “Push”.

6. Verify the files have been modified by going to your Github repository’s page:

Update Code on Pi

Now let’s get the updated code on the Raspberry Pi, and publish the app again. After we do that, we can run the updated app and ensure it works as the Discord bot!

1. SSH into your Pi.

2. Navigate to the source code folder /home/pi/csharpi:

    cd /home/pi/csharpi

3. Run:

    git pull

(you should see that some changes have been made)

gitpull.png

4. Now let’s publish and run the application (since we already created config.json in /home/pi/bot earlier, it should pick up that file and have the token ready). The first command publishes the application, then we change directory to the published application, and run it.

dotnet publish -o /home/pi/bot
cd /home/pi/bot
dotnet csharpi.dll

(note) The restore/publishing process will take a while, but you only need to run it if your code has changed.

If all goes well, we should see our bot connected message, and we can test it out in the Discord server!

itworks.png
helloworldpi.png

And there you have it, a Discord bot running on a Raspberry Pi, with .NET Core and C#. The next parts of this series will go over proper command handling (hint: not how we did it here), and more!

C# Discord Bot Series

Next post:

If you have any feedback or questions, feel free to leave a comment below!

C# Discord Bot on Raspberry Pi: Setting Things Up

C# Discord Bot on Raspberry Pi: Setting Things Up

CSharPi: Getting Started

RPi-Logo-Reg-SCREEN.png

What if I said it was possible to run a Discord bot on a Raspberry Pi natively? With .NET Core we can do just that.

Getting Started

Let’s get some prerequisites out of the way. The first thing we’ll do is install the .NET Core SDK on our local machine, as well as our Raspberry Pi. Next, we’ll ensure Git is installed both places as well.

Finally, I will be using Visual Studio Code for this project, so I will go over installing that as well.

If you are familiar with all of these concepts, and have created Discord bots before, feel free to skip to the next part of the series:

Otherwise, carry on!

Installing .NET Core

Local Machine

Let’s start out by installing the .NET Core SDK on our local machine. You’ll want to start here, and follow specific instructions for your OS: https://dotnet.microsoft.com/download.

Once installed, you can verify it is working by dropping to the command line (OSX you’ll use Terminal, Windows you want PowerShell, and Linux whatever your favorite shell is), and running: dotnet --help

You should see something like this:

dotnethelp.png

Raspberry Pi

The one prerequisite here is that you can SSH into your Raspberry Pi. If you need some help enabling SSH, check out this article here: https://www.raspberrypi.org/documentation/remote-access/ssh/.

If you’d like to setup your Raspberry Pi headless, and ensure it connects to your WiFi network and automatically enables SSH on first boot, check out my post:

Once you’re sshed in (Windows users that need help check this out: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh/ssh-under-windows), you can start installing .NET Core on your Pi.

The following commands will:

1. Navigate to the home directory

2. Download the .NET Core SDK

3. Create the directory /opt/dotnet

4. Uncompress the archive we downloaded into /opt/dotnet

5. Create a symbolic link so we can use the dotnet command

cd ~
wget https://download.visualstudio.microsoft.com/download/pr/35c09285-4114-44f7-aa7d-47fe75a55eda/ac5a8f1bc324f2a6cd021237528441d4/dotnet-sdk-2.2.100-linux-arm.tar.gz
sudo mkdir /opt/dotnet
sudo tar -zxf dotnet-sdk-2.2.100-linux-arm.tar.gz -C /opt/dotnet
sudo ln -s /opt/dotnet/dotnet /usr/local/bin

If all goes well, we should be able to run: dotnet --version.

dotnetpi.png

Installing Git

Local Machine

Check out this URL for help installing Git on your local machine: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git.

We can verify it works after installing by dropping to your favorite shell and running: git.

git.png

Raspberry Pi

If you are using the minimal (lite) version of Raspbian, or need Git installed otherwise, run: sudo apt install git, and accept any prompts with “y”.

gitin.png

Now we can run: git to verify it installed correctly.

gitpi.png

Visual Studio Code

Visual Studio Code has become my editor of choice, even for some C# projects. We will install the client onto our local machine so we can edit the code there. For help installing Visual Studio Code, go here: https://code.visualstudio.com.

Once you have Visual Studio Code installed, open it up and click the extensions icon on the left (looks like a square). Then search for and install the C# extension. In this screenshot, it is the only one in the list for me that does not have an install option, and has a gear next to it (since I already have it installed). Once you install it, it will give you the option to reload Visual Studio Code.

C# ext.png

Creating the .NET Core Project

Now we will be creating the .NET Core project. The machine we will be doing this on is our local machine, not the Pi.

Hello World

Let’s start by creating a dotnet console application.

1. Fire up your favorite shell, and run: dotnet new console -n csharpi.

This will create a new directory named csharpi, and create the project there.

dotnetnewc.png

2. Now open up the csharpi folder with Visual Studio Code. You will see a prompt in the lower right-hand corner asking if you want to add required assets, click “Yes”.

assets.png

If all went well, you will now see a .vscode folder in the list.

vscodefolder.png

3. Now to test things out. Click Program.cs to see the example code the default project provides that will write out “Hello World” to the console. Go to the Debug menu, and click “Start Debugging” (or simply hit F5).

debut menu.png
helloworked.png

If you see “Hello World”, our project has been created, and we have the ability to edit it in Visual Studio Code.

Git Repository Initialization

Now we will initialize the local instance of the Git repository, and push up our code to Github. If you don’t have an account on Github, you can create one, here: https://github.com/join?source=header-home. Once you create it, log in and hang on because we will be using it shortly.

Local Initialization

1. Open up your favorite terminal / console and navigate to the csharpi folder we created. Once there, run: git init. Keep the window open.

gitinit.png

2. Open up the csharpi folder in Visual Studio Code. You’ll notice the Git icon (branching line) has an indicator that there are 22 files to work with. Let’s fix that by creating a file named .gitignore, and tell Git what files we don’t want to commit.

3. Click the File icon in VS Code to see the files in the project, and then click the create new file icon. Name the file .gitignore

newfile.png
filename.png

4. Open the .gitignore file by clicking it, and add the following contents:

        /bin
        /obj
        *.json

This will tell Git to ignore the /bin and /obj folders, as well as any file ending in .json. Since we will be creating a json configuration file with a token we do not want to share in it later, this is a good thing.

contents.png

5. Switch back to the terminal / console window you have open and run:

        git add .        
        git commit -m 'first for csharpi'
addstage.png

6. Now go to Github and login.

7. Once logged in, click the [+] icon in the upper right, and select “New repository”.

newrepo.png

8. On the next page, give it a name and description. Keep it public to follow along with this tutorial. Cloning it will be much easier on the Pi that way.

create.png

9. Click “Create repository, and check out the instructions on the next screen. You’ll see that we completed most of it already.

10. Run the two commands for existing repository.

For me it is (your commands will look similar, with the url after origin reflecting your username and repo name):

            git remote add origin https://github.com/gngrninja/csharpi.git                     
            git push -u origin master

11. You should now see the following on the command line, and on Github once you refresh the page:

pushit.png
reposuccess.png

Cloning the Repository to the Pi

Now let’s test that .NET Core is working correctly on the Pi.

1. Go to your Github repository for this project (for me it is https://github.com/gngrninja/csharpi), and click “Clone or download”, and then copy the URL.

copyurl.png

2. SSH into your Raspberry Pi and run git clone, and then paste in the URL we copied from the above step.:

        git clone https://github.com/gngrninja/csharpi.git

3. We can now change directory to csharpi, check out the contents, and try running it (essentially in debug mode):

        cd csharpi
        ls
        dotnet run
cdrun.png

4. Debugging on the Pi with .NET Core is not fun. It takes forever, and is on their list to fix (as much as they can, considering it is not the fastest processor). Eventually, however, you should see the following:

helloworldpi.png

5. Fret not, running the compiled/published application is much faster. To see this happen let’s create a folder under our home folder that will contain the published version of our code.

        mkdir /home/pi/bot

6. The /home/pi/csharpi folder will contain the code we clone from the repo, that we work on from our main machine via VS Code. The /home/pi/bot folder will contain the published version of that code, that executes much faster. Let’s ensure we are in the source code folder, and run the command to publish the app:

        cd /home/pi/csharpi
        dotnet publish -o /home/pi/bot
        
publish.png

7. Now that it is published, we can run the application. It will execute much faster as the published version.

        cd /home/pi/bot
        dotnet csharpi.dll

(I know right, running a dll on *nix?!)

runpub.png

Success! We now have the groundwork laid for creating the Discord bot.

Now let’s get a Discord bot going:

Raspberry Pi Headless Setup With WiFi and SSH Enabled

Raspberry Pi Headless Setup With WiFi and SSH Enabled

Headless Raspberry Pi Setup With Raspbian

RPi-Logo-Reg-SCREEN.png

The Raspberry Pi is an awesome little computer that can do just about anything. It’s around $35 USD for the 3B+ model, which is the one I prefer to use most of the time. This post will go over how to install Raspbian, configure WiFi, and enable SSH (all without using a monitor, mouse, or keyboard attached to the Pi).

Install Raspbian

1. Head on over to https://www.raspberrypi.org/downloads/raspbian/, and download the latest image. For this example I will be downloading the ZIP compressed image for Raspbian Buster Lite.

buster.png
Screen Shot 2019-06-25 at 3.35.48 PM.png

2. Next, we will need an app that allows us to write the image to an SD card or SSD (whatever we will be booting from on the Raspberry Pi). I like balenaEtcher. You can download etcher from here: https://www.balena.io/etcher/.

NOTE: If you have a shiny new Raspberry Pi 4, like I do now, you cannot boot from USB at this time, so you’ll have to use an SD Card. More information, here: https://www.raspberrypi.org/documentation/hardware/raspberrypi/booteeprom.md

dl etcher.png

3. Open Etcher, or whichever app you picked to write the image to the card/drive.

4. Connect the drive/SD card to your computer. I will be using an SSD with a USB converter.

hdd or sd.JPG

(note) Sometimes the voltage on the Pi’s USB ports can drop and affect the SSD attached. I have not had any problems using an SSD with the above linked convereter, but if you encounter issues you may want to look into a powered USB hub. The Pi 3B+ will automagically boot via USB, but if you have the Raspberry Pi v3, check out this link: https://www.raspberrypi.org/documentation/hardware/raspberrypi/bootmodes/msd.md

5. In Etcher, select “Select image“.

select image.png

6. Now choose “Select drive”, and pick the drive / SD card you recently attached, and click “Continue”.

select drive.png
open drive.png

7. Finally, click “Flash!

flash.png
wait.png

Setup WiFi Network Join on Boot

Now we will want to have the Pi join your WiFi network when it boots up. To do this we will create a file that will tell it what network to join, and its password.

1. If you don’t see the boot volume on your machine, safely disconnect and reconnect the SSD or SD card to your computer.

2. You should now see the boot volume attached to your machine.

boot.jpg

3. Using your favorite text editor, create a file named wpa_supplicant.conf.

Here is the content you’ll want for the file (be sure to replace the content in quotes with your SSID and password):

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
ap_scan=1
fast_reauth=1
country=JP

network={
	ssid="Your network's SSID"
	psk="Your network's password/psk"
	id_str="0"
	priority=100
}

4. Save the file/copy it to the boot volume.

copy file.png

Enable SSH on Boot

Next we will be enabling SSH on boot. To do this we simply need to create an empy file named ssh (no extension), and copy it to the boot volume.

1. Create the empty file (you can use Notepad, or any other text editor to do it) named ssh (no extension). For this example, I will use the Terminal app on OSX to create and copy the file.

cd ~/Desktop/
touch ssh
cp ssh /Volumes/boot
empty file create copy.png

2. Now you’ll want to safely eject the boot volume, and attach it to your Raspberry Pi!

safe.png

First Boot and Setup

Now we will want to plug the the storage device into the Pi (SD card or SSD -> USB converter), and power it on. Once it boots up we will find the IP address it was assigned from the router, and ssh in to change the default ssh password.

1. Once the storage device is attached to your Raspberry Pi, plug it in to power it on!

2. Log into your router, and look for something called attached devices/devices or DHCP client list. Keep refreshing the list, and eventually you should see a device with the hostname raspberrypi. For more help locating a headless Pi, check out this link: https://www.raspberrypi.org/documentation/remote-access/ip-address.md

found it.png

3. Now that you have the IP address, ssh in! If you’re on Windows, and need some help, check out this article: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh/ssh-under-windows. For this example I will be using the Terminal app on OSX.
The username is pi, and the default password is raspberry.

ssh pi@192.168.1.29

If you are prompted to trust the host, type / select “yes”.
When prompted for the password, enter raspberry.

ssh in.png

4. The very next thing you’ll want to do is change the default password. To do this, use the command (ensuring you are indeed sshed into the Pi): passwd.

(note) You will first be prompted for the current password, so enter raspberry for that. Then select your new password, and confirm it. You will not be able to see anything as you type them out.

change pw.png

[back to top]

Congratulations!

(well, hopefully)

If all went well, you should now have a Raspberry Pi that boots up and joins your WiFi network. You can SSH in to configure things, and do whatever you need to with the device. The files we created earlier (wpa_supplicant.conf and ssh) are removed from the boot volume after the first boot, so you have nothing to worry about there.

Let me know if you have any questions or feedback in the comments, below!