Giter Club home page Giter Club logo

discord-bot-template's Introduction

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

discord-bot-template's People

Contributors

dependabot[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.