Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

166
LINES

< > BotCompany Repo | #1030603 // The Owl [updating to JDA 4.2]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 11163K of libraries. Click here for Pure Java version (16701L/89K).

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

Author comment

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: #1030603
Snippet name: The Owl [updating to JDA 4.2]
Eternal ID of this version: #1030603/3
Text MD5: 1b2e521101966269f7f8cc0cae799b6b
Transpilation MD5: 25f386f5bc90ede0d843deb4f69b0085
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:24:56
Source code size: 5865 bytes / 166 lines
Pitched / IR pitched: No / No
Views / Downloads: 172 / 237
Version history: 2 change(s)
Referenced in: [show references]