Uses 9908K of libraries. Click here for Pure Java version (15879L/116K).
1 | !7 |
2 | |
3 | !include once #1026298 // DynTalkBot2 for JDA 4.0 |
4 | |
5 | module GreeterBot extends DynTalkBot2<GreeterBot.ByServer> { |
6 | long greetingsPerformed; |
7 | |
8 | void init { |
9 | super.init(); |
10 | makeByServer = () -> new ByServer; |
11 | useAGIBlueForDropPunctuation = false; |
12 | preprocessAtSelfToMyName = true; |
13 | dropPunctuation = false; |
14 | |
15 | dm_vmBus_onMessage_q discordGuildLeave(voidfunc(Map map) { |
16 | ret unless map.get('module) == module(); |
17 | O event = map.get('event); |
18 | O user = rcall getUser(event); |
19 | S name = cast rcall getName(user); |
20 | long guildID = discord_guildIDFromEvent_gen(event); |
21 | if (guildID == 0) fail("Guild zero: " + map); |
22 | getByServer(guildID, true).onUserLeave(name, map); |
23 | }); |
24 | } |
25 | |
26 | class ByServer extends DynTalkBot2.ByServer { |
27 | bool enabled = true; |
28 | S msg = "Welcome <user>!"; |
29 | S goodbyeMsg = "<user> just left us"; |
30 | long channelToGreetIn; |
31 | int delay = 2; // seconds before posting greeting |
32 | |
33 | void onUserJoin(long userID, O... _) { |
34 | S msg = replace(this.msg, "<user>", discordAt(userID)); |
35 | if (channelToGreetIn == 0) setField(channelToGreetIn := preferredChannelID); |
36 | if (channelToGreetIn == 0) fail("Have no greeting channel"); |
37 | if (!boolPar noDelay(_)) sleepSeconds(delay); |
38 | postInChannel(channelToGreetIn, msg); |
39 | } |
40 | |
41 | void onUserLeave(S userName, O... _) { |
42 | postInChannel(channelToGreetIn, replace(goodbyeMsg, "<user>", or2(userName, "an unknown user")); |
43 | } |
44 | |
45 | S processSimplifiedLine(S s, O... _) { |
46 | try answer super.processSimplifiedLine(s, _); // adding authorized users etc. |
47 | |
48 | new Matches m; |
49 | |
50 | S sOld = s; |
51 | s = dropPrefixOrNull(myPrefix(), s); |
52 | if (s == null) { |
53 | print("no got prefix: " + quote(myPrefix()) + " / " + quote(sOld)); |
54 | null; |
55 | } |
56 | |
57 | if (eqic(s, "prefer this channel")) { |
58 | try answer checkAuth(_); |
59 | long channelID = longPar channelID(_); |
60 | if (channelID == 0) ret "No channel"; |
61 | setField(channelToGreetIn := channelID); |
62 | ret "OK, will greet here from now on."; |
63 | } |
64 | |
65 | if (eqic(s, "simulate greeting")) { |
66 | print("userID=" + longPar userID(_)); |
67 | onUserJoin(longPar userID(_), paramsPlus(_, noDelay := true)); |
68 | ret "Greeting simulated"; |
69 | } |
70 | |
71 | if (eqic(s, "simulate goodbye")) { |
72 | O user = rcall getAuthor(optPar event(_)); |
73 | S name = cast rcall getName(user); |
74 | onUserLeave(name, _); |
75 | ret "Goodbye simulated"; |
76 | } |
77 | |
78 | if (eqic(s, "get message")) ret msg; |
79 | if (eqic(s, "get goodbye message")) ret goodbyeMsg; |
80 | |
81 | if (eqic(s, "get delay")) ret "Current greeting delay is: " + nSeconds(delay); |
82 | if (swic(s, "set delay ", m)) { |
83 | int i = parseFirstInt(m.rest()); |
84 | setField(delay := clamp(i, 0, 60)); |
85 | ret "Greeting delay set to " + nSeconds(delay); |
86 | } |
87 | |
88 | if (swic_trim(s, "set message:", m)) { |
89 | try answer checkAuth(_); |
90 | setField(msg := dropPrefixTrim("Message changed to:", m.rest())); |
91 | ret "Message changed to: " + msg; |
92 | } |
93 | |
94 | if (swic_trim(s, "set goodbye message:", m)) { |
95 | try answer checkAuth(_); |
96 | setField(goodbyeMsg := m.rest(); |
97 | ret "Goodbye message changed to: " + msg; |
98 | } |
99 | |
100 | if (eqic(s, "enable")) { |
101 | try answer checkAuth(_); |
102 | setField(enabled := true); |
103 | ret "Enabled. Will greet every new arrival!"; |
104 | } |
105 | |
106 | if (eqic(s, "disable")) { |
107 | try answer checkAuth(_); |
108 | setField(enabled := true); |
109 | ret "Disabled. Will not greet anyone."; |
110 | } |
111 | |
112 | if (eqic(s, "advanced help")) |
113 | ret trim([[ |
114 | Advanced commands: |
115 | |
116 | @me **get delay** -- get delay before posting greeting |
117 | @me **set delay to 5 seconds** -- change greeting delay |
118 | ]]).replace("@me", atSelf()); |
119 | |
120 | if (eqic(s, "help")) |
121 | ret trim([[ |
122 | I am a simple greeter (and farewell) bot. Status: ]] + enabledDisabled(enabled) + [[. Commands: |
123 | |
124 | @me **simulate greeting** -- I greet you so you can test me |
125 | @me **get message** -- show current greeting message |
126 | @me **set message: Hello <user>! Bla bla...** -- change greeting. <user> is replaced with user name |
127 | @me **simulate goodbye** -- I pretend you walked away (although I know you didn't) |
128 | @me **get goodbye message** -- show current goodbye message (what I post when people leave) |
129 | @me **set goodbye message: <user> just left us** -- change farewell message |
130 | @me **prefer this channel** -- posts greetings in the channel you write this in |
131 | @me **enable/disable** -- enable/disable the auto-greeting |
132 | @me **masters**/**add master**/**delete master** -- change bot ownership |
133 | @me **source**/**support channel** -- see sources or join the support discord |
134 | @me **advanced help** -- see advanced help |
135 | |
136 | [Bot made by https://BotCompany.de] |
137 | ]]).replace("@me", atSelf()); |
138 | |
139 | null; |
140 | } |
141 | } |
142 | } |
Began life as a copy of #1025122
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1025129 |
Snippet name: | Samantha v2 [server-aware, LIVE] |
Eternal ID of this version: | #1025129/38 |
Text MD5: | 84df5ecce2bd7e21d08468d9d595daf2 |
Transpilation MD5: | 9023249bd2efad168b43f41757bba9ef |
Author: | stefan |
Category: | javax / bots |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-05-26 21:18:20 |
Source code size: | 5064 bytes / 142 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 425 / 93282 |
Version history: | 37 change(s) |
Referenced in: | [show references] |