Przeglądaj źródła

Fix code around

ghorsington 5 lat temu
rodzic
commit
ac03ba1a7b
1 zmienionych plików z 51 dodań i 13 usunięć
  1. 51 13
      bot/src/commands/contest.ts

+ 51 - 13
bot/src/commands/contest.ts

@@ -1,5 +1,5 @@
 import { ICommand } from "./command";
-import { Dict, compareNumbers } from "../util";
+import { Dict, compareNumbers, isAuthorisedAsync } from "../util";
 import { ReactionCollector, Message, TextChannel, RichEmbed, MessageReaction, User, Collector, Collection, SnowflakeUtil } from "discord.js";
 import yaml from "yaml";
 import { getRepository, getManager } from "typeorm";
@@ -52,6 +52,7 @@ type ContestEntryMessage = {
 
 //TODO: Convert into a transaction
 async function updateContestStatus(contest: Contest) {
+    let contestRepo = getRepository(Contest);
     let voteRepo = getRepository(ContestVote);
     let entryRepo = getRepository(ContestEntry);
 
@@ -102,20 +103,25 @@ async function updateContestStatus(contest: Contest) {
             console.log(`Failed to update entry ${entry.msgId} for contest ${contest.id} because ${err}!`);
             delete entries[entry.msgId];
         }
-
-        let newEntries = (await channel.fetchMessages({
-            after: SnowflakeUtil.generate(newestEntry || contest.startDate)
-        })).filter(m => m.attachments.size != 0);
-
-        for(let [_, msg] of newEntries)
-            await registerEntry(msg, contest);
     }
 
-    if(contest.endDate < new Date()){
-        contest.active = false;
+    let newEntries = (await channel.fetchMessages({
+        after: SnowflakeUtil.generate(newestEntry || contest.startDate)
+    })).filter(m => m.attachments.size != 0);
 
-        if(contest.announceWinners)
+    for (let [_, msg] of newEntries)
+        await registerEntry(msg, contest);
+
+    if (contest.endDate < new Date()) {
+        if (contest.announceWinners)
             await printResults(contest, channel);
+        await contestRepo.update(contest.id, { active: false });
+    } else {
+        scheduleJob(contest.endDate, stopContest.bind(null, contest.id));
+        activeContests[contest.id] = {
+            id: contest.id,
+            voteReaction: contest.voteReaction
+        };
     }
 }
 
@@ -328,7 +334,7 @@ async function createContest(msg: Message, info: ContestCreationOptions) {
     let repo = getRepository(Contest);
 
     let contest = await repo.findOne({
-        where: { channel: info.in }
+        where: { channel: info.in, active: true }
     });
 
     if (contest) {
@@ -351,6 +357,10 @@ async function createContest(msg: Message, info: ContestCreationOptions) {
     await msg.channel.send(`${msg.author.toString()} Started contest (ID: ${contest.id})`);
 
     scheduleJob(contest.endDate, stopContest.bind(null, contest.id));
+    activeContests[contest.id] = {
+        id: contest.id,
+        voteReaction: contest.voteReaction
+    };
 }
 
 async function onMessage(actionsDone: boolean, m: Message, content: string) {
@@ -424,6 +434,11 @@ export default <ICommand>{
         {
             pattern: "create contest",
             action: async (m, contents) => {
+                if (!await isAuthorisedAsync(m.member)) {
+                    m.channel.send(`${m.author.toString()} You're not authorised to create contests!`);
+                    return;
+                }
+
                 let message = m.content.trim().substr(client.user.toString().length).trim().substr("create contest".length).trim();
                 let contestData: ContestCreationOptions = { ...CONTEST_DEFAULTS, ...yaml.parse(message) };
                 await createContest(m, contestData);
@@ -438,7 +453,30 @@ export default <ICommand>{
         {
             pattern: /end contest( (\d*))?/,
             action: async (m, contents, matches) => {
-                await m.channel.send("Heck");
+                if (!await isAuthorisedAsync(m.member)) {
+                    m.channel.send(`${m.author.toString()} You're not authorised to create contests!`);
+                    return;
+                }
+
+                let repo = getRepository(Contest);
+                let contestId = +matches[1];
+
+                let contest = await repo.findOne({
+                    where: { id: contestId },
+                    select: ["id", "active"]
+                });
+
+                if (!contest) {
+                    await m.channel.send(`${m.author.toString()} Can't find contest with ID ${contestId}`);
+                    return;
+                }
+
+                if (!contest.active) {
+                    await m.channel.send(`${m.author.toString()} The contest with ID ${contestId} is already inactive!`);
+                    return;
+                }
+
+                await stopContest(contest.id);
             }
         },
         {