-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ikafly144 <[email protected]>
- Loading branch information
Showing
48 changed files
with
3,864 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/disgoorg/json" | ||
"github.com/disgoorg/snowflake/v2" | ||
"github.com/google/uuid" | ||
"github.com/redis/go-redis/v9" | ||
"github.com/sabafly/gobot/internal/smap" | ||
) | ||
|
||
type GuildDataDB interface { | ||
Get(id snowflake.ID) (GuildData, error) | ||
Set(id snowflake.ID, data GuildData) error | ||
Del(id snowflake.ID) error | ||
Mu(id snowflake.ID) *sync.Mutex | ||
} | ||
|
||
var _ GuildDataDB = (*guildDataDBImpl)(nil) | ||
|
||
type guildDataDBImpl struct { | ||
db *redis.Client | ||
guildDataLocks smap.SyncedMap[snowflake.ID, *sync.Mutex] | ||
} | ||
|
||
func (g *guildDataDBImpl) Get(id snowflake.ID) (GuildData, error) { | ||
res := g.db.HGet(context.TODO(), "guild-data", id.String()) | ||
if err := res.Err(); err != nil { | ||
if err != redis.Nil { | ||
return GuildData{}, err | ||
} | ||
} | ||
buf := []byte(res.Val()) | ||
val := GuildData{} | ||
if err := json.Unmarshal(buf, &val); err != nil { | ||
return GuildData{}, err | ||
} | ||
return val, nil | ||
} | ||
|
||
func (g *guildDataDBImpl) Set(id snowflake.ID, data GuildData) error { | ||
buf, err := json.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
res := g.db.HSet(context.TODO(), "guild-data", id.String(), buf) | ||
if err := res.Err(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (g *guildDataDBImpl) Del(id snowflake.ID) error { | ||
res := g.db.HDel(context.TODO(), "guild-data", id.String()) | ||
if err := res.Err(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (g *guildDataDBImpl) Mu(gid snowflake.ID) *sync.Mutex { | ||
v, _ := g.guildDataLocks.LoadOrStore(gid, new(sync.Mutex)) | ||
return v | ||
} | ||
|
||
const GuildDataVersion = 11 | ||
|
||
type GuildData struct { | ||
ID snowflake.ID `json:"id"` | ||
RolePanel map[uuid.UUID]GuildDataRolePanel `json:"role_panel"` | ||
RolePanelLimit int `json:"role_panel_limit"` | ||
UserLevels map[snowflake.ID]GuildDataUserLevel `json:"user_levels"` | ||
Config GuildDataConfig `json:"config"` | ||
BumpStatus BumpStatus `json:"bump_status"` | ||
|
||
MCStatusPanel map[uuid.UUID]string `json:"mc_status_panel"` | ||
MCStatusPanelName map[string]int `json:"mc_status_panel_name"` | ||
MCStatusPanelMax int `json:"mc_status_panel_max"` | ||
|
||
UserLevelExcludeChannels map[snowflake.ID]string `json:"user_level_exclude_channels"` | ||
|
||
RolePanelV2 map[uuid.UUID]string `json:"role_panel_v2"` | ||
RolePanelV2Name map[string]int `json:"role_panel_v2_name"` | ||
RolePanelV2Placed map[string]uuid.UUID `json:"role_panel_v2_placed"` | ||
RolePanelV2PlacedConfig map[string]RolePanelV2Config `json:"role_panel_v2_placed_config"` | ||
RolePanelV2Limit int `json:"role_panel_v2_limit"` | ||
|
||
RolePanelV2Editing map[uuid.UUID]uuid.UUID `json:"role_panel_v2_editing"` | ||
|
||
RolePanelV2EditingEmoji map[uuid.UUID][2]snowflake.ID `json:"role_panel_v2_emoji"` | ||
|
||
DataVersion *int `json:"data_version,omitempty"` | ||
} | ||
|
||
func NewMessageSuffix(target snowflake.ID, suffix string, rule MessageSuffixRuleType) MessageSuffix { | ||
return MessageSuffix{ | ||
Target: target, | ||
Suffix: suffix, | ||
RuleType: rule, | ||
} | ||
} | ||
|
||
type MessageSuffix struct { | ||
Target snowflake.ID `json:"target"` | ||
Suffix string `json:"suffix"` | ||
RuleType MessageSuffixRuleType `json:"rule_type"` | ||
} | ||
|
||
type MessageSuffixRuleType int | ||
|
||
const ( | ||
MessageSuffixRuleTypeWarning = iota | ||
MessageSuffixRuleTypeDelete | ||
MessageSuffixRuleTypeWebhook | ||
) | ||
|
||
type BumpStatus struct { | ||
BumpEnabled bool `json:"bump_enabled"` | ||
BumpChannel *snowflake.ID `json:"bump_channel"` | ||
BumpRole *snowflake.ID `json:"bump_role"` | ||
BumpMessage [2]string `json:"bump_message"` | ||
BumpRemind [2]string `json:"bump_remind"` | ||
LastBump time.Time `json:"last_bump"` | ||
LastBumpChannel *snowflake.ID `json:"last_bump_channel"` | ||
UpEnabled bool `json:"up_enabled"` | ||
UpChannel *snowflake.ID `json:"up_channel"` | ||
UpRole *snowflake.ID `json:"up_role"` | ||
UpMessage [2]string `json:"up_message"` | ||
UpRemind [2]string `json:"up_remind"` | ||
LastUp time.Time `json:"last_up"` | ||
LastUpChannel *snowflake.ID `json:"last_up_channel"` | ||
|
||
BumpCountMap map[snowflake.ID]uint64 `json:"bump_count_map"` | ||
UpCountMap map[snowflake.ID]uint64 `json:"up_count_map"` | ||
} | ||
|
||
type GuildDataConfig struct { | ||
LevelUpMessage string `json:"level_up_message"` | ||
LevelUpMessageChannel *snowflake.ID `json:"level_up_message_channel"` | ||
} | ||
|
||
type GuildDataUserLevel struct { | ||
UserDataLevel | ||
MessageCount int64 `json:"message_count"` | ||
LastMessageTime time.Time `json:"last_message_time"` | ||
} | ||
|
||
func (g *GuildData) UnmarshalJSON(b []byte) error { | ||
type guildData GuildData | ||
var v struct { | ||
guildData | ||
} | ||
if err := json.Unmarshal(b, &v); err != nil { | ||
return err | ||
} | ||
*g = GuildData(v.guildData) | ||
return nil | ||
} | ||
|
||
type GuildDataRolePanel struct { | ||
OnList bool `json:"on_list"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/disgoorg/disgo/discord" | ||
"github.com/disgoorg/json" | ||
"github.com/disgoorg/snowflake/v2" | ||
"github.com/google/uuid" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
type RolePanelV2DB interface { | ||
Get(id uuid.UUID) (*RolePanelV2, error) | ||
Set(id uuid.UUID, data *RolePanelV2) error | ||
Del(id uuid.UUID) error | ||
} | ||
|
||
type rolePanelV2DBImpl struct { | ||
db *redis.Client | ||
} | ||
|
||
func (r *rolePanelV2DBImpl) Get(id uuid.UUID) (*RolePanelV2, error) { | ||
res := r.db.HGet(context.TODO(), "role-panel-v2", id.String()) | ||
if err := res.Err(); err != nil { | ||
return nil, err | ||
} | ||
data := &RolePanelV2{} | ||
if err := json.Unmarshal([]byte(res.Val()), data); err != nil { | ||
return nil, err | ||
} | ||
return data, nil | ||
} | ||
|
||
func (r *rolePanelV2DBImpl) Set(id uuid.UUID, data *RolePanelV2) error { | ||
buf, err := json.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
res := r.db.HSet(context.TODO(), "role-panel-v2", id.String(), buf) | ||
if err := res.Err(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (r *rolePanelV2DBImpl) Del(id uuid.UUID) error { | ||
res := r.db.HDel(context.TODO(), "role-panel-v2", id.String()) | ||
if err := res.Err(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func NewRolePanelV2(name, description string) *RolePanelV2 { | ||
return &RolePanelV2{ | ||
ID: uuid.New(), | ||
Name: name, | ||
Description: description, | ||
Roles: []RolePanelV2Role{}, | ||
} | ||
} | ||
|
||
type RolePanelV2 struct { | ||
ID uuid.UUID `json:"uuid"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
Roles []RolePanelV2Role `json:"roles"` | ||
} | ||
|
||
type rolePanelV2MessageBuilder[T any] interface { | ||
AddEmbeds(...discord.Embed) T | ||
AddContainerComponents(...discord.ContainerComponent) T | ||
} | ||
|
||
func NewRolePanelV2Config() RolePanelV2Config { | ||
return RolePanelV2Config{ | ||
PanelType: RolePanelV2TypeNone, | ||
ButtonStyle: discord.ButtonStyleSuccess, | ||
ButtonShowName: false, | ||
SimpleSelectMenu: true, | ||
} | ||
} | ||
|
||
type RolePanelV2Config struct { | ||
PanelType RolePanelV2Type `json:"panel_type"` | ||
ButtonStyle discord.ButtonStyle `json:"button_style"` | ||
ButtonShowName bool `json:"show_name"` | ||
SimpleSelectMenu bool `json:"simple_select_menu"` | ||
HideNotice bool `json:"hide_notice"` | ||
UseDisplayName bool `json:"use_display_name"` | ||
} | ||
|
||
type RolePanelV2Role struct { | ||
RoleID snowflake.ID `json:"role_id"` | ||
RoleName string `json:"role_name"` | ||
Emoji *discord.ComponentEmoji `json:"emoji"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/disgoorg/disgo/discord" | ||
"github.com/disgoorg/snowflake/v2" | ||
"github.com/google/uuid" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
type RolePanelV2EditDB interface { | ||
Get(id uuid.UUID) (data *RolePanelV2Edit, err error) | ||
Set(id uuid.UUID, data *RolePanelV2Edit) (err error) | ||
Del(id uuid.UUID) (err error) | ||
} | ||
|
||
type rolePanelV2EditDBImpl struct { | ||
db *redis.Client | ||
} | ||
|
||
func (self rolePanelV2EditDBImpl) Get(id uuid.UUID) (*RolePanelV2Edit, error) { | ||
res := self.db.Get(context.TODO(), "role-panel-v2-edit"+id.String()) | ||
if err := res.Err(); err != nil { | ||
return nil, err | ||
} | ||
data := &RolePanelV2Edit{} | ||
if err := json.Unmarshal([]byte(res.Val()), data); err != nil { | ||
return nil, err | ||
} | ||
return data, nil | ||
} | ||
|
||
func (self rolePanelV2EditDBImpl) Set(id uuid.UUID, data *RolePanelV2Edit) error { | ||
buf, err := json.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
res := self.db.Set(context.TODO(), "role-panel-v2-edit"+id.String(), buf, (time.Minute*15)-(time.Since(data.CreatedAt))) | ||
if err := res.Err(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (self rolePanelV2EditDBImpl) Del(id uuid.UUID) error { | ||
res := self.db.Del(context.TODO(), "role-panel-v2-edit"+id.String()) | ||
if err := res.Err(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
type RolePanelV2Edit struct { | ||
ID uuid.UUID `json:"id"` | ||
CreatedAt time.Time `json:"created_at"` | ||
|
||
RolePanelID uuid.UUID `json:"role_panel_id"` | ||
|
||
GuildID snowflake.ID `json:"guild_id"` | ||
ChannelID snowflake.ID `json:"channel_id"` | ||
MessageID snowflake.ID `json:"message_id"` | ||
EmojiMode bool `json:"emoji_mode"` | ||
EmojiLocale discord.Locale `json:"emoji_locale"` | ||
|
||
SelectedID *snowflake.ID | ||
} | ||
|
||
func (r RolePanelV2Edit) IsSelected(id snowflake.ID) bool { | ||
return r.SelectedID != nil && *r.SelectedID == id | ||
} | ||
|
||
func (r RolePanelV2Edit) HasSelectedRole() bool { | ||
return r.SelectedID != nil | ||
} |
Oops, something went wrong.