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

169
LINES

< > BotCompany Repo | #1025822 // The Owl v2 [LIVE]

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

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

!7

set flag JDA40.

!include once #1026298 // DynTalkBot2 for JDA 4.x

// changes in this version:
// updated to JDA 4.2

// TODO: delete data on kick

cmodule2 TheOwl extends DynTalkBot2<.ByServer> {
  long greetingsPerformed;
  long clientID = 631845126095896579;
  switchable S mythicalBotsAPIKey;
  int serverCountPostedToMythicalBots;
  
  void init {
    super.init();
    setField(myName := "owl");
    makeByServer = () -> new ByServer;
    useAGIBlueForDropPunctuation = false;
    preprocessAtSelfToMyName = true;
    dropPunctuation = false;
    set membersIntent;
    
    onDiscordStarted(r {
      doEveryDayAndNow(r sendServerCountIfChanged);
    });
    
    dm_vmBus_onMessage_q discordGuildLeave(voidfunc(Map map) {
      ret unless map.get('module) == module();
      O event = map.get('event);
      O user = rcall getUser(event);
      S name = cast rcall getName(user);
      long guildID = discord_guildIDFromEvent_gen(event);
      if (guildID == 0) fail("Guild zero: " + map);
      getByServer(guildID, true).onUserLeave(name, map);
    });
  }
 
  class ByServer extends DynTalkBot2<TheOwl.ByServer>.ByServer {
    bool enabled = true;
    S msg = "Welcome <user>!";
    S goodbyeMsg = "<user> just left us";
    long channelToGreetIn;
    int delay = 2; // seconds before posting greeting

    void onUserJoin(long userID, O... _) {
      S msg = replace(this.msg, "<user>", discordAt(userID));
      if (channelToGreetIn == 0) setField(channelToGreetIn := preferredChannelInGuild);
      if (channelToGreetIn == 0) fail("Have no greeting channel"); 
      if (!boolPar noDelay(_)) sleepSeconds(delay);
      postInChannel(channelToGreetIn, msg);
    }
    
    void onUserLeave(S userName, O... _) {
      postInChannel(channelToGreetIn, replace(goodbyeMsg, "<user>", or2(userName, "an unknown user"));
    }
    
    S processSimplifiedLine(S s, O... _) {
      try answer super.processSimplifiedLine(s, _); // adding authorized users etc.
      
      new Matches m;
      
      S sOld = s;
      s = dropPrefixOrNull(myPrefix(), s);
      if (s == null) {
        print("no got prefix: " + quote(myPrefix()) + " / " + quote(sOld));
        null;
      }
      
      if (eqic(s, "prefer this channel")) {
        try answer checkPerGuildAuth(_);
        long channelID = longPar channelID(_);
        if (channelID == 0) ret "No channel";
        setField(channelToGreetIn := channelID);
        ret "OK, will greet here from now on.";
      }
      
      if (eqic(s, "simulate greeting")) {
        print("userID=" + longPar userID(_));
        onUserJoin(longPar userID(_), paramsPlus(_, noDelay := true));
        ret "Greeting simulated";
      }
      
      if (eqic(s, "simulate goodbye")) {
        O user = rcall getAuthor(optPar event(_));
        S name = cast rcall getName(user);
        onUserLeave(name, _);
        ret "Goodbye simulated";
      }
      
      if (eqic(s, "get message")) ret msg;
      if (eqic(s, "get goodbye message")) ret goodbyeMsg;
      
      if (eqic(s, "get delay")) ret "Current greeting delay is: " + nSeconds(delay);
      if (swic(s, "set delay ", m)) {
        int i = parseFirstInt(m.rest());
        setField(delay := clamp(i, 0, 60));
        ret "Greeting delay set to " + nSeconds(delay);
      }
      
      if (swic_trim(s, "set message:", m)) {
        try answer checkPerGuildAuth(_);
        setField(msg := dropPrefixTrim("Message changed to:", m.rest()));
        ret "Message changed to: " + msg;
      }
      
      if (swic_trim(s, "set goodbye message:", m)) {
        try answer checkPerGuildAuth(_);
        setField(goodbyeMsg := m.rest());
        ret "Goodbye message changed to: " + goodbyeMsg;
      }
      
      if (eqic(s, "enable")) {
        try answer checkPerGuildAuth(_);
        setField(enabled := true);
        ret "Enabled. Will greet every new arrival!";
      }
      
      if (eqic(s, "disable")) {
        try answer checkPerGuildAuth(_);
        setField(enabled := true);
        ret "Disabled. Will not greet anyone.";
      }
      
      if (eqic(s, "advanced help"))
        ret trim([[
Advanced commands:
        
@me **masters** -- show who controls me
@me **add guild master @user** -- allow @user to control me
@me **delete guild master @user** -- @user can no longer control me
@me **get delay** -- get delay before posting greeting
@me **set delay to 5 seconds** -- change greeting delay
]]).replace("@me", atSelf());
      
      if (eqic(s, "help"))
        ret trim([[
I am a simple greeter (and farewell) bot. Status: ]] + enabledDisabled(enabled) + [[. Commands:

@me **simulate greeting** -- I greet you so you can test me
@me **get message** -- show current greeting message
@me **set message: Hello <user>! Bla bla...** -- change greeting. <user> is replaced with user name
@me **simulate goodbye** -- I pretend you walked away (although I know you didn't)
@me **get goodbye message** -- show current goodbye message (what I post when people leave)
@me **set goodbye message: <user> just left us** -- change farewell message
@me **prefer this channel** -- posts greetings in the channel you write this in
@me **enable/disable** -- enable/disable the auto-greeting
@me **support channel** -- join the support discord
@me **advanced help** -- see advanced help
  
[Bot made by https://BotCompany.de]
  ]]).replace("@me", atSelfOrMyName());
  
      null;
    }
  }
  
  void sendServerCountIfChanged enter {
    if (empty(mythicalBotsAPIKey)) ret;
    print("sendServerCountIfChanged");
    int count = liveGuildCount();
    if (count != serverCountPostedToMythicalBots
      && postServerCountToMythicalBotsXYZ(clientID, mythicalBotsAPIKey, count))
        setField(serverCountPostedToMythicalBots := count);
  }
}

Author comment

Began life as a copy of #1025635

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: #1025822
Snippet name: The Owl v2 [LIVE]
Eternal ID of this version: #1025822/17
Text MD5: 463bd4061b2ba4e78887269d95b75f21
Transpilation MD5: 0913da96526cc5acfbeddf5ae6f20d84
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-26 11:42:37
Source code size: 5929 bytes / 169 lines
Pitched / IR pitched: No / No
Views / Downloads: 223 / 86644
Version history: 16 change(s)
Referenced in: #1030603 - The Owl [updating to JDA 4.2]
#1030604 - The Owl v2 [backup]