basic setup, /ping added
This commit is contained in:
9
config.toml
Normal file
9
config.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[log]
|
||||||
|
level = "info"
|
||||||
|
format = "text"
|
||||||
|
add_source = true
|
||||||
|
|
||||||
|
[bot]
|
||||||
|
dev_guilds = []
|
||||||
|
token = "token_here"
|
||||||
|
name= "HomesteadRelay"
|
||||||
15
go.mod
15
go.mod
@@ -1,3 +1,18 @@
|
|||||||
module homestead/homestead_to_go
|
module homestead/homestead_to_go
|
||||||
|
|
||||||
go 1.25.4
|
go 1.25.4
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/disgoorg/disgo v0.18.16
|
||||||
|
github.com/disgoorg/paginator v0.0.0-20240725182907-1bdf780b5586
|
||||||
|
github.com/disgoorg/snowflake/v2 v2.0.3
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.4
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/disgoorg/json v1.2.0 // indirect
|
||||||
|
github.com/gorilla/websocket v1.5.3 // indirect
|
||||||
|
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad // indirect
|
||||||
|
golang.org/x/crypto v0.31.0 // indirect
|
||||||
|
golang.org/x/sys v0.28.0 // indirect
|
||||||
|
)
|
||||||
|
|||||||
26
go.sum
26
go.sum
@@ -0,0 +1,26 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/disgoorg/disgo v0.18.16 h1:Yk6pA9TaGbuM4hWfWafH0jAfmkWvZBFY7rh49DgljGE=
|
||||||
|
github.com/disgoorg/disgo v0.18.16/go.mod h1:dXYVH059d6aK7mI+Nh/3svSRWedNd09P7C2VX3RqbJY=
|
||||||
|
github.com/disgoorg/json v1.2.0 h1:6e/j4BCfSHIvucG1cd7tJPAOp1RgnnMFSqkvZUtEd1Y=
|
||||||
|
github.com/disgoorg/json v1.2.0/go.mod h1:BHDwdde0rpQFDVsRLKhma6Y7fTbQKub/zdGO5O9NqqA=
|
||||||
|
github.com/disgoorg/paginator v0.0.0-20240725182907-1bdf780b5586 h1:GcdAmaZYq/iHmeV8iSPEeN1JHB3dgfq6EcujSsHfZFg=
|
||||||
|
github.com/disgoorg/paginator v0.0.0-20240725182907-1bdf780b5586/go.mod h1:6dmOx00CV/GNYip5FZbe9k2mw39trmpdY6meXdCrfrw=
|
||||||
|
github.com/disgoorg/snowflake/v2 v2.0.3 h1:3B+PpFjr7j4ad7oeJu4RlQ+nYOTadsKapJIzgvSI2Ro=
|
||||||
|
github.com/disgoorg/snowflake/v2 v2.0.3/go.mod h1:W6r7NUA7DwfZLwr00km6G4UnZ0zcoLBRufhkFWgAc4c=
|
||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad h1:qIQkSlF5vAUHxEmTbaqt1hkJ/t6skqEGYiMag343ucI=
|
||||||
|
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad/go.mod h1:/pA7k3zsXKdjjAiUhB5CjuKib9KJGCaLvZwtxGC8U0s=
|
||||||
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
||||||
|
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||||
|
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
||||||
|
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
84
main.go
84
main.go
@@ -1,5 +1,89 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"homestead/homestead_to_go/relay"
|
||||||
|
"homestead/homestead_to_go/relay/commands"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/disgoorg/disgo/bot"
|
||||||
|
"github.com/disgoorg/disgo/handler"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
path := flag.String("config", "config.toml", "path to config")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
cfg, err := relay.LoadConfig(*path)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to read config", slog.Any("err", err))
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%v\n", cfg)
|
||||||
|
|
||||||
|
setupLogger(cfg.Log)
|
||||||
|
slog.Info(fmt.Sprintf("Starting %s...", cfg.Bot.Name))
|
||||||
|
|
||||||
|
b := relay.New(*cfg)
|
||||||
|
|
||||||
|
h := handler.New()
|
||||||
|
h.Command("/ping", commands.PingHandler)
|
||||||
|
|
||||||
|
if err = b.SetupBot(h, bot.NewListenerFunc(b.OnReady) /*handlers.MessageHandler(b)*/); err != nil {
|
||||||
|
slog.Error("Failed to setup bot", slog.Any("err", err))
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
b.Client.Close(ctx)
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err = handler.SyncCommands(b.Client, commands.Commands, cfg.Bot.DevGuilds); err != nil {
|
||||||
|
slog.Error("Failed to sync commands", slog.Any("err", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err = b.Client.OpenGateway(ctx); err != nil {
|
||||||
|
slog.Error("Failed to open gateway", slog.Any("err", err))
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("Bot is running. Press CTRL-C to exit.")
|
||||||
|
s := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
<-s
|
||||||
|
slog.Info("Shutting down bot...")
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupLogger(cfg relay.LogConfig) {
|
||||||
|
opts := &slog.HandlerOptions{
|
||||||
|
AddSource: cfg.AddSource,
|
||||||
|
Level: cfg.Level,
|
||||||
|
}
|
||||||
|
|
||||||
|
var sHandler slog.Handler
|
||||||
|
switch cfg.Format {
|
||||||
|
case "json":
|
||||||
|
sHandler = slog.NewJSONHandler(os.Stdout, opts)
|
||||||
|
case "text":
|
||||||
|
sHandler = slog.NewTextHandler(os.Stdout, opts)
|
||||||
|
default:
|
||||||
|
slog.Error("Unknown log format", slog.String("format", cfg.Format))
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
slog.SetDefault(slog.New(sHandler))
|
||||||
|
}
|
||||||
|
|
||||||
// $ git config user.name <user>
|
// $ git config user.name <user>
|
||||||
// $ git config user.email <email>
|
// $ git config user.email <email>
|
||||||
// $ go mod tidy; go mod download
|
// $ go mod tidy; go mod download
|
||||||
|
|||||||
48
relay/bot.go
Normal file
48
relay/bot.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package relay
|
||||||
|
|
||||||
|
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/paginator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func New(cfg Config) *Bot {
|
||||||
|
return &Bot{
|
||||||
|
Cfg: cfg,
|
||||||
|
Paginator: paginator.New(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Client = client
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) OnReady(_ *events.Ready) {
|
||||||
|
slog.Info(b.Cfg.Bot.Name + " 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))
|
||||||
|
}
|
||||||
|
}
|
||||||
14
relay/commands/commands.go
Normal file
14
relay/commands/commands.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import "github.com/disgoorg/disgo/discord"
|
||||||
|
|
||||||
|
func getPingCommand() discord.SlashCommandCreate {
|
||||||
|
return discord.SlashCommandCreate{
|
||||||
|
Name: "ping",
|
||||||
|
Description: "Check whether the bot is responding.",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var Commands = []discord.ApplicationCommandCreate{
|
||||||
|
getPingCommand(),
|
||||||
|
}
|
||||||
47
relay/commands/ping.go
Normal file
47
relay/commands/ping.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/disgoorg/disgo/discord"
|
||||||
|
"github.com/disgoorg/disgo/handler"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PingHandler(e *handler.CommandEvent) error {
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
latency := now.Sub(e.CreatedAt())
|
||||||
|
msLatency := latency.Milliseconds()
|
||||||
|
if msLatency < 0 {
|
||||||
|
msLatency = -msLatency
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gateway latency
|
||||||
|
var gwMs int64
|
||||||
|
if gw := e.Client().Gateway(); gw != nil {
|
||||||
|
gwMs = gw.Latency().Milliseconds()
|
||||||
|
} else {
|
||||||
|
gwMs = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
lat := float64(msLatency)
|
||||||
|
if lat > 5000 {
|
||||||
|
lat = 5000
|
||||||
|
}
|
||||||
|
red := int((lat / 5000) * 255)
|
||||||
|
green := 255 - red
|
||||||
|
color := (red << 16) | (green << 8)
|
||||||
|
|
||||||
|
embed := discord.NewEmbedBuilder().
|
||||||
|
SetTitle("🏓 Pong!").
|
||||||
|
AddField("Interaction latency", fmt.Sprintf("%d ms", msLatency), true).
|
||||||
|
AddField("Gateway latency", fmt.Sprintf("%d ms", gwMs), true).
|
||||||
|
SetColor(color).
|
||||||
|
Build()
|
||||||
|
|
||||||
|
return e.CreateMessage(discord.NewMessageCreateBuilder().
|
||||||
|
SetEmbeds(embed).
|
||||||
|
Build(),
|
||||||
|
)
|
||||||
|
}
|
||||||
21
relay/config.go
Normal file
21
relay/config.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package relay
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/pelletier/go-toml/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadConfig(path string) (*Config, error) {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to open config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var cfg Config
|
||||||
|
if err = toml.NewDecoder(file).Decode(&cfg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &cfg, nil
|
||||||
|
}
|
||||||
32
relay/structs.go
Normal file
32
relay/structs.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package relay
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log/slog"
|
||||||
|
|
||||||
|
"github.com/disgoorg/disgo/bot"
|
||||||
|
"github.com/disgoorg/paginator"
|
||||||
|
"github.com/disgoorg/snowflake/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Bot struct {
|
||||||
|
Cfg Config
|
||||||
|
Client bot.Client
|
||||||
|
Paginator *paginator.Manager
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Log LogConfig `toml:"log"`
|
||||||
|
Bot BotConfig `toml:"bot"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type BotConfig struct {
|
||||||
|
DevGuilds []snowflake.ID `toml:"dev_guilds"`
|
||||||
|
Token string `toml:"token"`
|
||||||
|
Name string `toml:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type LogConfig struct {
|
||||||
|
Level slog.Level `toml:"level"`
|
||||||
|
Format string `toml:"format"`
|
||||||
|
AddSource bool `toml:"add_source"`
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user