This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (126 loc) · 3.68 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"log/slog"
"os"
"os/signal"
"time"
"github.com/apprehensions/rbxbin"
cs "github.com/apprehensions/rbxweb/clientsettings"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/api/cmdroute"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/diamondburned/arikawa/v3/state"
"github.com/diamondburned/arikawa/v3/utils/json/option"
)
func main() {
t := flag.String("token", "", "Discord Bot Token")
c := flag.Int64("channel", 1143583777831010394, "Channel ID")
flag.Parse()
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
errorResponse := func(err error) *api.InteractionResponseData {
return &api.InteractionResponseData{
Content: option.NewNullableString("**Error:** " + err.Error()),
Flags: discord.EphemeralMessage,
AllowedMentions: &api.AllowedMentions{ /* none */ },
}
}
r := cmdroute.NewRouter()
r.AddFunc("query", func(_ context.Context, d cmdroute.CommandData) *api.InteractionResponseData {
var o struct {
GUID string `discord:"guid"`
}
if err := d.Options.Unmarshal(&o); err != nil {
return errorResponse(err)
}
js, err := rbxbin.DefaultMirror.GetJobs()
if err != nil {
return errorResponse(err)
}
var f *rbxbin.Job
for _, j := range js {
if j.GUID == o.GUID {
f = j
}
}
if f == nil {
return errorResponse(errors.New("GUID not found in deployment history"))
}
e := discord.NewEmbed()
e.Title = f.GUID
e.Fields = []discord.EmbedField{
{Name: "Build", Value: f.Build},
{Name: "Time", Value: f.Time.String()},
{Name: "Version", Value: fmt.Sprintf("`%s`", f.Version)},
{Name: "GitHash", Value: f.GitHash},
}
return &api.InteractionResponseData{Embeds: &[]discord.Embed{*e}}
})
s := state.New("Bot " + *t)
s.AddInteractionHandler(r)
s.AddIntents(gateway.IntentGuilds)
if err := cmdroute.OverwriteCommands(s, []api.CreateCommandData{{
Name: "query",
Description: "Retrieve general information about a GUID",
Options: discord.CommandOptions{&discord.StringOption{
OptionName: "guid",
Description: "Deployment GUID to query",
Required: true,
}},
}}); err != nil {
log.Fatalln("cannot update commands:", err)
}
s.AddHandler(func(*gateway.ReadyEvent) {
if err := s.Gateway().Send(ctx, &gateway.UpdatePresenceCommand{
Activities: []discord.Activity{{
Name: "Roblox's binaries",
Type: discord.WatchingActivity,
}},
}); err != nil {
log.Fatalln("cannot update activity:", err)
}
slog.Info("Connected!")
})
k := make(map[cs.BinaryType]*cs.ClientVersion)
for _, b := range []cs.BinaryType{cs.WindowsPlayer, cs.WindowsStudio64} {
k[b] = nil
}
go func() {
for {
for b, v := range k {
cv, err := cs.GetClientVersion(b, "")
if err != nil {
slog.Error("Failed to fetch ClientVersion", "error", err)
continue
}
k[b] = cv
slog.Info("Fetched ClientVersion", "binary", b, "version", cv)
if v == nil || cv.GUID == v.GUID {
continue
}
e := discord.NewEmbed()
e.Color = 0xCC241D
e.Title = fmt.Sprintf("%s@%s", b.Short(), "LIVE")
e.Description = fmt.Sprintf(
"```diff\n- %s (%s)\n+ %s (%s)\n```\n",
v.Version, v.GUID, cv.Version, cv.GUID,
)
slog.Info("Sending updated ClientVersion", "binary", b, "old_version", v, "new_version", cv)
if _, err := s.SendEmbeds(discord.ChannelID(*c), *e); err != nil {
slog.Error("Failed to send update", "error", err)
continue
}
}
time.Sleep(2 * time.Minute)
}
}()
if err := s.Connect(ctx); err != nil {
log.Fatalln("connection closed:", err)
}
}