modules/commands: Separate into their own package
parent
825cf44216
commit
05512cbbd2
@ -1,194 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"PermissionGacha/db"
|
||||
"PermissionGacha/modules/active"
|
||||
mlevel "PermissionGacha/modules/level"
|
||||
"PermissionGacha/modules/log"
|
||||
"PermissionGacha/modules/roles"
|
||||
"PermissionGacha/modules/starboard"
|
||||
)
|
||||
|
||||
const emojiCheck = "✅"
|
||||
const prestigeRequirement = 90000
|
||||
|
||||
var prestigeList = make(map[string]string)
|
||||
|
||||
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.Bot {
|
||||
return
|
||||
}
|
||||
go active.Reward(s, m)
|
||||
|
||||
if m.Content == "pgqlevel" {
|
||||
handleXPRequest(s, m)
|
||||
}
|
||||
if m.Content == "pgqshard" {
|
||||
handleShardRequest(s, m)
|
||||
}
|
||||
if m.Content == "pgqprestige" {
|
||||
handlePrestigeRequest(s, m)
|
||||
}
|
||||
if strings.HasPrefix(m.Content, "pgqstar ") {
|
||||
handleStarRequest(s, m)
|
||||
}
|
||||
}
|
||||
|
||||
func handleXPRequest(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
level, rawXP, err := mlevel.GetLevelAndXP(m.Author.ID)
|
||||
if err != nil {
|
||||
log.Error(s, "handleXPRequest", "GetLevelAndXP", err)
|
||||
return
|
||||
}
|
||||
randomSource := make([]byte, 1)
|
||||
rand.Read(randomSource)
|
||||
xp := rawXP - (rawXP % 100) + ((int(randomSource[0]) + 128) % 100)
|
||||
levelUpRequirement := mlevel.GetLevelUpRequirement(level)
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
fmt.Sprintf(
|
||||
"<@%s> is now level %d. (%d XP/%d XP) [%.1f%%]",
|
||||
m.Author.ID, level, xp, levelUpRequirement, float64(xp)/float64(levelUpRequirement)*100,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func handleShardRequest(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
shard, err := db.GetShard(m.Author.ID)
|
||||
if err != nil {
|
||||
log.Error(s, "handleShardRequest", "GetShard", err)
|
||||
return
|
||||
}
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
fmt.Sprintf(
|
||||
"<@%s> has %d shards.",
|
||||
m.Author.ID, shard,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func handlePrestigeRequest(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
currentPrestigeLevel, err := db.GetPrestigeMultiplier(m.Author.ID)
|
||||
if err != nil {
|
||||
log.Error(s, "handlePrestigeRequest", "GetPrestigeMultiplier", err)
|
||||
return
|
||||
}
|
||||
xp, err := db.GetXP(m.Author.ID)
|
||||
if err != nil {
|
||||
log.Error(s, "handlePrestigeRequest", "GetXP", err)
|
||||
return
|
||||
}
|
||||
newPrestigeLevel := xp/prestigeRequirement + 1
|
||||
|
||||
level, _ := mlevel.ConvertXPIntoLevel(xp)
|
||||
|
||||
if level < 5 && currentPrestigeLevel == 1 {
|
||||
s.ChannelMessageSend(m.ChannelID, "**PRESTIGE > **Prestige is only unlocked after level 5!")
|
||||
return
|
||||
}
|
||||
|
||||
var lowerLevelWarning string
|
||||
if newPrestigeLevel <= currentPrestigeLevel {
|
||||
lowerLevelWarning = "**WARNING:** *Your new prestige multiplier is __NOT__ higher than your current prestige multiplier.*"
|
||||
}
|
||||
nearestPowerOfTen := math.Pow10(int(math.Floor(math.Log10(float64(newPrestigeLevel)))))
|
||||
nextNearestLogicalPrestige := int((math.Floor(float64(newPrestigeLevel)/nearestPowerOfTen) + 1) * nearestPowerOfTen)
|
||||
|
||||
msg, err := s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
fmt.Sprintf(
|
||||
"<@%s>, here's your prestige multiplier:\n**Now: **__%dx__\n**New: **__%dx__\n**Next: **__%dx__ [%.1f%%]\n%s\n"+
|
||||
"React with :white_check_mark: to proceed with prestiging.",
|
||||
m.Author.ID,
|
||||
currentPrestigeLevel,
|
||||
newPrestigeLevel,
|
||||
nextNearestLogicalPrestige, float64(xp)/float64(prestigeRequirement*(nextNearestLogicalPrestige-1))*100,
|
||||
lowerLevelWarning,
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error(s, "handlePrestigeRequest", "send", err)
|
||||
return
|
||||
}
|
||||
prestigeList[msg.ID] = m.Author.ID
|
||||
err = s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiCheck)
|
||||
if err != nil {
|
||||
log.Error(s, "handlePrestigeRequest", "addReaction", err)
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
time.Sleep(15 * time.Second)
|
||||
err = s.MessageReactionsRemoveAll(msg.ChannelID, msg.ID)
|
||||
if err != nil {
|
||||
log.Error(s, "handlePrestigeRequest", "removeReaction", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func checkPrestigeReact(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
|
||||
if prestigeList[m.MessageID] != m.UserID {
|
||||
return
|
||||
}
|
||||
if m.Emoji.Name != emojiCheck {
|
||||
return
|
||||
}
|
||||
xp, err := db.GetXP(m.UserID)
|
||||
if err != nil {
|
||||
log.Error(s, "checkPrestigeReact", "GetXP", err)
|
||||
return
|
||||
}
|
||||
newPrestigeLevel := xp/prestigeRequirement + 1
|
||||
go roles.RemoveAllLevelRoles(s, m.UserID)
|
||||
err = db.SetPrestigeMultiplier(m.UserID, newPrestigeLevel)
|
||||
if err != nil {
|
||||
log.Error(s, "checkPrestigeReact", "SetPrestigeMultiplier", err)
|
||||
return
|
||||
}
|
||||
s.ChannelMessageSend(m.ChannelID,
|
||||
fmt.Sprintf(
|
||||
"**PRESTIGE > ** <@%s>, you have prestiged!", m.UserID,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func handleStarRequest(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
commandSplit := strings.SplitN(m.Content, " ", 2)
|
||||
if len(commandSplit) == 1 {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
return
|
||||
}
|
||||
linkSplit := strings.SplitN(commandSplit[1], "/", 7)
|
||||
if len(linkSplit) < 7 {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
return
|
||||
}
|
||||
if linkSplit[0] != "https:" && linkSplit[0] != "http:" {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
return
|
||||
}
|
||||
if linkSplit[2] != "discordapp.com" && !strings.HasSuffix(linkSplit[2], ".discordapp.com") {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
return
|
||||
}
|
||||
if linkSplit[3] != "channels" {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
return
|
||||
}
|
||||
if linkSplit[4] != m.GuildID {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
return
|
||||
}
|
||||
err := starboard.AddReact(s, linkSplit[5], linkSplit[6])
|
||||
if err != nil {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package commands
|
||||
|
||||
import "github.com/bwmarrin/discordgo"
|
||||
|
||||
type command struct {
|
||||
Name string
|
||||
Handler func(s *discordgo.Session, args []string, m *discordgo.MessageCreate) error
|
||||
}
|
||||
|
||||
var list = []command{
|
||||
command{
|
||||
Name: "level",
|
||||
Handler: handleLevelCommand,
|
||||
},
|
||||
command{
|
||||
Name: "shard",
|
||||
Handler: handleShardCommand,
|
||||
},
|
||||
command{
|
||||
Name: "prestige",
|
||||
Handler: handlePrestigeCommand,
|
||||
},
|
||||
command{
|
||||
Name: "star",
|
||||
Handler: handleStarCommand,
|
||||
},
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"PermissionGacha/modules/level"
|
||||
)
|
||||
|
||||
func handleLevelCommand(s *discordgo.Session, args []string, m *discordgo.MessageCreate) error {
|
||||
levels, rawXP, err := level.GetLevelAndXP(m.Author.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("commands: handleLevelCommand: error while getting level and XP: %w", err)
|
||||
}
|
||||
randomSource := make([]byte, 1)
|
||||
rand.Read(randomSource)
|
||||
xp := rawXP - (rawXP % 100) + ((int(randomSource[0]) + 128) % 100)
|
||||
levelUpRequirement := level.GetLevelUpRequirement(levels)
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
fmt.Sprintf(
|
||||
"<@%s> is now level %d. (%d XP/%d XP) [%.1f%%]",
|
||||
m.Author.ID, levels, xp, levelUpRequirement, float64(xp)/float64(levelUpRequirement)*100,
|
||||
),
|
||||
)
|
||||
return nil
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"PermissionGacha/modules/config"
|
||||
)
|
||||
|
||||
func Parse(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.Bot {
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(m.Content, config.CommandPrefix) {
|
||||
return
|
||||
}
|
||||
|
||||
commands := strings.Split(strings.TrimPrefix(m.Content, config.CommandPrefix), " ")
|
||||
|
||||
for _, v := range list {
|
||||
if v.Name == commands[0] {
|
||||
v.Handler(s, commands, m)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"PermissionGacha/db"
|
||||
"PermissionGacha/modules/level"
|
||||
"PermissionGacha/modules/log"
|
||||
"PermissionGacha/modules/roles"
|
||||
)
|
||||
|
||||
const emojiCheck = "✅"
|
||||
const prestigeRequirement = 90000
|
||||
|
||||
var prestigeList = make(map[string]string)
|
||||
|
||||
func handlePrestigeCommand(s *discordgo.Session, args []string, m *discordgo.MessageCreate) error {
|
||||
//TODO: Factorize into backend parts
|
||||
currentPrestigeLevel, err := db.GetPrestigeMultiplier(m.Author.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("commands: handlePrestigeCommand: error while getting current prestige level: %w", err)
|
||||
}
|
||||
xp, err := db.GetXP(m.Author.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("commands: handlePrestigeCommand: error while getting current XP: %w", err)
|
||||
}
|
||||
newPrestigeLevel := xp/prestigeRequirement + 1
|
||||
|
||||
currentLevel, _ := level.ConvertXPIntoLevel(xp)
|
||||
|
||||
if currentLevel < 5 && currentPrestigeLevel == 1 {
|
||||
s.ChannelMessageSend(m.ChannelID, "**PRESTIGE > **Prestige is only unlocked after level 5!")
|
||||
return nil
|
||||
}
|
||||
|
||||
var lowerLevelWarning string
|
||||
if newPrestigeLevel <= currentPrestigeLevel {
|
||||
lowerLevelWarning = "**WARNING:** *Your new prestige multiplier is __NOT__ higher than your current prestige multiplier.*"
|
||||
}
|
||||
nearestPowerOfTen := math.Pow10(int(math.Floor(math.Log10(float64(newPrestigeLevel)))))
|
||||
nextNearestLogicalPrestige := int((math.Floor(float64(newPrestigeLevel)/nearestPowerOfTen) + 1) * nearestPowerOfTen)
|
||||
|
||||
msg, err := s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
fmt.Sprintf(
|
||||
"<@%s>, here's your prestige multiplier:\n**Now: **__%dx__\n**New: **__%dx__\n**Next: **__%dx__ [%.1f%%]\n%s\n"+
|
||||
"React with :white_check_mark: to proceed with prestiging.",
|
||||
m.Author.ID,
|
||||
currentPrestigeLevel,
|
||||
newPrestigeLevel,
|
||||
nextNearestLogicalPrestige, float64(xp)/float64(prestigeRequirement*(nextNearestLogicalPrestige-1))*100,
|
||||
lowerLevelWarning,
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("commands: handlePrestigeCommand: error while sending message: %w", err)
|
||||
}
|
||||
prestigeList[msg.ID] = m.Author.ID
|
||||
err = s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiCheck)
|
||||
if err != nil {
|
||||
return fmt.Errorf("commands: handlePrestigeCommand: error while adding reaction: %w", err)
|
||||
}
|
||||
|
||||
time.Sleep(15 * time.Second)
|
||||
delete(prestigeList, msg.ID)
|
||||
err = s.MessageReactionsRemoveAll(msg.ChannelID, msg.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("commands: handlePrestigeCommand: error while removing reaction: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ListenPrestigeReact(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
|
||||
if prestigeList[m.MessageID] != m.UserID {
|
||||
return
|
||||
}
|
||||
if m.Emoji.Name != emojiCheck {
|
||||
return
|
||||
}
|
||||
xp, err := db.GetXP(m.UserID)
|
||||
if err != nil {
|
||||
log.Error(s, "checkPrestigeReact", "GetXP", err)
|
||||
return
|
||||
}
|
||||
newPrestigeLevel := xp/prestigeRequirement + 1
|
||||
go roles.RemoveAllLevelRoles(s, m.UserID)
|
||||
err = db.SetPrestigeMultiplier(m.UserID, newPrestigeLevel)
|
||||
if err != nil {
|
||||
log.Error(s, "checkPrestigeReact", "SetPrestigeMultiplier", err)
|
||||
return
|
||||
}
|
||||
s.ChannelMessageSend(m.ChannelID,
|
||||
fmt.Sprintf(
|
||||
"**PRESTIGE > ** <@%s>, you have prestiged!", m.UserID,
|
||||
),
|
||||
)
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"PermissionGacha/db"
|
||||
)
|
||||
|
||||
func handleShardCommand(s *discordgo.Session, args []string, m *discordgo.MessageCreate) error {
|
||||
shard, err := db.GetShard(m.Author.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("commands: handleShardCommand: error while getting shards: %w", err)
|
||||
}
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
fmt.Sprintf(
|
||||
"<@%s> has %d shards.",
|
||||
m.Author.ID, shard,
|
||||
),
|
||||
)
|
||||
return nil
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"PermissionGacha/modules/starboard"
|
||||
)
|
||||
|
||||
func handleStarCommand(s *discordgo.Session, args []string, m *discordgo.MessageCreate) error {
|
||||
if len(args) == 1 {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
return nil
|
||||
}
|
||||
linkSplit := strings.SplitN(args[1], "/", 7)
|
||||
if len(linkSplit) < 7 {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
return nil
|
||||
}
|
||||
if linkSplit[0] != "https:" && linkSplit[0] != "http:" {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
return nil
|
||||
}
|
||||
if linkSplit[2] != "discordapp.com" && !strings.HasSuffix(linkSplit[2], ".discordapp.com") {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
return nil
|
||||
}
|
||||
if linkSplit[3] != "channels" {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
return nil
|
||||
}
|
||||
if linkSplit[4] != m.GuildID {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
return nil
|
||||
}
|
||||
err := starboard.AddReact(s, linkSplit[5], linkSplit[6])
|
||||
if err != nil {
|
||||
s.ChannelMessageSend(m.ChannelID, "**STAR > **It doesn't seem like you've sent a valid message link!")
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
package config
|
||||
|
||||
const CommandPrefix = "pgq"
|
Reference in New Issue