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
|
```bash
|
||||||
$ git clone https://github.com/anhgelus/les-copaings-bot.git
|
$ 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
|
3. Go into the repository and build the program
|
||||||
```bash
|
```bash
|
||||||
$ go build .
|
$ go build .
|
||||||
|
@ -68,11 +68,6 @@ The default configuration is
|
||||||
debug = false
|
debug = false
|
||||||
author = "anhgelus"
|
author = "anhgelus"
|
||||||
|
|
||||||
[redis]
|
|
||||||
address = "localhost:6379"
|
|
||||||
password = ""
|
|
||||||
db = 0
|
|
||||||
|
|
||||||
[database]
|
[database]
|
||||||
host = "localhost"
|
host = "localhost"
|
||||||
user = ""
|
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)
|
- `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
|
- `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].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].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
|
- `[database].password` is the user's password of postgres to use (using docker, it must be the same value as
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Debug bool `toml:"debug"`
|
Debug bool `toml:"debug"`
|
||||||
Author string `toml:"author"`
|
Author string `toml:"author"`
|
||||||
Redis *gokord.RedisCredentials `toml:"redis"`
|
|
||||||
Database *PostgresConfig `toml:"database"`
|
Database *PostgresConfig `toml:"database"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,14 +54,12 @@ func (c *Config) GetAuthor() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) GetRedisCredentials() *gokord.RedisCredentials {
|
func (c *Config) GetRedisCredentials() *gokord.RedisCredentials {
|
||||||
return c.Redis
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) SetDefaultValues() {
|
func (c *Config) SetDefaultValues() {
|
||||||
c.Debug = false
|
c.Debug = false
|
||||||
c.Author = "anhgelus"
|
c.Author = "anhgelus"
|
||||||
c.Redis = &gokord.RedisCredentials{}
|
|
||||||
c.Redis.SetDefaultValues()
|
|
||||||
c.Database = &PostgresConfig{}
|
c.Database = &PostgresConfig{}
|
||||||
c.Database.SetDefaultValues()
|
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:
|
volumes:
|
||||||
- ./config:/app/config
|
- ./config:/app/config
|
||||||
depends_on:
|
depends_on:
|
||||||
- redis
|
|
||||||
- postgres
|
- postgres
|
||||||
redis:
|
|
||||||
image: docker.io/redis:alpine
|
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:alpine
|
image: postgres:alpine
|
||||||
env_file:
|
env_file:
|
||||||
|
|
75
events.go
75
events.go
|
@ -1,27 +1,26 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/anhgelus/gokord/utils"
|
"github.com/anhgelus/gokord/utils"
|
||||||
"github.com/anhgelus/les-copaings-bot/config"
|
"github.com/anhgelus/les-copaings-bot/config"
|
||||||
"github.com/anhgelus/les-copaings-bot/exp"
|
"github.com/anhgelus/les-copaings-bot/exp"
|
||||||
"github.com/anhgelus/les-copaings-bot/user"
|
"github.com/anhgelus/les-copaings-bot/user"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ConnectedSince = "connected_since"
|
|
||||||
NotConnected = -1
|
NotConnected = -1
|
||||||
MaxTimeInVocal = 60 * 60 * 6
|
MaxTimeInVocal = 60 * 60 * 6
|
||||||
MaxXpPerMessage = 250
|
MaxXpPerMessage = 250
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
connectedSince = map[string]int64{}
|
||||||
|
)
|
||||||
|
|
||||||
func OnMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func OnMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
if m.Author.Bot {
|
if m.Author.Bot {
|
||||||
return
|
return
|
||||||
|
@ -52,79 +51,45 @@ func OnVoiceUpdate(s *discordgo.Session, e *discordgo.VoiceStateUpdate) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cfg := config.GetGuildConfig(e.GuildID)
|
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 e.BeforeUpdate == nil && e.ChannelID != "" {
|
||||||
if cfg.IsDisabled(e.ChannelID) {
|
if cfg.IsDisabled(e.ChannelID) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
onConnection(s, e, client)
|
onConnection(s, e)
|
||||||
} else if e.BeforeUpdate != nil && e.ChannelID == "" {
|
} else if e.BeforeUpdate != nil && e.ChannelID == "" {
|
||||||
if cfg.IsDisabled(e.BeforeUpdate.ChannelID) {
|
if cfg.IsDisabled(e.BeforeUpdate.ChannelID) {
|
||||||
return
|
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())
|
utils.SendDebug("User connected", "username", e.Member.DisplayName())
|
||||||
c := user.GetCopaing(e.UserID, e.GuildID)
|
connectedSince[e.UserID] = time.Now().Unix()
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func onDisconnect(s *discordgo.Session, e *discordgo.VoiceStateUpdate, client *redis.Client) {
|
func onDisconnect(s *discordgo.Session, e *discordgo.VoiceStateUpdate) {
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
c := user.GetCopaing(e.UserID, e.GuildID)
|
c := user.GetCopaing(e.UserID, e.GuildID)
|
||||||
key := c.GenKey(ConnectedSince)
|
// check the validity of user
|
||||||
res := client.Get(context.Background(), key)
|
con := connectedSince[e.UserID]
|
||||||
// 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)
|
|
||||||
if con == NotConnected {
|
if con == NotConnected {
|
||||||
utils.SendWarn(fmt.Sprintf(
|
utils.SendWarn(fmt.Sprintf(
|
||||||
"User %s diconnect from a vocal but was registered as not connected", e.Member.DisplayName(),
|
"User %s diconnect from a vocal but was registered as not connected", e.Member.DisplayName(),
|
||||||
))
|
))
|
||||||
return
|
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
|
timeInVocal := now - con
|
||||||
|
utils.SendDebug("User disconnected", "username", e.Member.DisplayName(), "time in vocal", timeInVocal)
|
||||||
|
connectedSince[e.UserID] = NotConnected
|
||||||
|
// add exp
|
||||||
if timeInVocal < 0 {
|
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
|
return
|
||||||
}
|
}
|
||||||
if timeInVocal > MaxTimeInVocal {
|
if timeInVocal > MaxTimeInVocal {
|
||||||
|
@ -137,7 +102,7 @@ func onDisconnect(s *discordgo.Session, e *discordgo.VoiceStateUpdate, client *r
|
||||||
if len(cfg.FallbackChannel) == 0 {
|
if len(cfg.FallbackChannel) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = s.ChannelMessageSend(cfg.FallbackChannel, fmt.Sprintf(
|
_, err := s.ChannelMessageSend(cfg.FallbackChannel, fmt.Sprintf(
|
||||||
"%s est maintenant niveau %d", e.Member.Mention(), newLevel,
|
"%s est maintenant niveau %d", e.Member.Mention(), newLevel,
|
||||||
))
|
))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
5
main.go
5
main.go
|
@ -27,10 +27,11 @@ var (
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&token, "token", "", "token of the bot")
|
flag.StringVar(&token, "token", "", "token of the bot")
|
||||||
flag.Parse()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
gokord.UseRedis = false
|
||||||
err := gokord.SetupConfigs(&Config{}, []*gokord.ConfigInfo{})
|
err := gokord.SetupConfigs(&Config{}, []*gokord.ConfigInfo{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -176,8 +177,6 @@ func main() {
|
||||||
if stopPeriodicReducer != nil {
|
if stopPeriodicReducer != nil {
|
||||||
stopPeriodicReducer <- true
|
stopPeriodicReducer <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
config.CloseRedisClient()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func afterInit(dg *discordgo.Session) {
|
func afterInit(dg *discordgo.Session) {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/anhgelus/gokord"
|
"github.com/anhgelus/gokord"
|
||||||
"github.com/anhgelus/gokord/utils"
|
"github.com/anhgelus/gokord/utils"
|
||||||
"time"
|
"time"
|
||||||
|
@ -60,10 +59,6 @@ func (c *Copaing) Save() error {
|
||||||
return gokord.DB.Save(c).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 {
|
func (c *Copaing) Delete() error {
|
||||||
return gokord.DB.Where("guild_id = ? AND discord_id = ?", c.GuildID, c.DiscordID).Delete(c).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