diff options
author | WlodekM <[email protected]> | 2025-01-24 11:08:24 +0200 |
---|---|---|
committer | WlodekM <[email protected]> | 2025-01-24 11:08:24 +0200 |
commit | 8358d211ae3d6559956b6727bd35666429059f81 (patch) | |
tree | b1a852af150a69ea05ab894fa9ffa55f76dae865 |
definitely the first commit
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | bot.ts | 27 | ||||
-rw-r--r-- | deno.json | 8 | ||||
-rw-r--r-- | main.ts | 82 |
4 files changed, 119 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8e31f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +creds.json +db.json \ No newline at end of file diff --git a/bot.ts b/bot.ts new file mode 100644 index 0000000..eed58db --- /dev/null +++ b/bot.ts @@ -0,0 +1,27 @@ +import Maelink from "./main.ts"; +import { username, password } from "./creds.json" with {type: "json"}; + +const ml = new Maelink() +console.log( + await ml.login(username, password) +) + +interface Post { + _id: string, + p: string, + u: string, + e: string, + reply_to: null | string, + post_id: string +} + +// ml.ws.onmessage = e => console.log(e) + +ml.on('post', ({p: post, _id}: Post) => { + console.log(post) + if (!post.startsWith(`@wlod-bot`)) return; + ml.sendMessage({ + message: 'hello', + replyTo: _id + }) +}) diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..5b320c2 --- /dev/null +++ b/deno.json @@ -0,0 +1,8 @@ +{ + "tasks": { + "dev": "deno run --watch main.ts" + }, + "imports": { + "@std/assert": "jsr:@std/assert@1" + } +} diff --git a/main.ts b/main.ts new file mode 100644 index 0000000..6a485e0 --- /dev/null +++ b/main.ts @@ -0,0 +1,82 @@ +import { EventEmitter } from "node:events"; + +interface MessageArg { + message: string, + replyTo?: string +} + +export default class MAELINK extends EventEmitter { + private token: string | null = null; + private _ws: WebSocket; + private http: string; + username?: string; + + get ws(): WebSocket { + return this._ws + } + + get(path: string, headers: Record<string, string> = {}) { + console.log(this.http + path); + if (this.token) headers = {...headers, token: this.token}; + return fetch(this.http + path, { + headers, + }) + } + + post(path: string, body: string | object, headers: Record<string, string> = {}) { + console.log(this.http + path); + if (this.token) headers = {...headers, token: this.token}; + if (typeof body == "object") { + headers['content-type'] = 'application/json' + body = JSON.stringify(body); + } + return fetch(this.http + path, { + headers, + body, + method: 'POST' + }) + } + + constructor ( + server: string = 'wss://maelink-ws.derpygamer2142.com', + http: string = 'https://maelink-http.derpygamer2142.com' + ) { + super() + this._ws = new WebSocket(server) + this.http = http + // deno-lint-ignore no-this-alias + const client = this; + this._ws.addEventListener('message', function (ev) { + const data = JSON.parse(ev.data); + switch (data.cmd) { + case 'post_home': + client.emit('post', data.post) + break; + + default: + break; + } + }) + } + + async login(username: string, password: string) { + const resp = await this.post('/login', { username, password }); + if (resp.status != 200) throw 'Status not ok, status is ' + resp.status; + const data = await resp.json() + if (data.status !== 'success') throw 'Login failed: ' + data.message + this.token = data.token + this.username = username + return data + } + + sendMessage(message: string | MessageArg) { + if (!this.token) throw 'You must be logged in to send messages' + if (!message || typeof message == 'object' && message.message.length < 1) throw 'Message cannot be empty' + this._ws.send(JSON.stringify({ + cmd: 'post', + p: typeof message == 'object' ? message.message : message, + token: this.token, + reply_to: typeof message == 'object' ? message.replyTo : undefined + })); + } +} |