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).

1  
!7
2  
3  
concept CLotteryResult {
4  
  S date;
5  
  L<Int> numbers;
6  
  S jackpot;
7  
  
8  
  [stdToString]
9  
}
10  
11  
standardBot1 LotteryBot {
12  
  long resultsChannelID;
13  
  
14  
  init {
15  
    dbIndexing(CLotteryResult, 'date);
16  
    onDiscordStarted(r { doEveryHourAndNow(r grabResults) });
17  
  }
18  
  
19  
  allServers {
20  
    S lotteryURL = "https://www.lotteryusa.com/missouri/show-me-cash/";
21  
22  
    void grabResults enter {
23  
      LotteryResult r = scrapeLotteryResults(loadPage(lotteryURL));
24  
      if (r == null) ret with print("No lottery results!?");
25  
      CLotteryResult lr, bool isNew = unpair uniq2(CLotteryResult, date := r.date);
26  
      copyFields(r, lr);
27  
      change();
28  
      print("Grabbed: " + lr + ", new: " + isNew);
29  
      
30  
      if (isNew) postLastResult_allServers();
31  
    }
32  
    
33  
    void postLastResult_allServers {
34  
      L<Guild> guilds = discord.getGuilds();
35  
      for (Guild guild : guilds) pcall {
36  
        ByServer bs = getByServer(guild);
37  
        if (bs != null)
38  
          bs.postLastResult();
39  
      }
40  
    }
41  
    
42  
    S renderResult(CLotteryResult r) {
43  
      ret formatCSVLine(flattenList2(r.date, r.numbers, r.jackpot));
44  
    }
45  
    
46  
    CLotteryResult latestResult() {
47  
      ret first(conceptsSortedByFieldDesc CLotteryResult('date));
48  
    }
49  
  }
50  
  
51  
  processSimplified {
52  
    optPar Message msg;
53  
    Message.Attachment attachment = msg == null ? null : first(msg.getAttachments());
54  
    if (attachment != null && authed(_)) {
55  
      File file = prepareCacheProgramFile(guildID + "/" + attachment.getFileName());
56  
      print("Downloading attachment: " + file);
57  
      deleteFile(file);
58  
      if (!attachment.download(file)) ret "Couldn't download attachment";
59  
      S contents = loadTextFile(file);
60  
      contents = replaceAll(contents, "^,+\r?\n", "");
61  
      if (!startsWith(contents, "\"") && !startsWithDigit(contents)) null; //ret "Attachment is not a CSV text file";
62  
      LLS l = parseCSV_unquote_noHeader(contents);
63  
      l = map(l, row -> l(row) != 7 ? row
64  
        : spliceList(row, 1, 6, ll(joinWithComma(subList(row, 1, 6)))));
65  
      Set<Int> lengths = mapToSet(lambda1 l, l);
66  
      if (neq(lengths, litset(3))) ret "CSV does not have 3 columns throughout";
67  
      for (LS line : l) {
68  
        S date = first(line);
69  
        if (!regexpMatches(regexpYMDminus(), date)) ret "Bad date: " + backtickQuote(date);
70  
        CLotteryResult lr = uniq(CLotteryResult, +date);
71  
        cset(lr, numbers := map parseInt(tok_splitAtComma(second(line))), jackpot := third(line));
72  
      }
73  
      
74  
      ret "OK, " + nResults(l) + " imported";
75  
    }
76  
    
77  
    S s0 = s;
78  
    s = dropMyPrefixOpt(s);
79  
    
80  
    if "clear lottery results" {
81  
      try answer checkAuth(_);
82  
      ret "OK, " + nResults(deleteConcepts(CLotteryResult)) + " deleted";
83  
    }
84  
85  
    if "lottery|lottery results|show me cash" {
86  
      optPar long channelID;
87  
      L<CLotteryResult> l = sortedByFieldDesc date(list(CLotteryResult));
88  
      if (empty(l)) ret "No lottery results gathered yet";
89  
      S date1 = first(l).date;
90  
      S text = windowsLineBreaks(mapToLines_rtrim(l, lambda1 renderResult));
91  
      uploadFileInChannel(channelID,
92  
        toUtf8(text),
93  
        "lottery-" + date1 + ".csv", null, null);
94  
      null;
95  
    }
96  
    
97  
    if "Post lottery here" {
98  
      long channelID = longPar channelID(_);
99  
      if (channelID == 0) ret "Please do this in a channel";
100  
      if (resultsChannelID != channelID) {
101  
        resultsChannelID = channelID;
102  
        change();
103  
      }
104  
      doAfter(2.0, r postLastResult);
105  
      ret "OK, I will post new lottery results in this channel. Type `stop posting lottery` to stop.";
106  
    }
107  
      
108  
    if "stop posting lottery" {
109  
      resultsChannelID = 0;
110  
      change();
111  
      ret "OK";
112  
    }
113  
    
114  
    if null (s = dropMyPrefixOrNull(s0)) null;
115  
    
116  
    if "help" {
117  
      ret [[
118  
@me `lottery` - download latest lottery results as CSV
119  
@me `clear lottery results` - delete all lottery results
120  
121  
To add lottery results, just upload a CSV file.
122  
]].replace("@me", atSelf());
123  
    }
124  
  }
125  
  
126  
  void postLastResult {
127  
    if (resultsChannelID != 0 && latestResult() != null)
128  
      postInChannel(resultsChannelID, "Here are the latest lottery results: " + renderResult(latestResult()));
129  
  }
130  
}

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: 302 / 68132
Version history: 35 change(s)
Referenced in: [show references]