Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple dumb raffle #9

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion organizers_bot/bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from . import config
from . import transcript
from . import ctfnote
from .raffle import Raffle

import asyncio
import functools
Expand Down Expand Up @@ -43,6 +44,7 @@ def setup():
slash = discord_slash.SlashCommand(bot, sync_commands=True)
log = logging.getLogger("bot")
trans_mgr = transcript.TranscriptManager(bot)
current_raffle: typing.Optional[Raffle] = None

@bot.event
async def on_ready():
Expand Down Expand Up @@ -80,7 +82,7 @@ async def ping(ctx: discord_slash.SlashContext):
required=False)
])
@require_role(config.mgmt.player_role)
async def create_challenge_channel(ctx: discord_slash.SlashContext,
async def create_challenge_channel(ctx: discord_slash.SlashContext,
category: str, challenge: str, ctfid = None):
cat = discord.utils.find(lambda c: c.name == category, ctx.guild.categories)
created = await ctx.guild.create_text_channel(challenge, position=0, category=cat)
Expand Down Expand Up @@ -285,6 +287,49 @@ async def ctfnote_leader(ctx: discord_slash.SlashContext):
async def ctfnote_import_from_ctftime(ctx: discord_slash.SlashContext, link: str):
await ctfnote.import_ctf_from_ctftime(ctx, link)

@slash.slash(name="raffle",
description="Simple dumb raffle a prize",
guild_ids=[config.bot.guild],
options=[
create_option(name="prize",
description="What prize to raffle",
option_type=SlashCommandOptionType.STRING,
required=True),
])
@require_role(config.mgmt.admin_role)
async def raffle(ctx: discord_slash.SlashContext, prize: str):
nonlocal current_raffle
current_raffle = raffle.Raffle(prize, set())
await ctx.send(f"Raffling {prize}! use /enter_raffle to enter.")

@slash.slash(name="enter_raffle",
description="Enter a simple dumb raffle",
guild_ids=[config.bot.guild],
options=[
])
@require_role(config.mgmt.player_role)
async def enter_raffle(ctx: discord_slash.SlashContext):
nonlocal current_raffle
if current_raffle is None:
await ctx.send("no such raffle")
return
current_raffle.add_participant(ctx.author_id)

@slash.slash(name="end_raffle",
description="Simple dumb end the raffle and draw a winner",
guild_ids=[config.bot.guild],
options=[
])
@require_role(config.mgmt.admin_role)
async def raffle(ctx: discord_slash.SlashContext):
nonlocal current_raffle
if current_raffle is None:
await ctx.send("no such raffle")
return
winner = current_raffle.draw()
await ctx.send(f"<@{winner}> gets {current_raffle.prize}!")
current_raffle = None

## Keep this last :)
return bot

Expand Down
38 changes: 38 additions & 0 deletions organizers_bot/raffle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import gql
from gql import Client
from gql.transport.exceptions import TransportQueryError
from gql.transport.aiohttp import AIOHTTPTransport
from gql.transport.websockets import WebsocketsTransport
import logging
from gql.transport.aiohttp import log as aio_logger
aio_logger.setLevel(logging.WARNING)
from gql.transport.websockets import log as websockets_logger
websockets_logger.setLevel(logging.WARNING)

from string import ascii_letters, digits
from random import choice, randrange
from datetime import datetime
from dataclasses import dataclass
import dateutil # parser, tz
import logging
import asyncio
from . import queries
import discord
import discord_slash # type: ignore
import json

log = logging.getLogger("Raffle")

@dataclass
class Raffle:
prize: str
participants: dict[int, float]

def add_participant(self, participant_id):
self.participants[participant_id] = 1.0

def draw(self):
return random.choices(self.participants.keys(), self.participants.values())