feat: leveling system refractor
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"velox-bot/internal/commands/level/shared"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
var Slvl = &discordgo.ApplicationCommand{
|
||||
Name: "slvl",
|
||||
Description: "Leveling System",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
slvlToggleOption(),
|
||||
slvlSetLevelOption(),
|
||||
slvlSetChannelOption(),
|
||||
slvlSetMessageOption(),
|
||||
slvlSetRewardOption(),
|
||||
},
|
||||
}
|
||||
|
||||
func SlvlHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
data := i.ApplicationCommandData()
|
||||
if len(data.Options) == 0 {
|
||||
return
|
||||
}
|
||||
switch data.Options[0].Name {
|
||||
case "toggle":
|
||||
slvlToggleHandler(s, i)
|
||||
case "set":
|
||||
slvlSetLevelHandler(s, i)
|
||||
case "set-channel":
|
||||
slvlSetChannelHandler(s, i)
|
||||
case "set-message":
|
||||
slvlSetMessageHandler(s, i)
|
||||
case "set-reward":
|
||||
slvlSetRewardHandler(s, i)
|
||||
default:
|
||||
shared.RespondEphemeral(s, i, "Invalid subcommand.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"velox-bot/internal/commands/level/shared"
|
||||
"velox-bot/internal/db/services"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func slvlSetChannelOption() *discordgo.ApplicationCommandOption {
|
||||
return &discordgo.ApplicationCommandOption{
|
||||
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||
Name: "set-channel",
|
||||
Description: "Set the level up channel",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionChannel,
|
||||
Name: "channel",
|
||||
Description: "Channel for level-up messages",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func slvlSetChannelHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if !shared.RequireManageGuild(s, i) || !shared.RequireLevelSystemEnabled(s, i) {
|
||||
return
|
||||
}
|
||||
if !shared.RequireLevelSettingsService(s, i) {
|
||||
return
|
||||
}
|
||||
|
||||
guildID, ok := shared.ParseGuildID(s, i)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
optMap := shared.SubcommandOptionMap(i)
|
||||
channelOpt, ok := optMap["channel"]
|
||||
if !ok {
|
||||
shared.RespondEphemeral(s, i, "Missing channel option.")
|
||||
return
|
||||
}
|
||||
|
||||
ch := channelOpt.ChannelValue(s)
|
||||
if ch == nil {
|
||||
shared.RespondEphemeral(s, i, "Invalid channel.")
|
||||
return
|
||||
}
|
||||
|
||||
channelID, err := strconv.ParseInt(ch.ID, 10, 64)
|
||||
if err != nil {
|
||||
shared.RespondEphemeral(s, i, "Invalid channel ID.")
|
||||
return
|
||||
}
|
||||
|
||||
if err := services.Global.LevelSettings.SetLevelUpChannel(context.Background(), guildID, channelID); err != nil {
|
||||
shared.RespondEphemeral(s, i, "Failed to set level up channel.")
|
||||
return
|
||||
}
|
||||
|
||||
shared.RespondEphemeral(s, i, "Level up channel set to "+ch.Mention()+".")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"velox-bot/internal/commands/level/shared"
|
||||
"velox-bot/internal/db/services"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func slvlSetLevelOption() *discordgo.ApplicationCommandOption {
|
||||
return &discordgo.ApplicationCommandOption{
|
||||
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||
Name: "set",
|
||||
Description: "Set the level of a user",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionUser,
|
||||
Name: "user",
|
||||
Description: "The user to set the level of",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionInteger,
|
||||
Name: "level",
|
||||
Description: "The level to set the user to",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func slvlSetLevelHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if !shared.RequireManageGuild(s, i) || !shared.RequireLevelSystemEnabled(s, i) {
|
||||
return
|
||||
}
|
||||
if services.Global == nil || services.Global.Level == nil {
|
||||
shared.RespondEphemeral(s, i, "Leveling service is not available.")
|
||||
return
|
||||
}
|
||||
|
||||
optMap := shared.SubcommandOptionMap(i)
|
||||
userOpt, ok := optMap["user"]
|
||||
if !ok {
|
||||
shared.RespondEphemeral(s, i, "Missing user option.")
|
||||
return
|
||||
}
|
||||
levelOpt, ok := optMap["level"]
|
||||
if !ok {
|
||||
shared.RespondEphemeral(s, i, "Missing level option.")
|
||||
return
|
||||
}
|
||||
|
||||
targetUser := userOpt.UserValue(s)
|
||||
if targetUser == nil {
|
||||
shared.RespondEphemeral(s, i, "Invalid user.")
|
||||
return
|
||||
}
|
||||
if targetUser.Bot {
|
||||
shared.RespondEphemeral(s, i, "You can't set a bot's level.")
|
||||
return
|
||||
}
|
||||
|
||||
newLevel := int(levelOpt.IntValue())
|
||||
guildID, ok := shared.ParseGuildID(s, i)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
userID, err := strconv.ParseInt(targetUser.ID, 10, 64)
|
||||
if err != nil {
|
||||
shared.RespondEphemeral(s, i, "Invalid user ID.")
|
||||
return
|
||||
}
|
||||
|
||||
if err := services.Global.Level.SetLevel(context.Background(), guildID, userID, newLevel); err != nil {
|
||||
shared.RespondEphemeral(s, i, "Failed to set level.")
|
||||
return
|
||||
}
|
||||
|
||||
shared.RespondEphemeral(s, i, "Level set to "+strconv.Itoa(newLevel)+" for "+targetUser.Mention()+".")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"velox-bot/internal/commands/level/shared"
|
||||
"velox-bot/internal/db/services"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func slvlSetMessageOption() *discordgo.ApplicationCommandOption {
|
||||
return &discordgo.ApplicationCommandOption{
|
||||
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||
Name: "set-message",
|
||||
Description: "Set the level up message",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "message",
|
||||
Description: "Level-up message. Variables: {user}, {level}",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func slvlSetMessageHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if !shared.RequireManageGuild(s, i) || !shared.RequireLevelSystemEnabled(s, i) {
|
||||
return
|
||||
}
|
||||
if !shared.RequireLevelSettingsService(s, i) {
|
||||
return
|
||||
}
|
||||
|
||||
guildID, ok := shared.ParseGuildID(s, i)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
optMap := shared.SubcommandOptionMap(i)
|
||||
messageOpt, ok := optMap["message"]
|
||||
if !ok {
|
||||
shared.RespondEphemeral(s, i, "Missing message option.")
|
||||
return
|
||||
}
|
||||
|
||||
message := messageOpt.StringValue()
|
||||
if message == "" {
|
||||
message = "GG {user}, you reached level {level}!"
|
||||
}
|
||||
|
||||
if err := services.Global.LevelSettings.SetLevelUpMessage(context.Background(), guildID, message); err != nil {
|
||||
shared.RespondEphemeral(s, i, "Failed to set level up message.")
|
||||
return
|
||||
}
|
||||
|
||||
shared.RespondEphemeral(s, i, "Level up message set.")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"velox-bot/internal/commands/level/shared"
|
||||
"velox-bot/internal/db/services"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func slvlSetRewardOption() *discordgo.ApplicationCommandOption {
|
||||
return &discordgo.ApplicationCommandOption{
|
||||
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||
Name: "set-reward",
|
||||
Description: "Set role reward for a level",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionInteger,
|
||||
Name: "level",
|
||||
Description: "Level to reward",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionRole,
|
||||
Name: "role",
|
||||
Description: "Role to grant",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func slvlSetRewardHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if !shared.RequireManageGuild(s, i) || !shared.RequireLevelSystemEnabled(s, i) {
|
||||
return
|
||||
}
|
||||
if !shared.RequireLevelSettingsService(s, i) {
|
||||
return
|
||||
}
|
||||
|
||||
guildID, ok := shared.ParseGuildID(s, i)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
optMap := shared.SubcommandOptionMap(i)
|
||||
levelOpt, ok := optMap["level"]
|
||||
if !ok {
|
||||
shared.RespondEphemeral(s, i, "Missing level option.")
|
||||
return
|
||||
}
|
||||
roleOpt, ok := optMap["role"]
|
||||
if !ok {
|
||||
shared.RespondEphemeral(s, i, "Missing role option.")
|
||||
return
|
||||
}
|
||||
|
||||
level := int(levelOpt.IntValue())
|
||||
role := roleOpt.RoleValue(s, i.GuildID)
|
||||
if role == nil {
|
||||
shared.RespondEphemeral(s, i, "Invalid role.")
|
||||
return
|
||||
}
|
||||
roleID, err := strconv.ParseInt(role.ID, 10, 64)
|
||||
if err != nil {
|
||||
shared.RespondEphemeral(s, i, "Invalid role ID.")
|
||||
return
|
||||
}
|
||||
|
||||
if err := services.Global.LevelSettings.SetRoleRewardForLevel(context.Background(), guildID, level, roleID); err != nil {
|
||||
shared.RespondEphemeral(s, i, "Failed to set reward.")
|
||||
return
|
||||
}
|
||||
|
||||
shared.RespondEphemeral(s, i, "Reward set: level "+strconv.Itoa(level)+" -> "+role.Mention())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"velox-bot/internal/commands/level/shared"
|
||||
"velox-bot/internal/db/services"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func slvlToggleOption() *discordgo.ApplicationCommandOption {
|
||||
return &discordgo.ApplicationCommandOption{
|
||||
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||
Name: "toggle",
|
||||
Description: "Toggle the leveling system",
|
||||
}
|
||||
}
|
||||
|
||||
func slvlToggleHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if !shared.RequireManageGuild(s, i) {
|
||||
return
|
||||
}
|
||||
if !shared.RequireLevelSettingsService(s, i) {
|
||||
return
|
||||
}
|
||||
guildID, ok := shared.ParseGuildID(s, i)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
enabled, err := services.Global.LevelSettings.ToggleLevelSystem(context.Background(), guildID)
|
||||
if err != nil {
|
||||
shared.RespondEphemeral(s, i, "Failed to toggle leveling system.")
|
||||
return
|
||||
}
|
||||
status := "disabled"
|
||||
if enabled {
|
||||
status = "enabled"
|
||||
}
|
||||
shared.RespondEphemeral(s, i, "Leveling system "+status+" for this server.")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user