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

141
LINES

< > BotCompany Repo | #1025635 // The Owl [old]

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

Uses 5909K of libraries. Click here for Pure Java version (14708L/80K).

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

Author comment

Began life as a copy of #1025129

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: #1025635
Snippet name: The Owl [old]
Eternal ID of this version: #1025635/7
Text MD5: d8966ce876996c91350ac4246c7200cc
Transpilation MD5: 046d414ca03d61fc77232e162e7304c0
Author: stefan
Category: javax / bots
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-24 13:16:12
Source code size: 5038 bytes / 141 lines
Pitched / IR pitched: No / No
Views / Downloads: 203 / 1980
Version history: 6 change(s)
Referenced in: [show references]