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