This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
121 lines (98 loc) · 2.65 KB
/
client.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package compactdisc
import (
"bytes"
"encoding/json"
"net/http"
"github.com/bwmarrin/discordgo"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type Request[T RequestPayload] struct {
Operation string `json:"op"`
Data T `json:"data"`
}
func ConvertRequest[T RequestPayload](req Request[json.RawMessage]) Request[T] {
var data T
_ = json.Unmarshal(req.Data, &data)
return Request[T]{
Operation: req.Operation,
Data: data,
}
}
func (req Request[T]) ToRaw() Request[json.RawMessage] {
b, _ := json.Marshal(req.Data)
return Request[json.RawMessage]{
Operation: req.Operation,
Data: b,
}
}
type OperationName string
const (
OperationNameSyncUser = "SYNC_USER"
OperationNameSendMessage = "SEND_MESSAGE"
)
type (
MessageSend = discordgo.MessageSend
)
type RequestPayload interface {
json.RawMessage | RequestPayloadSyncUser | RequestPayloadSendMessage
}
type RequestPayloadSyncUser struct {
UserID primitive.ObjectID `json:"user_id"`
Revoke bool `json:"revoke"`
}
type RequestPayloadSendMessage struct {
Channel string `json:"channel"`
Message MessageSend `json:"message"`
Webhook bool `json:"webhook"`
}
type Instance interface {
SyncUser(userID primitive.ObjectID) (*http.Response, error)
RevokeUser(userID primitive.ObjectID) (*http.Response, error)
SendMessage(channel string, message MessageSend, webhook bool) (*http.Response, error)
}
type cdInst struct {
addr string
httpClient *http.Client
}
func New(addr string) Instance {
cl := http.Client{}
return &cdInst{
addr: addr,
httpClient: &cl,
}
}
func (inst *cdInst) request(r Request[json.RawMessage]) (*http.Response, error) {
b, err := json.Marshal(&r)
if err != nil {
return nil, err
}
return inst.httpClient.Post(inst.addr, "application/json", bytes.NewBuffer(b))
}
func (inst *cdInst) SyncUser(userID primitive.ObjectID) (*http.Response, error) {
return inst.request(Request[RequestPayloadSyncUser]{
Operation: OperationNameSyncUser,
Data: RequestPayloadSyncUser{
UserID: userID,
},
}.ToRaw())
}
func (inst *cdInst) RevokeUser(userID primitive.ObjectID) (*http.Response, error) {
return inst.request(Request[RequestPayloadSyncUser]{
Operation: OperationNameSyncUser,
Data: RequestPayloadSyncUser{
UserID: userID,
Revoke: true,
},
}.ToRaw())
}
// SendMessage implements Instance
func (inst *cdInst) SendMessage(channel string, message discordgo.MessageSend, webhook bool) (*http.Response, error) {
return inst.request(Request[RequestPayloadSendMessage]{
Operation: OperationNameSendMessage,
Data: RequestPayloadSendMessage{
Channel: channel,
Message: message,
Webhook: webhook,
},
}.ToRaw())
}