Skip to content

Commit

Permalink
シャードを使うように (#102)
Browse files Browse the repository at this point in the history
* depend: 依存関係を更新

* fix: シャードマネージャーで起動するよう変更

* fix: 各シャードのレイテンシを返すように

* improve: dbセットアップ時に疎通確認

* improve: シャード数を表示するように
  • Loading branch information
ikafly144 authored May 15, 2023
1 parent ba5210c commit ef507ae
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 13 deletions.
6 changes: 4 additions & 2 deletions bot/commands/ping.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package commands

import (
"fmt"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/sabafly/gobot/bot/db"
Expand Down Expand Up @@ -29,8 +31,8 @@ func pingHandler(b *botlib.Bot[db.DB]) handler.CommandHandler {
Title: "🏓 " + translate.Message(e.Locale(), "command_text_ping_response_embed_title"),
Fields: []discord.EmbedField{
{
Name: "DiscordAPI",
Value: e.Client().Gateway().Latency().String(),
Name: fmt.Sprintf("DiscordAPI(#%d)", e.ShardID()),
Value: e.Client().ShardManager().Shard(e.ShardID()).Latency().String(),
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion bot/commands/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ func pollComponentChangeChoiceEmoji(b *botlib.Bot[db.DB]) func(e *events.Compone
var removeButton func()
remove = b.Handler.AddMessage(handler.Message{
UUID: uuid.New(),
ChannelID: channel.ID,
ChannelID: &channel.ID,
AuthorID: &e.Member().User.ID,
Check: func(ctx *events.MessageCreate) bool {
b.Logger.Debug("check")
Expand Down
2 changes: 1 addition & 1 deletion bot/commands/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ func roleComponentEditRoleEmojiHandler(b *botlib.Bot[db.DB]) func(event *events.
author := event.Member()
remove = b.Handler.AddMessage(handler.Message{
UUID: uuid.New(),
ChannelID: channel.ID,
ChannelID: &channel.ID,
AuthorID: &author.User.ID,
Handler: func(event *events.MessageCreate) error {
if event.Message.Author.ID != author.User.ID || !emoji.MatchString(event.Message.Content) {
Expand Down
5 changes: 5 additions & 0 deletions bot/db/db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"context"
"fmt"

"github.com/go-redis/redis/v8"
Expand Down Expand Up @@ -30,6 +31,10 @@ func SetupDatabase(cfg DBConfig) (DB, error) {
Addr: fmt.Sprintf("%s:%s", cfg.Host, cfg.Port),
DB: cfg.DB,
})
res := db.Ping(context.TODO())
if err := res.Err(); err != nil {
return nil, err
}
return &dbImpl{
db: db,
pollCreate: &pollCreateDBImpl{db: db},
Expand Down
16 changes: 10 additions & 6 deletions bot/gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"os/signal"
"runtime"
"syscall"
"time"

"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/events"
Expand Down Expand Up @@ -154,6 +153,12 @@ func Run(file_path, lang_path string) {
b.Client.EventManager().AddEventListeners(&events.ListenerAdapter{
OnGuildJoin: b.OnGuildJoin,
OnGuildLeave: b.OnGuildLeave,
OnGuildReady: func(event *events.GuildReady) {
b.Logger.Infof("guild ready: %s", event.GuildID)
},
OnGuildsReady: func(event *events.GuildsReady) {
b.Logger.Infof("guilds on shard %d ready", event.ShardID())
},
})

if cfg.ShouldSyncCommands {
Expand All @@ -164,13 +169,12 @@ func Run(file_path, lang_path string) {
b.Handler.SyncCommands(b.Client, guilds...)
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err = b.Client.OpenGateway(ctx); err != nil {
b.Logger.Errorf("Failed to connect to gateway: %s", err)
if err := b.Client.OpenShardManager(context.TODO()); err != nil {
b.Logger.Fatalf("failed to open shard manager: %s", err)
}
defer b.Client.Close(context.TODO())
defer b.Client.ShardManager().Close(context.TODO())

b.Logger.Infof("shard count: %d", len(b.Client.ShardManager().Shards()))
b.Logger.Info("Bot is running. Press CTRL-C to exit.")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
Expand Down
2 changes: 1 addition & 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 v1.5.0
github.com/sabafly/sabafly-lib v1.5.1
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.7.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
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 v1.5.0 h1:L210Vmzr5MRBW464nCNNd8FrowFeam/u7okox66Q1Fo=
github.com/sabafly/sabafly-lib v1.5.0/go.mod h1:T8GLVoK7q7mpvOQQ/GRy4ed5CtPrKnhDQ9tapEFb+fs=
github.com/sabafly/sabafly-lib v1.5.1 h1:HvveC89LBvS1IXoIahH4PcTiEDMb9lDD3mP9ct7WRQk=
github.com/sabafly/sabafly-lib v1.5.1/go.mod h1:T8GLVoK7q7mpvOQQ/GRy4ed5CtPrKnhDQ9tapEFb+fs=
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

0 comments on commit ef507ae

Please sign in to comment.