basic setup, /ping added
This commit is contained in:
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