Uses 11163K of libraries. Click here for Pure Java version (16716L/90K).
1 | !7 |
2 | |
3 | set flag JDA40. |
4 | |
5 | !include once #1026298 // DynTalkBot2 for JDA 4.x |
6 | |
7 | // changes in this version: |
8 | // updated to JDA 4.2 |
9 | |
10 | // TODO: delete data on kick |
11 | |
12 | cmodule2 TheOwl extends DynTalkBot2<.ByServer> { |
13 | long greetingsPerformed; |
14 | long clientID = 631845126095896579; |
15 | switchable S mythicalBotsAPIKey; |
16 | int serverCountPostedToMythicalBots; |
17 | |
18 | void init { |
19 | super.init(); |
20 | setField(myName := "owl"); |
21 | makeByServer = () -> new ByServer; |
22 | useAGIBlueForDropPunctuation = false; |
23 | preprocessAtSelfToMyName = true; |
24 | dropPunctuation = false; |
25 | set membersIntent; |
26 | |
27 | onDiscordStarted(r { |
28 | doEveryDayAndNow(r sendServerCountIfChanged); |
29 | }); |
30 | |
31 | dm_vmBus_onMessage_q discordGuildLeave(voidfunc(Map map) { |
32 | ret unless map.get('module) == module(); |
33 | O event = map.get('event); |
34 | O user = rcall getUser(event); |
35 | S name = cast rcall getName(user); |
36 | long guildID = discord_guildIDFromEvent_gen(event); |
37 | if (guildID == 0) fail("Guild zero: " + map); |
38 | getByServer(guildID, true).onUserLeave(name, map); |
39 | }); |
40 | } |
41 | |
42 | class ByServer extends DynTalkBot2<TheOwl.ByServer>.ByServer { |
43 | bool enabled = true; |
44 | S msg = "Welcome <user>!"; |
45 | S goodbyeMsg = "<user> just left us"; |
46 | long channelToGreetIn; |
47 | int delay = 2; // seconds before posting greeting |
48 | |
49 | void onUserJoin(long userID, O... _) { |
50 | S msg = replace(this.msg, "<user>", discordAt(userID)); |
51 | if (channelToGreetIn == 0) setField(channelToGreetIn := preferredChannelInGuild); |
52 | if (channelToGreetIn == 0) fail("Have no greeting channel"); |
53 | if (!boolPar noDelay(_)) sleepSeconds(delay); |
54 | postInChannel(channelToGreetIn, msg); |
55 | } |
56 | |
57 | void onUserLeave(S userName, O... _) { |
58 | postInChannel(channelToGreetIn, replace(goodbyeMsg, "<user>", or2(userName, "an unknown user")); |
59 | } |
60 | |
61 | S processSimplifiedLine(S s, O... _) { |
62 | try answer super.processSimplifiedLine(s, _); // adding authorized users etc. |
63 | |
64 | new Matches m; |
65 | |
66 | S sOld = s; |
67 | s = dropPrefixOrNull(myPrefix(), s); |
68 | if (s == null) { |
69 | print("no got prefix: " + quote(myPrefix()) + " / " + quote(sOld)); |
70 | null; |
71 | } |
72 | |
73 | if (eqic(s, "prefer this channel")) { |
74 | try answer checkPerGuildAuth(_); |
75 | long channelID = longPar channelID(_); |
76 | if (channelID == 0) ret "No channel"; |
77 | setField(channelToGreetIn := channelID); |
78 | ret "OK, will greet here from now on."; |
79 | } |
80 | |
81 | if (eqic(s, "simulate greeting")) { |
82 | print("userID=" + longPar userID(_)); |
83 | onUserJoin(longPar userID(_), paramsPlus(_, noDelay := true)); |
84 | ret "Greeting simulated"; |
85 | } |
86 | |
87 | if (eqic(s, "simulate goodbye")) { |
88 | O user = rcall getAuthor(optPar event(_)); |
89 | S name = cast rcall getName(user); |
90 | onUserLeave(name, _); |
91 | ret "Goodbye simulated"; |
92 | } |
93 | |
94 | if (eqic(s, "get message")) ret msg; |
95 | if (eqic(s, "get goodbye message")) ret goodbyeMsg; |
96 | |
97 | if (eqic(s, "get delay")) ret "Current greeting delay is: " + nSeconds(delay); |
98 | if (swic(s, "set delay ", m)) { |
99 | int i = parseFirstInt(m.rest()); |
100 | setField(delay := clamp(i, 0, 60)); |
101 | ret "Greeting delay set to " + nSeconds(delay); |
102 | } |
103 | |
104 | if (swic_trim(s, "set message:", m)) { |
105 | try answer checkPerGuildAuth(_); |
106 | setField(msg := dropPrefixTrim("Message changed to:", m.rest())); |
107 | ret "Message changed to: " + msg; |
108 | } |
109 | |
110 | if (swic_trim(s, "set goodbye message:", m)) { |
111 | try answer checkPerGuildAuth(_); |
112 | setField(goodbyeMsg := m.rest()); |
113 | ret "Goodbye message changed to: " + goodbyeMsg; |
114 | } |
115 | |
116 | if (eqic(s, "enable")) { |
117 | try answer checkPerGuildAuth(_); |
118 | setField(enabled := true); |
119 | ret "Enabled. Will greet every new arrival!"; |
120 | } |
121 | |
122 | if (eqic(s, "disable")) { |
123 | try answer checkPerGuildAuth(_); |
124 | setField(enabled := true); |
125 | ret "Disabled. Will not greet anyone."; |
126 | } |
127 | |
128 | if (eqic(s, "advanced help")) |
129 | ret trim([[ |
130 | Advanced commands: |
131 | |
132 | @me **masters** -- show who controls me |
133 | @me **add guild master @user** -- allow @user to control me |
134 | @me **delete guild master @user** -- @user can no longer control me |
135 | @me **get delay** -- get delay before posting greeting |
136 | @me **set delay to 5 seconds** -- change greeting delay |
137 | ]]).replace("@me", atSelf()); |
138 | |
139 | if (eqic(s, "help")) |
140 | ret trim([[ |
141 | I am a simple greeter (and farewell) bot. Status: ]] + enabledDisabled(enabled) + [[. Commands: |
142 | |
143 | @me **simulate greeting** -- I greet you so you can test me |
144 | @me **get message** -- show current greeting message |
145 | @me **set message: Hello <user>! Bla bla...** -- change greeting. <user> is replaced with user name |
146 | @me **simulate goodbye** -- I pretend you walked away (although I know you didn't) |
147 | @me **get goodbye message** -- show current goodbye message (what I post when people leave) |
148 | @me **set goodbye message: <user> just left us** -- change farewell message |
149 | @me **prefer this channel** -- posts greetings in the channel you write this in |
150 | @me **enable/disable** -- enable/disable the auto-greeting |
151 | @me **support channel** -- join the support discord |
152 | @me **advanced help** -- see advanced help |
153 | |
154 | [Bot made by https://BotCompany.de] |
155 | ]]).replace("@me", atSelfOrMyName()); |
156 | |
157 | null; |
158 | } |
159 | } |
160 | |
161 | void sendServerCountIfChanged enter { |
162 | if (empty(mythicalBotsAPIKey)) ret; |
163 | print("sendServerCountIfChanged"); |
164 | int count = liveGuildCount(); |
165 | if (count != serverCountPostedToMythicalBots |
166 | && postServerCountToMythicalBotsXYZ(clientID, mythicalBotsAPIKey, count)) |
167 | setField(serverCountPostedToMythicalBots := count); |
168 | } |
169 | } |
Began life as a copy of #1025635
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: | #1025822 |
Snippet name: | The Owl v2 [LIVE] |
Eternal ID of this version: | #1025822/17 |
Text MD5: | 463bd4061b2ba4e78887269d95b75f21 |
Transpilation MD5: | 0913da96526cc5acfbeddf5ae6f20d84 |
Author: | stefan |
Category: | javax / bots |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-01-26 11:42:37 |
Source code size: | 5929 bytes / 169 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 240 / 86794 |
Version history: | 16 change(s) |
Referenced in: | [show references] |