aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <anhgelus@anhgelus.world>2025-05-21 18:46:27 +0200
committerAnhgelus Morhtuuzh <anhgelus@anhgelus.world>2025-05-21 18:46:27 +0200
commitceffa15763fe3f61621beb7fd400bc43d2aadc9e (patch)
treebb434b1cc650659303a23f878efbe0f2907fbc41
parent642025681befa35f67cc7c25383eb782ac01feac (diff)
feat(db): disable redis and use local map to store values
-rw-r--r--README.md10
-rw-r--r--config.go11
-rw-r--r--config/redis.go28
-rw-r--r--docker-compose.yml3
-rw-r--r--events.go75
-rw-r--r--main.go5
-rw-r--r--user/member.go5
7 files changed, 27 insertions, 110 deletions
diff --git a/README.md b/README.md
index 5b49b14..800e1d2 100644
--- a/README.md
+++ b/README.md
@@ -47,7 +47,7 @@ You can stop the compose file with `docker compose down`
```bash
$ git clone https://github.com/anhgelus/les-copaings-bot.git
```
-2. Install Go 1.22+
+2. Install Go 1.24+
3. Go into the repository and build the program
```bash
$ go build .
@@ -68,11 +68,6 @@ The default configuration is
debug = false
author = "anhgelus"
-[redis]
-address = "localhost:6379"
-password = ""
-db = 0
-
[database]
host = "localhost"
user = ""
@@ -83,9 +78,6 @@ port = 5432
- `debug` is true if the bot is in debug mode (don't turn it on unless you are modifying the source code)
- `author` is the author's name
-- `[redis].address` is the address of redis (using docker, it's `redis:6379`)
-- `[redis].password` is the redis's password
-- `[redis].db` is the db to use
- `[database].host` is the host of postgres (using docker, it's `postgres`)
- `[database].user` is the user of postgres to use (using docker, it must be the same value as `POSTGRES_USER` in `.env`)
- `[database].password` is the user's password of postgres to use (using docker, it must be the same value as
diff --git a/config.go b/config.go
index 421f1b1..7a8c57b 100644
--- a/config.go
+++ b/config.go
@@ -9,10 +9,9 @@ import (
)
type Config struct {
- Debug bool `toml:"debug"`
- Author string `toml:"author"`
- Redis *gokord.RedisCredentials `toml:"redis"`
- Database *PostgresConfig `toml:"database"`
+ Debug bool `toml:"debug"`
+ Author string `toml:"author"`
+ Database *PostgresConfig `toml:"database"`
}
type PostgresConfig struct {
@@ -55,14 +54,12 @@ func (c *Config) GetAuthor() string {
}
func (c *Config) GetRedisCredentials() *gokord.RedisCredentials {
- return c.Redis
+ return nil
}
func (c *Config) SetDefaultValues() {
c.Debug = false
c.Author = "anhgelus"
- c.Redis = &gokord.RedisCredentials{}
- c.Redis.SetDefaultValues()
c.Database = &PostgresConfig{}
c.Database.SetDefaultValues()
}
diff --git a/config/redis.go b/config/redis.go
deleted file mode 100644
index bfec5a0..0000000
--- a/config/redis.go
+++ /dev/null
@@ -1,28 +0,0 @@
-package config
-
-import (
- "github.com/anhgelus/gokord"
- "github.com/anhgelus/gokord/utils"
- "github.com/redis/go-redis/v9"
-)
-
-var redisClient *redis.Client
-
-func GetRedisClient() (*redis.Client, error) {
- if redisClient == nil {
- var err error
- redisClient, err = gokord.BaseCfg.GetRedisCredentials().Connect()
- return redisClient, err
- }
- return redisClient, nil
-}
-
-func CloseRedisClient() {
- if redisClient == nil {
- return
- }
- err := redisClient.Close()
- if err != nil {
- utils.SendAlert("config/redis.go - Closing redis client", err.Error())
- }
-}
diff --git a/docker-compose.yml b/docker-compose.yml
index 2575340..77b28da 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -7,10 +7,7 @@ services:
volumes:
- ./config:/app/config
depends_on:
- - redis
- postgres
- redis:
- image: docker.io/redis:alpine
postgres:
image: postgres:alpine
env_file:
diff --git a/events.go b/events.go
index a5c9273..50306dc 100644
--- a/events.go
+++ b/events.go
@@ -1,27 +1,26 @@
package main
import (
- "context"
- "errors"
"fmt"
"github.com/anhgelus/gokord/utils"
"github.com/anhgelus/les-copaings-bot/config"
"github.com/anhgelus/les-copaings-bot/exp"
"github.com/anhgelus/les-copaings-bot/user"
"github.com/bwmarrin/discordgo"
- "github.com/redis/go-redis/v9"
- "strconv"
"strings"
"time"
)
const (
- ConnectedSince = "connected_since"
NotConnected = -1
MaxTimeInVocal = 60 * 60 * 6
MaxXpPerMessage = 250
)
+var (
+ connectedSince = map[string]int64{}
+)
+
func OnMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot {
return
@@ -52,79 +51,45 @@ func OnVoiceUpdate(s *discordgo.Session, e *discordgo.VoiceStateUpdate) {
return
}
cfg := config.GetGuildConfig(e.GuildID)
- client, err := config.GetRedisClient()
- if err != nil {
- utils.SendAlert("events.go - Getting redis client", err.Error())
- return
- }
if e.BeforeUpdate == nil && e.ChannelID != "" {
if cfg.IsDisabled(e.ChannelID) {
return
}
- onConnection(s, e, client)
+ onConnection(s, e)
} else if e.BeforeUpdate != nil && e.ChannelID == "" {
if cfg.IsDisabled(e.BeforeUpdate.ChannelID) {
return
}
- onDisconnect(s, e, client)
+ onDisconnect(s, e)
}
}
-func onConnection(_ *discordgo.Session, e *discordgo.VoiceStateUpdate, client *redis.Client) {
+func onConnection(_ *discordgo.Session, e *discordgo.VoiceStateUpdate) {
utils.SendDebug("User connected", "username", e.Member.DisplayName())
- c := user.GetCopaing(e.UserID, e.GuildID)
- err := client.Set(
- context.Background(),
- c.GenKey(ConnectedSince),
- strconv.FormatInt(time.Now().Unix(), 10),
- 0,
- ).Err()
- if err != nil {
- utils.SendAlert("events.go - Setting connected_since", err.Error())
- }
+ connectedSince[e.UserID] = time.Now().Unix()
}
-func onDisconnect(s *discordgo.Session, e *discordgo.VoiceStateUpdate, client *redis.Client) {
+func onDisconnect(s *discordgo.Session, e *discordgo.VoiceStateUpdate) {
now := time.Now().Unix()
c := user.GetCopaing(e.UserID, e.GuildID)
- key := c.GenKey(ConnectedSince)
- res := client.Get(context.Background(), key)
- // check validity of user (1)
- if errors.Is(res.Err(), redis.Nil) {
- utils.SendWarn(fmt.Sprintf(
- "User %s diconnect from a vocal but does not have a connected_since", e.Member.DisplayName(),
- ))
- return
- }
- if res.Err() != nil {
- utils.SendAlert("events.go - Getting connected_since", res.Err().Error())
- err := client.Set(context.Background(), key, strconv.Itoa(NotConnected), 0).Err()
- if err != nil {
- utils.SendAlert("events.go - Set connected_since to not connected after get err", err.Error())
- }
- return
- }
- con, err := res.Int64()
- if err != nil {
- utils.SendAlert("events.go - Converting result to int64", err.Error())
- return
- }
- // check validity of user (2)
+ // check the validity of user
+ con := connectedSince[e.UserID]
if con == NotConnected {
utils.SendWarn(fmt.Sprintf(
"User %s diconnect from a vocal but was registered as not connected", e.Member.DisplayName(),
))
return
}
- utils.SendDebug("User disconnected", "username", e.Member.DisplayName(), "since", con)
- err = client.Set(context.Background(), key, strconv.Itoa(NotConnected), 0).Err()
- if err != nil {
- utils.SendAlert("events.go - Set connected_since to not connected", err.Error())
- }
- // add exp
timeInVocal := now - con
+ utils.SendDebug("User disconnected", "username", e.Member.DisplayName(), "time in vocal", timeInVocal)
+ connectedSince[e.UserID] = NotConnected
+ // add exp
if timeInVocal < 0 {
- utils.SendAlert("events.go - Calculating time spent in vocal", "the time is negative", "discord_id", e.UserID, "guild_id", e.GuildID)
+ utils.SendAlert(
+ "events.go - Calculating time spent in vocal", "the time is negative",
+ "discord_id", e.UserID,
+ "guild_id", e.GuildID,
+ )
return
}
if timeInVocal > MaxTimeInVocal {
@@ -137,7 +102,7 @@ func onDisconnect(s *discordgo.Session, e *discordgo.VoiceStateUpdate, client *r
if len(cfg.FallbackChannel) == 0 {
return
}
- _, err = s.ChannelMessageSend(cfg.FallbackChannel, fmt.Sprintf(
+ _, err := s.ChannelMessageSend(cfg.FallbackChannel, fmt.Sprintf(
"%s est maintenant niveau %d", e.Member.Mention(), newLevel,
))
if err != nil {
diff --git a/main.go b/main.go
index 46bcb61..0a52445 100644
--- a/main.go
+++ b/main.go
@@ -27,10 +27,11 @@ var (
func init() {
flag.StringVar(&token, "token", "", "token of the bot")
- flag.Parse()
}
func main() {
+ flag.Parse()
+ gokord.UseRedis = false
err := gokord.SetupConfigs(&Config{}, []*gokord.ConfigInfo{})
if err != nil {
panic(err)
@@ -176,8 +177,6 @@ func main() {
if stopPeriodicReducer != nil {
stopPeriodicReducer <- true
}
-
- config.CloseRedisClient()
}
func afterInit(dg *discordgo.Session) {
diff --git a/user/member.go b/user/member.go
index 71a369b..77ceb2e 100644
--- a/user/member.go
+++ b/user/member.go
@@ -1,7 +1,6 @@
package user
import (
- "fmt"
"github.com/anhgelus/gokord"
"github.com/anhgelus/gokord/utils"
"time"
@@ -60,10 +59,6 @@ func (c *Copaing) Save() error {
return gokord.DB.Save(c).Error
}
-func (c *Copaing) GenKey(key string) string {
- return fmt.Sprintf("%s:%s:%s", c.GuildID, c.DiscordID, key)
-}
-
func (c *Copaing) Delete() error {
return gokord.DB.Where("guild_id = ? AND discord_id = ?", c.GuildID, c.DiscordID).Delete(c).Error
}