-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
76 lines (63 loc) · 1.61 KB
/
message.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
package chat
import (
"bytes"
"encoding/json"
"fmt"
"io"
)
type generalMessage struct {
Bold bool `json:"bold,omitempty"`
Italic bool `json:"italic,omitempty"`
Underlined bool `json:"underlined,omitempty"`
Strikethrough bool `json:"strikethrough,omitempty"`
Obfuscated bool `json:"obfuscated,omitempty"`
Font string `json:"font,omitempty"`
Color string `json:"color,omitempty"`
Insertion string `json:"insertion,omitempty"`
ClickEvent *ClickEvent `json:"clickEvent,omitempty"`
HoverEvent *HoverEvent `json:"hoverEvent,omitempty"`
Translate string `json:"translate"`
With []Message `json:"with,omitempty"`
Extra []Message `json:"extra,omitempty"`
}
type Message struct {
generalMessage
Text string `json:"text"`
}
type translatableMessage struct {
generalMessage
Text string `json:"text,omitempty"`
}
func (s Message) MarshalJson() ([]byte, error) {
if s.Translate != "" {
return json.Marshal(translatableMessage(s))
}
return json.Marshal(s)
}
func (s Message) UnmarshallJson(data []byte) error {
data = bytes.TrimSpace(data)
if len(data) == 0 {
return io.EOF
}
switch data[0] {
case '"':
return json.Unmarshal(data, &s.Text)
case '{':
return json.Unmarshal(data, &s)
case '[':
return json.Unmarshal(data, &s.Extra)
default:
return fmt.Errorf("unknown message type %s", string(data[0]))
}
}
func (s Message) Append(extraMessage ...Message) Message {
s.Extra = append(s.Extra, extraMessage...)
return s
}
func (s Message) WithColor(color string) Message {
s.Color = color
return s
}
func Text(text string) Message {
return Message{Text: text}
}