-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (65 loc) · 1.54 KB
/
main.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
package main
import (
"flag"
"fmt"
"math/rand"
"github.com/charmbracelet/lipgloss"
"github.com/gempir/go-twitch-irc/v3"
)
var channel string
type messageHistory struct {
users map[string]*lipgloss.Style
msgs []*twitch.PrivateMessage
}
func init() {
flag.StringVar(&channel, "user", "Raven_Tech", "Join <users> chat channel.")
flag.Parse()
}
func main() {
client := twitch.NewAnonymousClient() // for an anonymous user (no write capabilities)
// client := twitch.NewClient("yourtwitchusername", "oauth:123123123")
state := NewMessageHistory()
client.OnPrivateMessage(privateMessageFunc(state))
client.OnConnect(connected)
client.Join(channel)
err := client.Connect()
if err != nil {
panic(err)
}
}
func NewMessageHistory() *messageHistory {
return &messageHistory{
users: make(map[string]*lipgloss.Style),
}
}
func privateMessageFunc(m *messageHistory) func(twitch.PrivateMessage) {
return func(message twitch.PrivateMessage) {
fmt.Printf("<%v>: %v\n",
m.getStyle(message.User).Render(message.User.DisplayName),
message.Message,
)
}
}
func (m *messageHistory) getStyle(user twitch.User) *lipgloss.Style {
var style *lipgloss.Style
var ok bool
if style, ok = m.users[user.ID]; !ok {
if len(user.Color) == 0 {
user.Color = fmt.Sprintf(
"#%x%x%x",
rand.Intn(256),
rand.Intn(256),
rand.Intn(256),
)
}
tstyle := lipgloss.
NewStyle().
Foreground(lipgloss.Color(user.Color))
style = &tstyle
m.users[user.ID] = style
}
return style
}
func connected() {
fmt.Println("Connected to server.")
}