Skip to content

Discord bot template with slash command support written in Go

License

Notifications You must be signed in to change notification settings

korzyk123/discord-bot-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dbt discord-bot-template

discord bot template with slash command support

GitHub License

Discord bot template with support for custom slash commands written in Go

Getting Started

Prerequisites

You need to have Go installed on your local machine. This project uses Go 1.21.

Installation

To start, clone this repo

git clone https://github.com/korzyk123/discord-bot-template

navigate to cloned dir

cd discord-bot-template

Running

Run the main package

-i application ID
-t bot token

go run discord-bot-template -i appid -t token

Usage

All slash commands are defined in cmds/commands.go using the built-in CommandDefinition struct

Examples:

A command with interaction response

{
    Command: &discordgo.ApplicationCommand{
        Name:        "ping",
        Description: "Ping-pong",
    },
    Action: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
        _ = s.InteractionRespond(
            i.Interaction,
            &discordgo.InteractionResponse{
                Type: discordgo.InteractionResponseChannelMessageWithSource,
                Data: &discordgo.InteractionResponseData{
                    Content: "pong",
                },
            },
        )
    },
}

A command with one required argument

{
    Command: &discordgo.ApplicationCommand{
        Name:        "reply",
        Description: "Reply with custom message",
        Options: []*discordgo.ApplicationCommandOption{
            {
                Name:        "msg",
                Description: "Message",
                Type:        discordgo.ApplicationCommandOptionString,
                Required:    true,
            },
        },
    },
    Action: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
        _ = s.InteractionRespond(
            i.Interaction,
            &discordgo.InteractionResponse{
                Type: discordgo.InteractionResponseChannelMessageWithSource,
                Data: &discordgo.InteractionResponseData{
                    Content: i.ApplicationCommandData().Options[0].StringValue(),
                },
            },
        )
    },
}

A command with multiple arguments, one of which is optional

{
    Command: &discordgo.ApplicationCommand{
        Name:        "pow",
        Description: "Return result of exponentiation",
        Options: []*discordgo.ApplicationCommandOption{
            {
                Name:        "base",
                Description: "Base",
                Type:        discordgo.ApplicationCommandOptionInteger,
                Required:    true,
            },
            {
                Name:        "exp",
                Description: "Exponent",
                Type:        discordgo.ApplicationCommandOptionInteger,
                Required:    false,
            },
        },
    },
    Action: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
        var args []float64

        for _, arg := range i.ApplicationCommandData().Options {
            args = append(args, float64(arg.IntValue()))
        }

        if len(args) < 2 {
            args = append(args, 2) // Default value for 2nd argument
        }

        result := int64(math.Pow(args[0], args[1]))

        _ = s.InteractionRespond(
            i.Interaction,
            &discordgo.InteractionResponse{
                Type: discordgo.InteractionResponseChannelMessageWithSource,
                Data: &discordgo.InteractionResponseData{
                    Content: strconv.FormatInt(result, 10),
                },
            },
        )
    },
}

Whenever the package starts, the slash commands are automatically overwritten in bulk to match the local code.

If you modify the source code, you might need to stop the running instance and go run it again to apply the changes to production.

Built With

License

This project is licensed under the Unlicense license - see the LICENSE file for details

About

Discord bot template with slash command support written in Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages