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

140
LINES

< > BotCompany Repo | #1025821 // Samantha v3 [per-guild auth, dev.]

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

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

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

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: #1025821
Snippet name: Samantha v3 [per-guild auth, dev.]
Eternal ID of this version: #1025821/3
Text MD5: 225803cd266724c03e22c78506441ad6
Transpilation MD5: 903b806e53408644fdde0f262fffa958
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-23 14:59:14
Source code size: 5053 bytes / 140 lines
Pitched / IR pitched: No / No
Views / Downloads: 206 / 289
Version history: 2 change(s)
Referenced in: [show references]