/
current_league.go
100 lines (88 loc) · 2.45 KB
/
current_league.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package commands
import (
"bytes"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/nireo/ribels/utils"
"github.com/olekukonko/tablewriter"
"os"
)
func CurrentLeagueGameCommand(session *discordgo.Session, msg *discordgo.MessageCreate, args []string) {
content := ""
sendMessage, err := session.ChannelMessageSend(msg.ChannelID, "Searching...")
region, err := utils.CheckValidRegion(args[2])
if err != nil {
_, _ = session.ChannelMessageSend(msg.ChannelID, "Problem with parsing server")
return
}
client := utils.NewRiotClient(region, os.Getenv("LEAGUE_API"))
summonerName := args[1]
match, err := client.GetLiveMatchBySummonerName(summonerName)
var bt, rt []*discordgo.MessageEmbedField
if err == nil {
// create a new writer for the ascii table
buf := new(bytes.Buffer)
table := tablewriter.NewWriter(buf)
// table settings
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCaption(true, fmt.Sprintf("Current match: %session", summonerName))
table.SetHeader([]string{"Team", "Champion", "Summoner", "Solo"})
// create message embed fields for both teams
headers := []string{"Champion", "Summoner", "Solo"}
for i := range headers {
bt = append(bt, &discordgo.MessageEmbedField{
Name: headers[i],
Value: "\u200b",
Inline: true,
})
rt = append(rt, &discordgo.MessageEmbedField{
Name: headers[i],
Value: "\u200b",
Inline: true,
})
}
// for each player in the match add a message embed field
for _, player := range match {
row := []*discordgo.MessageEmbedField{
{
Value: player.Champion,
Inline: true,
},
{
Value: player.SummonerName,
Inline: true,
},
{
Value: player.Solo,
Inline: true,
},
}
if player.Team == "BLUE" {
bt = append(bt, row...)
} else if player.Team == "RED" {
rt = append(rt, row...)
}
}
//create the table
var tab [][]string
for _, m := range match {
// add row to table
tab = append(tab, []string{
m.Team,
m.Champion,
m.SummonerName,
m.Solo},
)
}
table.AppendBulk(tab)
table.Render()
// ``` are used this way discord makes the message format more flexible
content = "```" + buf.String() + "```"
}
// if there was no table, an error has occured so we prompt the user
if content == "" {
content = "Summoner most likely not in a game"
}
// give out the table with the current game
_, _ = session.ChannelMessageEdit(sendMessage.ChannelID, sendMessage.ID, content)
}