/
common_top.go
82 lines (70 loc) · 2.62 KB
/
common_top.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package commands
import (
"fmt"
"strconv"
"github.com/bwmarrin/discordgo"
"github.com/nireo/ribels/utils"
)
// CommonTopCommandHandler is a generalization of the other osu! related top play commands. This makes it so
// for each osu! mode we don't need a new command. Instead we just use this one.
func CommonTopCommandHandler(s *discordgo.Session, m *discordgo.MessageCreate, args []string, mode string) {
osuName, err := utils.GetOsuUsername(m.Author.ID, args)
if err != nil {
_, _ = s.ChannelMessageSend(m.ChannelID, "Error getting osu username")
return
}
topPlays, err := utils.GetModeTopPlays(osuName, mode)
if err != nil {
_, _ = s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
userId := topPlays[0].UserID
var content string
for index, play := range topPlays {
// load the beatmap so that we can get more information other than the ID
beatmap, err := utils.GetOsuBeatmapMods(play.BeatmapID, play.EnabledMods)
if err != nil {
continue
}
// do all the needed bitwise calculations to get the mods; the error will never happen,
// but handle it for good merit!
mods, err := utils.GetMods(play.EnabledMods)
if err != nil {
_, _ = s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
ppFloat, err := strconv.ParseFloat(play.PP, 64)
if err != nil {
_, _ = s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
starFloat, err := strconv.ParseFloat(beatmap.Difficulty, 64)
if err != nil {
_, _ = s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
content += fmt.Sprintf("**%d. %s[%s] +%s** [%.2f★]\n",
(index + 1), beatmap.Title, beatmap.Version, mods, starFloat)
content += fmt.Sprintf("▸ %s ▸ **%.2f** ▸ %s%%\n",
utils.RankEmojis[play.Rank], ppFloat, play.CalculateTopPlayAcc())
content += fmt.Sprintf("▸ %s ▸ x%s/%s ▸ [%s/%s/%s/%s]\n",
play.Score, play.MaxCombo, beatmap.MaxCombo, play.Count300, play.Count100, play.Count50, play.CountMiss)
content += fmt.Sprintf("▸ Score Set %s\n\n", play.Date)
}
var messageEmbed discordgo.MessageEmbed
messageEmbed.Type = "rich"
messageEmbed.Color = 44504
messageEmbed.Description = content
messageEmbed.Footer = &discordgo.MessageEmbedFooter{
Text: "On osu! Official Server",
}
messageEmbed.Author = &discordgo.MessageEmbedAuthor{
IconURL: fmt.Sprintf("http://s.ppy.sh/a/%s", userId),
Name: fmt.Sprintf("Top 3 osu! Standard plays for %s", osuName),
URL: fmt.Sprintf("http://s.ppy.sh/a/%s", userId),
}
messageEmbed.Thumbnail = &discordgo.MessageEmbedThumbnail{
URL: fmt.Sprintf("http://s.ppy.sh/a/%s", userId),
}
_, _ = s.ChannelMessageSendEmbed(m.ChannelID, &messageEmbed)
}