From c547901e004c582af2c3614d67711a4803f4fc90 Mon Sep 17 00:00:00 2001 From: spiral Date: Tue, 6 Dec 2022 10:45:38 +0000 Subject: [PATCH] feat(stats): push guild count to bot status --- services/scheduled_tasks/go.mod | 2 +- services/scheduled_tasks/go.sum | 2 ++ services/scheduled_tasks/main.go | 6 ++++++ services/scheduled_tasks/tasks.go | 27 ++++++++++++++++++++++++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/services/scheduled_tasks/go.mod b/services/scheduled_tasks/go.mod index 5f00d123..830dc19f 100644 --- a/services/scheduled_tasks/go.mod +++ b/services/scheduled_tasks/go.mod @@ -20,5 +20,5 @@ require ( github.com/pkg/errors v0.9.1 // indirect golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be // indirect golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect - golang.org/x/text v0.3.7 // indirect + golang.org/x/text v0.5.0 // indirect ) diff --git a/services/scheduled_tasks/go.sum b/services/scheduled_tasks/go.sum index d38a7a72..c44602df 100644 --- a/services/scheduled_tasks/go.sum +++ b/services/scheduled_tasks/go.sum @@ -164,6 +164,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= +golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= diff --git a/services/scheduled_tasks/main.go b/services/scheduled_tasks/main.go index 3521a9e2..44365650 100644 --- a/services/scheduled_tasks/main.go +++ b/services/scheduled_tasks/main.go @@ -11,7 +11,13 @@ import ( "github.com/getsentry/sentry-go" ) +var set_guild_count = false + func main() { + if _, ok := os.LookupEnv("SET_GUILD_COUNT"); ok { + set_guild_count = true + } + err := sentry.Init(sentry.ClientOptions{ Dsn: os.Getenv("SENTRY_DSN"), }) diff --git a/services/scheduled_tasks/tasks.go b/services/scheduled_tasks/tasks.go index 374cd90f..e5d52be8 100644 --- a/services/scheduled_tasks/tasks.go +++ b/services/scheduled_tasks/tasks.go @@ -4,6 +4,9 @@ import ( "context" "fmt" "log" + + "golang.org/x/text/language" + "golang.org/x/text/message" ) func task_main() { @@ -11,6 +14,7 @@ func task_main() { update_db_meta() update_stats() + update_bot_status() } var table_stat_keys = []string{"system", "member", "group", "switch", "message"} @@ -44,7 +48,7 @@ func update_db_message_meta() { } } -func update_stats() { +func get_discord_counts() (int, int) { redisStats := run_redis_query() guild_count := 0 @@ -56,6 +60,12 @@ func update_stats() { channel_count += v.ChannelCount } + return guild_count, channel_count +} + +func update_stats() { + guild_count, channel_count := get_discord_counts() + do_stats_insert("guilds", guild_count) do_stats_insert("channels", channel_count) @@ -65,3 +75,18 @@ func update_stats() { do_stats_insert(plural(key), int(val)) } } + +func update_bot_status() { + if !set_guild_count { + return + } + + guild_count, _ := get_discord_counts() + p := message.NewPrinter(language.English) + s := p.Sprintf("%d\n", guild_count) + + cmd := rdb.Set(context.Background(), "discord_botstatus", "in "+s+" servers", 0) + if err := cmd.Err(); err != nil { + panic(err) + } +}