Uses 4998K of libraries. Click here for Pure Java version (13770L/101K).
1 | asclass DynServerAwareDiscordBot<A extends DynServerAwareDiscordBot.ByServer> extends DynDiscordHopper { |
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 #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: | 396 / 1142 |
Version history: | 34 change(s) |
Referenced in: | [show references] |