Skip to content

Commit

Permalink
update bot template
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jun 5, 2024
1 parent 88377d6 commit a2cb170
Show file tree
Hide file tree
Showing 25 changed files with 382 additions and 304 deletions.
6 changes: 3 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.idea/
.github/
.editorconfig
README.md
LICENSE
config.json
config.json.example
config.toml
config.example.toml
44 changes: 0 additions & 44 deletions .editorconfig

This file was deleted.

45 changes: 0 additions & 45 deletions .github/workflows/docker-build.yml

This file was deleted.

48 changes: 48 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Docker

on: [ push ]

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=ref,event=pr
type=sha,prefix=
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm/v7,linux/arm64/v8
push: true
build-args: VERSION=${{ steps.meta.outputs.version }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
26 changes: 0 additions & 26 deletions .github/workflows/go-test.yml

This file was deleted.

29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test

on: [ push ]

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.22.x
- name: Test
run: go test -v ./...
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.22.x
- name: Lint
uses: golangci/golangci-lint-action@v6
with:
version: latest
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.idea/
config.json
config.toml
14 changes: 14 additions & 0 deletions .golangci.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[linters]
disable-all = true
enable = [
"gosimple",
"govet",
"gofmt",
"ineffassign",
"staticcheck",
"sloglint",
"whitespace",
"errname",
"errorlint",
"importas"
]
7 changes: 5 additions & 2 deletions cmd/Dockerfile → Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM golang:1.18-alpine AS build
FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS build

WORKDIR /build

Expand All @@ -11,16 +11,19 @@ COPY . .
ARG TARGETOS
ARG TARGETARCH
ARG VERSION=dev
ARG COMMIT=unknown

RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
CGO_ENABLED=0 \
GOOS=$TARGETOS \
GOARCH=$TARGETARCH \
go build -ldflags="-X 'main.version=${VERSION}'-w -s" -o bot cmd/main.go
go build -ldflags="-X 'main.version=${VERSION}' -X 'main.commit=${COMMIT}'" -o bot github.com/disgoorg/bot-template

FROM alpine

COPY --from=build /build/bot /bin/bot

ENTRYPOINT ["/bin/bot"]

CMD ["-config", "/var/lib/config.toml"]
62 changes: 59 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,65 @@
[![Docker](https://github.com/disgoorg/bot-template/actions/workflows/docker-build.yml/badge.svg)](https://github.com/disgoorg/bot-template/actions/workflows/docker-build.yml)
[![Test](https://github.com/disgoorg/bot-template/actions/workflows/go-test.yml/badge.svg)](https://github.com/disgoorg/bot-template/actions/workflows/go-test.yml)
[![Go Report][0]][1]
[![Go Version][2]][3]
[![License][4]][5]
[![Docker][6]][7]
[![Test][8]][9]

# bot-template

This is a simple bot template for creating a bot which includes a config, application commands, components modals(using [handler](https://github.com/disgoorg/disgo/tree/master/handler) and event listeners.

Optional CLI Flags:
CLI Flags:
- `--config-path=your-config-path`: Path to the config file.
- `--sync-commands=true`: Synchronize commands with the discord.

## Usage

1. Click on the `Use this template` button to create a new repository from this template.
2. Clone the repository to your local machine.
3. Open the project in your favorite IDE.
4. Copy the `config.example.toml` file to `config.toml` and fill in the required fields.
5. Run the bot using `go run .`
6. Invite the bot to your server using the invite link generated by [discord developer portal][10].
7.

## Configuration

The configuration file is in TOML format. The format is as follows:

```toml
[log]
# valid levels are "debug", "info", "warn", "error"
level = "info"
# valid formats are "text" and "json"
format = "text"
# whether to add the log source to the log message
add_source = true

[bot]
# add guild ids the commands should sync to, leave empty to sync globally
dev_guilds = []
# the bot token
token = "..."
```

## License

The bot template is licensed under the [Apache License 2.0][5].


[0]: https://goreportcard.com/badge/github.com/disgoorg/bot-template
[1]: https://goreportcard.com/report/github.com/disgoorg/bot-template

[2]: https://img.shields.io/github/go-mod/go-version/disgoorg/bot-template
[3]: https://golang.org/doc/devel/release.html

[4]: https://img.shields.io/github/license/disgoorg/bot-template
[5]: LICENSE

[6]: https://github.com/disgoorg/bot-template/actions/workflows/docker.yml/badge.svg
[7]: https://github.com/disgoorg/bot-template/actions/workflows/docker.yml

[8]: https://github.com/disgoorg/bot-template/actions/workflows/test.yml/badge.svg
[9]: https://github.com/disgoorg/bot-template/actions/workflows/test.yml

[10]: https://discord.com/developers/applications
34 changes: 19 additions & 15 deletions bot.go → bottemplate/bot.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,56 @@
package dbot
package bottemplate

import (
"context"
"log/slog"
"time"

"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/cache"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/log"
"github.com/disgoorg/paginator"
)

func New(logger log.Logger, version string, config Config) *Bot {
func New(cfg Config, version string, commit string) *Bot {
return &Bot{
Logger: logger,
Config: config,
Cfg: cfg,
Paginator: paginator.New(),
Version: version,
Commit: commit,
}
}

type Bot struct {
Logger log.Logger
Cfg Config
Client bot.Client
Paginator *paginator.Manager
Config Config
Version string
Commit string
}

func (b *Bot) SetupBot(listeners ...bot.EventListener) {
var err error
b.Client, err = disgo.New(b.Config.Token,
bot.WithLogger(b.Logger),
func (b *Bot) SetupBot(listeners ...bot.EventListener) error {
client, err := disgo.New(b.Cfg.Bot.Token,
bot.WithGatewayConfigOpts(gateway.WithIntents(gateway.IntentGuilds, gateway.IntentGuildMessages, gateway.IntentMessageContent)),
bot.WithCacheConfigOpts(cache.WithCaches(cache.FlagGuilds)),
bot.WithEventListeners(b.Paginator),
bot.WithEventListeners(listeners...),
)
if err != nil {
b.Logger.Fatal("Failed to setup b: ", err)
return err
}

b.Client = client
return nil
}

func (b *Bot) OnReady(_ *events.Ready) {
b.Logger.Infof("Butler ready")
if err := b.Client.SetPresence(context.TODO(), gateway.WithListeningActivity("you"), gateway.WithOnlineStatus(discord.OnlineStatusOnline)); err != nil {
b.Logger.Errorf("Failed to set presence: %s", err)
slog.Info("bot-template ready")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := b.Client.SetPresence(ctx, gateway.WithListeningActivity("you"), gateway.WithOnlineStatus(discord.OnlineStatusOnline)); err != nil {
slog.Error("Failed to set presence", slog.Any("err", err))
}
}
File renamed without changes.
Loading

0 comments on commit a2cb170

Please sign in to comment.