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

60
LINES

< > BotCompany Repo | #1023493 - DynDiscordHopper [eats Discord tokens & duplicates itself, seems to work]

JavaX fragment (include) [tags: use-pretranspiled]

Uses 4998K of libraries. Click here for Pure Java version (11911L/85K).

abstract sclass DynDiscordHopper > DynPrintLogAndEnabled {
  !include #1023434 // Discord
  bool discordEnabled() { ret enabled; }
  bool printToModule() { true; }
  
  long preferredChannelID;
  
  enhanceFrame {
    internalFramePopupMenuItem(f, "Start keep-alive module", rThread startKeepAliveModule);
  }

  start {
    startDiscord();
    dm_vmBus_onMessage_q incomingDiscordMessage(voidfunc(Map map) {
      if (!enabled) ret;
      O module = map.get('module);
      if (!dm_isMe(module)) ret;
      S s = getString content(map);
      
      long channelID = toLong(map.get('channelID));
      if (channelID != 0 && preferredChannelID == 0)
        setField(preferredChannelID := channelID);
      
      S answer = answer(s, map);
      if (nempty(answer))
        dm_call(module, 'reply, map, answer);
    });
  }
  
  S answer(S s, Map map) {
    if (eq(s, "!bot count"))
      ret lstr(dm_activeDiscordTokens());

    LS tokens = extractPossibleDiscordTokens(s);
    for unnull (S token : tokens) {
      S answer = "That's a Discord token!";
      if (contains(concatLists((LLS) vmBus_queryAll activeDiscordTokens()), token))
        answer += " And I'm there already.";
      else {
        answer += " Jumping there!!";
        dm_showNewModuleWithParams(dm_moduleLibID(), discordToken := token);
      }
      reply(map, answer);
    }
    
    null;
  }
  
  S atSelf() { ret discordAt(discordBotID); }
  
  // API
  
  S migrateToType(S moduleLibID) {
    S moduleID = assertNotNull(dm_showNewModuleWithParams(moduleLibID, +discordToken));
    print("Migrated to new module " + moduleID + ", disabling me");
    setEnabled(false);
    dm_reload(); // actually disable Discord
    ret moduleID;
  }
}

Author comment

Began life as a copy of #1023440

download  show line numbers  debug dex   

Travelled to 3 computer(s): cfunsshuasjs, mqqgnosmbjvj, tvejysmllsmz

No comments. add comment

Snippet ID: #1023493
Snippet name: DynDiscordHopper [eats Discord tokens & duplicates itself, seems to work]
Eternal ID of this version: #1023493/11
Text MD5: 68eeb7bfb3aef781f449169c3a4c2b2e
Transpilation MD5: 68b52c244ca4dc16b9e2654004790dff
Author: stefan
Category: javax / discord / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-08 12:58:17
Source code size: 1777 bytes / 60 lines
Pitched / IR pitched: No / No
Views / Downloads: 79 / 217
Version history: 10 change(s)
Referenced in: [show references]