Skip to content

Commit

Permalink
レベル機能
Browse files Browse the repository at this point in the history
Signed-off-by: ikafly144 <[email protected]>
  • Loading branch information
ikafly144 committed Nov 30, 2023
1 parent e143721 commit 81135e8
Show file tree
Hide file tree
Showing 48 changed files with 3,864 additions and 274 deletions.
12 changes: 9 additions & 3 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/cache"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/disgo/rest"
"github.com/disgoorg/disgo/sharding"
_ "github.com/go-sql-driver/mysql"
"github.com/joho/godotenv"
"github.com/sabafly/gobot/bot/commands/debug"
"github.com/sabafly/gobot/bot/commands/level"
"github.com/sabafly/gobot/bot/commands/message"
"github.com/sabafly/gobot/bot/commands/ping"
"github.com/sabafly/gobot/bot/commands/role"
userinfo "github.com/sabafly/gobot/bot/commands/user_info"
"github.com/sabafly/gobot/bot/components"
"github.com/sabafly/gobot/ent"
"github.com/sabafly/gobot/internal/translate"
Expand All @@ -60,9 +63,7 @@ func run() error {
AddSource: true,
Level: slog.LevelInfo,
})))
if err := godotenv.Load(); err != nil {
return fmt.Errorf(".envファイルを読み込めません: %w", err)
}
_ = godotenv.Load()

config, err := components.Load("gobot.yml")
if err != nil {
Expand Down Expand Up @@ -96,6 +97,8 @@ func run() error {
ping.Command(),
message.Command(component),
role.Command(component),
level.Command(component),
userinfo.Command(component),
)

token := os.Getenv("TOKEN")
Expand All @@ -111,6 +114,9 @@ func run() error {
gateway.WithIntents(gateway.IntentsGuild, gateway.IntentsPrivileged),
),
),
bot.WithRestClientConfigOpts(
rest.WithUserAgent(fmt.Sprintf("DiscordBot (%s, %s)", disgo.GitHub, disgo.Version)),
),
bot.WithEventManagerConfigOpts(
bot.WithAsyncEventsEnabled(),
),
Expand Down
165 changes: 165 additions & 0 deletions bot/commands/debug/db/guild_data.go
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"`
}
98 changes: 98 additions & 0 deletions bot/commands/debug/db/role_panel_v2.go
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 {

Check failure on line 19 in bot/commands/debug/db/role_panel_v2.go

View workflow job for this annotation

GitHub Actions / lint

type `rolePanelV2DBImpl` is unused (unused)
db *redis.Client
}

func (r *rolePanelV2DBImpl) Get(id uuid.UUID) (*RolePanelV2, error) {

Check failure on line 23 in bot/commands/debug/db/role_panel_v2.go

View workflow job for this annotation

GitHub Actions / lint

func `(*rolePanelV2DBImpl).Get` is unused (unused)
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 {

Check failure on line 35 in bot/commands/debug/db/role_panel_v2.go

View workflow job for this annotation

GitHub Actions / lint

func `(*rolePanelV2DBImpl).Set` is unused (unused)
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 {

Check failure on line 47 in bot/commands/debug/db/role_panel_v2.go

View workflow job for this annotation

GitHub Actions / lint

func `(*rolePanelV2DBImpl).Del` is unused (unused)
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 {

Check failure on line 71 in bot/commands/debug/db/role_panel_v2.go

View workflow job for this annotation

GitHub Actions / lint

type `rolePanelV2MessageBuilder` is unused (unused)
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"`
}
77 changes: 77 additions & 0 deletions bot/commands/debug/db/role_panel_v2_edit.go
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 {

Check failure on line 20 in bot/commands/debug/db/role_panel_v2_edit.go

View workflow job for this annotation

GitHub Actions / lint

type `rolePanelV2EditDBImpl` is unused (unused)
db *redis.Client
}

func (self rolePanelV2EditDBImpl) Get(id uuid.UUID) (*RolePanelV2Edit, error) {

Check failure on line 24 in bot/commands/debug/db/role_panel_v2_edit.go

View workflow job for this annotation

GitHub Actions / lint

func `rolePanelV2EditDBImpl.Get` is unused (unused)
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 {

Check failure on line 36 in bot/commands/debug/db/role_panel_v2_edit.go

View workflow job for this annotation

GitHub Actions / lint

func `rolePanelV2EditDBImpl.Set` is unused (unused)
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 {

Check failure on line 48 in bot/commands/debug/db/role_panel_v2_edit.go

View workflow job for this annotation

GitHub Actions / lint

func `rolePanelV2EditDBImpl.Del` is unused (unused)
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
}
Loading

0 comments on commit 81135e8

Please sign in to comment.