chore(formatting,ci): more formatting commands
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"dislate/internals/discord/bot/commands"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
|
||||
"dislate/internals/discord/bot/commands"
|
||||
|
||||
dgo "github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
@@ -94,7 +95,6 @@ func (b *Bot) registerCommands() error {
|
||||
sc := opts[isSub]
|
||||
|
||||
err := subCmds[sc.Name].Handle(s, ic)
|
||||
|
||||
if err != nil {
|
||||
_ = s.InteractionRespond(ic.Interaction, &dgo.InteractionResponse{
|
||||
Type: dgo.InteractionResponseDeferredChannelMessageWithSource,
|
||||
|
||||
@@ -20,6 +20,7 @@ type ManageChannel struct {
|
||||
func NewManageChannel(db gconf.DB) ManageChannel {
|
||||
return ManageChannel{db}
|
||||
}
|
||||
|
||||
func (c ManageChannel) Info() *dgo.ApplicationCommand {
|
||||
var permissions int64 = dgo.PermissionManageChannels
|
||||
|
||||
@@ -29,6 +30,7 @@ func (c ManageChannel) Info() *dgo.ApplicationCommand {
|
||||
DefaultMemberPermissions: &permissions,
|
||||
}
|
||||
}
|
||||
|
||||
func (c ManageChannel) Subcommands() []Command {
|
||||
return []Command{
|
||||
channelsInfo(c),
|
||||
@@ -36,9 +38,11 @@ func (c ManageChannel) Subcommands() []Command {
|
||||
channelsSetLang(c),
|
||||
}
|
||||
}
|
||||
|
||||
func (c ManageChannel) Handle(s *dgo.Session, i *dgo.InteractionCreate) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c ManageChannel) Components() []Component {
|
||||
return []Component{}
|
||||
}
|
||||
@@ -64,6 +68,7 @@ func (c channelsInfo) Info() *dgo.ApplicationCommand {
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
func (c channelsInfo) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||
opts := getOptions(ic.ApplicationCommandData().Options)
|
||||
|
||||
@@ -96,16 +101,17 @@ func (c channelsInfo) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||
Flags: dgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c channelsInfo) Components() []Component {
|
||||
return []Component{}
|
||||
}
|
||||
|
||||
func (c channelsInfo) Subcommands() []Command {
|
||||
return []Command{}
|
||||
}
|
||||
@@ -139,6 +145,7 @@ func (c channelsLink) Info() *dgo.ApplicationCommand {
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
func (c channelsLink) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||
opts := getOptions(ic.ApplicationCommandData().Options)
|
||||
|
||||
@@ -209,16 +216,17 @@ func (c channelsLink) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||
Flags: dgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c channelsLink) Components() []Component {
|
||||
return []Component{}
|
||||
}
|
||||
|
||||
func (c channelsLink) Subcommands() []Command {
|
||||
return []Command{}
|
||||
}
|
||||
@@ -253,6 +261,7 @@ func (c channelsSetLang) Info() *dgo.ApplicationCommand {
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
func (c channelsSetLang) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||
opts := getOptions(ic.ApplicationCommandData().Options)
|
||||
|
||||
@@ -302,16 +311,17 @@ func (c channelsSetLang) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error
|
||||
Flags: dgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c channelsSetLang) Components() []Component {
|
||||
return []Component{}
|
||||
}
|
||||
|
||||
func (c channelsSetLang) Subcommands() []Command {
|
||||
return []Command{}
|
||||
}
|
||||
@@ -344,7 +354,8 @@ func getChannelInfo(db gconf.DB, ch gdb.Channel) (*dgo.MessageEmbed, error) {
|
||||
g[i] = "<#" + gi.ID + ">"
|
||||
}
|
||||
|
||||
return &dgo.MessageEmbed{Title: "Channel Information",
|
||||
return &dgo.MessageEmbed{
|
||||
Title: "Channel Information",
|
||||
Fields: []*dgo.MessageEmbedField{
|
||||
{Name: "ID", Value: ch.ID, Inline: true},
|
||||
{Name: "Language", Value: string(ch.Language), Inline: true},
|
||||
|
||||
@@ -17,6 +17,7 @@ type ManageConfig struct {
|
||||
func NewMagageConfig(db gconf.DB) ManageConfig {
|
||||
return ManageConfig{db}
|
||||
}
|
||||
|
||||
func (c ManageConfig) Info() *dgo.ApplicationCommand {
|
||||
var permissions int64 = dgo.PermissionAdministrator
|
||||
|
||||
@@ -26,12 +27,15 @@ func (c ManageConfig) Info() *dgo.ApplicationCommand {
|
||||
DefaultMemberPermissions: &permissions,
|
||||
}
|
||||
}
|
||||
|
||||
func (c ManageConfig) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c ManageConfig) Components() []Component {
|
||||
return []Component{}
|
||||
}
|
||||
|
||||
func (c ManageConfig) Subcommands() []Command {
|
||||
return []Command{
|
||||
loggerConfigChannel(c),
|
||||
@@ -42,6 +46,7 @@ func (c ManageConfig) Subcommands() []Command {
|
||||
type loggerConfigChannel struct {
|
||||
db gconf.DB
|
||||
}
|
||||
|
||||
func (c loggerConfigChannel) Info() *dgo.ApplicationCommand {
|
||||
var permissions int64 = dgo.PermissionAdministrator
|
||||
return &dgo.ApplicationCommand{
|
||||
@@ -49,9 +54,9 @@ func (c loggerConfigChannel) Info() *dgo.ApplicationCommand {
|
||||
Description: "Change logging channel",
|
||||
DefaultMemberPermissions: &permissions,
|
||||
Options: []*dgo.ApplicationCommandOption{{
|
||||
Type: dgo.ApplicationCommandOptionChannel,
|
||||
Required: true,
|
||||
Name: "log-channel",
|
||||
Type: dgo.ApplicationCommandOptionChannel,
|
||||
Required: true,
|
||||
Name: "log-channel",
|
||||
Description: "The channel to send log messages and errors to",
|
||||
ChannelTypes: []dgo.ChannelType{
|
||||
dgo.ChannelTypeGuildText,
|
||||
@@ -59,6 +64,7 @@ func (c loggerConfigChannel) Info() *dgo.ApplicationCommand {
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
func (c loggerConfigChannel) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||
opts := getOptions(ic.ApplicationCommandData().Options)
|
||||
|
||||
@@ -91,15 +97,17 @@ func (c loggerConfigChannel) Handle(s *dgo.Session, ic *dgo.InteractionCreate) e
|
||||
Type: dgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &dgo.InteractionResponseData{
|
||||
Content: fmt.Sprintf("Logging channel changed to %s", *guild.Config.LoggingChannel),
|
||||
Flags: dgo.MessageFlagsEphemeral,
|
||||
Flags: dgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (c loggerConfigChannel) Components() []Component {
|
||||
return []Component{}
|
||||
}
|
||||
|
||||
func (c loggerConfigChannel) Subcommands() []Command {
|
||||
return []Command{}
|
||||
}
|
||||
@@ -107,6 +115,7 @@ func (c loggerConfigChannel) Subcommands() []Command {
|
||||
type loggerConfigLevel struct {
|
||||
db gconf.DB
|
||||
}
|
||||
|
||||
func (c loggerConfigLevel) Info() *dgo.ApplicationCommand {
|
||||
var permissions int64 = dgo.PermissionAdministrator
|
||||
return &dgo.ApplicationCommand{
|
||||
@@ -114,9 +123,9 @@ func (c loggerConfigLevel) Info() *dgo.ApplicationCommand {
|
||||
Description: "Change logging channel",
|
||||
DefaultMemberPermissions: &permissions,
|
||||
Options: []*dgo.ApplicationCommandOption{{
|
||||
Type: dgo.ApplicationCommandOptionString,
|
||||
Required: true,
|
||||
Name: "log-level",
|
||||
Type: dgo.ApplicationCommandOptionString,
|
||||
Required: true,
|
||||
Name: "log-level",
|
||||
Description: "The logging level of messages and errors",
|
||||
Choices: []*dgo.ApplicationCommandOptionChoice{
|
||||
{Name: "Debug", Value: slog.LevelDebug.String()},
|
||||
@@ -127,6 +136,7 @@ func (c loggerConfigLevel) Info() *dgo.ApplicationCommand {
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
func (c loggerConfigLevel) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||
opts := getOptions(ic.ApplicationCommandData().Options)
|
||||
|
||||
@@ -161,15 +171,17 @@ func (c loggerConfigLevel) Handle(s *dgo.Session, ic *dgo.InteractionCreate) err
|
||||
Type: dgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &dgo.InteractionResponseData{
|
||||
Content: fmt.Sprintf("Logging level changed to %s", l),
|
||||
Flags: dgo.MessageFlagsEphemeral,
|
||||
Flags: dgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (c loggerConfigLevel) Components() []Component {
|
||||
return []Component{}
|
||||
}
|
||||
|
||||
func (c loggerConfigLevel) Subcommands() []Command {
|
||||
return []Command{}
|
||||
}
|
||||
|
||||
@@ -16,13 +16,14 @@ type Error interface {
|
||||
}
|
||||
|
||||
type defaultError struct {
|
||||
err string
|
||||
err string
|
||||
args []slog.Attr
|
||||
}
|
||||
|
||||
func NewError(err string, args ...slog.Attr) defaultError {
|
||||
return defaultError{err, args}
|
||||
}
|
||||
|
||||
func New(err string, args ...slog.Attr) defaultError {
|
||||
return NewError(err, args...)
|
||||
}
|
||||
@@ -51,7 +52,7 @@ func (err defaultError) Reply(s *dgo.Session, m *dgo.Message) {
|
||||
}
|
||||
|
||||
func (err defaultError) LogReply(l *slog.Logger, s *dgo.Session, m *dgo.Message) {
|
||||
err.Reply(s,m)
|
||||
err.Reply(s, m)
|
||||
err.Log(l)
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ func (b *Bot) registerEventHandlers() {
|
||||
events.NewMessageCreate(b.db, b.translator).Serve,
|
||||
events.NewMessageEdit(b.db, b.translator).Serve,
|
||||
events.NewReady(b.logger, b.db).Serve,
|
||||
events.NewThreadCreate(b.db, b.translator).Serve,
|
||||
}
|
||||
for _, h := range ehs {
|
||||
b.session.AddHandler(h)
|
||||
|
||||
@@ -18,6 +18,7 @@ type GuildCreate struct {
|
||||
func NewGuildCreate(log *slog.Logger, db gconf.DB) GuildCreate {
|
||||
return GuildCreate{log, db}
|
||||
}
|
||||
|
||||
func (h GuildCreate) Serve(s *dgo.Session, e *dgo.GuildCreate) {
|
||||
err := h.db.GuildInsert(gdb.Guild[gconf.ConfigString]{ID: e.Guild.ID})
|
||||
|
||||
@@ -41,6 +42,7 @@ type Ready struct {
|
||||
func NewReady(log *slog.Logger, db gconf.DB) EventHandler[*dgo.Ready] {
|
||||
return Ready{log, db}
|
||||
}
|
||||
|
||||
func (h Ready) Serve(s *dgo.Session, e *dgo.Ready) {
|
||||
for _, g := range e.Guilds {
|
||||
err := h.db.GuildInsert(gdb.Guild[gconf.ConfigString]{ID: g.ID})
|
||||
|
||||
@@ -22,6 +22,7 @@ type MessageCreate struct {
|
||||
func NewMessageCreate(db gconf.DB, t translator.Translator) MessageCreate {
|
||||
return MessageCreate{db, t}
|
||||
}
|
||||
|
||||
func (h MessageCreate) Serve(s *dgo.Session, ev *dgo.MessageCreate) {
|
||||
if ev.Message.Author.Bot {
|
||||
return
|
||||
@@ -127,7 +128,6 @@ func (h MessageCreate) Serve(s *dgo.Session, ev *dgo.MessageCreate) {
|
||||
}(c)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type MessageEdit struct {
|
||||
@@ -138,6 +138,7 @@ type MessageEdit struct {
|
||||
func NewMessageEdit(db gconf.DB, t translator.Translator) MessageEdit {
|
||||
return MessageEdit{db, t}
|
||||
}
|
||||
|
||||
func (h MessageEdit) Serve(s *dgo.Session, ev *dgo.MessageUpdate) {
|
||||
if ev.Message.Author.Bot {
|
||||
return
|
||||
@@ -221,11 +222,10 @@ func (h MessageEdit) Serve(s *dgo.Session, ev *dgo.MessageUpdate) {
|
||||
}(m)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func getUserWebhook(s *dgo.Session, channelID string, user *dgo.User) (*dgo.Webhook, error) {
|
||||
var whName = "DISLATE_USER_WEBHOOK_" + user.ID
|
||||
whName := "DISLATE_USER_WEBHOOK_" + user.ID
|
||||
|
||||
ws, err := s.ChannelWebhooks(channelID)
|
||||
if err != nil {
|
||||
@@ -247,7 +247,7 @@ func getUserWebhook(s *dgo.Session, channelID string, user *dgo.User) (*dgo.Webh
|
||||
return w, nil
|
||||
}
|
||||
|
||||
func getMessage(db gconf.DB, m *dgo.Message, lang lang.Language) (guilddb.Message, error) {
|
||||
func getMessage(db gconf.DB, m *dgo.Message, lang lang.Language) (guilddb.Message, error) {
|
||||
msg, err := db.Message(m.GuildID, m.ChannelID, m.ID)
|
||||
|
||||
if e.Is(err, guilddb.ErrNotFound) {
|
||||
@@ -261,14 +261,17 @@ func getMessage(db gconf.DB, m *dgo.Message, lang lang.Language) (guilddb.Messa
|
||||
}
|
||||
|
||||
return msg, nil
|
||||
|
||||
}
|
||||
|
||||
func getTranslatedMessage(db gconf.DB, m, original *dgo.Message, lang lang.Language) (guilddb.Message, error) {
|
||||
func getTranslatedMessage(
|
||||
db gconf.DB,
|
||||
m, original *dgo.Message,
|
||||
lang lang.Language,
|
||||
) (guilddb.Message, error) {
|
||||
msg, err := db.Message(m.GuildID, m.ChannelID, m.ID)
|
||||
|
||||
if e.Is(err, guilddb.ErrNotFound) {
|
||||
if err :=db.MessageInsert(guilddb.NewTranslatedMessage(
|
||||
if err := db.MessageInsert(guilddb.NewTranslatedMessage(
|
||||
m.GuildID,
|
||||
m.ChannelID,
|
||||
m.ID,
|
||||
@@ -278,7 +281,7 @@ func getTranslatedMessage(db gconf.DB, m, original *dgo.Message, lang lang.Lang
|
||||
)); err != nil {
|
||||
return guilddb.Message{}, err
|
||||
}
|
||||
msg, err =db.Message(m.GuildID, m.ChannelID, m.ID)
|
||||
msg, err = db.Message(m.GuildID, m.ChannelID, m.ID)
|
||||
if err != nil {
|
||||
return guilddb.Message{}, err
|
||||
}
|
||||
@@ -287,5 +290,4 @@ func getTranslatedMessage(db gconf.DB, m, original *dgo.Message, lang lang.Lang
|
||||
}
|
||||
|
||||
return msg, nil
|
||||
|
||||
}
|
||||
|
||||
@@ -13,12 +13,14 @@ type Config struct {
|
||||
}
|
||||
|
||||
type ConfigString struct {
|
||||
LoggingChannel *string `json:"logging_channel"`
|
||||
LoggingChannel *string `json:"logging_channel"`
|
||||
LoggingLevel *slog.Level `json:"logging_level"`
|
||||
}
|
||||
|
||||
type Guild gdb.Guild[ConfigString]
|
||||
type DB gdb.GuildDB[ConfigString]
|
||||
type (
|
||||
Guild gdb.Guild[ConfigString]
|
||||
DB gdb.GuildDB[ConfigString]
|
||||
)
|
||||
|
||||
func (g Guild) GetConfig(s *dgo.Session) (*Config, error) {
|
||||
var l *slog.Logger
|
||||
@@ -36,7 +38,7 @@ func (g Guild) GetConfig(s *dgo.Session) (*Config, error) {
|
||||
} else {
|
||||
lv = slog.LevelInfo
|
||||
}
|
||||
l = slog.New(NewGuildHandler(s,c, &slog.HandlerOptions{
|
||||
l = slog.New(NewGuildHandler(s, c, &slog.HandlerOptions{
|
||||
Level: lv,
|
||||
}))
|
||||
} else {
|
||||
|
||||
@@ -20,7 +20,8 @@ func NewGuildHandler(s *dgo.Session, c *dgo.Channel, opts *slog.HandlerOptions)
|
||||
type disabledHandler struct {
|
||||
*slog.TextHandler
|
||||
}
|
||||
func (_ disabledHandler) Enabled(_ context.Context,_ slog.Level) bool {
|
||||
|
||||
func (_ disabledHandler) Enabled(_ context.Context, _ slog.Level) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user