/
printer.go
94 lines (73 loc) · 2.18 KB
/
printer.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
package discord
import (
"fmt"
"strings"
"github.com/Trojan295/discord-bingo-bot/pkg/bingo/game"
"github.com/bwmarrin/discordgo"
"github.com/olekukonko/tablewriter"
)
func boardToString(board *game.Board) string {
tableString := strings.Builder{}
table := tablewriter.NewWriter(&tableString)
table.SetRowLine(true)
table.SetColWidth(20)
for y, v := range board.FieldTexts {
row := make([]string, 5)
copy(row, v)
for x := 0; x < 5; x++ {
row[x] = fmt.Sprintf("%d. ", y*5+x+1) + row[x]
if board.Ticked[y][x] == 1 {
row[x] = "██████" + row[x] + "██████"
}
}
table.Append(row)
}
table.Render()
return tableString.String()
}
func getBoardMessages(board *game.Board) []string {
messages := make([]string, 0)
boardString := boardToString(board)
lines := strings.Split(boardString, "\n")
buffer := strings.Builder{}
for _, line := range lines {
if buffer.Len()+len(line) > 2000 {
messages = append(messages, "`"+buffer.String()+"`")
buffer.Reset()
}
buffer.WriteString(line)
buffer.WriteRune('\n')
}
messages = append(messages, "`"+buffer.String()+"`")
return messages
}
func PrintMessage(v interface{}, m *discordgo.MessageCreate) []string {
switch msg := v.(type) {
case game.TextMessage:
return []string{msg.Text}
case game.ListTextsResponse:
builder := strings.Builder{}
builder.WriteString("**Texts:**\n")
for idx, text := range msg.Texts {
builder.WriteString(fmt.Sprintf("%d. %s\n", idx+1, text))
}
return []string{builder.String()}
case game.ShowBoardResponse:
return getBoardMessages(msg.Board)
case game.BingoWonResponse:
boardMsgs := getBoardMessages(msg.Board)
return append([]string{fmt.Sprintf("**Yeah! %s, you won!**", m.Author.Mention())}, boardMsgs...)
case game.HelpResponse:
builder := strings.Builder{}
builder.WriteString("**Help:**\n")
for _, command := range msg.Commands {
builder.WriteString(fmt.Sprintf("`%s` - %s\n", command.Cmd, command.Description))
}
if msg.ContactPage != "" {
builder.WriteString("\n")
builder.WriteString(fmt.Sprintf("You can get support for the bot here: %s.\n", msg.ContactPage))
}
return []string{builder.String()}
}
return []string{}
}