-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e26a3ff
commit 5b20438
Showing
7 changed files
with
122 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
build: | ||
go build -o bin/PixelBot . | ||
go build -o bin/PixelBot | ||
|
||
clean: | ||
rm -f bin/bot | ||
|
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
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,32 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Paranoia8972/PixelBot/internal/pkg/utils" | ||
"github.com/bwmarrin/discordgo" | ||
) | ||
|
||
func LevelCommand(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
userID := i.Member.User.ID | ||
guildID := i.GuildID | ||
|
||
level, xp, err := utils.GetUserLevelAndXP(guildID, userID) | ||
if err != nil { | ||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ | ||
Type: discordgo.InteractionResponseChannelMessageWithSource, | ||
Data: &discordgo.InteractionResponseData{ | ||
Content: "Error retrieving your level.", | ||
Flags: 64, | ||
}, | ||
}) | ||
return | ||
} | ||
|
||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ | ||
Type: discordgo.InteractionResponseChannelMessageWithSource, | ||
Data: &discordgo.InteractionResponseData{ | ||
Content: fmt.Sprintf("You are level %d with %d XP!", level, xp), | ||
}, | ||
}) | ||
} |
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
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,61 @@ | ||
package utils | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
) | ||
|
||
type UserData struct { | ||
XP int | ||
Level int | ||
} | ||
|
||
var ( | ||
userData = make(map[string]map[string]*UserData) | ||
userDataMutex = &sync.RWMutex{} | ||
) | ||
|
||
func GetUserLevelAndXP(guildID, userID string) (int, int, error) { | ||
userDataMutex.RLock() | ||
defer userDataMutex.RUnlock() | ||
|
||
if guild, ok := userData[guildID]; ok { | ||
if user, ok := guild[userID]; ok { | ||
return user.Level, user.XP, nil | ||
} | ||
} | ||
return 0, 0, errors.New("user data not found") | ||
} | ||
|
||
func AddXP(guildID, userID string, xp int) error { | ||
userDataMutex.Lock() | ||
defer userDataMutex.Unlock() | ||
|
||
if _, ok := userData[guildID]; !ok { | ||
userData[guildID] = make(map[string]*UserData) | ||
} | ||
|
||
if _, ok := userData[guildID][userID]; !ok { | ||
userData[guildID][userID] = &UserData{XP: 0, Level: 0} | ||
} | ||
|
||
userData[guildID][userID].XP += xp | ||
return nil | ||
} | ||
|
||
func CheckLevelUp(guildID, userID string) (bool, int, error) { | ||
userDataMutex.Lock() | ||
defer userDataMutex.Unlock() | ||
|
||
if guild, ok := userData[guildID]; ok { | ||
if user, ok := guild[userID]; ok { | ||
requiredXP := (user.Level + 1) * 100 | ||
if user.XP >= requiredXP { | ||
user.Level++ | ||
return true, user.Level, nil | ||
} | ||
return false, user.Level, nil | ||
} | ||
} | ||
return false, 0, errors.New("user data not found") | ||
} |