mid day commit
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
[log]
|
[log]
|
||||||
level = "info"
|
level = "info"
|
||||||
format = "text"
|
|
||||||
add_source = true
|
add_source = true
|
||||||
|
|
||||||
[bot]
|
[bot]
|
||||||
dev_guilds = []
|
dev_guilds = []
|
||||||
token = "token_here"
|
token = "MTQ0NDI1MzEyNjM5ODg0MDkyMw.GhaGdH.FCevb77atgpDSLwz9KRZIBg-3xEo1WCnXyn9uk"
|
||||||
name= "HomesteadRelay"
|
name= "HomesteadRelay"
|
||||||
|
|||||||
47
main.go
47
main.go
@@ -26,13 +26,21 @@ func main() {
|
|||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%v\n", cfg)
|
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,
|
||||||
|
})))
|
||||||
|
|
||||||
setupLogger(cfg.Log)
|
|
||||||
slog.Info(fmt.Sprintf("Starting %s...", cfg.Bot.Name))
|
slog.Info(fmt.Sprintf("Starting %s...", cfg.Bot.Name))
|
||||||
|
|
||||||
b := relay.New(*cfg)
|
//
|
||||||
|
|
||||||
|
b := relay.New(*cfg)
|
||||||
h := handler.New()
|
h := handler.New()
|
||||||
h.Command("/ping", commands.PingHandler)
|
h.Command("/ping", commands.PingHandler)
|
||||||
h.Command("/sync", commands.SyncHandler)
|
h.Command("/sync", commands.SyncHandler)
|
||||||
@@ -42,49 +50,32 @@ func main() {
|
|||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
|
||||||
|
defer b.Client.Close(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
b.Client.Close(ctx)
|
|
||||||
}()
|
//
|
||||||
|
|
||||||
if err = handler.SyncCommands(b.Client, commands.Commands, cfg.Bot.DevGuilds); err != nil {
|
if err = handler.SyncCommands(b.Client, commands.Commands, cfg.Bot.DevGuilds); err != nil {
|
||||||
slog.Error("Failed to sync commands", slog.Any("err", err))
|
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 {
|
if err = b.Client.OpenGateway(ctx); err != nil {
|
||||||
slog.Error("Failed to open gateway", slog.Any("err", err))
|
slog.Error("Failed to open gateway", slog.Any("err", err))
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info("Bot is running. Press CTRL-C to exit.")
|
slog.Info("Bot is running.")
|
||||||
|
|
||||||
s := make(chan os.Signal, 1)
|
s := make(chan os.Signal, 1)
|
||||||
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
<-s
|
<-s
|
||||||
|
|
||||||
slog.Info("Shutting down bot...")
|
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
|
||||||
|
|||||||
69
relay/bot.go
69
relay/bot.go
@@ -2,9 +2,11 @@ package relay
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"homestead/homestead_to_go/relay/commands"
|
"homestead/homestead_to_go/relay/commands"
|
||||||
"log"
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/disgoorg/disgo"
|
"github.com/disgoorg/disgo"
|
||||||
@@ -24,28 +26,71 @@ func New(cfg Config) *Bot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func onMessageCreate(event *events.MessageCreate) {
|
func onMessageCreate(event *events.MessageCreate) {
|
||||||
// Ignore bot messages
|
|
||||||
if event.Message.Author.Bot {
|
if event.Message.Author.Bot {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if channel is registered
|
|
||||||
if !commands.ChannelStore.IsRegistered(event.Message.ChannelID) {
|
if !commands.ChannelStore.IsRegistered(event.Message.ChannelID) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log the message
|
content := event.Message.Content
|
||||||
log.Printf("[%s] %s#%s: %s",
|
|
||||||
|
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.ChannelID,
|
||||||
event.Message.Author.Username,
|
event.Message.Author.Username,
|
||||||
event.Message.Author.Discriminator,
|
content,
|
||||||
event.Message.Content,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// You can also log attachments, embeds, etc.
|
|
||||||
if len(event.Message.Attachments) > 0 {
|
|
||||||
log.Printf(" └─ Attachments: %d", len(event.Message.Attachments))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func plural(count int, singular, plural string) string {
|
||||||
|
if count == 1 {
|
||||||
|
return singular
|
||||||
|
}
|
||||||
|
return plural
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) SetupBot(listeners ...bot.EventListener) error {
|
func (b *Bot) SetupBot(listeners ...bot.EventListener) error {
|
||||||
@@ -72,7 +117,7 @@ func (b *Bot) OnReady(_ *events.Ready) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if err := b.Client.SetPresence(ctx, gateway.WithListeningActivity("you"), gateway.WithOnlineStatus(discord.OnlineStatusOnline)); err != nil {
|
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))
|
slog.Error("Failed to set presence", slog.Any("err", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,5 @@ type BotConfig struct {
|
|||||||
|
|
||||||
type LogConfig struct {
|
type LogConfig struct {
|
||||||
Level slog.Level `toml:"level"`
|
Level slog.Level `toml:"level"`
|
||||||
Format string `toml:"format"`
|
|
||||||
AddSource bool `toml:"add_source"`
|
AddSource bool `toml:"add_source"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user