Uses 4998K of libraries. Click here for Pure Java version (13770L/101K).
asclass DynServerAwareDiscordBot<A extends DynServerAwareDiscordBot.ByServer> extends DynDiscordHopper { new Map<Long, A> dataByServer; int guildCount; transient IF0<A> 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<A, Bool> 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); } }
Began life as a copy of #1024131
download show line numbers debug dex old transpilations
Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1025127 |
Snippet name: | DynServerAwareDiscordBot |
Eternal ID of this version: | #1025127/35 |
Text MD5: | f6e310a5fb4171c2e357b01b7c1d3812 |
Transpilation MD5: | c9fe8fe5895aeb9e84411b5240cf7f70 |
Author: | stefan |
Category: | javax / discord / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-12-13 18:58:33 |
Source code size: | 3331 bytes / 116 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 397 / 1142 |
Version history: | 34 change(s) |
Referenced in: | #1034051 - DynShardedServerAwareDiscordBot #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674) |