Skip to content

Commit

Permalink
mee6インポート
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 49eb796 commit 96f0d39
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 3 deletions.
100 changes: 100 additions & 0 deletions bot/commands/level/level.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package level

import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
"slices"
"strconv"
"strings"
Expand All @@ -11,6 +14,7 @@ import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/snowflake/v2"
"github.com/sabafly/gobot/bot/components"
"github.com/sabafly/gobot/bot/components/generic"
"github.com/sabafly/gobot/ent/member"
Expand Down Expand Up @@ -132,6 +136,10 @@ func Command(c *components.Components) components.Command {
},
},
},
discord.ApplicationCommandOptionSubCommand{
Name: "import-mee6",
Description: "import xp point from mee6",
},
},
},
},
Expand Down Expand Up @@ -428,6 +436,98 @@ func Command(c *components.Components) components.Command {
return nil
},
},
"/level/import-mee6": generic.PCommandHandler{
PCommandHandler: generic.PermissionCommandCheck("level.import-mee6"),
CommandHandler: func(c *components.Components, event *events.ApplicationCommandInteractionCreate) errors.Error {
g, err := c.GuildCreateID(event, *event.GuildID())
if err != nil {
return errors.NewError(err)
}

if g.LevelMee6Imported {
return errors.NewError(errors.ErrorMessage("components.level.import-mee6.message.already", event))
}

members := []discord.Member{}
member_count := 1000
afterID := snowflake.ID(0)
for member_count == 1000 {
m, err := event.Client().Rest().GetMembers(*event.GuildID(), member_count, afterID)
if err != nil {
return errors.NewError(err)
}
member_count = len(m)
members = append(members, m...)
afterID = m[len(m)-1].User.ID
}

slog.Info("mee6インポート", slog.Any("gid", event.GuildID()), slog.Int("member_count", len(members)))

member_count = 0
url := fmt.Sprintf("https://mee6.xyz/api/plugins/levels/leaderboard/%s", event.GuildID().String())
for page := 0; true; page++ {
response, err := http.Get(fmt.Sprintf("%s?page=%d", url, page))
if err != nil || response.StatusCode != http.StatusOK {
switch response.StatusCode {
case http.StatusUnauthorized:
if err := event.RespondMessage(
discord.NewMessageBuilder().
SetContent(
fmt.Sprintf("# FAILED\n```| STATUS CODE | %d\n| RESPONSE | %v```%s",
response.StatusCode,
err,
translate.Message(event.Locale(), "components.level.import-mee6.message.unauthorized",
translate.WithTemplate(map[string]any{"GuildID": *event.GuildID()}),
),
),
),
); err != nil {
return errors.NewError(err)
}
return nil
default:
if err := event.RespondMessage(
discord.NewMessageBuilder().
SetContent(fmt.Sprintf("# FAILED\n```| STATUS CODE | %d\n| RESPONSE | %v```", response.StatusCode, err)),
); err != nil {
return errors.NewError(err)
}
return nil
}
}
var leaderboard mee6LeaderBoard
if err := json.NewDecoder(response.Body).Decode(&leaderboard); err != nil {
return errors.NewError(err)
}
if len(leaderboard.Players) < 1 {
break
}
for _, player := range leaderboard.Players {
index := slices.IndexFunc(members, func(m discord.Member) bool { return m.User.ID == player.ID })
if index == -1 {
continue
}
slog.Info("mee6メンバーインポート", slog.Any("gid", event.GuildID()), slog.Any("member_id", player.ID))
m, err := c.MemberCreate(event, members[index].User, *event.GuildID())
if err != nil {
return errors.NewError(err)
}
m.Update().SetXp(xppoint.XP(player.Xp)).ExecX(event)
member_count++
}
}

g.Update().SetLevelMee6Imported(true).ExecX(event)

if err := event.RespondMessage(
discord.NewMessageBuilder().
SetContent(fmt.Sprintf("# SUCCEED\n```| IMPORTED MEMBER COUNT | %d```", member_count)),
); err != nil {
return errors.NewError(err)
}
return nil
},
},
},
ModalHandlers: map[string]generic.ModalHandler{
"level:up_message_modal": func(c *components.Components, event *events.ModalSubmitInteractionCreate) errors.Error {
Expand Down
50 changes: 50 additions & 0 deletions bot/commands/level/mee6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package level

import "github.com/disgoorg/snowflake/v2"

type mee6LeaderBoard struct {
Admin bool `json:"admin"`
BannerURL *string `json:"banner_url"`
Country string `json:"country"`
Guild mee6Guild `json:"guild"`
IsMember bool `json:"is_member"`
MonetizeOptions mee6MonetizeOptions `json:"monetize_options"`
Page int `json:"page"`
Player *mee6Player `json:"player"`
Players []mee6Player `json:"players"`
RoleRewards []any `json:"role_rewards"`
UserGuildSettings any `json:"user_guild_settings"`
XpPerMessage []int `json:"xp_per_message"`
XpRate float64 `json:"xp_rate"`
}

type mee6Guild struct {
AllowJoin bool `json:"allow_join"`
ApplicationCommandsEnabled bool `json:"application_commands_enabled"`
CommandsPrefix string `json:"commands_prefix"`
Icon string `json:"icon"`
ID string `json:"id"`
InviteLeaderboard bool `json:"invite_leaderboard"`
LeaderboardURL string `json:"leaderboard_url"`
Name string `json:"name"`
Premium bool `json:"premium"`
}

type mee6MonetizeOptions struct {
DisplayPlans bool `json:"display_plans"`
ShowcaseSubscribers bool `json:"showcase_subscribers"`
}

type mee6Player struct {
Avatar string `json:"avatar"`
DetailedXp []int64 `json:"detailed_xp"`
Discriminator string `json:"discriminator"`
GuildID string `json:"guild_id"`
ID snowflake.ID `json:"id"`
IsMonetizeSubscriber bool `json:"is_monetize_subscriber"`
Level int64 `json:"level"`
MessageCount int64 `json:"message_count"`
MonetizeXpBoost int64 `json:"monetize_xp_boost"`
Username string `json:"username"`
Xp int64 `json:"xp"`
}
13 changes: 13 additions & 0 deletions ent/guild.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions ent/guild/guild.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions ent/guild/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions ent/guild_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 96f0d39

Please sign in to comment.