Skip to content

Commit

Permalink
update: データベースを最適化
Browse files Browse the repository at this point in the history
  • Loading branch information
ikafly144 committed May 4, 2023
1 parent f07ec6b commit 3fd1ef5
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 36 deletions.
2 changes: 1 addition & 1 deletion bot/commands/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ func pollComponentChangeChoiceEmoji(b *botlib.Bot[db.DB]) func(e *events.Compone
AuthorID: &e.Member().User.ID,
Check: func(ctx *events.MessageCreate) bool {
b.Logger.Debug("check")
return emoji.Twemoji.MatchString(ctx.Message.Content)
return emoji.MatchString(ctx.Message.Content)
},
Handler: func(event *events.MessageCreate) error {
b.Logger.Debug("called message")
Expand Down
8 changes: 4 additions & 4 deletions bot/commands/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func rolePanelListHandler(b *botlib.Bot[db.DB]) func(event *events.ApplicationCo
mes, err := event.Client().Rest().GetMessage(rp.ChannelID, rp.MessageID)
if err != nil {
delete(gData.RolePanel, u)
err := b.DB.RolePanel().Remove(u)
err := b.DB.RolePanel().Del(u)
if err != nil {
return botlib.ReturnErr(event, err)
}
Expand Down Expand Up @@ -258,7 +258,7 @@ func roleComponentDeleteHandler(b *botlib.Bot[db.DB]) func(event *events.Compone
if err != nil {
return botlib.ReturnErr(event, err)
}
err = b.DB.RolePanel().Remove(panelID)
err = b.DB.RolePanel().Del(panelID)
if err != nil {
return botlib.ReturnErr(event, err)
}
Expand Down Expand Up @@ -716,10 +716,10 @@ func roleComponentEditRoleEmojiHandler(b *botlib.Bot[db.DB]) func(event *events.
ChannelID: channel.ID,
AuthorID: &author.User.ID,
Handler: func(event *events.MessageCreate) error {
if event.Message.Author.ID != author.User.ID || !emoji.Twemoji.MatchString(event.Message.Content) {
if event.Message.Author.ID != author.User.ID || !emoji.MatchString(event.Message.Content) {
return nil
}
matches := emoji.Twemoji.FindAllString(event.Message.Content, -1)
matches := emoji.FindAllString(event.Message.Content)
role.Emoji = botlib.ParseComponentEmoji(matches[0])
remove()
removeButton()
Expand Down
11 changes: 5 additions & 6 deletions bot/db/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import (
"github.com/disgoorg/disgo/discord"
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
"github.com/sabafly/sabafly-lib/db"
)

type CalcDB interface {
Get(uuid.UUID) (Calc, error)
Set(uuid.UUID, Calc) error
Remove(uuid.UUID) error
}
type CalcDB db.DBRecord[Calc, uuid.UUID]

var _ CalcDB = (*CalcDBImpl)(nil)

type CalcDBImpl struct {
db *redis.Client
Expand Down Expand Up @@ -49,7 +48,7 @@ func (c *CalcDBImpl) Set(id uuid.UUID, data Calc) error {
return nil
}

func (c *CalcDBImpl) Remove(id uuid.UUID) error {
func (c *CalcDBImpl) Del(id uuid.UUID) error {
res := c.db.Del(context.TODO(), id.String())
if err := res.Err(); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions bot/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func SetupDatabase(cfg DBConfig) (DB, error) {
}, nil
}

var _ DB = (*dbImpl)(nil)

type dbImpl struct {
db *redis.Client
pollCreate *pollCreateDBImpl
Expand Down
11 changes: 5 additions & 6 deletions bot/db/guild_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"github.com/disgoorg/snowflake/v2"
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
"github.com/sabafly/sabafly-lib/db"
)

type GuildDataDB interface {
Get(snowflake.ID) (GuildData, error)
Set(snowflake.ID, GuildData) error
Remove(snowflake.ID) error
}
type GuildDataDB db.DBRecord[GuildData, snowflake.ID]

var _ GuildDataDB = (*guildDataDBImpl)(nil)

type guildDataDBImpl struct {
db *redis.Client
Expand Down Expand Up @@ -44,7 +43,7 @@ func (g *guildDataDBImpl) Set(id snowflake.ID, data GuildData) error {
return nil
}

func (g *guildDataDBImpl) Remove(id snowflake.ID) error {
func (g *guildDataDBImpl) Del(id snowflake.ID) error {
res := g.db.HDel(context.TODO(), "guild-data", id.String())
if err := res.Err(); err != nil {
return err
Expand Down
11 changes: 5 additions & 6 deletions bot/db/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (

"github.com/go-redis/redis/v8"
"github.com/google/uuid"
"github.com/sabafly/sabafly-lib/db"
)

type InteractionsDB interface {
Get(id uuid.UUID) (string, error)
Set(id uuid.UUID, token string) error
Remove(id uuid.UUID) error
}
type InteractionsDB db.DBRecord[string, uuid.UUID]

var _ InteractionsDB = (*interactionsImpl)(nil)

type interactionsImpl struct {
db *redis.Client
Expand All @@ -38,7 +37,7 @@ func (i *interactionsImpl) Set(id uuid.UUID, token string) error {
return nil
}

func (i *interactionsImpl) Remove(id uuid.UUID) error {
func (i *interactionsImpl) Del(id uuid.UUID) error {
res := i.db.Del(context.TODO(), "interactions"+id.String())
if err := res.Err(); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions bot/db/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type PollDB interface {
Del(id uuid.UUID) error
}

var _ PollDB = (*pollDBImpl)(nil)

type pollDBImpl struct {
db *redis.Client
}
Expand Down
2 changes: 2 additions & 0 deletions bot/db/poll_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (

type PollCreateDB db.DBRecord[PollCreate, uuid.UUID]

var _ PollCreateDB = (*pollCreateDBImpl)(nil)

type pollCreateDBImpl struct {
db *redis.Client
}
Expand Down
6 changes: 4 additions & 2 deletions bot/db/role_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
type RolePanelDB interface {
Get(uuid.UUID) (RolePanel, error)
Set(RolePanel) error
Remove(uuid.UUID) error
Del(uuid.UUID) error
}

var _ RolePanelDB = (*rolePanelDBImpl)(nil)

type rolePanelDBImpl struct {
db *redis.Client
}
Expand Down Expand Up @@ -47,7 +49,7 @@ func (r *rolePanelDBImpl) Set(data RolePanel) error {
return nil
}

func (r *rolePanelDBImpl) Remove(id uuid.UUID) error {
func (r *rolePanelDBImpl) Del(id uuid.UUID) error {
res := r.db.HDel(context.TODO(), "rolepanel", id.String())
if err := res.Err(); err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions bot/db/role_panel_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
type RolePanelCreateDB interface {
Get(uuid.UUID) (RolePanelCreate, error)
Set(RolePanelCreate) error
Remove(uuid.UUID) error
Del(uuid.UUID) error
}

var _ RolePanelCreateDB = (*rolePanelCreateDBImpl)(nil)

type rolePanelCreateDBImpl struct {
db *redis.Client
}
Expand Down Expand Up @@ -48,7 +50,7 @@ func (r *rolePanelCreateDBImpl) Set(data RolePanelCreate) error {
return nil
}

func (r *rolePanelCreateDBImpl) Remove(id uuid.UUID) error {
func (r *rolePanelCreateDBImpl) Del(id uuid.UUID) error {
res := r.db.Del(context.TODO(), "rolepanelcreate"+id.String())
if err := res.Err(); err != nil {
return err
Expand Down
9 changes: 4 additions & 5 deletions bot/gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ var (
)

func init() {
if _, err := translate.LoadTranslations("lang/"); err != nil {
panic(err)
}
botlib.BotName = "gobot"
botlib.Color = 0x89d53c
}

func Run(file_path string) {

func Run(file_path, lang_path string) {
if _, err := translate.LoadTranslations(lang_path); err != nil {
panic(err)
}
cfg, err := botlib.LoadConfig(file_path)
if err != nil {
panic("failed to load config: " + err.Error())
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/go-redis/redis/v8 v8.11.5
github.com/google/uuid v1.3.0
github.com/mattn/go-colorable v0.1.13
github.com/sabafly/sabafly-lib v0.3.1
github.com/sabafly/sabafly-lib v0.3.2
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.7.0
)
Expand All @@ -20,11 +20,13 @@ require (
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/disgoorg/log v1.2.0 // indirect
github.com/disgoorg/paginator v0.0.0-20230104145353-f988d828ede9 // indirect
github.com/forPelevin/gomoji v1.1.8 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/nicksnyder/go-i18n/v2 v2.2.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.8.0 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/disgoorg/paginator v0.0.0-20230104145353-f988d828ede9 h1:h7feN1V8sRJG
github.com/disgoorg/paginator v0.0.0-20230104145353-f988d828ede9/go.mod h1:k+g1jz3HxI97c5IGsScxOZYqhjZvDkNKLma7pUXiGcw=
github.com/disgoorg/snowflake/v2 v2.0.1 h1:CuUxGLwggUxEswZOmZ+mZ5i0xSumQdXW9tXW7uGqe+0=
github.com/disgoorg/snowflake/v2 v2.0.1/go.mod h1:SPU9c2CNn5DSyb86QcKtdZgix9osEtKrHLW4rMhfLCs=
github.com/forPelevin/gomoji v1.1.8 h1:JElzDdt0TyiUlecy6PfITDL6eGvIaxqYH1V52zrd0qQ=
github.com/forPelevin/gomoji v1.1.8/go.mod h1:8+Z3KNGkdslmeGZBC3tCrwMrcPy5GRzAD+gL9NAwMXg=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
Expand All @@ -43,9 +45,11 @@ github.com/pelletier/go-toml/v2 v2.0.7 h1:muncTPStnKRos5dpVKULv2FVd4bMOhNePj9Cjg
github.com/pelletier/go-toml/v2 v2.0.7/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
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/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sabafly/sabafly-lib v0.3.1 h1:EWuzubV5u2XzHPsn1AfYilC+zXwN+extdOWGspD5+QE=
github.com/sabafly/sabafly-lib v0.3.1/go.mod h1:D4wjpKn0hyB7VnwCwkRoKIZrRO5HQCeGv6lliBLs1co=
github.com/sabafly/sabafly-lib v0.3.2 h1:yOppLzUAQ+CMd6zc1OrSqYSpulK1xi36Czylzrjn9hA=
github.com/sabafly/sabafly-lib v0.3.2/go.mod h1:a8c+NK/M8VvE9snCzqttYwRc2oH0F69BdjCI29Gs7Yo=
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b h1:qYTY2tN72LhgDj2rtWG+LI6TXFl2ygFQQ4YezfVaGQE=
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b/go.mod h1:/pA7k3zsXKdjjAiUhB5CjuKib9KJGCaLvZwtxGC8U0s=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func main() {

func init() {
botCmd.Flags().String("config", "config.json", "config file of bot")
botCmd.Flag("config").Shorthand = "c"
botCmd.Flags().String("lang", "lang", "lang file path")
botCmd.Flag("lang").Shorthand = "l"
root.AddCommand(botCmd)
}

Expand All @@ -35,6 +38,6 @@ var botCmd = &cobra.Command{
"config",
},
Run: func(cmd *cobra.Command, args []string) {
bot.Run(cmd.Flag("config").Value.String())
bot.Run(cmd.Flag("config").Value.String(), cmd.Flag("lang").Value.String())
},
}

0 comments on commit 3fd1ef5

Please sign in to comment.