feat(db): disable redis and use local map to store values
This commit is contained in:
parent
642025681b
commit
ceffa15763
7 changed files with 27 additions and 110 deletions
10
README.md
10
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
|
||||
|
|
11
config.go
11
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()
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
|
@ -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:
|
||||
|
|
75
events.go
75
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 {
|
||||
|
|
5
main.go
5
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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue