Browse Source

Add beta support for codesignal

master
Jaeha Choi 10 months ago
parent
commit
8217748d10
Signed by: Jaeha.Choi GPG Key ID: 7D8ACCBCE8DC6282
  1. 118
      main.py
  2. 42
      test.py

118
main.py

@ -196,7 +196,7 @@ class Bot:
else:
return "❌ Error: slug not found."
def mark_as_solved(self, author_id: str, msg: str) -> int:
def mark_as_solved(self, author_id: str, msg: str, platform: int) -> int:
msg = msg.split(" ", 2)
if len(msg) == 2 and msg[1].find("```") != -1:
@ -218,34 +218,49 @@ class Bot:
code = code.strip()
if slug in self.slug_solved:
category = self.category[slug]
if author_id not in self.players:
return 4
if platform == 0:
if slug in self.slug_solved:
category = self.category[slug]
if author_id not in self.players:
return 4
# Updating
if author_id in self.slug_solved[slug]:
# Updating
if author_id in self.slug_solved[slug]:
return_code = 1
# Adding
else:
self.slug_solved[slug].append(author_id)
self.players[author_id]["solved"] += 1
self.players[author_id]["not_solved"] -= 1
return_code = 0
Path(CODE_OUTPUT_DIR + author_id + "/" + category + "/").mkdir(parents=True, exist_ok=True)
with open(CODE_OUTPUT_DIR + author_id + "/" + category + "/" + slug + ".py", "w") as file:
file.write(self._retrieve_date(slug) + code + "\n")
with open(DATA_DIR + "players.json", "w") as file:
json.dump(self.players, file)
with open(DATA_DIR + "solved.json", "w") as file:
json.dump(self.slug_solved, file)
return return_code
else:
# Slug not found
return 2
elif platform == 1:
code_dir = CODE_OUTPUT_DIR + author_id + "/codesignal/"
code_path = code_dir + slug + ".py"
Path(code_dir).mkdir(parents=True, exist_ok=True)
if os.path.exists(code_path):
return_code = 1
# Adding
else:
self.slug_solved[slug].append(author_id)
self.players[author_id]["solved"] += 1
self.players[author_id]["not_solved"] -= 1
return_code = 0
Path(CODE_OUTPUT_DIR + author_id + "/" + category + "/").mkdir(parents=True, exist_ok=True)
with open(CODE_OUTPUT_DIR + author_id + "/" + category + "/" + slug + ".py", "w") as file:
file.write(self._retrieve_date(slug) + code + "\n")
with open(DATA_DIR + "players.json", "w") as file:
json.dump(self.players, file)
with open(DATA_DIR + "solved.json", "w") as file:
json.dump(self.slug_solved, file)
with open(CODE_OUTPUT_DIR + author_id + "/codesignal/" + slug + ".py", "w") as file:
file.write(code + "\n")
return return_code
else:
# Slug not found
return 2
# internal error
return -1
def show_ans_by(self, msg: str):
def show_ans_by(self, msg: str, platform: int):
msg = msg.split()
ans_author = msg[1].strip()
slug = msg[2].strip()
@ -258,17 +273,33 @@ class Bot:
else:
ans_author_id = ans_author[2:-1]
if slug in self.slug_solved:
if ans_author_id in self.slug_solved[slug]:
with open(CODE_OUTPUT_DIR + ans_author_id + "/" + self.category[slug] + "/" + slug + ".py",
"r") as file:
s = "✔️ Answer By: " + self.players[ans_author_id]["name"] + "\n```"
if platform == 0:
# Hackerrank
directory = CODE_OUTPUT_DIR + ans_author_id + "/" + self.category[slug] + "/"
if slug in self.slug_solved:
if ans_author_id in self.slug_solved[slug]:
with open(directory + slug + ".py", "r") as file:
s = "✅ Answer By: " + self.players[ans_author_id]["name"] + "\n```"
s += file.read()
return s + "```"
else:
return "❌ Error: user %s did not solve this question." % ans_author
else:
return "❌ Error: slug/question not found."
elif platform == 1:
# Code signal
directory = CODE_OUTPUT_DIR + ans_author_id + "/codesignal/"
code_path = directory + slug + ".py"
if os.path.exists(code_path):
with open(directory + slug + ".py", "r") as file:
s = "✅ Answer By: " + self.players[ans_author_id]["name"] + "\n```"
s += file.read()
return s + "```"
else:
return "❌ Error: user %s did not solve this question." % ans_author
else:
return "❌ Error: slug not found."
return "❌ Error: internal bug: incorrect platform."
# def show_leaderboard(self) -> str:
# s = "✨ Leaderboard ✨\n```"
@ -354,14 +385,14 @@ def main(admin_channel):
await channel.send("Questions updating... Takes about ~2 min.")
async with channel.typing():
hackerrank_bot.update_files()
await channel.send("✔️ Questions successfully updated. " + random.choice(SUCCESS_EMOJI))
await channel.send(" Questions successfully updated. " + random.choice(SUCCESS_EMOJI))
else:
await channel.send("❌ Questions update failed. Incorrect channel." + random.choice(ERROR_EMOJI))
elif msg == "register":
res = hackerrank_bot.register(author_id, author_name)
if res == 0:
await channel.send("✔️ %s is registered! %s" % (author_name, random.choice(SUCCESS_EMOJI)))
await channel.send(" %s is registered! %s" % (author_name, random.choice(SUCCESS_EMOJI)))
else:
await channel.send("%s was already registered. %s" % (author_name, ERROR_EMOJI))
@ -370,11 +401,11 @@ def main(admin_channel):
await channel.send(hackerrank_bot.solved_by(msg))
elif msg.startswith("solve"):
res = hackerrank_bot.mark_as_solved(author_id, msg)
res = hackerrank_bot.mark_as_solved(author_id, msg, 0)
if res == 0:
await channel.send("✔️ Marked as solved! " + random.choice(SUCCESS_EMOJI))
await channel.send(" Marked as solved! " + random.choice(SUCCESS_EMOJI))
elif res == 1:
await channel.send("✔️ Answer updated! " + random.choice(SUCCESS_EMOJI))
await channel.send(" Answer updated! " + random.choice(SUCCESS_EMOJI))
elif res == 2:
await channel.send("❌ Error: invalid slug. " + random.choice(ERROR_EMOJI))
elif res == 3:
@ -387,7 +418,7 @@ def main(admin_channel):
await channel.send(hackerrank_bot.show_players())
elif msg.startswith("answerBy") or msg.startswith("ansBy"):
await channel.send(hackerrank_bot.show_ans_by(msg))
await channel.send(hackerrank_bot.show_ans_by(msg, 0))
# elif msg == "ranking":
# await message.channel.send(hackerrank_bot.show_leaderboard())
@ -395,15 +426,30 @@ def main(admin_channel):
elif msg == "help":
s = "`\\updateQuestions`:\n\t\tUpdate questions. Can be used only in a pre-defined channel.\n"
s += "`\\register`:\n\t\tRegister as a player.\n"
s += "`\\player`:\n\t\tList all players and update their name if changed.\n"
s += "`\\whoSolved *question_slug*`:\n\t\tList players who solved the question.\n"
s += "`\\answerBy *@player* *question_slug*`: \n\t\tShow specified player's answer.\n"
s += "`\\solve *question_slug* *code*`: \n\t\tMark the question as solved, and upload a code.\n"
s += "\t\t- If the question was already marked as solved, the answer gets updated.\n"
s += "\t\t- *Code* can be passed in as a code block (e.g. `code`).\n"
s += "`\\help`: \n\t\tView this message."
s += "`\\help`: \n\t\tView this message.\n"
s += "**Beta Features**\n"
s += "`\\player`:\n\t\tList all players and update their name if changed.\n"
s += "`\\csAnswerBy *@player* *question_number*`: \n\t\tShow specified player's answer.\n"
s += "`\\csSolve *question_number* *code*`: \n\t\tMark the question as solved, and upload a code.\n"
await channel.send(s)
# Preliminary support for codesignal
elif msg.startswith("cs"):
msg = msg[2:]
if msg.startswith("Solve"):
res = hackerrank_bot.mark_as_solved(author_id, msg, 1)
if res == 0:
await channel.send("✅ Marked as solved! " + random.choice(SUCCESS_EMOJI))
elif res == 1:
await channel.send("✅ Answer updated! " + random.choice(SUCCESS_EMOJI))
elif msg.startswith("AnswerBy"):
await channel.send(hackerrank_bot.show_ans_by(msg, 1))
elif msg == "meme":
await channel.send(get_meme())
else:

42
test.py

@ -1,42 +0,0 @@
import sys
import discord
DEBUG = False
# https://discordapp.com/oauth2/authorize?client_id=714332479741231105&scope=bot
client = discord.Client()
@client.event
async def on_message(message):
if message.author.bot:
return None
author_id = str(message.author.id)
author_name = message.author.name
channel = message.channel
channel_id = message.channel.id
msg = message.content
print("Author ID:", author_id)
print("Author ID:", author_name)
print("channel:", channel)
print("channel.id:", channel_id)
print("msg:", msg)
print("\n")
if str(msg).startswith("-"):
await channel.send("<a:dogdance:725265335564894279>")
@client.event
async def on_ready():
print("=============")
print("Logged in as ")
print(client.user.name)
print(client.user.id)
print("=============")
client.run(sys.argv[1])
Loading…
Cancel
Save