Skip to content

Commit

Permalink
fix: syntax error
Browse files Browse the repository at this point in the history
  • Loading branch information
apogiatzis committed Nov 22, 2024
1 parent 3ab0bcd commit 08c5bff
Showing 1 changed file with 42 additions and 46 deletions.
88 changes: 42 additions & 46 deletions ovisbot/extensions/ctf/ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,16 @@
"htb",
]

CHALLENGE_DIFFICULTIES = [
"none",
"easy",
"medium",
"hard"
]
CHALLENGE_DIFFICULTIES = ["none", "easy", "medium", "hard"]

EMOJI = {
"solved": "✅",
"unsolved": "⌛"
}
EMOJI = {"solved": "✅", "unsolved": "⌛"}

CHALLENGE_TAGS = ({
'solved': discord.ForumTag(name="solved", emoji=EMOJI["solved"]),
'unsolved': discord.ForumTag(name="unsolved", emoji=EMOJI["unsolved"])
} | {x: discord.ForumTag(name=x) for x in CHALLENGE_CATEGORIES}
CHALLENGE_TAGS = (
{
"solved": discord.ForumTag(name="solved", emoji=EMOJI["solved"]),
"unsolved": discord.ForumTag(name="unsolved", emoji=EMOJI["unsolved"]),
}
| {x: discord.ForumTag(name=x) for x in CHALLENGE_CATEGORIES}
| {x: discord.ForumTag(name=x) for x in CHALLENGE_DIFFICULTIES}
)

Expand Down Expand Up @@ -191,7 +185,10 @@ async def create(self, ctx, ctf_name):
chall_overwrites = overwrites
chall_overwrites[ctfrole].send_messages = False
chall_channel = await self.guild.create_forum(
name="challs", category=category, available_tags=CHALLENGE_TAGS.values(), overwrites=chall_overwrites
name="challs",
category=category,
available_tags=CHALLENGE_TAGS.values(),
overwrites=chall_overwrites,
)
CTF(name=category, created_at=datetime.datetime.now(), challenges=[]).save()
await success(ctx.message)
Expand Down Expand Up @@ -247,20 +244,26 @@ async def addchallenge(self, ctx, challname, category, difficulty="none"):
notebook_url = create_corimd_notebook()

# Find challenges forum channel
category_channels = [x for x in ctx.channel.category.channels if x.name ==
"challs" and isinstance(x, discord.ForumChannel)]
category_channels = [
x
for x in ctx.channel.category.channels
if x.name == "challs" and isinstance(x, discord.ForumChannel)
]
if len(category_channels) != 1:
raise CTF.DoesNotExist

challenges_forum = category_channels[0]
chosen_tags = ['unsolved', category]
if difficulty != 'none':
chosen_tags = ["unsolved", category]
if difficulty != "none":
chosen_tags.append(difficulty)
tags = [
tag for tag in challenges_forum.available_tags if tag.name in chosen_tags]
tag for tag in challenges_forum.available_tags if tag.name in chosen_tags
]
challenge_channel, _ = await challenges_forum.create_thread(
name=f"{EMOJI['unsolved']} - {challenge_name}", content="@here Ατε να δούμε δώστου πίεση!",
applied_tags=tags)
name=f"{EMOJI['unsolved']} - {challenge_name}",
content="@here Ατε να δούμε δώστου πίεση!",
applied_tags=tags,
)

new_challenge = Challenge(
name=challenge_name,
Expand Down Expand Up @@ -349,8 +352,7 @@ async def notes(self, ctx):
ctf = CTF.objects.get({"name": ctx.channel.category.name})

# Find challenge in CTF by name
challenge = next(
(c for c in ctf.challenges if c.name == chall_name), None)
challenge = next((c for c in ctf.challenges if c.name == chall_name), None)

if not challenge:
raise NotInChallengeChannelException
Expand Down Expand Up @@ -407,8 +409,7 @@ async def solve(self, ctx):
ctf = CTF.objects.get({"name": ctx.channel.category.name})

# Find challenge in CTF by name
challenge = next(
(c for c in ctf.challenges if c.name == chall_name), None)
challenge = next((c for c in ctf.challenges if c.name == chall_name), None)

if not challenge:
raise NotInChallengeChannelException
Expand All @@ -423,8 +424,7 @@ async def solve(self, ctx):

solvers_str = escape_md(
", ".join(
[ctx.message.author.name] +
[m.name for m in ctx.message.mentions]
[ctx.message.author.name] + [m.name for m in ctx.message.mentions]
)
)

Expand All @@ -441,9 +441,10 @@ async def solve(self, ctx):
if isinstance(reward_text, list):
reward_text = random.choice(reward_text)

new_tags = change_tags(
ctx.channel, add=['solved'], remove=['unsolved'])
await ctx.channel.edit(name=f"{EMOJI['solved']} - {chall_name}", applied_tags=new_tags)
new_tags = change_tags(ctx.channel, add=["solved"], remove=["unsolved"])
await ctx.channel.edit(
name=f"{EMOJI['solved']} - {chall_name}", applied_tags=new_tags
)

await ctx.channel.send(
"Πελλαμός! {0}! Congrats for solving {1}. Έλα {2} {3}".format(
Expand All @@ -467,8 +468,7 @@ async def solve_error(self, ctx, error):
)
elif isinstance(error.original, ChallengeAlreadySolvedException):
await ctx.channel.send(
f'Άρκησες! This challenge has already been solved by {
", ".join(error.original.solved_by)}!'
f'Άρκησες! This challenge has already been solved by {", ".join(error.original.solved_by)}!'
)

@ctf.command()
Expand All @@ -479,12 +479,12 @@ async def unsolve(self, ctx):
chall_name = get_chall_name(ctx)
ctf = CTF.objects.get({"name": ctx.channel.category.name})

new_tags = change_tags(
ctx.channel, add=['unsolved'], remove=['solved'])
await ctx.channel.edit(name=f"{EMOJI['unsolved']} - {chall_name}", applied_tags=new_tags)
new_tags = change_tags(ctx.channel, add=["unsolved"], remove=["solved"])
await ctx.channel.edit(
name=f"{EMOJI['unsolved']} - {chall_name}", applied_tags=new_tags
)

challenge = next(
(c for c in ctf.challenges if c.name == chall_name), None)
challenge = next((c for c in ctf.challenges if c.name == chall_name), None)

if not challenge:
raise NotInChallengeChannelException
Expand Down Expand Up @@ -887,17 +887,15 @@ async def check_reminders(self):
for reminder in copy.deepcopy(ctf_doc.pending_reminders):
if now >= reminder:
logger.info(
"Sending reminder to {0}".format(
reminders_channel)
"Sending reminder to {0}".format(reminders_channel)
)
delta = abs(ctf_doc.start_date - now)
if now > ctf_doc.start_date:
reminder_text = "⏰ Ατέ ρε αχαμάκκιες... Εν να μπείτε? Το **{0}** άρκεψε εσσιει τζαί **{1}** λεπτά! ⏰"
else:
reminder_text = "⏰ Ντριιιινγκ... Ντριιινγκ!! Ατέ μανα μου, ξυπνάτε! Το **{0}** ξεκινά σε **{1}** λεπτά! ⏰"
await reminders_channel.send(
reminder_text.format(
ctf.name, int(delta.seconds / 60))
reminder_text.format(ctf.name, int(delta.seconds / 60))
)
ctf_doc.pending_reminders.remove(reminder)
ctf_doc.save()
Expand Down Expand Up @@ -936,19 +934,17 @@ def change_tags(thread: discord.Thread, add: list[str] = [], remove: list[str] =
for t in new_tags:
if t.name in remove:
new_tags.remove(t)
new_tags += [x for x in thread.parent.available_tags
if x.name in add]
new_tags += [x for x in thread.parent.available_tags if x.name in add]
return new_tags


def get_chall_name(ctx):
name = ctx.channel.name
for emoji in EMOJI.values():
if name.startswith(emoji + " - "):
return name[len(emoji + " - "):]
return name[len(emoji + " - ") :]
return name


async def setup(bot):
await bot.add_cog(Ctf(bot))

0 comments on commit 08c5bff

Please sign in to comment.