Зачатки апи для создания команд

This commit is contained in:
lost+skunk 2025-01-09 01:25:20 +03:00
parent 8277809e4d
commit c22eba6f87
3 changed files with 58 additions and 33 deletions

View File

@ -5,8 +5,8 @@ import asdf: deserialize, serializeToJson;
import util;
void main() {
init("syt_c29mdHBpZ2VvbmVz_QqNnkNjuuAffMKfGfUEO_0SqrAx", "hot-chilli.im");
sync();
init("", "hot-chilli.im");
sync;
}
HTTP http;
@ -14,6 +14,7 @@ string homeserver;
void init(string token, string hs) {
http = HTTP();
http.addRequestHeader("Authorization", "Bearer " ~ token);
http.tcpNoDelay = true;
homeserver = "https://" ~ hs;
get(homeserver~"/_matrix/federation/v1/version");
}
@ -29,43 +30,38 @@ void send(T)(T content, string roomid, string type = "m.room.message") {
// }
void sync() {
import core.memory;
import object: destroy;
import commands;
string str = homeserver ~ "/_matrix/client/v3/sync?set_presence=online";
Sync content = get(str, http).deserialize!Sync; str ~= "&since=";
for (;;) {
string bthUrl = str ~ content.next_batch;
writeln(content.next_batch);
destroy(content);
GC.free(&content);
content = get(bthUrl, http).deserialize!Sync;
// 🇮🇳🇮🇳🇮🇳
foreach (room, _; content.rooms.invite) {
writeln("Joining to room: ", room);
post(homeserver~"/_matrix/client/v3/rooms/"~room~"/join", null, http);
send(MSG("Yasno << Ponyatno"), room);
send(MSG("Привет, я СканкиБот"), room);
}
foreach (room, roomContent; content.rooms.join) {
foreach(event; roomContent.timeline.events) {
if (event.type == "m.room.message") {
try {
auto evt = deserialize!MSG(event.content.data);
writeln(room, ": ", evt.body);
switch (evt.body) {
case "!huy":
send(MSG(":orehussmile:",
`<img data-mx-emoticon height="32" src="mxc://4d2.org/XvWYAuhASYRHtYvtspsrWvtU" alt=":orehussmile:" title=":orehussmile:">`), room);
break;
case "!версия": send(MSG("Compiler version: "~intToStr(__VERSION__)), room); break;
case "скунс": send(MSG("еблан"), room); break;
case "!avatar":
string url = cast(string)get(homeserver~"/_matrix/client/v3/profile/"~event.sender~"/avatar_url");
if (url == "{}") {send(MSG("Avatar is empty"), room); break;}
send(MSG(event.sender, `<img src="`~url[15..$-2]~`">`), room);
break;
default: break;
foreach (member; __traits(allMembers, commands)) {
alias mmbr = __traits(getMember, commands, member);
if (evt.body[1..$] == member) {
static foreach (attr; __traits(getAttributes, mmbr)) {
static if (is(attr == Command)) {
static if (is(mmbr == function))
send(mmbr(), room);
else static if (__traits(isStaticArray, mmbr))
send(MSG(mmbr[0], mmbr[1]), room);
else
send(MSG(mmbr), room);
}
}
}
}
} catch (Exception e) {
writeln(e.msg);
@ -76,11 +72,3 @@ void sync() {
}
}
}
string intToStr(T)(T num) {
char[] buf;
for(short i; num > 0; ++i) {
buf = (num % 10 + '0')~buf;
num /= 10;
} return cast(string)buf;
}

29
source/commands.d Normal file
View File

@ -0,0 +1,29 @@
module commands;
import util;
enum Command; // UDA
// auto avatar(string[] arguments) @Command {
// string url = cast(string)get(homeserver~"/_matrix/client/v3/profile/"~event.sender~"/avatar_url");
// if (url == "{}")
// return MSG("User has no avatar");
// return MSG(event.sender, `<img src="`~url[15..$-2]~`">`);
// }
@Command string[2] huy = [":orehussmile:",
`<img data-mx-emoticon height="32" alt=":orehussmile:" title=":orehussmile:"
src="mxc://4d2.org/XvWYAuhASYRHtYvtspsrWvtU" >`];
@Command string ver = "SkunkyBot Pre-Alpha 0.1 :: https://git.bloat.cat/skunky/skunkybot-d";
static @Command string compver = "Compiler version: "~intToStr(__VERSION__);
// switch (evt.body) {
// case "!huy":
// send(MSG(":orehussmile:",
// ), room);
// break;
// case "!версия": send(), room); break;
// case "скунс": send(MSG("еблан"), room); break;
// case "!avatar":
//
// break;
// default: break;
// }

View File

@ -1,8 +1,16 @@
module util;
import asdf;
string intToStr(T)(T num) {
char[] buf;
for(short i; num > 0; ++i) {
buf = (num % 10 + '0')~buf;
num /= 10;
} return cast(string)buf;
}
struct JsonObject {
import std.conv: to;
import mir.conv: to;
string data;
SerdeException deserializeFromAsdf(Asdf data) {