chore(formatting,ci): more formatting commands
This commit is contained in:
@@ -25,7 +25,10 @@
|
|||||||
sqlite
|
sqlite
|
||||||
sqlitebrowser
|
sqlitebrowser
|
||||||
go
|
go
|
||||||
|
gofumpt
|
||||||
golangci-lint
|
golangci-lint
|
||||||
|
golines
|
||||||
|
gotools
|
||||||
delve
|
delve
|
||||||
docker-compose
|
docker-compose
|
||||||
(templ system)
|
(templ system)
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
package bot
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"dislate/internals/discord/bot/commands"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"dislate/internals/discord/bot/commands"
|
||||||
|
|
||||||
dgo "github.com/bwmarrin/discordgo"
|
dgo "github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -94,7 +95,6 @@ func (b *Bot) registerCommands() error {
|
|||||||
sc := opts[isSub]
|
sc := opts[isSub]
|
||||||
|
|
||||||
err := subCmds[sc.Name].Handle(s, ic)
|
err := subCmds[sc.Name].Handle(s, ic)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = s.InteractionRespond(ic.Interaction, &dgo.InteractionResponse{
|
_ = s.InteractionRespond(ic.Interaction, &dgo.InteractionResponse{
|
||||||
Type: dgo.InteractionResponseDeferredChannelMessageWithSource,
|
Type: dgo.InteractionResponseDeferredChannelMessageWithSource,
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ type ManageChannel struct {
|
|||||||
func NewManageChannel(db gconf.DB) ManageChannel {
|
func NewManageChannel(db gconf.DB) ManageChannel {
|
||||||
return ManageChannel{db}
|
return ManageChannel{db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ManageChannel) Info() *dgo.ApplicationCommand {
|
func (c ManageChannel) Info() *dgo.ApplicationCommand {
|
||||||
var permissions int64 = dgo.PermissionManageChannels
|
var permissions int64 = dgo.PermissionManageChannels
|
||||||
|
|
||||||
@@ -29,6 +30,7 @@ func (c ManageChannel) Info() *dgo.ApplicationCommand {
|
|||||||
DefaultMemberPermissions: &permissions,
|
DefaultMemberPermissions: &permissions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ManageChannel) Subcommands() []Command {
|
func (c ManageChannel) Subcommands() []Command {
|
||||||
return []Command{
|
return []Command{
|
||||||
channelsInfo(c),
|
channelsInfo(c),
|
||||||
@@ -36,9 +38,11 @@ func (c ManageChannel) Subcommands() []Command {
|
|||||||
channelsSetLang(c),
|
channelsSetLang(c),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ManageChannel) Handle(s *dgo.Session, i *dgo.InteractionCreate) error {
|
func (c ManageChannel) Handle(s *dgo.Session, i *dgo.InteractionCreate) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ManageChannel) Components() []Component {
|
func (c ManageChannel) Components() []Component {
|
||||||
return []Component{}
|
return []Component{}
|
||||||
}
|
}
|
||||||
@@ -64,6 +68,7 @@ func (c channelsInfo) Info() *dgo.ApplicationCommand {
|
|||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c channelsInfo) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
func (c channelsInfo) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||||
opts := getOptions(ic.ApplicationCommandData().Options)
|
opts := getOptions(ic.ApplicationCommandData().Options)
|
||||||
|
|
||||||
@@ -96,16 +101,17 @@ func (c channelsInfo) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
|||||||
Flags: dgo.MessageFlagsEphemeral,
|
Flags: dgo.MessageFlagsEphemeral,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c channelsInfo) Components() []Component {
|
func (c channelsInfo) Components() []Component {
|
||||||
return []Component{}
|
return []Component{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c channelsInfo) Subcommands() []Command {
|
func (c channelsInfo) Subcommands() []Command {
|
||||||
return []Command{}
|
return []Command{}
|
||||||
}
|
}
|
||||||
@@ -139,6 +145,7 @@ func (c channelsLink) Info() *dgo.ApplicationCommand {
|
|||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c channelsLink) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
func (c channelsLink) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||||
opts := getOptions(ic.ApplicationCommandData().Options)
|
opts := getOptions(ic.ApplicationCommandData().Options)
|
||||||
|
|
||||||
@@ -209,16 +216,17 @@ func (c channelsLink) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
|||||||
Flags: dgo.MessageFlagsEphemeral,
|
Flags: dgo.MessageFlagsEphemeral,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c channelsLink) Components() []Component {
|
func (c channelsLink) Components() []Component {
|
||||||
return []Component{}
|
return []Component{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c channelsLink) Subcommands() []Command {
|
func (c channelsLink) Subcommands() []Command {
|
||||||
return []Command{}
|
return []Command{}
|
||||||
}
|
}
|
||||||
@@ -253,6 +261,7 @@ func (c channelsSetLang) Info() *dgo.ApplicationCommand {
|
|||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c channelsSetLang) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
func (c channelsSetLang) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||||
opts := getOptions(ic.ApplicationCommandData().Options)
|
opts := getOptions(ic.ApplicationCommandData().Options)
|
||||||
|
|
||||||
@@ -302,16 +311,17 @@ func (c channelsSetLang) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error
|
|||||||
Flags: dgo.MessageFlagsEphemeral,
|
Flags: dgo.MessageFlagsEphemeral,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c channelsSetLang) Components() []Component {
|
func (c channelsSetLang) Components() []Component {
|
||||||
return []Component{}
|
return []Component{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c channelsSetLang) Subcommands() []Command {
|
func (c channelsSetLang) Subcommands() []Command {
|
||||||
return []Command{}
|
return []Command{}
|
||||||
}
|
}
|
||||||
@@ -344,7 +354,8 @@ func getChannelInfo(db gconf.DB, ch gdb.Channel) (*dgo.MessageEmbed, error) {
|
|||||||
g[i] = "<#" + gi.ID + ">"
|
g[i] = "<#" + gi.ID + ">"
|
||||||
}
|
}
|
||||||
|
|
||||||
return &dgo.MessageEmbed{Title: "Channel Information",
|
return &dgo.MessageEmbed{
|
||||||
|
Title: "Channel Information",
|
||||||
Fields: []*dgo.MessageEmbedField{
|
Fields: []*dgo.MessageEmbedField{
|
||||||
{Name: "ID", Value: ch.ID, Inline: true},
|
{Name: "ID", Value: ch.ID, Inline: true},
|
||||||
{Name: "Language", Value: string(ch.Language), Inline: true},
|
{Name: "Language", Value: string(ch.Language), Inline: true},
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ type ManageConfig struct {
|
|||||||
func NewMagageConfig(db gconf.DB) ManageConfig {
|
func NewMagageConfig(db gconf.DB) ManageConfig {
|
||||||
return ManageConfig{db}
|
return ManageConfig{db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ManageConfig) Info() *dgo.ApplicationCommand {
|
func (c ManageConfig) Info() *dgo.ApplicationCommand {
|
||||||
var permissions int64 = dgo.PermissionAdministrator
|
var permissions int64 = dgo.PermissionAdministrator
|
||||||
|
|
||||||
@@ -26,12 +27,15 @@ func (c ManageConfig) Info() *dgo.ApplicationCommand {
|
|||||||
DefaultMemberPermissions: &permissions,
|
DefaultMemberPermissions: &permissions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ManageConfig) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
func (c ManageConfig) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ManageConfig) Components() []Component {
|
func (c ManageConfig) Components() []Component {
|
||||||
return []Component{}
|
return []Component{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ManageConfig) Subcommands() []Command {
|
func (c ManageConfig) Subcommands() []Command {
|
||||||
return []Command{
|
return []Command{
|
||||||
loggerConfigChannel(c),
|
loggerConfigChannel(c),
|
||||||
@@ -42,6 +46,7 @@ func (c ManageConfig) Subcommands() []Command {
|
|||||||
type loggerConfigChannel struct {
|
type loggerConfigChannel struct {
|
||||||
db gconf.DB
|
db gconf.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c loggerConfigChannel) Info() *dgo.ApplicationCommand {
|
func (c loggerConfigChannel) Info() *dgo.ApplicationCommand {
|
||||||
var permissions int64 = dgo.PermissionAdministrator
|
var permissions int64 = dgo.PermissionAdministrator
|
||||||
return &dgo.ApplicationCommand{
|
return &dgo.ApplicationCommand{
|
||||||
@@ -49,9 +54,9 @@ func (c loggerConfigChannel) Info() *dgo.ApplicationCommand {
|
|||||||
Description: "Change logging channel",
|
Description: "Change logging channel",
|
||||||
DefaultMemberPermissions: &permissions,
|
DefaultMemberPermissions: &permissions,
|
||||||
Options: []*dgo.ApplicationCommandOption{{
|
Options: []*dgo.ApplicationCommandOption{{
|
||||||
Type: dgo.ApplicationCommandOptionChannel,
|
Type: dgo.ApplicationCommandOptionChannel,
|
||||||
Required: true,
|
Required: true,
|
||||||
Name: "log-channel",
|
Name: "log-channel",
|
||||||
Description: "The channel to send log messages and errors to",
|
Description: "The channel to send log messages and errors to",
|
||||||
ChannelTypes: []dgo.ChannelType{
|
ChannelTypes: []dgo.ChannelType{
|
||||||
dgo.ChannelTypeGuildText,
|
dgo.ChannelTypeGuildText,
|
||||||
@@ -59,6 +64,7 @@ func (c loggerConfigChannel) Info() *dgo.ApplicationCommand {
|
|||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c loggerConfigChannel) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
func (c loggerConfigChannel) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||||
opts := getOptions(ic.ApplicationCommandData().Options)
|
opts := getOptions(ic.ApplicationCommandData().Options)
|
||||||
|
|
||||||
@@ -91,15 +97,17 @@ func (c loggerConfigChannel) Handle(s *dgo.Session, ic *dgo.InteractionCreate) e
|
|||||||
Type: dgo.InteractionResponseChannelMessageWithSource,
|
Type: dgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &dgo.InteractionResponseData{
|
Data: &dgo.InteractionResponseData{
|
||||||
Content: fmt.Sprintf("Logging channel changed to %s", *guild.Config.LoggingChannel),
|
Content: fmt.Sprintf("Logging channel changed to %s", *guild.Config.LoggingChannel),
|
||||||
Flags: dgo.MessageFlagsEphemeral,
|
Flags: dgo.MessageFlagsEphemeral,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c loggerConfigChannel) Components() []Component {
|
func (c loggerConfigChannel) Components() []Component {
|
||||||
return []Component{}
|
return []Component{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c loggerConfigChannel) Subcommands() []Command {
|
func (c loggerConfigChannel) Subcommands() []Command {
|
||||||
return []Command{}
|
return []Command{}
|
||||||
}
|
}
|
||||||
@@ -107,6 +115,7 @@ func (c loggerConfigChannel) Subcommands() []Command {
|
|||||||
type loggerConfigLevel struct {
|
type loggerConfigLevel struct {
|
||||||
db gconf.DB
|
db gconf.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c loggerConfigLevel) Info() *dgo.ApplicationCommand {
|
func (c loggerConfigLevel) Info() *dgo.ApplicationCommand {
|
||||||
var permissions int64 = dgo.PermissionAdministrator
|
var permissions int64 = dgo.PermissionAdministrator
|
||||||
return &dgo.ApplicationCommand{
|
return &dgo.ApplicationCommand{
|
||||||
@@ -114,9 +123,9 @@ func (c loggerConfigLevel) Info() *dgo.ApplicationCommand {
|
|||||||
Description: "Change logging channel",
|
Description: "Change logging channel",
|
||||||
DefaultMemberPermissions: &permissions,
|
DefaultMemberPermissions: &permissions,
|
||||||
Options: []*dgo.ApplicationCommandOption{{
|
Options: []*dgo.ApplicationCommandOption{{
|
||||||
Type: dgo.ApplicationCommandOptionString,
|
Type: dgo.ApplicationCommandOptionString,
|
||||||
Required: true,
|
Required: true,
|
||||||
Name: "log-level",
|
Name: "log-level",
|
||||||
Description: "The logging level of messages and errors",
|
Description: "The logging level of messages and errors",
|
||||||
Choices: []*dgo.ApplicationCommandOptionChoice{
|
Choices: []*dgo.ApplicationCommandOptionChoice{
|
||||||
{Name: "Debug", Value: slog.LevelDebug.String()},
|
{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 {
|
func (c loggerConfigLevel) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
|
||||||
opts := getOptions(ic.ApplicationCommandData().Options)
|
opts := getOptions(ic.ApplicationCommandData().Options)
|
||||||
|
|
||||||
@@ -161,15 +171,17 @@ func (c loggerConfigLevel) Handle(s *dgo.Session, ic *dgo.InteractionCreate) err
|
|||||||
Type: dgo.InteractionResponseChannelMessageWithSource,
|
Type: dgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &dgo.InteractionResponseData{
|
Data: &dgo.InteractionResponseData{
|
||||||
Content: fmt.Sprintf("Logging level changed to %s", l),
|
Content: fmt.Sprintf("Logging level changed to %s", l),
|
||||||
Flags: dgo.MessageFlagsEphemeral,
|
Flags: dgo.MessageFlagsEphemeral,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c loggerConfigLevel) Components() []Component {
|
func (c loggerConfigLevel) Components() []Component {
|
||||||
return []Component{}
|
return []Component{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c loggerConfigLevel) Subcommands() []Command {
|
func (c loggerConfigLevel) Subcommands() []Command {
|
||||||
return []Command{}
|
return []Command{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,13 +16,14 @@ type Error interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type defaultError struct {
|
type defaultError struct {
|
||||||
err string
|
err string
|
||||||
args []slog.Attr
|
args []slog.Attr
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewError(err string, args ...slog.Attr) defaultError {
|
func NewError(err string, args ...slog.Attr) defaultError {
|
||||||
return defaultError{err, args}
|
return defaultError{err, args}
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(err string, args ...slog.Attr) defaultError {
|
func New(err string, args ...slog.Attr) defaultError {
|
||||||
return NewError(err, args...)
|
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) {
|
func (err defaultError) LogReply(l *slog.Logger, s *dgo.Session, m *dgo.Message) {
|
||||||
err.Reply(s,m)
|
err.Reply(s, m)
|
||||||
err.Log(l)
|
err.Log(l)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ func (b *Bot) registerEventHandlers() {
|
|||||||
events.NewMessageCreate(b.db, b.translator).Serve,
|
events.NewMessageCreate(b.db, b.translator).Serve,
|
||||||
events.NewMessageEdit(b.db, b.translator).Serve,
|
events.NewMessageEdit(b.db, b.translator).Serve,
|
||||||
events.NewReady(b.logger, b.db).Serve,
|
events.NewReady(b.logger, b.db).Serve,
|
||||||
|
events.NewThreadCreate(b.db, b.translator).Serve,
|
||||||
}
|
}
|
||||||
for _, h := range ehs {
|
for _, h := range ehs {
|
||||||
b.session.AddHandler(h)
|
b.session.AddHandler(h)
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ type GuildCreate struct {
|
|||||||
func NewGuildCreate(log *slog.Logger, db gconf.DB) GuildCreate {
|
func NewGuildCreate(log *slog.Logger, db gconf.DB) GuildCreate {
|
||||||
return GuildCreate{log, db}
|
return GuildCreate{log, db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h GuildCreate) Serve(s *dgo.Session, e *dgo.GuildCreate) {
|
func (h GuildCreate) Serve(s *dgo.Session, e *dgo.GuildCreate) {
|
||||||
err := h.db.GuildInsert(gdb.Guild[gconf.ConfigString]{ID: e.Guild.ID})
|
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] {
|
func NewReady(log *slog.Logger, db gconf.DB) EventHandler[*dgo.Ready] {
|
||||||
return Ready{log, db}
|
return Ready{log, db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h Ready) Serve(s *dgo.Session, e *dgo.Ready) {
|
func (h Ready) Serve(s *dgo.Session, e *dgo.Ready) {
|
||||||
for _, g := range e.Guilds {
|
for _, g := range e.Guilds {
|
||||||
err := h.db.GuildInsert(gdb.Guild[gconf.ConfigString]{ID: g.ID})
|
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 {
|
func NewMessageCreate(db gconf.DB, t translator.Translator) MessageCreate {
|
||||||
return MessageCreate{db, t}
|
return MessageCreate{db, t}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h MessageCreate) Serve(s *dgo.Session, ev *dgo.MessageCreate) {
|
func (h MessageCreate) Serve(s *dgo.Session, ev *dgo.MessageCreate) {
|
||||||
if ev.Message.Author.Bot {
|
if ev.Message.Author.Bot {
|
||||||
return
|
return
|
||||||
@@ -127,7 +128,6 @@ func (h MessageCreate) Serve(s *dgo.Session, ev *dgo.MessageCreate) {
|
|||||||
}(c)
|
}(c)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessageEdit struct {
|
type MessageEdit struct {
|
||||||
@@ -138,6 +138,7 @@ type MessageEdit struct {
|
|||||||
func NewMessageEdit(db gconf.DB, t translator.Translator) MessageEdit {
|
func NewMessageEdit(db gconf.DB, t translator.Translator) MessageEdit {
|
||||||
return MessageEdit{db, t}
|
return MessageEdit{db, t}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h MessageEdit) Serve(s *dgo.Session, ev *dgo.MessageUpdate) {
|
func (h MessageEdit) Serve(s *dgo.Session, ev *dgo.MessageUpdate) {
|
||||||
if ev.Message.Author.Bot {
|
if ev.Message.Author.Bot {
|
||||||
return
|
return
|
||||||
@@ -221,11 +222,10 @@ func (h MessageEdit) Serve(s *dgo.Session, ev *dgo.MessageUpdate) {
|
|||||||
}(m)
|
}(m)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUserWebhook(s *dgo.Session, channelID string, user *dgo.User) (*dgo.Webhook, error) {
|
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)
|
ws, err := s.ChannelWebhooks(channelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -247,7 +247,7 @@ func getUserWebhook(s *dgo.Session, channelID string, user *dgo.User) (*dgo.Webh
|
|||||||
return w, nil
|
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)
|
msg, err := db.Message(m.GuildID, m.ChannelID, m.ID)
|
||||||
|
|
||||||
if e.Is(err, guilddb.ErrNotFound) {
|
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
|
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)
|
msg, err := db.Message(m.GuildID, m.ChannelID, m.ID)
|
||||||
|
|
||||||
if e.Is(err, guilddb.ErrNotFound) {
|
if e.Is(err, guilddb.ErrNotFound) {
|
||||||
if err :=db.MessageInsert(guilddb.NewTranslatedMessage(
|
if err := db.MessageInsert(guilddb.NewTranslatedMessage(
|
||||||
m.GuildID,
|
m.GuildID,
|
||||||
m.ChannelID,
|
m.ChannelID,
|
||||||
m.ID,
|
m.ID,
|
||||||
@@ -278,7 +281,7 @@ func getTranslatedMessage(db gconf.DB, m, original *dgo.Message, lang lang.Lang
|
|||||||
)); err != nil {
|
)); err != nil {
|
||||||
return guilddb.Message{}, err
|
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 {
|
if err != nil {
|
||||||
return guilddb.Message{}, err
|
return guilddb.Message{}, err
|
||||||
}
|
}
|
||||||
@@ -287,5 +290,4 @@ func getTranslatedMessage(db gconf.DB, m, original *dgo.Message, lang lang.Lang
|
|||||||
}
|
}
|
||||||
|
|
||||||
return msg, nil
|
return msg, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,12 +13,14 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ConfigString struct {
|
type ConfigString struct {
|
||||||
LoggingChannel *string `json:"logging_channel"`
|
LoggingChannel *string `json:"logging_channel"`
|
||||||
LoggingLevel *slog.Level `json:"logging_level"`
|
LoggingLevel *slog.Level `json:"logging_level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Guild gdb.Guild[ConfigString]
|
type (
|
||||||
type DB gdb.GuildDB[ConfigString]
|
Guild gdb.Guild[ConfigString]
|
||||||
|
DB gdb.GuildDB[ConfigString]
|
||||||
|
)
|
||||||
|
|
||||||
func (g Guild) GetConfig(s *dgo.Session) (*Config, error) {
|
func (g Guild) GetConfig(s *dgo.Session) (*Config, error) {
|
||||||
var l *slog.Logger
|
var l *slog.Logger
|
||||||
@@ -36,7 +38,7 @@ func (g Guild) GetConfig(s *dgo.Session) (*Config, error) {
|
|||||||
} else {
|
} else {
|
||||||
lv = slog.LevelInfo
|
lv = slog.LevelInfo
|
||||||
}
|
}
|
||||||
l = slog.New(NewGuildHandler(s,c, &slog.HandlerOptions{
|
l = slog.New(NewGuildHandler(s, c, &slog.HandlerOptions{
|
||||||
Level: lv,
|
Level: lv,
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ func NewGuildHandler(s *dgo.Session, c *dgo.Channel, opts *slog.HandlerOptions)
|
|||||||
type disabledHandler struct {
|
type disabledHandler struct {
|
||||||
*slog.TextHandler
|
*slog.TextHandler
|
||||||
}
|
}
|
||||||
func (_ disabledHandler) Enabled(_ context.Context,_ slog.Level) bool {
|
|
||||||
|
func (_ disabledHandler) Enabled(_ context.Context, _ slog.Level) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package guilddb
|
package guilddb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"dislate/internals/translator/lang"
|
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"dislate/internals/translator/lang"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Guild[C any] struct {
|
type Guild[C any] struct {
|
||||||
@@ -38,6 +39,7 @@ type Message struct {
|
|||||||
func NewMessage(GuildID, ChannelID, ID string, lang lang.Language) Message {
|
func NewMessage(GuildID, ChannelID, ID string, lang lang.Language) Message {
|
||||||
return Message{GuildID, ChannelID, ID, lang, nil, nil}
|
return Message{GuildID, ChannelID, ID, lang, nil, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTranslatedMessage(
|
func NewTranslatedMessage(
|
||||||
GuildID, ChannelID, ID string,
|
GuildID, ChannelID, ID string,
|
||||||
lang lang.Language,
|
lang lang.Language,
|
||||||
@@ -143,9 +145,11 @@ type GuildDB[C any] interface {
|
|||||||
GuildUpdate(g Guild[C]) error
|
GuildUpdate(g Guild[C]) error
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrNoAffect = errors.New("Not able to affect anything in the database")
|
var (
|
||||||
var ErrNotFound = errors.New("Object not found in the database")
|
ErrNoAffect = errors.New("Not able to affect anything in the database")
|
||||||
var ErrPreconditionFailed = errors.New("Precondition failed")
|
ErrNotFound = errors.New("Object not found in the database")
|
||||||
var ErrInvalidObject = errors.New("Invalid object")
|
ErrPreconditionFailed = errors.New("Precondition failed")
|
||||||
var ErrInternal = errors.New("Internal error while trying to use database")
|
ErrInvalidObject = errors.New("Invalid object")
|
||||||
var ErrConfigParsing = errors.New("Error while parsing Guild's config")
|
ErrInternal = errors.New("Internal error while trying to use database")
|
||||||
|
ErrConfigParsing = errors.New("Error while parsing Guild's config")
|
||||||
|
)
|
||||||
|
|||||||
@@ -159,7 +159,6 @@ func (db *SQLiteDB[C]) MessageDelete(m Message) error {
|
|||||||
DELETE channels
|
DELETE channels
|
||||||
WHERE "GuildID" = $1 AND "OriginChannelID" = $2 AND "OriginID" = $3
|
WHERE "GuildID" = $1 AND "OriginChannelID" = $2 AND "OriginID" = $3
|
||||||
`, m.GuildID, m.ChannelID, m.ID)
|
`, m.GuildID, m.ChannelID, m.ID)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Join(ErrInternal, err)
|
return errors.Join(ErrInternal, err)
|
||||||
}
|
}
|
||||||
@@ -200,7 +199,6 @@ func (db *SQLiteDB[C]) selectMessages(query string, args ...any) ([]Message, err
|
|||||||
SELECT GuildID, ChannelID, ID, Language, OriginChannelID, OriginID FROM messages
|
SELECT GuildID, ChannelID, ID, Language, OriginChannelID, OriginID FROM messages
|
||||||
%s
|
%s
|
||||||
`, query), args...)
|
`, query), args...)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []Message{}, errors.Join(ErrInternal, err)
|
return []Message{}, errors.Join(ErrInternal, err)
|
||||||
}
|
}
|
||||||
@@ -283,7 +281,6 @@ func (db *SQLiteDB[C]) ChannelDelete(c Channel) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *SQLiteDB[C]) ChannelGroup(guildID, channelID string) (ChannelGroup, error) {
|
func (db *SQLiteDB[C]) ChannelGroup(guildID, channelID string) (ChannelGroup, error) {
|
||||||
|
|
||||||
var j string
|
var j string
|
||||||
err := db.sql.QueryRow(fmt.Sprintf(`
|
err := db.sql.QueryRow(fmt.Sprintf(`
|
||||||
SELECT Channels FROM channelGroups, json_each(Channels)
|
SELECT Channels FROM channelGroups, json_each(Channels)
|
||||||
@@ -433,7 +430,6 @@ func (db *SQLiteDB[C]) selectChannels(query string, args ...any) ([]Channel, err
|
|||||||
SELECT GuildID, ID, Language FROM channels
|
SELECT GuildID, ID, Language FROM channels
|
||||||
%s
|
%s
|
||||||
`, query), args...)
|
`, query), args...)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []Channel{}, errors.Join(ErrInternal, err)
|
return []Channel{}, errors.Join(ErrInternal, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,9 +14,11 @@ type MockTranslator struct{}
|
|||||||
func NewMockTranslator() MockTranslator {
|
func NewMockTranslator() MockTranslator {
|
||||||
return MockTranslator{}
|
return MockTranslator{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t MockTranslator) Translate(from, to lang.Language, text string) (string, error) {
|
func (t MockTranslator) Translate(from, to lang.Language, text string) (string, error) {
|
||||||
return text, nil
|
return text, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t MockTranslator) Detect(text string) (lang.Language, error) {
|
func (t MockTranslator) Detect(text string) (lang.Language, error) {
|
||||||
return lang.EN, nil
|
return lang.EN, nil
|
||||||
}
|
}
|
||||||
|
|||||||
6
main.go
6
main.go
@@ -23,8 +23,10 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// var translation_provider = flag.String("tprovider", string(GOOGLE_TRANSLATE), "Translation provider")
|
// var translation_provider = flag.String("tprovider", string(GOOGLE_TRANSLATE), "Translation provider")
|
||||||
var database_file = flag.String("db", "file:./guild.db", "SQLite database file/location")
|
var (
|
||||||
var discord_token = flag.String("token", os.Getenv("DISCORD_TOKEN"), "Discord bot authentication token")
|
database_file = flag.String("db", "file:./guild.db", "SQLite database file/location")
|
||||||
|
discord_token = flag.String("token", os.Getenv("DISCORD_TOKEN"), "Discord bot authentication token")
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|||||||
6
makefile
6
makefile
@@ -3,6 +3,12 @@ PORT?=8080
|
|||||||
lint:
|
lint:
|
||||||
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1 run
|
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1 run
|
||||||
|
|
||||||
|
fmt:
|
||||||
|
go fmt .
|
||||||
|
go run golang.org/x/tools/cmd/goimports@v0.24.0 -l -w .
|
||||||
|
go run github.com/segmentio/golines@v0.12.2 -l -w .
|
||||||
|
go run mvdan.cc/gofumpt@v0.7.0 -l -w .
|
||||||
|
|
||||||
build:
|
build:
|
||||||
go build -o bin/dislate
|
go build -o bin/dislate
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user