Warning: session_start(): open(/var/lib/php/sessions/sess_5e6cjq4tsbi6v86ilsfhle3org, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
asclass DynShardedServerAwareDiscordBot extends DynShardedDiscordHopper {
new Map dataByServer;
int guildCount;
transient IF0 makeByServer; // mandatory
void startMe {
super.startMe();
dm_vmBus_onMessage_q incomingDiscordReaction(voidfunc(Map map) {
if (!enabled) ret;
O module = map.get('module);
if (!dm_isMe(module)) ret;
MessageReaction reaction = cast map.get('reaction);
Guild guild = reaction.getGuild();
if (guild == null) ret with print("Ignoring reaction without guild");
getByServer(guild).onMessageReaction(map, true);
});
dm_vmBus_onMessage_q discordReactionRemoved(voidfunc(Map map) {
if (!enabled) ret;
O module = map.get('module);
if (!dm_isMe(module)) ret;
MessageReaction reaction = cast map.get('reaction);
Guild guild = reaction.getGuild();
if (guild == null) ret with print("Ignoring reaction remove without guild");
getByServer(guild).onMessageReaction(map, false);
});
}
class ByServer extends DynamicObject {
long guildID;
bool isGuild;
transient Guild guild;
S answer(S s, Map map) { null; }
bool setField(S name, O value) {
if (set_trueIfChanged(this, name, value)) false;
ret true with _change();
}
Member getSelfMember(Guild guild) {
ret guild.getMember(getSelfUser());
}
void onMessageReaction(Map map, bool added) {
print("onMessageReaction " + added);
}
}
@Override S answer(S input, Map map) {
try answer super.answer(input, map); // discord hopping
long guildID = toLong(map.get('guildID));
Guild guild = cast map.get('guild);
long id = guildID;
print("Guild ID: " + guildID);
if (id == 0) {
id = toLong(map.get('userID));
print("User ID: " + id);
}
// config by guild or user
A data = guild != null ? getByServer(guild) : getByServer(id, id == guildID);
Message msg = cast map.get('msg);
if (msg != null && nempty(msg.getAttachments()))
ret data.answer(input, map);
else
ret lines_rtrim(nempties(
map(s -> data.answer(s, map), splitIntoLines(input))));
}
A getByServer(long id, bool isGuild) {
ret pairA(getByServer2(id, isGuild));
}
Pair getByServer2(long id, bool isGuild) {
A data, bool isNew = unpair syncGetOrCreate2(dataByServer, id, makeByServer);
data.isGuild = isGuild;
if (isGuild && data.guildID == 0) {
data.guildID = id;
change();
}
if (isNew) {
if (data.isGuild) ++guildCount;
change();
}
ret pair(data, isNew);
}
A getByServer(Guild guild) {
if (guild == null) null;
A a = getByServer(guild.getIdLong(), true);
a.guild = guild;
ret a;
}
start {
if (guildCount == 0)
setField(guildCount := countValuesWhere_sync(dataByServer, isGuild := true));
}
int liveGuildCount() {
ret l(discord.getGuilds());
}
long idForByServer(ByServer bs) {
ret bs == null ? 0 : toLong(keyForValue_sync(dataByServer, (A) bs));
}
LS splitIntoLines(S s) {
ret tlft(s);
}
}