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

116
LINES

< > BotCompany Repo | #1034056 // DynShardedServerAwareDiscordBot renamed

JavaX fragment (include)

asclass DynServerAwareDiscordBot<A extends DynServerAwareDiscordBot.ByServer> extends DynDiscordHopper {
  new Map<Long, A> dataByServer;
  int guildCount;
  transient IF0<A> makeByServer; // mandatory
  
  void startMe {
    super.startMe();
    dm_vmBus_onMessage_q incomingDiscordReaction(voidfunc(Map map) {
      if (!enabled) ret;
      O module = map.get('module);
      if (!dm_isMe(module)) ret;
      
      MessageReaction reaction = cast map.get('reaction);
      Guild guild = reaction.getGuild();
      if (guild == null) ret with print("Ignoring reaction without guild");
      getByServer(guild).onMessageReaction(map, true);
    });
    
    dm_vmBus_onMessage_q discordReactionRemoved(voidfunc(Map map) {
      if (!enabled) ret;
      O module = map.get('module);
      if (!dm_isMe(module)) ret;
      
      MessageReaction reaction = cast map.get('reaction);
      Guild guild = reaction.getGuild();
      if (guild == null) ret with print("Ignoring reaction remove without guild");
      getByServer(guild).onMessageReaction(map, false);
    });
  }
  
  class ByServer extends DynamicObject {
    long guildID;
    bool isGuild;
    transient Guild guild;
    
    S answer(S s, Map map) { null; }

    bool setField(S name, O value) {
      if (set_trueIfChanged(this, name, value)) false;
      ret true with _change();
    }
    
    Member getSelfMember(Guild guild) {
      ret guild.getMember(getSelfUser());
    }
    
    void onMessageReaction(Map map, bool added) {
      print("onMessageReaction " + added);
    }
  }
  
  @Override S answer(S input, Map map) {
    try answer super.answer(input, map); // discord hopping
    
    long guildID = toLong(map.get('guildID));
    Guild guild = cast map.get('guild);
    long id = guildID;
    print("Guild ID: " + guildID);
    if (id == 0) {
      id = toLong(map.get('userID));
      print("User ID: " + id);
    }
      
    // config by guild or user
    A data = guild != null ? getByServer(guild) : getByServer(id, id == guildID);
    
    Message msg = cast map.get('msg);
    if (msg != null && nempty(msg.getAttachments()))
      ret data.answer(input, map);
    else
      ret lines_rtrim(nempties(
        map(s -> data.answer(s, map), splitIntoLines(input))));
  }
  
  A getByServer(long id, bool isGuild) {
    ret pairA(getByServer2(id, isGuild));
  }
  
  Pair<A, Bool> getByServer2(long id, bool isGuild) {
    A data, bool isNew = unpair syncGetOrCreate2(dataByServer, id, makeByServer);
    data.isGuild = isGuild;
    if (isGuild && data.guildID == 0) {
      data.guildID = id;
      change();
    }
    if (isNew) {
      if (data.isGuild) ++guildCount;
      change();
    }
    ret pair(data, isNew);
  }
  
  A getByServer(Guild guild) {
    if (guild == null) null;
    A a = getByServer(guild.getIdLong(), true);
    a.guild = guild;
    ret a;
  }
  
  start {
    if (guildCount == 0)
      setField(guildCount := countValuesWhere_sync(dataByServer, isGuild := true));
  }
  
  int liveGuildCount() {
    ret l(discord.getGuilds());
  }
  
  long idForByServer(ByServer bs) {
    ret bs == null ? 0 : toLong(keyForValue_sync(dataByServer, (A) bs));
  }
  
  LS splitIntoLines(S s) {
    ret tlft(s);
  }
}

Author comment

Began life as a copy of #1034051

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034056
Snippet name: DynShardedServerAwareDiscordBot renamed
Eternal ID of this version: #1034056/1
Text MD5: f6e310a5fb4171c2e357b01b7c1d3812
Author: stefan
Category: javax / discord / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-19 17:09:28
Source code size: 3331 bytes / 116 lines
Pitched / IR pitched: No / No
Views / Downloads: 173 / 182
Referenced in: [show references]