delete dev.sh and helpers from the models package, use gopgutil to build conditions in filters
This commit is contained in:
parent
ab6bbfa9c2
commit
1c9c5c6f6a
|
@ -2,7 +2,7 @@ package discord
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
@ -147,7 +147,7 @@ func (s *Session) init() error {
|
|||
|
||||
err := s.dg.Open()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening ws connection: %s", err.Error())
|
||||
return errors.Wrap(err, "error opening ws connection")
|
||||
}
|
||||
|
||||
if err := s.UpdateStatus(s.cfg.Status); err != nil {
|
||||
|
@ -176,7 +176,7 @@ func (s *Session) SendEmbed(channelID string, message *discordgo.MessageEmbed) e
|
|||
baseNumberOfCharacters += len(message.Footer.Text)
|
||||
}
|
||||
|
||||
splittedFields := [][]*discordgo.MessageEmbedField{}
|
||||
var splittedFields [][]*discordgo.MessageEmbedField
|
||||
characters := baseNumberOfCharacters
|
||||
fromIndex := 0
|
||||
fieldsLen := len(fields)
|
||||
|
@ -231,8 +231,8 @@ func (s *Session) handleNewMessage(_ *discordgo.Session, m *discordgo.MessageCre
|
|||
return
|
||||
}
|
||||
|
||||
splitted := strings.Split(m.Content, " ")
|
||||
args := splitted[1:]
|
||||
parts := strings.Split(m.Content, " ")
|
||||
args := parts[1:]
|
||||
svr := &models.Server{
|
||||
ID: m.GuildID,
|
||||
Lang: message.GetDefaultLanguage().String(),
|
||||
|
@ -247,7 +247,7 @@ func (s *Session) handleNewMessage(_ *discordgo.Session, m *discordgo.MessageCre
|
|||
localizer: message.NewLocalizer(svr.Lang),
|
||||
}
|
||||
|
||||
cmd := Command(splitted[0])
|
||||
cmd := Command(parts[0])
|
||||
h := s.handlers.find(cmd)
|
||||
if h != nil {
|
||||
if h.requireAdmPermissions {
|
||||
|
|
|
@ -255,7 +255,7 @@ func (s *Session) handleHelpCommand(ctx *commandCtx, m *discordgo.MessageCreate,
|
|||
SetURL("https://dcbot.tribalwarshelp.com/").
|
||||
SetDescription(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpDescription,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpDescription, "Commands offered by bot"),
|
||||
DefaultMessage: message.FallbackMsg(message.HelpDescription, "Commands offered by the bot"),
|
||||
})).
|
||||
AddField(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpForAllUsers,
|
||||
|
|
1
go.mod
1
go.mod
|
@ -5,6 +5,7 @@ go 1.16
|
|||
require (
|
||||
github.com/Kichiyaki/appmode v0.0.0-20210502105643-0a26207c548d
|
||||
github.com/Kichiyaki/go-pg-logrus-query-logger/v10 v10.0.0-20210428180109-fb97298564d9
|
||||
github.com/Kichiyaki/gopgutil/v10 v10.0.0-20210505093434-655fa2df248f
|
||||
github.com/Kichiyaki/goutil v0.0.0-20210504132659-3d843a787db7
|
||||
github.com/bwmarrin/discordgo v0.22.0
|
||||
github.com/dustin/go-humanize v1.0.0
|
||||
|
|
11
main.go
11
main.go
|
@ -3,7 +3,6 @@ package main
|
|||
import (
|
||||
"github.com/Kichiyaki/appmode"
|
||||
"github.com/Kichiyaki/goutil/envutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
|
@ -107,9 +106,13 @@ func main() {
|
|||
}
|
||||
defer sess.Close()
|
||||
|
||||
c := cron.New(cron.WithChain(
|
||||
cron.SkipIfStillRunning(cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))),
|
||||
))
|
||||
c := cron.New(
|
||||
cron.WithChain(
|
||||
cron.SkipIfStillRunning(
|
||||
cron.PrintfLogger(logrus.StandardLogger()),
|
||||
),
|
||||
),
|
||||
)
|
||||
_cron.Attach(c, _cron.Config{
|
||||
ServerRepo: serverRepo,
|
||||
ObservationRepo: observationRepo,
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
package models
|
||||
|
||||
import "strings"
|
||||
|
||||
func addPrefixToColumnName(column, prefix string) string {
|
||||
if prefix != "" && !strings.HasPrefix(column, prefix+".") {
|
||||
column = wrapStringInDoubleQuotes(prefix) + "." + wrapStringInDoubleQuotes(column)
|
||||
} else {
|
||||
column = wrapStringInDoubleQuotes(column)
|
||||
}
|
||||
return column
|
||||
}
|
||||
|
||||
func wrapStringInDoubleQuotes(str string) string {
|
||||
return `"` + str + `"`
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"github.com/Kichiyaki/gopgutil/v10"
|
||||
"github.com/tribalwarshelp/shared/tw/twmodel"
|
||||
"time"
|
||||
|
||||
|
@ -38,24 +39,19 @@ type ObservationFilter struct {
|
|||
DefaultFilter
|
||||
}
|
||||
|
||||
func (f *ObservationFilter) ApplyWithPrefix(prefix string) func(q *orm.Query) (*orm.Query, error) {
|
||||
return func(q *orm.Query) (*orm.Query, error) {
|
||||
if len(f.ID) > 0 {
|
||||
column := addPrefixToColumnName("id", prefix)
|
||||
q = q.Where(column+" = ANY(?)", pg.Array(f.ID))
|
||||
}
|
||||
if len(f.Server) > 0 {
|
||||
column := addPrefixToColumnName("server", prefix)
|
||||
q = q.Where(column+" = ANY(?)", pg.Array(f.Server))
|
||||
}
|
||||
if len(f.GroupID) > 0 {
|
||||
column := addPrefixToColumnName("group_id", prefix)
|
||||
q = q.Where(column+" = ANY(?)", pg.Array(f.GroupID))
|
||||
}
|
||||
return f.DefaultFilter.Apply(q)
|
||||
func (f *ObservationFilter) ApplyWithPrefix(q *orm.Query, alias string) (*orm.Query, error) {
|
||||
if len(f.ID) > 0 {
|
||||
q = q.Where(gopgutil.BuildConditionArray("?"), gopgutil.AddAliasToColumnName("id", alias), pg.Array(f.ID))
|
||||
}
|
||||
if len(f.Server) > 0 {
|
||||
q = q.Where(gopgutil.BuildConditionArray("?"), gopgutil.AddAliasToColumnName("server", alias), pg.Array(f.Server))
|
||||
}
|
||||
if len(f.GroupID) > 0 {
|
||||
q = q.Where(gopgutil.BuildConditionArray("?"), gopgutil.AddAliasToColumnName("group_id", alias), pg.Array(f.GroupID))
|
||||
}
|
||||
return f.DefaultFilter.Apply(q)
|
||||
}
|
||||
|
||||
func (f *ObservationFilter) Apply(q *orm.Query) (*orm.Query, error) {
|
||||
return f.ApplyWithPrefix("observation")(q)
|
||||
return f.ApplyWithPrefix(q, "observation")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"github.com/Kichiyaki/gopgutil/v10"
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
@ -19,16 +20,13 @@ type ServerFilter struct {
|
|||
DefaultFilter
|
||||
}
|
||||
|
||||
func (f *ServerFilter) ApplyWithPrefix(prefix string) func(q *orm.Query) (*orm.Query, error) {
|
||||
return func(q *orm.Query) (*orm.Query, error) {
|
||||
if len(f.ID) > 0 {
|
||||
column := addPrefixToColumnName("id", prefix)
|
||||
q = q.Where(column+" = ANY(?)", pg.Array(f.ID))
|
||||
}
|
||||
return f.DefaultFilter.Apply(q)
|
||||
func (f *ServerFilter) ApplyWithPrefix(q *orm.Query, alias string) (*orm.Query, error) {
|
||||
if len(f.ID) > 0 {
|
||||
q = q.Where(gopgutil.BuildConditionArray("?"), gopgutil.AddAliasToColumnName("id", alias), pg.Array(f.ID))
|
||||
}
|
||||
return f.DefaultFilter.Apply(q)
|
||||
}
|
||||
|
||||
func (f *ServerFilter) Apply(q *orm.Query) (*orm.Query, error) {
|
||||
return f.ApplyWithPrefix("server")(q)
|
||||
return f.ApplyWithPrefix(q, "server")
|
||||
}
|
||||
|
|
Reference in New Issue