Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e188ceab79 | |||
| 06c17cc2e9 | |||
| e045d0d01f |
8
config.toml
Normal file
8
config.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[log]
|
||||||
|
level = "info"
|
||||||
|
add_source = true
|
||||||
|
|
||||||
|
[bot]
|
||||||
|
dev_guilds = []
|
||||||
|
token = "MTQ0NDI1MzEyNjM5ODg0MDkyMw.GhaGdH.FCevb77atgpDSLwz9KRZIBg-3xEo1WCnXyn9uk"
|
||||||
|
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=
|
||||||
|
|||||||
76
main.go
76
main.go
@@ -1,5 +1,81 @@
|
|||||||
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := commands.InitStorage(); err != nil {
|
||||||
|
slog.Error("Error initializing storage", "err", err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||||
|
AddSource: cfg.Log.AddSource,
|
||||||
|
Level: cfg.Log.Level,
|
||||||
|
})))
|
||||||
|
|
||||||
|
slog.Info(fmt.Sprintf("Starting %s...", cfg.Bot.Name))
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
b := relay.New(*cfg)
|
||||||
|
h := handler.New()
|
||||||
|
h.Command("/ping", commands.PingHandler)
|
||||||
|
h.Command("/sync", commands.SyncHandler)
|
||||||
|
|
||||||
|
if err = b.SetupBot(h, bot.NewListenerFunc(b.OnReady)); err != nil {
|
||||||
|
slog.Error("Failed to setup bot", slog.Any("err", err))
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
|
||||||
|
defer b.Client.Close(ctx)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
if err = handler.SyncCommands(b.Client, commands.Commands, cfg.Bot.DevGuilds); err != nil {
|
||||||
|
slog.Error("Failed to sync commands", slog.Any("err", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
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.")
|
||||||
|
|
||||||
|
s := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
<-s
|
||||||
|
|
||||||
|
slog.Info("Shutting down bot...")
|
||||||
|
}
|
||||||
|
|
||||||
// $ 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
|
||||||
|
|||||||
123
relay/bot.go
Normal file
123
relay/bot.go
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
package relay
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"homestead/homestead_to_go/relay/commands"
|
||||||
|
"log"
|
||||||
|
"log/slog"
|
||||||
|
"strings"
|
||||||
|
"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 onMessageCreate(event *events.MessageCreate) {
|
||||||
|
if event.Message.Author.Bot {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !commands.ChannelStore.IsRegistered(event.Message.ChannelID) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content := event.Message.Content
|
||||||
|
|
||||||
|
var parts []string
|
||||||
|
if len(event.Message.Attachments) > 0 {
|
||||||
|
images := 0
|
||||||
|
gifs := 0
|
||||||
|
videos := 0
|
||||||
|
files := 0
|
||||||
|
|
||||||
|
for _, att := range event.Message.Attachments {
|
||||||
|
if att.ContentType != nil {
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(*att.ContentType, "image/gif"):
|
||||||
|
gifs++
|
||||||
|
case strings.HasPrefix(*att.ContentType, "image/"):
|
||||||
|
images++
|
||||||
|
case strings.HasPrefix(*att.ContentType, "video/"):
|
||||||
|
videos++
|
||||||
|
default:
|
||||||
|
files++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if images > 0 {
|
||||||
|
parts = append(parts, fmt.Sprintf("%d %s", images, plural(images, "Image", "Images")))
|
||||||
|
}
|
||||||
|
if gifs > 0 {
|
||||||
|
parts = append(parts, fmt.Sprintf("%d %s", gifs, plural(gifs, "Gif", "Gifs")))
|
||||||
|
}
|
||||||
|
if videos > 0 {
|
||||||
|
parts = append(parts, fmt.Sprintf("%d %s", videos, plural(videos, "Video", "Videos")))
|
||||||
|
}
|
||||||
|
if files > 0 {
|
||||||
|
parts = append(parts, fmt.Sprintf("%d %s", files, plural(files, "File", "Files")))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(event.Message.Embeds) > 0 {
|
||||||
|
parts = append(parts, fmt.Sprintf("%d %s", len(event.Message.Embeds), plural(len(event.Message.Embeds), "Embed", "Embeds")))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(parts) > 0 {
|
||||||
|
content = fmt.Sprintf("%s", strings.Join(parts, ", "))
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("[%s] %s: %s",
|
||||||
|
event.Message.ChannelID,
|
||||||
|
event.Message.Author.Username,
|
||||||
|
content,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func plural(count int, singular, plural string) string {
|
||||||
|
if count == 1 {
|
||||||
|
return singular
|
||||||
|
}
|
||||||
|
return plural
|
||||||
|
}
|
||||||
|
|
||||||
|
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...),
|
||||||
|
bot.WithEventListeners(&events.ListenerAdapter{
|
||||||
|
OnMessageCreate: onMessageCreate,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
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.WithWatchingActivity("HideoutKitty"), gateway.WithOnlineStatus(discord.OnlineStatusIdle)); err != nil {
|
||||||
|
slog.Error("Failed to set presence", slog.Any("err", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
52
relay/commands/commands.go
Normal file
52
relay/commands/commands.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/disgoorg/disgo/discord"
|
||||||
|
"github.com/disgoorg/json"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getPingCommand() discord.SlashCommandCreate {
|
||||||
|
return discord.SlashCommandCreate{
|
||||||
|
Name: "ping",
|
||||||
|
Description: "Check whether the bot is responding.",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSyncCommand() discord.SlashCommandCreate {
|
||||||
|
perms := json.NewNullable(discord.PermissionManageChannels)
|
||||||
|
|
||||||
|
return discord.SlashCommandCreate{
|
||||||
|
Name: "sync",
|
||||||
|
Description: "Register/remove a channel to sync.",
|
||||||
|
DefaultMemberPermissions: &perms,
|
||||||
|
Options: []discord.ApplicationCommandOption{
|
||||||
|
discord.ApplicationCommandOptionSubCommand{
|
||||||
|
Name: "register",
|
||||||
|
Description: "Register a channel for logging",
|
||||||
|
Options: []discord.ApplicationCommandOption{
|
||||||
|
discord.ApplicationCommandOptionChannel{
|
||||||
|
Name: "channel",
|
||||||
|
Description: "Channel to register",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
discord.ApplicationCommandOptionSubCommand{
|
||||||
|
Name: "remove",
|
||||||
|
Description: "Remove a channel from logging",
|
||||||
|
Options: []discord.ApplicationCommandOption{
|
||||||
|
discord.ApplicationCommandOptionChannel{
|
||||||
|
Name: "channel",
|
||||||
|
Description: "Channel to remove",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var Commands = []discord.ApplicationCommandCreate{
|
||||||
|
getPingCommand(),
|
||||||
|
getSyncCommand(),
|
||||||
|
}
|
||||||
48
relay/commands/ping.go
Normal file
48
relay/commands/ping.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
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 > 1500 {
|
||||||
|
lat = 1500
|
||||||
|
}
|
||||||
|
red := int((lat / 1500) * 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(),
|
||||||
|
)
|
||||||
|
}
|
||||||
74
relay/commands/sync.go
Normal file
74
relay/commands/sync.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"homestead/homestead_to_go/relay/storage"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/disgoorg/disgo/discord"
|
||||||
|
"github.com/disgoorg/disgo/handler"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ChannelStore *storage.ChannelStorage
|
||||||
|
|
||||||
|
func InitStorage() error {
|
||||||
|
var err error
|
||||||
|
ChannelStore, err = storage.NewChannelStorage("registered_channels.json")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func SyncHandler(e *handler.CommandEvent) error {
|
||||||
|
data := e.SlashCommandInteractionData()
|
||||||
|
|
||||||
|
subcommand := data.SubCommandName
|
||||||
|
if subcommand == nil {
|
||||||
|
return e.CreateMessage(discord.MessageCreate{
|
||||||
|
Content: "Invalid subcommand!",
|
||||||
|
Flags: discord.MessageFlagEphemeral,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
channelID, ok := data.OptSnowflake("channel")
|
||||||
|
if !ok {
|
||||||
|
return e.CreateMessage(discord.MessageCreate{
|
||||||
|
Content: "Channel not provided!",
|
||||||
|
Flags: discord.MessageFlagEphemeral,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
switch *subcommand {
|
||||||
|
case "register":
|
||||||
|
if err := ChannelStore.AddChannel(channelID); err != nil {
|
||||||
|
log.Printf("Error registering channel: %v", err)
|
||||||
|
return e.CreateMessage(discord.MessageCreate{
|
||||||
|
Content: "❌ Failed to register channel!",
|
||||||
|
Flags: discord.MessageFlagEphemeral,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.CreateMessage(discord.MessageCreate{
|
||||||
|
Content: fmt.Sprintf("✅ Registered <#%s> for message logging!", channelID),
|
||||||
|
Flags: discord.MessageFlagEphemeral,
|
||||||
|
})
|
||||||
|
|
||||||
|
case "remove":
|
||||||
|
if err := ChannelStore.RemoveChannel(channelID); err != nil {
|
||||||
|
log.Printf("Error removing channel: %v", err)
|
||||||
|
return e.CreateMessage(discord.MessageCreate{
|
||||||
|
Content: "❌ Failed to remove channel!",
|
||||||
|
Flags: discord.MessageFlagEphemeral,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.CreateMessage(discord.MessageCreate{
|
||||||
|
Content: fmt.Sprintf("✅ Removed <#%s> from message logging!", channelID),
|
||||||
|
Flags: discord.MessageFlagEphemeral,
|
||||||
|
})
|
||||||
|
|
||||||
|
default:
|
||||||
|
return e.CreateMessage(discord.MessageCreate{
|
||||||
|
Content: "Unknown subcommand!",
|
||||||
|
Flags: discord.MessageFlagEphemeral,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
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
|
||||||
|
}
|
||||||
80
relay/storage/storage.go
Normal file
80
relay/storage/storage.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/disgoorg/snowflake/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewChannelStorage(filename string) (*ChannelStorage, error) {
|
||||||
|
storage := &ChannelStorage{
|
||||||
|
filename: filename,
|
||||||
|
Channels: make(map[snowflake.ID]bool),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := storage.Load(); err != nil && !os.IsNotExist(err) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return storage, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ChannelStorage) Load() error {
|
||||||
|
s.mutex.Lock()
|
||||||
|
defer s.mutex.Unlock()
|
||||||
|
|
||||||
|
data, err := os.ReadFile(s.filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Unmarshal(data, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ChannelStorage) Save() error {
|
||||||
|
s.mutex.Lock()
|
||||||
|
defer s.mutex.Unlock()
|
||||||
|
|
||||||
|
data, err := json.MarshalIndent(s, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.WriteFile(s.filename, data, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ChannelStorage) AddChannel(channelID snowflake.ID) error {
|
||||||
|
s.mutex.Lock()
|
||||||
|
s.Channels[channelID] = true
|
||||||
|
s.mutex.Unlock()
|
||||||
|
|
||||||
|
return s.Save()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ChannelStorage) RemoveChannel(channelID snowflake.ID) error {
|
||||||
|
s.mutex.Lock()
|
||||||
|
delete(s.Channels, channelID)
|
||||||
|
s.mutex.Unlock()
|
||||||
|
|
||||||
|
return s.Save()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ChannelStorage) IsRegistered(channelID snowflake.ID) bool {
|
||||||
|
s.mutex.RLock()
|
||||||
|
defer s.mutex.RUnlock()
|
||||||
|
|
||||||
|
return s.Channels[channelID]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ChannelStorage) GetAll() []snowflake.ID {
|
||||||
|
s.mutex.RLock()
|
||||||
|
defer s.mutex.RUnlock()
|
||||||
|
|
||||||
|
channels := make([]snowflake.ID, 0, len(s.Channels))
|
||||||
|
for channelID := range s.Channels {
|
||||||
|
channels = append(channels, channelID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return channels
|
||||||
|
}
|
||||||
13
relay/storage/structs.go
Normal file
13
relay/storage/structs.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/disgoorg/snowflake/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ChannelStorage struct {
|
||||||
|
mutex sync.RWMutex
|
||||||
|
filename string
|
||||||
|
Channels map[snowflake.ID]bool `json:"channels"`
|
||||||
|
}
|
||||||
31
relay/structs.go
Normal file
31
relay/structs.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
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"`
|
||||||
|
AddSource bool `toml:"add_source"`
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user