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

130
LINES

< > BotCompany Repo | #1025913 // Lottery Bot

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

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

!7

concept CLotteryResult {
  S date;
  L<Int> numbers;
  S jackpot;
  
  [stdToString]
}

standardBot1 LotteryBot {
  long resultsChannelID;
  
  init {
    dbIndexing(CLotteryResult, 'date);
    onDiscordStarted(r { doEveryHourAndNow(r grabResults) });
  }
  
  allServers {
    S lotteryURL = "https://www.lotteryusa.com/missouri/show-me-cash/";

    void grabResults enter {
      LotteryResult r = scrapeLotteryResults(loadPage(lotteryURL));
      if (r == null) ret with print("No lottery results!?");
      CLotteryResult lr, bool isNew = unpair uniq2(CLotteryResult, date := r.date);
      copyFields(r, lr);
      change();
      print("Grabbed: " + lr + ", new: " + isNew);
      
      if (isNew) postLastResult_allServers();
    }
    
    void postLastResult_allServers {
      L<Guild> guilds = discord.getGuilds();
      for (Guild guild : guilds) pcall {
        ByServer bs = getByServer(guild);
        if (bs != null)
          bs.postLastResult();
      }
    }
    
    S renderResult(CLotteryResult r) {
      ret formatCSVLine(flattenList2(r.date, r.numbers, r.jackpot));
    }
    
    CLotteryResult latestResult() {
      ret first(conceptsSortedByFieldDesc CLotteryResult('date));
    }
  }
  
  processSimplified {
    optPar Message msg;
    Message.Attachment attachment = msg == null ? null : first(msg.getAttachments());
    if (attachment != null && authed(_)) {
      File file = prepareCacheProgramFile(guildID + "/" + attachment.getFileName());
      print("Downloading attachment: " + file);
      deleteFile(file);
      if (!attachment.download(file)) ret "Couldn't download attachment";
      S contents = loadTextFile(file);
      contents = replaceAll(contents, "^,+\r?\n", "");
      if (!startsWith(contents, "\"") && !startsWithDigit(contents)) null; //ret "Attachment is not a CSV text file";
      LLS l = parseCSV_unquote_noHeader(contents);
      l = map(l, row -> l(row) != 7 ? row
        : spliceList(row, 1, 6, ll(joinWithComma(subList(row, 1, 6)))));
      Set<Int> lengths = mapToSet(lambda1 l, l);
      if (neq(lengths, litset(3))) ret "CSV does not have 3 columns throughout";
      for (LS line : l) {
        S date = first(line);
        if (!regexpMatches(regexpYMDminus(), date)) ret "Bad date: " + backtickQuote(date);
        CLotteryResult lr = uniq(CLotteryResult, +date);
        cset(lr, numbers := map parseInt(tok_splitAtComma(second(line))), jackpot := third(line));
      }
      
      ret "OK, " + nResults(l) + " imported";
    }
    
    S s0 = s;
    s = dropMyPrefixOpt(s);
    
    if "clear lottery results" {
      try answer checkAuth(_);
      ret "OK, " + nResults(deleteConcepts(CLotteryResult)) + " deleted";
    }

    if "lottery|lottery results|show me cash" {
      optPar long channelID;
      L<CLotteryResult> l = sortedByFieldDesc date(list(CLotteryResult));
      if (empty(l)) ret "No lottery results gathered yet";
      S date1 = first(l).date;
      S text = windowsLineBreaks(mapToLines_rtrim(l, lambda1 renderResult));
      uploadFileInChannel(channelID,
        toUtf8(text),
        "lottery-" + date1 + ".csv", null, null);
      null;
    }
    
    if "Post lottery here" {
      long channelID = longPar channelID(_);
      if (channelID == 0) ret "Please do this in a channel";
      if (resultsChannelID != channelID) {
        resultsChannelID = channelID;
        change();
      }
      doAfter(2.0, r postLastResult);
      ret "OK, I will post new lottery results in this channel. Type `stop posting lottery` to stop.";
    }
      
    if "stop posting lottery" {
      resultsChannelID = 0;
      change();
      ret "OK";
    }
    
    if null (s = dropMyPrefixOrNull(s0)) null;
    
    if "help" {
      ret [[
@me `lottery` - download latest lottery results as CSV
@me `clear lottery results` - delete all lottery results

To add lottery results, just upload a CSV file.
]].replace("@me", atSelf());
    }
  }
  
  void postLastResult {
    if (resultsChannelID != 0 && latestResult() != null)
      postInChannel(resultsChannelID, "Here are the latest lottery results: " + renderResult(latestResult()));
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1025913
Snippet name: Lottery Bot
Eternal ID of this version: #1025913/36
Text MD5: 926b9213ec9e1e81012a5562eb77abbd
Transpilation MD5: 629b8325ec45efda831fa109e3b70ec6
Author: stefan
Category: javax / discord bots
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-16 16:46:50
Source code size: 4263 bytes / 130 lines
Pitched / IR pitched: No / No
Views / Downloads: 240 / 67160
Version history: 35 change(s)
Referenced in: [show references]