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

116
LINES

< > BotCompany Repo | #1034051 // DynShardedServerAwareDiscordBot

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

Transpiled version (20210L) is out of date.

asclass DynShardedServerAwareDiscordBot<A extends DynShardedServerAwareDiscordBot.ByServer> extends DynShardedDiscordHopper {
  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 #1025127

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034051
Snippet name: DynShardedServerAwareDiscordBot
Eternal ID of this version: #1034051/4
Text MD5: 06a2a032a18c567a67913edc6b96e234
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:59:25
Source code size: 3358 bytes / 116 lines
Pitched / IR pitched: No / No
Views / Downloads: 103 / 173
Version history: 3 change(s)
Referenced in: #1034056 - DynShardedServerAwareDiscordBot renamed
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)