aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2023-06-13 23:40:40 -0400
committerXe Iaso <me@xeiaso.net>2023-06-13 23:58:29 -0400
commita6c11eb8e407898e83251c202d4b82702812c6f9 (patch)
tree4c56754af33ef7abc4a618a81a22ce0e702f9d57
parentf6ea314b42ceeac11235ea257f7a47a2e6b774ce (diff)
downloadx-a6c11eb8e407898e83251c202d4b82702812c6f9.tar.xz
x-a6c11eb8e407898e83251c202d4b82702812c6f9.zip
cmd/marabot: new command to test revolt bot stuff
Signed-off-by: Xe Iaso <me@xeiaso.net>
-rw-r--r--cardio/cardio.go6
-rw-r--r--cmd/marabot/main.go82
2 files changed, 85 insertions, 3 deletions
diff --git a/cardio/cardio.go b/cardio/cardio.go
index 880e4cd..ef67a83 100644
--- a/cardio/cardio.go
+++ b/cardio/cardio.go
@@ -43,7 +43,7 @@ import (
// monitor and alert on this value changing erratically.
func Heartbeat(ctx context.Context, min, max time.Duration) (<-chan struct{}, func(), func()) {
heartbeat := make(chan struct{}, 1) // output channel
- currDelay := (max + min) / 2 // start at half speed
+ currDelay := max // start at max speed
var currDelayLock sync.Mutex
var counter *expvar.Int
@@ -83,11 +83,11 @@ func Heartbeat(ctx context.Context, min, max time.Duration) (<-chan struct{}, fu
toSleep := currDelay
currDelayLock.Unlock()
time.Sleep(toSleep)
-
+
if counter != nil {
counter.Set(int64(toSleep))
}
-
+
select {
case heartbeat <- struct{}{}:
default:
diff --git a/cmd/marabot/main.go b/cmd/marabot/main.go
new file mode 100644
index 0000000..12c0b1a
--- /dev/null
+++ b/cmd/marabot/main.go
@@ -0,0 +1,82 @@
+package main
+
+import (
+ "context"
+ "flag"
+ "os"
+ "os/signal"
+ "syscall"
+ "time"
+
+ "within.website/ln"
+ "within.website/ln/opname"
+ "within.website/x/internal"
+ "within.website/x/web/revolt"
+)
+
+var (
+ revoltToken = flag.String("revolt-token", "", "Revolt bot token")
+ revoltAPIServer = flag.String("revolt-api-server", "https://api.revolt.chat", "API server for Revolt")
+ revoltWebsocketServer = flag.String("revolt-ws-server", "wss://ws.revolt.chat", "Websocket server for Revolt")
+)
+
+func main() {
+ internal.HandleStartup()
+
+ ctx, cancel := context.WithCancel(opname.With(context.Background(), "marabot"))
+ defer cancel()
+
+ ln.Log(ctx, ln.Action("starting up"))
+
+ // Init a new client.
+ client := revolt.NewWithEndpoint(*revoltToken, *revoltAPIServer, *revoltWebsocketServer)
+
+ mr := &MaraRevolt{
+ cli: client,
+ }
+
+ client.Connect(ctx, mr)
+
+ // Wait for close.
+ sc := make(chan os.Signal, 1)
+
+ signal.Notify(
+ sc,
+ syscall.SIGINT,
+ syscall.SIGTERM,
+ os.Interrupt,
+ )
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case <-sc:
+ ln.Log(ctx, ln.Info("shutting down"))
+ cancel()
+ time.Sleep(150 * time.Millisecond)
+ return
+ }
+ }
+}
+
+type MaraRevolt struct {
+ cli *revolt.Client
+ revolt.NullHandler
+}
+
+func (m *MaraRevolt) MessageCreate(ctx context.Context, msg *revolt.Message) error {
+ if msg.Content == "!ping" {
+ sendMsg := &revolt.SendMessage{
+ Masquerade: &revolt.Masquerade{
+ Name: "cipra",
+ AvatarURL: "https://cdn.xeiaso.net/avatar/" + internal.Hash("cipra", "yasomi"),
+ },
+ }
+ sendMsg.SetContent("🏓 Pong!")
+
+ if _, err := msg.Reply(true, sendMsg); err != nil {
+ return err
+ }
+ }
+ return nil
+}