Skip to content

Commit

Permalink
Commit half-baked spaghetti mess from late september/early november
Browse files Browse the repository at this point in the history
I barely remember what I was trying to do here, let alone how far I got
or if it works. If you (future perry or some other person) want to pick
it back up, good luck.
  • Loading branch information
perrycate committed Dec 3, 2020
1 parent 3642cd8 commit 8eeae09
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
10 changes: 5 additions & 5 deletions bracket.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class Bracket:
def __init__(self, token, tourney_id):
self._challonge_token = token
self._tourney_id = tourney_id
self._players = self._fetch_players_by_id()
self._players = self._fetch_players_by_name()

def _fetch_players_by_id(self):
def _fetch_players_by_name(self):
raw = util.make_request(
CHALLONGE_API, f'/tournaments/{self._tourney_id}/participants.json', {'api_key': self._challonge_token})

# Raw format is a list of dicts, all with one property "participant".
# Convert into dict of players by ID.
return {p["participant"]["id"]: p["participant"]["name"] for p in raw}
# Convert into dict of players by name.
return {p["participant"]["name"]: p["participant"]["id"] for p in raw}

@property
def players(self):
Expand All @@ -65,7 +65,7 @@ def add_players(self, names):
data=payload,
raise_exception_on_http_error=True)

self._players = self._fetch_players_by_id()
self._players = self._fetch_players_by_name()


def _sanity_check():
Expand Down
63 changes: 47 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@
import os
import sys

from discord.ext import commands

import bracket as challonge_bracket

from tournament import Tournament

from discord.ext import commands

DISCORD_TOKEN_VAR = 'DISCORD_BOT_TOKEN'
CHALLONGE_TOKEN_VAR = 'CHALLONGE_TOKEN'
PREFIX = '!'

TOKEN_ENV_VAR = 'DISCORD_BOT_TOKEN'
# Check for auth token.
if DISCORD_TOKEN_VAR not in os.environ:
sys.exit("{0} not found in system environment. Try running again with the prefix '{0}=<insert discord bot token here>'".format(
DISCORD_TOKEN_VAR))
discord_auth = os.environ[DISCORD_TOKEN_VAR]
if CHALLONGE_TOKEN_VAR not in os.environ:
sys.exit("{0} not found in system environment. Try running again with the prefix '{0}=<insert discord bot token here>'".format(
CHALLONGE_TOKEN_VAR))
challonge_auth = os.environ[CHALLONGE_TOKEN_VAR]


# Create bot instance.
bot = commands.Bot(command_prefix='!')
bot = commands.Bot(command_prefix=PREFIX)
tournaments_by_guild_id = {}


Expand Down Expand Up @@ -44,19 +58,36 @@ async def open(ctx, dest_channel, message="Registration is open! React to this m
await ctx.send("Registration message posted!")


@bot.event
async def on_raw_reaction_add(payload):
user = payload.member
reaction = payload.emoji
channel = bot.get_channel(payload.channel_id)
await channel.send(f"{user} reacted with {reaction}")

# Check for auth token.
if TOKEN_ENV_VAR not in os.environ:
sys.exit("{0} not found in system environment. Try running again with the prefix '{0}=<insert discord bot token here>'".format(
TOKEN_ENV_VAR))
auth = os.environ[TOKEN_ENV_VAR]
@bot.command()
async def begin(ctx, name="Tournament"):
guild = ctx.message.channel.guild
tournament = tournaments_by_guild_id.get(guild.id)
if tournament is None:
await ctx.send(f"No tournament to begin! Start a tournament with {PREFIX}open <channel> <message>")

# Collect all the users who reacted to the registration message.
reg_message = await guild.get_channel(tournament.channel_id).fetch_message(tournament.registration_message_id)
dids = {}
for r in reg_message.reactions:
async for u in r.users():
dids[u.id] = u.name

# Create a challonge bracket, and match challonge IDs to discord IDs.
bracket = challonge_bracket.create(challonge_auth, name)
dids_to_cids = {}
for did, name in dids.items():
cid = bracket.players[name]
dids_to_cids[did] = cid
print(dids_to_cids)

# TODO do something with the data structure I just made.
# TODO see if this shit works.

# Log in and begin reading and responding to messages.
# Nothing else will run below this line.
bot.run(auth)
bot.run(discord_auth)


def _sanity_check():
# discord_auth, challonge_auth
pass

0 comments on commit 8eeae09

Please sign in to comment.