🚧 This wiki is a work in progress. Please refer to the official documentation for the most accurate and up-to-date information. 🚧

Main File

So far, what we’ve had as a project looks like this

    • go.mod
    • go.sum
  • Not so much going on huh?

    main.go

    Add a new file called main.go, this is our main entrypoint for our bot.

    Here’s a code snippet you can use to get started:

    main.go
     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
    
    package main
    
    import (
      "context"
      "fmt"
      "os"
      "os/signal"
      "syscall"
      "time"
    
      // Import DisGo packages
      "github.com/disgoorg/disgo"
      "github.com/disgoorg/disgo/bot"
      "github.com/disgoorg/disgo/discord"
      "github.com/disgoorg/disgo/events"
      "github.com/disgoorg/disgo/gateway"
      "github.com/disgoorg/snowflake/v2"
    )
    
    var (
      token   = os.Getenv("DISCORD_BOT_TOKEN")
      guildID = snowflake.GetEnv("DISCORD_GUILD_ID")
    )
    
    func main() {
      // Create a new client instance
      client, err := disgo.New(token,
        // Set gateway configuration options
        bot.WithGatewayConfigOpts(
          // Set enabled intents
          gateway.WithIntents(
            gateway.IntentGuilds,
            gateway.IntentGuildMessages,
            gateway.IntentDirectMessages,
          ),
        ),
        // Listen to the Ready event in order to know when the bot is connected
        bot.WithEventListenerFunc(func(e *events.Ready) {
              fmt.Println("Bot is connected as", e.User.Username)
        }),
      )
      if err != nil {
        panic(err)
      }
    
      // Ensure we close the client on exit
      defer func() {
        ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
        client.Close(ctx)
      }()
    
      // Connect to the gateway
      ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
      defer cancel()
      if err = client.OpenGateway(ctx); err != nil {
        panic(err)
      }
    
      // Wait here until CTRL+C or other term signal is received.
      fmt.Println("Bot is now running. Press CTRL+C to exit.")
      s := make(chan os.Signal, 1)
      signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
      <-s
      fmt.Println("Shutting down bot...")
    }

    That’s a lot of code, but don’t worry if you don’t understand everything right now. We’ll break it down

    First we create a new DisGo client using our bot token

    main.go
    16
    17
    18
    19
    
    func main() {
      // Create a new client instance
      client, err := disgo.New(token,
      //...

    This creates a new DisGo client that will provide all the functionality we need to interact with the Discord API.

    Next we set some options for the gateway connection, including which intents we want to enable.

    main.go
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
        // Set gateway configuration options
        bot.WithGatewayConfigOpts(
          // Set enabled intents (https://discord.com/developers/docs/events/gateway#gateway-intents)
          gateway.WithIntents(
            gateway.IntentGuilds,
            gateway.IntentGuildMessages,
            gateway.IntentDirectMessages,
          ),
        ),
        // Listen to the Ready event in order to know when the bot is connected
        bot.WithEventListenerFunc(func(e *events.Ready) {
          fmt.Print("Bot is connected as", e.User.Username)
        }),

    Here we specify that we want to receive events related to guilds (servers), guild messages, and direct messages.

    We’ve also set up an event listener for the Ready event, which is triggered when the bot successfully connects to Discord.

    After creating the client, we set up a deferred function to ensure that the client is properly closed when the application exits:

    main.go
    38
    39
    40
    41
    42
    43
    
    // Ensure we close the client on exit
      defer func() {
        ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
        client.Close(ctx)
      }()

    We then try to connect to the gateway

    main.go
    44
    45
    46
    47
    48
    49
    
      // Connect to the gateway
      ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
      defer cancel()
      if err = client.OpenGateway(ctx); err != nil {
        panic(err)
      }

    And if everything works correctly, our bot is now ready to run and connect to Discord!

    Running your application

    If you’ve been following along, your project structure should now look like this

    • go.mod
    • go.sum
    • main.go
  • Head to the terminal and run the following command inside your project directory to run your bot

    go run main.go

    If everything is set up correctly, you should see a message indicating that your bot is connected with the username you’ve set for it.

    Bot is Connected as YourBotName

    Congratulations! You’ve successfully set up the main file for your DisGo bot and connected it to Discord.

    ⚠️
    Remember, never share your bot token with anyone or expose it in public repositories. Keep it secure!

    Now we have a bot, but it doesn’t do much yet. In the next section, we’ll start adding some functionality to our bot!

    Last updated on