Transpiled version (20210L) is out of date.
1 | asclass DynShardedServerAwareDiscordBot<A extends DynShardedServerAwareDiscordBot.ByServer> extends DynShardedDiscordHopper { |
2 | new Map<Long, A> dataByServer; |
3 | int guildCount; |
4 | transient IF0<A> makeByServer; // mandatory |
5 | |
6 | void startMe { |
7 | super.startMe(); |
8 | dm_vmBus_onMessage_q incomingDiscordReaction(voidfunc(Map map) { |
9 | if (!enabled) ret; |
10 | O module = map.get('module); |
11 | if (!dm_isMe(module)) ret; |
12 | |
13 | MessageReaction reaction = cast map.get('reaction); |
14 | Guild guild = reaction.getGuild(); |
15 | if (guild == null) ret with print("Ignoring reaction without guild"); |
16 | getByServer(guild).onMessageReaction(map, true); |
17 | }); |
18 | |
19 | dm_vmBus_onMessage_q discordReactionRemoved(voidfunc(Map map) { |
20 | if (!enabled) ret; |
21 | O module = map.get('module); |
22 | if (!dm_isMe(module)) ret; |
23 | |
24 | MessageReaction reaction = cast map.get('reaction); |
25 | Guild guild = reaction.getGuild(); |
26 | if (guild == null) ret with print("Ignoring reaction remove without guild"); |
27 | getByServer(guild).onMessageReaction(map, false); |
28 | }); |
29 | } |
30 | |
31 | class ByServer extends DynamicObject { |
32 | long guildID; |
33 | bool isGuild; |
34 | transient Guild guild; |
35 | |
36 | S answer(S s, Map map) { null; } |
37 | |
38 | bool setField(S name, O value) { |
39 | if (set_trueIfChanged(this, name, value)) false; |
40 | ret true with _change(); |
41 | } |
42 | |
43 | Member getSelfMember(Guild guild) { |
44 | ret guild.getMember(getSelfUser()); |
45 | } |
46 | |
47 | void onMessageReaction(Map map, bool added) { |
48 | //print("onMessageReaction " + added); |
49 | } |
50 | } |
51 | |
52 | @Override S answer(S input, Map map) { |
53 | try answer super.answer(input, map); // discord hopping |
54 | |
55 | long guildID = toLong(map.get('guildID)); |
56 | Guild guild = cast map.get('guild); |
57 | long id = guildID; |
58 | //print("Guild ID: " + guildID); |
59 | if (id == 0) { |
60 | id = toLong(map.get('userID)); |
61 | //print("User ID: " + id); |
62 | } |
63 | |
64 | // config by guild or user |
65 | A data = guild != null ? getByServer(guild) : getByServer(id, id == guildID); |
66 | |
67 | Message msg = cast map.get('msg); |
68 | if (msg != null && nempty(msg.getAttachments())) |
69 | ret data.answer(input, map); |
70 | else |
71 | ret lines_rtrim(nempties( |
72 | map(s -> data.answer(s, map), splitIntoLines(input)))); |
73 | } |
74 | |
75 | A getByServer(long id, bool isGuild) { |
76 | ret pairA(getByServer2(id, isGuild)); |
77 | } |
78 | |
79 | Pair<A, Bool> getByServer2(long id, bool isGuild) { |
80 | A data, bool isNew = unpair syncGetOrCreate2(dataByServer, id, makeByServer); |
81 | data.isGuild = isGuild; |
82 | if (isGuild && data.guildID == 0) { |
83 | data.guildID = id; |
84 | change(); |
85 | } |
86 | if (isNew) { |
87 | if (data.isGuild) ++guildCount; |
88 | change(); |
89 | } |
90 | ret pair(data, isNew); |
91 | } |
92 | |
93 | A getByServer(Guild guild) { |
94 | if (guild == null) null; |
95 | A a = getByServer(guild.getIdLong(), true); |
96 | a.guild = guild; |
97 | ret a; |
98 | } |
99 | |
100 | start { |
101 | if (guildCount == 0) |
102 | setField(guildCount := countValuesWhere_sync(dataByServer, isGuild := true)); |
103 | } |
104 | |
105 | int liveGuildCount() { |
106 | ret l(discord.getGuilds()); |
107 | } |
108 | |
109 | long idForByServer(ByServer bs) { |
110 | ret bs == null ? 0 : toLong(keyForValue_sync(dataByServer, (A) bs)); |
111 | } |
112 | |
113 | LS splitIntoLines(S s) { |
114 | ret tlft(s); |
115 | } |
116 | } |
Began life as a copy of #1025127
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034051 |
Snippet name: | DynShardedServerAwareDiscordBot |
Eternal ID of this version: | #1034051/4 |
Text MD5: | 06a2a032a18c567a67913edc6b96e234 |
Author: | stefan |
Category: | javax / discord / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-01-19 17:59:25 |
Source code size: | 3358 bytes / 116 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 104 / 176 |
Version history: | 3 change(s) |
Referenced in: | [show references] |