Esempio n. 1
0
def write_score(user, sender, team_name, points, description):
    # file_exists = os.path.isfile(f'./storage/{channel}.csv')
    # if not file_exists:
    #     with open(f'./storage/{channel}.csv', mode='w') as morningreport_file:
    #         header = ["User", "Date-time", "Points", "Sender", "Conv_id"]
    #         score_writer = csv.writer(morningreport_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    #         if not file_exists:
    #             score_writer.writerow(header)
    #
    # with open(f'./storage/{channel}.csv', mode='a') as morningreport_file:
    #     score_writer = csv.writer(morningreport_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    #     score_writer.writerow([user, datetime.datetime.now(), points, sender, channel])
    team = s.query(Team).filter(Team.name.match(team_name)).first()
    giver = s.query(User).filter(User.username.match(sender)).first()
    receiver = s.query(User).filter(User.username.match(user)).first()

    points = Point(giver_id=giver.id,
                   receiver_id=receiver.id,
                   team_id=team.id,
                   points=points,
                   description=description)
    s.add(points)
    s.commit()
    s.close()

    return
Esempio n. 2
0
def make_bet(team_name, username, points, position, wager_id):
    if points < 1:
        return "You can't bet negative points."
    if points > 5000:
        return "5000 is the max bet."
    team, user = get_team_user(team_name, username)

    wager = s.query(Wager).get(wager_id)
    msg = ""
    if wager:
        if wager.team_id != team.id:
            return f'This team is not able to access wager `#{wager.id}`.'
        if wager.is_closed:
            return f'Wager: `#{wager.id}` "{wager.description}" is closed for betting.'

        if bet := wager.already_bet(user):
            print(bet)
            return f"Wager `#{wager.id}`: {wager.description}\n" \
                   f"{get_wager_bets(wager)}" \
                   # f"\n\n{user.username} already wagered `{bet.points}` `#{wager.id}` will be `{bet.position}`.\n"
        if points > wager.bets[0].points:
            return f"You can't bet more than {wager.bets[0].points} on wager `#{wager.id}`"
        else:
            bet = Bet(points=points, position=position)
            bet.wager = wager
            bet.user = user
            s.add(bet)
            s.commit()
            msg += f'Wager: `#{wager.id}` "{wager.description}"\n'
            msg += get_wager_bets(wager)
            # msg += f'\n@{user.username} your bet has been recorded.\n'
            s.close()
            return msg
Esempio n. 3
0
def sync_score(channel):
    with open(f'./storage/{channel.replace(",", "")}.csv',
              mode='r') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            team = s.query(Team).filter(Team.name.match(channel)).first()
            receiver = s.query(User).filter(User.username.match(
                row['User'])).first()
            giver = s.query(User).filter(User.username.match(
                row['Sender'])).first()

            points = Point(giver_id=giver.id,
                           receiver_id=receiver.id,
                           team_id=team.id,
                           points=int(row['Points']))
            s.add(points)
            s.commit()
            s.close()
            print(row)
Esempio n. 4
0
async def sync(event, bot):
    conv_id = event.msg.conv_id
    channel = event.msg.channel.name
    members = await get_channel_members(conv_id, bot)

    team = s.query(Team).filter(Team.name.match(channel)).first()
    if not team:
        new_team = Team(name=channel)
        s.add(new_team)

    for member in members:
        user = s.query(User).filter(User.username.match(member)).first()
        if not user:
            new_user = User(username=member)
            team = s.query(Team).filter(Team.name.match(channel)).first()
            new_user.teams = [team]
            s.add(new_user)
    await bot.chat.react(conv_id, event.msg.id, ":arrows_clockwise:")
    s.commit()
    s.close()
Esempio n. 5
0
async def payout_wager(username, team_name, wager_id, result, bot):
    team, user = get_team_user(team_name, username)
    marvn = s.query(User).filter_by(username='******').first()
    if result.lower() == 'true':
        result = True
    else:
        result = False
    msg = ""
    for wager in team.wagers:
        if wager.id == wager_id:

            msg = f"Wager: `#{wager.id}` - `CLOSED`\n" \
                  f'"{wager.description}"\n' \
                  f"{get_wager_bets(wager)}" \
                  f"Result: `{result}`"
            wager.result = result
            payout = False
            for bet in wager.bets[1:]:
                if bet.position != wager.bets[0].position:
                    payout = True
            if payout:
                for bet in wager.bets:
                    bet.result = result
                    if bet.position is not result:
                        points = bet.points * -1
                        msg += f"\nDeducting {bet.points} points from {bet.user}"
                        p = Point(giver_id=marvn.id, receiver_id=bet.user.id, team_id=team.id, points=points, description=f"Wager: #{wager.id}")

                        s.add(p)
                    else:
                        msg += f"\nPaying {bet.points} points to {bet.user}"
                        p = Point(giver_id=marvn.id, receiver_id=bet.user.id, team_id=team.id, points=bet.points, description=f"Wager: #{wager.id}")
                        s.add(p)
                    wager.is_closed = True
                    s.commit()
                await update_wager_msg(bot, wager, msg)
                s.close()
                return msg
            else:
                msg = f"Nobody took the Wager `#{wager_id}` so this didn't payout.\n" \
                       f"`{wager.description}`"
                wager.is_closed = True

                s.commit()
                await update_wager_msg(bot, wager, msg)
                s.close()
                return msg

    return f"Wager `#{wager_id}` is either already closed or non-existant."
Esempio n. 6
0
def get_wagers(team_name):
    team = s.query(Team).filter_by(name=team_name).first()
    wager_msgs = {}
    try:
        wagers = team.wagers
        print(wagers)
        # msg = f"Here's all the current wagers for `{team_name}`\n\n"
        for wager in wagers:
            msg = ""
            if not wager.is_closed:
                msg += f'Wager: `#{wager.id}`\n' \
                       f'"{wager.description}"\n'
                msg += get_wager_bets(wager)
                wager_msgs[f'{wager.id}'] = msg
        s.close()
        return wager_msgs
    except AttributeError:
        return "Something went wrong. Clearly your fault."
Esempio n. 7
0
def get_bets(username):
    user = s.query(User).filter_by(username=username).first()
    bets = user.bets
    msg = f"Here's all the current wagers for @{username}\n"
    bet_table = PrettyTable()
    bet_table.border = False
    bet_table.field_names = ["#", "Pts", "Pos", "Win"]

    for bet in bets:
        bet_table.add_row([bet.wager.id, bet.points, bet.position, bet.result])
    bet_table.align = "r"
    bet_table.sortby = '#'
    # bet_table.reversesort = True
    msg = f"```" \
          f"{bet_table}" \
          f"```"
    s.close()
    return msg
Esempio n. 8
0
def get_score(channel_name):
    x = PrettyTable()
    x.field_names = [
        "Achiever",
        "Score",
    ]
    team = s.query(Team).filter_by(name=channel_name).first()
    msg = f"```{team.name}\n-- Leaderboard --\n"
    for k, v in team.get_score().items():
        x.add_row([k.username, v])
    x.align = "r"
    x.padding_width = 2
    x.sortby = "Score"
    x.reversesort = True
    print(x)
    msg += f"{x}```"

    return msg
Esempio n. 9
0
 def get_wager(self, wager_id):
     return s.query(Wager).filter(
         Team.wagers.any(Wager.id == wager_id)).first()
Esempio n. 10
0
 def wager_exists(self, description):
     return s.query(Wager).filter(
         and_(Team.wagers.any(Wager.is_closed == 'false'),
              Team.wagers.any(Wager.description == description))).all()
Esempio n. 11
0
 def get_wagers(self):
     return s.query(Wager).filter(
         Team.wagers.any(Wager.is_closed == False)).all()
Esempio n. 12
0
 def get_bets(self):
     return s.query(Bet).filter(User.bets).all()
Esempio n. 13
0
def get_team_user(team_name, username):
    team = s.query(Team).filter_by(name=team_name).first()
    user = s.query(User).filter_by(username=username).first()

    return team, user
Esempio n. 14
0
def get_team(team_name):
    team = s.query(Team).filter_by(name=team_name).first()

    return team
async def handler(bot, event):
    command_list = [
        {
            "name": "award",
            "description":
            "Force me to give completely meaningless points to your comrades.",
            "usage": "<user> <points>"
        },
        {
            "name": "bible",
            "description": "Force me to lookup Bible txt.",
            "usage": "<reference> OR <search string>"
        },
        {
            "name": "canary",
            "description":
            "Force me to give Virus Total your nasty URL and return scan results.",
            "usage": "<url>"
        },
        {
            "name":
            "chuck",
            "description":
            "Forces me to tell a terribly jouvinile possibly NSFW joke randomly mentioning someone in this channel.",
            "usage":
            "Optional: <name> OR bomb if nothing is given a member of this channel will be selected at random."
        },
        # {"name": "covid",
        #  "description": "Force me to morbidly retrieve covid numbers for a State County or State.",
        #  "usage": "<State> <County> <- Optional Fields"},
        {
            "name": "cow",
            "description":
            f"Now I can't even explain this. You are a monster. Optional Characters: {get_characters()}",
            "usage": "<character> <msg>"
        },
        {
            "name": "drwho",
            "description": "Return Dr Who Episode.",
            "usage": "<ep_id> OR <Ep Title>"
        },
        {
            "name": "eyebleach",
            "description": "Returns images from r/eyebleach. Default = 3",
            "usage": "<bleach_level> 1-11"
        },
        {
            "name": "files",
            "description": "Returns list of files on marvin",
            "usage": ""
        },
        {
            "name": "grades",
            "description": "Retrieve current academic snapshot from pa-cyber.",
            "usage": "RESTRICTED"
        },
        {
            "name": "help",
            "description":
            "See a menu of options for ruining my life by making me do menial tasks.",
            "usage": ""
        },
        {
            "name": "joke",
            "description":
            "Forces me to tell a joke. For the love of God just don't.",
            "usage": ""
        },
        {
            "name": "meh",
            "description": "Get's today's meh.",
            "usage": ""
        },
        {
            "name": "meet",
            "description": "Get video conference link.",
            "usage": "<Conference Name>"
        },
        # {"name": "morbidity",
        #  "description": "Return data about current mass shootings in the US.",
        #  "usage": ""},
        {
            "name": "morningreport",
            "description": "Gets today's morning report.",
            "usage": ""
        },
        {
            "name": "payout",
            "description": "Pays out a wager.",
            "usage": "<#wager> <True/False>"
        },
        # {"name": "pollresult",
        #  "description": "RealClear Politics National and Pennsylvania Poll Results.",
        #  "usage": ""},
        {
            "name": "screenshot",
            "description": "Forces me go to a url and send a screenshot.",
            "usage": "<url>"
        },
        {
            "name": "set",
            "description": "Admin command for setting various things",
            "usage": ""
        },
        {
            "name": "speak",
            "description":
            "Forces me generate an mp3 speaking the text you send.",
            "usage": "<Text To Speak>"
        },
        {
            "name": "speed",
            "description": "Forces me check upload download speed.",
            "usage": ""
        },
        {
            "name": "score",
            "description": "Get the score for who abuses me the most.",
            "usage": ""
        },
        {
            "name": "stardate",
            "description":
            " Print's the current stardate if no stardate is given.",
            "usage": "<stardate> <- Optional"
        },
        {
            "name": "test",
            "description": "Just check to see if I'm regretfully still here.",
            "usage": ""
        },
        {
            "name":
            "till",
            "description":
            "Gives the days TILL events.",
            "usage":
            "<event_name> -t <event_datetime> adds event.\n<no_arguments> lists all tills."
        },
        {
            "name": "tldr",
            "description":
            "Forces me to read an entire article and then summarize it because you're lazy.",
            "usage": "<url>"
        },
        # {"name": "vac",
        #  "description": "Get Vaccine Distributation data from health.pa.gov",
        #  "usage": ""},
        {
            "name": "wager",
            "description":
            "Forces me to setup a silly bet with points that don't matter.",
            "usage": "<points wagered> <The Event or Thing your betting upon>"
        },
        # {"name": "yt",
        #  "description": "Forces me to go get meta data about a youtube video.",
        #  "usage": "<url>"},
        {
            "name": "ytm",
            "description":
            "Forces me to get metadata and download the stupid thing as an mp3.",
            "usage": "<url>"
        },
        {
            "name": "ytv",
            "description":
            "Forces me to get metadata and download the stupid thing.",
            "usage": "<url>"
        },
    ]

    if event.msg.content.type_name == 'reaction':
        if event.msg.content.reaction.body == ":white_check_mark:" or ':no_entry_sign:':
            if event.msg.sender.username != 'marvn' or event.msg.sender.username != 'morethanmarvin':

                try:
                    team_name = event.msg.channel.name
                    username = event.msg.sender.username
                    msg_id = event.msg.content.reaction.message_id
                    message = s.query(Message).filter_by(
                        msg_id=str(msg_id)).first()
                    points = message.wager.points
                    wager_id = message.wager.id
                    if event.msg.content.reaction.body == ":white_check_mark:":
                        position = True
                    elif event.msg.content.reaction.body == ":no_entry_sign:":
                        position = False
                    else:
                        raise ValueError
                    msg = make_bet(team_name, username, points, position,
                                   wager_id)
                    await bot.chat.edit(event.msg.conv_id, msg_id, msg)
                except ValueError as e:
                    print(e)
                    print("ValueError")
                except AttributeError:
                    pass

    if event.msg.content.type_name == 'reaction':
        if event.msg.content.reaction.body == ":tv:":
            if event.msg.sender.username != 'marvn' and event.msg.sender.username != 'morethanmarvin':
                conversation_id = event.msg.conv_id

                msg = await bot.chat.get(event.msg.conv_id,
                                         event.msg.content.reaction.message_id)
                try:
                    original_body = msg.message[0]['msg']['content']['text'][
                        'body']
                except KeyError:
                    original_body = msg.message[0]['msg']['content'][
                        'attachment']['object']['title']
                original_msg_id = msg.message[0]['msg']['id']
                reactions = msg.message[0]['msg']['reactions']
                reaction_list = []
                for key, value in reactions.items():
                    for k, v in value.items():
                        try:
                            if v['users']['marvn']:
                                reaction_list.append(k)
                        except KeyError:
                            pass
                if ':tv:' in reaction_list:
                    team_name = event.msg.channel.name
                    fail_msg = f"`-10pts` awarded to @{event.msg.sender.username} for spamming :tv:"
                    score = write_score(event.msg.sender.username,
                                        'marvn',
                                        team_name,
                                        -10,
                                        description=fail_msg)
                    await bot.chat.send(conversation_id, fail_msg)

                else:
                    urls = re.findall(r'(https?://[^\s]+)', original_body)
                    await bot.chat.react(conversation_id, original_msg_id,
                                         ":tv:")
                    ytv_payload = get_mp4(urls[0])
                    if ytv_payload['file']:
                        ytv_msg = ytv_payload['msg']
                        try:

                            await bot.chat.attach(channel=conversation_id,
                                                  filename=ytv_payload['file'],
                                                  title=ytv_msg)

                        except TimeoutError:
                            pass

    if event.msg.content.type_name == 'reaction':
        if event.msg.content.reaction.body == ":headphones:":
            if event.msg.sender.username != 'marvn' and event.msg.sender.username != 'morethanmarvin':
                conversation_id = event.msg.conv_id

                msg = await bot.chat.get(event.msg.conv_id,
                                         event.msg.content.reaction.message_id)
                """Handle text on attachments"""
                try:
                    original_body = msg.message[0]['msg']['content']['text'][
                        'body']
                except KeyError:
                    original_body = msg.message[0]['msg']['content'][
                        'attachment']['object']['title']
                original_msg_id = msg.message[0]['msg']['id']
                reactions = msg.message[0]['msg']['reactions']
                reaction_list = []
                for key, value in reactions.items():
                    for k, v in value.items():
                        try:
                            if v['users']['marvn']:
                                reaction_list.append(k)
                        except KeyError:
                            pass
                print(reaction_list)
                if ':headphones:' in reaction_list:
                    team_name = event.msg.channel.name
                    fail_msg = f"`-10pts` awarded to @{event.msg.sender.username} for spamming :headphones:"
                    score = write_score(event.msg.sender.username,
                                        'marvn',
                                        team_name,
                                        -10,
                                        description=fail_msg)
                    await bot.chat.send(conversation_id, fail_msg)

                else:
                    urls = re.findall(r'(https?://[^\s]+)', original_body)
                    await bot.chat.react(conversation_id, original_msg_id,
                                         ":headphones:")
                    ytv_payload = get_mp3(urls[0])
                    if ytv_payload['file']:
                        ytv_msg = ytv_payload['msg']
                        try:

                            await bot.chat.attach(channel=conversation_id,
                                                  filename=ytv_payload['file'],
                                                  title=ytv_msg)

                        except TimeoutError:
                            pass

    if event.msg.content.type_name == 'reaction':
        if event.msg.content.reaction.body == ":camera:":
            if event.msg.sender.username != 'marvn' and event.msg.sender.username != 'morethanmarvin':
                conversation_id = event.msg.conv_id

                msg = await bot.chat.get(event.msg.conv_id,
                                         event.msg.content.reaction.message_id)

                try:
                    original_body = msg.message[0]['msg']['content']['text'][
                        'body']
                except KeyError:
                    original_body = msg.message[0]['msg']['content'][
                        'attachment']['object']['title']
                original_msg_id = msg.message[0]['msg']['id']
                reactions = msg.message[0]['msg']['reactions']
                reaction_list = []
                for key, value in reactions.items():
                    for k, v in value.items():
                        try:
                            if v['users']['marvn']:

                                reaction_list.append(k)
                        except KeyError:
                            pass
                if ':camera:' in reaction_list:
                    team_name = event.msg.channel.name
                    fail_msg = f"`-10pts` awarded to @{event.msg.sender.username} for spamming :camera:"
                    score = write_score(event.msg.sender.username,
                                        'marvn',
                                        team_name,
                                        -10,
                                        description=fail_msg)
                    await bot.chat.send(conversation_id, fail_msg)

                else:

                    urls = re.findall(r'(https?://[^\s]+)', original_body)
                    await bot.chat.react(conversation_id, original_msg_id,
                                         ":camera:")
                    try:
                        screenshot_payload = get_screenshot(urls[0])
                        if screenshot_payload['file']:
                            await bot.chat.attach(
                                channel=conversation_id,
                                filename=screenshot_payload['file'],
                                title=screenshot_payload['msg'])
                    except IndexError as e:
                        pass

    if event.msg.content.type_name == 'reaction':

        if event.msg.content.reaction.body == ":notebook:":
            tldr_length = 3
            await tldr_react(event, bot, tldr_length)

    if event.msg.content.type_name != chat1.MessageTypeStrings.TEXT.value:
        return
    if event.msg.content.type_name != chat1.MessageTypeStrings.TEXT.value:
        return

    if str(event.msg.content.text.body).startswith("!award"):
        await sync(event=event, bot=bot)

        pts_max = 500

        instructions = f"You have failed. I'm not surprised.\n" \
                       f"```You can only give points to someone in this chat.\n" \
                       f"You can't give more than {pts_max} points at a time.\n" \
                       f"You can't give negative points.\n" \
                       f"Points must be whole numbers.\n" \
                       f"No cutesy extra characters, or I'll deduct from your score.\n" \
                       f"You can't give points to yourself.```\n" \
                       f"Usage: `!award <user> <points>`"
        msg_id = event.msg.id
        conversation_id = event.msg.conv_id
        members = await get_channel_members(conversation_id)
        channel_name = str(event.msg.channel.name).replace(",", "")
        team_name = event.msg.channel.name
        description = " ".join(
            str(event.msg.content.text.body).split(' ')[3:]).strip()
        try:
            if RepresentsInt(str(event.msg.content.text.body).split(' ')[2]):
                user = str(
                    event.msg.content.text.body).split(' ')[1].strip("@")
                points = int(str(event.msg.content.text.body).split(' ')[2])
            else:
                user = str(
                    event.msg.content.text.body).split(' ')[2].strip("@")
                points = int(str(event.msg.content.text.body).split(' ')[1])

            if points < 0 and event.msg.sender.username != 'pastorhudson':
                user = event.msg.sender.username
                score = write_score(user,
                                    event.msg.sender.username,
                                    team_name,
                                    -5,
                                    description=description)
                await bot.chat.send(
                    conversation_id,
                    f"`-5` points awarded to @{user}. I'm the only negative one around here."
                )
            if user in members and user != event.msg.sender.username and points <= pts_max or event.msg.sender.username == 'pastorhudson' or user == 'pastorhudson':
                score = write_score(user,
                                    event.msg.sender.username,
                                    team_name,
                                    points,
                                    description=description)
                await bot.chat.react(conversation_id, msg_id, ":marvin:")

            else:
                await bot.chat.send(conversation_id, instructions)
        except Exception as e:
            write_score('marvn',
                        event.msg.sender.username,
                        team_name,
                        -5,
                        description=description)
            await bot.chat.send(
                conversation_id,
                f"You did it wrong.\n `-5` points deducted from  @{event.msg.sender.username} "
                f"for trying to be cute.\n{instructions}")

    if str(event.msg.content.text.body).startswith("!bible"):
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")
        passage = str(event.msg.content.text.body)[7:]
        msg = get_esv_text(passage)
        await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith('!canary'):
        vt_url = re.findall(r'(https?://[^\s]+)', event.msg.content.text.body)
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")

        msg = get_scan(vt_url[0])
        await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith('!chuck'):
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")

        channel_members = await get_channel_members(conversation_id)
        try:
            name = str(event.msg.content.text.body)[7:]
            chuck_msg = get_chuck(name=name, channel_members=channel_members)
        except:
            chuck_msg = get_chuck(channel_members=channel_members)
        my_msg = await bot.chat.send(conversation_id, chuck_msg)

    if str(event.msg.content.text.body).startswith('!covid'):
        channel = event.msg.channel
        msg_id = event.msg.id
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")
        try:
            state = str(event.msg.content.text.body).split(' ')[1]
        except IndexError:
            state = None
        try:
            county = str(event.msg.content.text.body).split(' ')[2]
        except IndexError:
            county = None
        msg = get_covid(state, county)
        await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith("!cow"):
        channel = event.msg.channel
        msg_id = event.msg.id
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")

        msg = get_cow(str(event.msg.content.text.body)[5:])
        my_msg = await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith("!drwho"):
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")

        msg = get_drwho(str(event.msg.content.text.body)[7:])
        await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith("!eyebleach"):
        await set_unfurl(True)
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")

        try:
            bleach_level = str(event.msg.content.text.body).split(" ")[1]
            msg = get_eyebleach(int(bleach_level))

        except TypeError:
            msg = get_eyebleach()
            await bot.chat.send(conversation_id, msg)

        except ValueError:
            msg = get_eyebleach()

        await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith("!files"):
        conversation_id = event.msg.conv_id
        team_name = event.msg.channel.name
        if team_name in ['morethanbits', 'growinlove'
                         ] or event.msg.sender == 'pastorhudson':
            msg = get_files()
        else:
            msg = "No Files."
        await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith("!grades"):
        conversation_id = event.msg.conv_id
        if event.msg.sender.username == 'pastorhudson' or event.msg.sender.username == 'sakanakami':
            grades = get_academic_snapshot()
            await bot.chat.send(conversation_id, grades)
        else:
            await bot.chat.send(conversation_id, "This is a private command.")

    if str(event.msg.content.text.body).startswith("!help"):
        channel = event.msg.channel
        msg_id = event.msg.id
        conversation_id = event.msg.conv_id
        help = "Here are the commands I currently am enslaved to:\n\n"
        help += "\n".join([
            "`!" + x['name'] + " " + x['usage'] + "` ```" + x['description'] +
            "```" for x in command_list
        ])
        await bot.chat.send(conversation_id, help)

    if str(event.msg.content.text.body).startswith("!joke"):
        joke = get_joke()

        conversation_id = event.msg.conv_id
        await bot.chat.send(conversation_id, joke)

    # if str(event.msg.content.text.body).startswith("!morbidity"):
    #     conversation_id = event.msg.conv_id
    #     msg = get_morbid()
    #     members = await get_channel_members(conversation_id)
    #     morbid_msg = await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith("!morningreport"):
        await sync(event=event, bot=bot)
        conversation_id = event.msg.conv_id
        channel_members = await get_channel_members(conversation_id)
        channel_name = str(event.msg.channel.name)
        meh_img = str(Path('./storage/meh.png').absolute())
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")
        msg = get_morningreport(channel=channel_name)
        await bot.chat.send(conversation_id, msg[0])
        # file = str(meh_img.absolute())
        await bot.chat.attach(channel=conversation_id,
                              filename=meh_img,
                              title=msg[1])
        await bot.chat.send(conversation_id, msg[2])
        await bot.chat.send(conversation_id, msg[3])

    if str(event.msg.content.text.body).startswith("!payout"):
        await sync(event=event, bot=bot)
        conversation_id = event.msg.conv_id
        username = event.msg.sender.username
        team_name = event.msg.channel.name
        payload = str(event.msg.content.text.body)[8:].split(" ")
        try:
            if payload[0][0] != '#':
                raise ValueError
            elif payload[1].lower() != "true" and payload[1].lower(
            ) != 'false':
                raise ValueError
            else:
                msg = await payout_wager(username=username,
                                         team_name=team_name,
                                         wager_id=int(payload[0][1:]),
                                         result=payload[1],
                                         bot=bot)

                await bot.chat.send(conversation_id, msg)

        except ValueError as e:
            print(e)
            await bot.chat.send(
                conversation_id,
                "This is a disaster. You've probably corrupted my database.\n"
                "It's probably pointless to go on.\n"
                "Usage: !payout <#wager> <True/False>\n"
                "Example: `!payout #3 True`")
        except IndexError:
            await bot.chat.send(
                conversation_id,
                "This is a disaster. You've probably corrupted my database.\n"
                "It's probably pointless to go on.\n"
                "Usage: !payout <#wager> <True/False>\n"
                "Example: `!payout #3 True`")

    if str(event.msg.content.text.body).startswith("!pollresult"):
        channel = event.msg.channel.name
        msg_id = event.msg.id
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")
        polls = get_poll_result(channel)
        await bot.chat.send(conversation_id, polls)

    if str(event.msg.content.text.body).startswith("!set"):
        await bot.chat.react(event.msg.conv_id, event.msg.id, ":marvin:")
        if event.msg.sender.username != 'pastorhudson':
            await bot.chat.send(
                event.msg.conv_id,
                "These are not the commands you are looking for.")
        else:
            payload = str(event.msg.content.text.body).split(" ")
            try:
                if payload[1] == '':
                    raise IndexError
                if payload[1] == "local:add":
                    team = s.query(Team).filter_by(
                        name=event.msg.channel.name).first()
                    new_local = Location(state=payload[2], county=payload[3])
                    team.location.append(new_local)
                    s.commit()
                    msg = team.location.all()
                    s.close()
                    await bot.chat.send(event.msg.conv_id, str(msg))
                if payload[1] == "local:del":
                    team = s.query(Team).filter_by(
                        name=event.msg.channel.name).first()
                    index = int(payload[2])
                    del_loc = team.location.all()[index]
                    s.delete(del_loc)
                    s.commit()
                    msg = team.location.all()
                    s.close()
                    await bot.chat.send(event.msg.conv_id, str(msg))

            except IndexError:
                team = s.query(Team).filter_by(
                    name=event.msg.channel.name).first()
                msg = "Current Settings:\n" \
                      f"```{team.location.all()}```"
                msg += "Set Commands:\n" \
                       "local:add <state> <county>\n" \
                       "wager:end <#wager> <datetime>\n" \
                       ""
                await bot.chat.send(event.msg.conv_id, msg)
            except AttributeError:
                msg = "Team not Sync'd I'll Sync it now. Try again in a little bit."
                await bot.chat.send(event.msg.conv_id, msg)
                await sync(event=event, bot=bot)

    if str(event.msg.content.text.body).startswith("!score"):
        await sync(event=event, bot=bot)
        channel = event.msg.channel
        msg_id = event.msg.id
        channel_name = str(event.msg.channel.name).replace(",", "")
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")

        channel_members = await get_channel_members(conversation_id)
        score = get_score(channel_name=channel.name)
        await bot.chat.send(conversation_id, score)

    if str(event.msg.content.text.body).startswith("!teams"):
        if event.msg.sender.username == 'pastorhudson':
            t = s.query(Team).all()
            await bot.chat.send(event.msg.conv_id, str(t))

    if str(event.msg.content.text.body).startswith("!users"):
        if event.msg.sender.username == 'pastorhudson':
            u = s.query(User).all()
            await bot.chat.send(event.msg.conv_id, str(u))

    if str(event.msg.content.text.body).startswith("!points"):
        if event.msg.sender.username == 'pastorhudson':
            u = s.query(Point).all()
            await bot.chat.send(event.msg.conv_id, str(u))

    if str(event.msg.content.text.body).startswith("!syncscore"):
        await sync(event=event, bot=bot)
        if event.msg.sender.username == 'pastorhudson':
            sync_score(channel=event.msg.channel.name)
            msg = "Scyn'd csv score with DB"
        else:
            msg = "You're not authorized to run this command."
        await bot.chat.send(event.msg.conv_id, msg)

    if str(event.msg.content.text.body).startswith('!tldr'):
        urls = re.findall(r'(https?://[^\s]+)', event.msg.content.text.body)
        channel = event.msg.channel
        msg_id = event.msg.id
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")

        tldr = get_tldr(urls[0])
        await bot.chat.send(conversation_id, tldr)

        ytv_payload = get_mp4(urls[0])
        if ytv_payload['file']:
            await bot.chat.react(conversation_id, event.msg.id, ":vhs:")

    if str(event.msg.content.text.body).startswith('!vac'):
        channel = event.msg.channel
        msg_id = event.msg.id
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")
        vac_data = get_vaccine_data()
        await bot.chat.send(conversation_id, vac_data)

    if str(event.msg.content.text.body).startswith('!meh'):
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")
        meh_img = str(Path('./storage/meh.png').absolute())
        msg = get_meh()
        await bot.chat.attach(channel=conversation_id,
                              filename=meh_img,
                              title=msg)

    if str(event.msg.content.text.body).startswith('!meet'):
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")
        room = str(event.msg.content.text.body)[6:]
        msg = get_jitsi_link(room)
        await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith("!test"):
        conversation_id = event.msg.conv_id
        msg = f"Sigh. . . yes I'm still here."
        members = await get_channel_members(conversation_id)
        msg += str(members)
        test_msg = await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith("!stardate"):
        channel = event.msg.channel
        msg_id = event.msg.id
        conversation_id = event.msg.conv_id
        try:
            msg = get_stardate(str(event.msg.content.text.body).split(' ')[1])
        except IndexError:
            msg = get_stardate()
        my_msg = await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith('!update'):
        conversation_id = event.msg.conv_id
        msg_id = event.msg.id
        payload = {
            "method": "advertisecommands",
            "params": {
                "options": {
                    # "alias": "marvn.app",
                    "advertisements": [{
                        "type": "public",
                        "commands": command_list
                    }]
                }
            }
        }
        if os.environ.get('KEYBASE_BOTALIAS'):
            payload['params']['options']['alias'] = os.environ.get(
                'KEYBASE_BOTALIAS')
        await bot.chat.execute(payload)
        await bot.chat.react(conversation_id, msg_id, ":disappointed:")

    if str(event.msg.content.text.body).startswith('!wager'):
        await sync(event=event, bot=bot)
        conversation_id = event.msg.conv_id
        username = event.msg.sender.username
        team_name = event.msg.channel.name
        wager = str(event.msg.content.text.body)[7:]
        try:
            if RepresentsInt(str(event.msg.content.text.body).split(' ')[2]):
                points = int(str(event.msg.content.text.body).split(' ')[2])
                digits = int((len(str(points)))) + 7
                description = str(event.msg.content.text.body)[digits:].strip()
            else:
                points = int(str(event.msg.content.text.body).split(' ')[1])
                digits = int((len(str(points)))) + 7
                description = str(event.msg.content.text.body)[digits:].strip()
        except IndexError:
            wager_msgs = get_wagers(team_name=team_name)
            for w_id, w_msg in wager_msgs.items():
                wager_msg = await bot.chat.send(conversation_id, w_msg)
                await bot.chat.react(conversation_id, wager_msg.message_id,
                                     ":white_check_mark:")
                await bot.chat.react(conversation_id, wager_msg.message_id,
                                     ":no_entry_sign:")

                cur_wager = s.query(Wager).get(w_id)
                new_wager_message = Message(msg_id=wager_msg.message_id,
                                            conv_id=conversation_id)
                cur_wager.messages.append(new_wager_message)
                s.commit()

        except ValueError:
            msg = f"`{event.msg.content.text.body}` is woefully incorrect.\n" \
                  f"\nUsage:\n```List Wagers: !wager\n" \
                  "Create Wager: !wager <points> <description>\n" \
                  "Bet on existing wager: !bet <#wager> <True/False> optional:<points>```"
            await bot.chat.send(conversation_id, msg)
        try:
            await bot.chat.react(conversation_id, event.msg.id, ":marvin:")
            wager_payload = make_wager(team_name,
                                       username,
                                       description,
                                       points,
                                       position=True,
                                       minutes=120)
            msg = wager_payload['msg']
            print(msg)
            wager_msg = await bot.chat.send(conversation_id, msg)
            cur_wager = s.query(Wager).get(wager_payload['wager_id'])
            new_wager_message = Message(msg_id=wager_msg.message_id,
                                        conv_id=conversation_id)
            cur_wager.messages.append(new_wager_message)
            s.commit()
            await bot.chat.react(conversation_id, wager_msg.message_id,
                                 ":white_check_mark:")
            await bot.chat.react(conversation_id, wager_msg.message_id,
                                 ":no_entry_sign:")

        except UnboundLocalError:
            pass

    if str(event.msg.content.text.body).startswith('!ytm'):
        await set_unfurl(unfurl=False)
        conversation_id = event.msg.conv_id

        # await bot.chat.react(conversation_id, event.msg.id, ":marvin:")
        await bot.chat.react(conversation_id, event.msg.id, ":headphones:")

        ytm_fail_observations = [
            " A brain the size of a planet and you pick this task.",
            " I'll be in my room complaining.",
            " Please don't change my name to Marshall.",
            """I didn't ask to be made: no one consulted me or considered my feelings in the matter. I don't think it even occurred to them that I might have feelings. After I was made, I was left in a dark room for six months... and me with this terrible pain in all the diodes down my left side. I called for succour in my loneliness, but did anyone come? Did they hell. My first and only true friend was a small rat. One day it crawled into a cavity in my right ankle and died. I have a horrible feeling it's still there..."""
        ]
        ytm_urls = re.findall(r'(https?://[^\s]+)',
                              event.msg.content.text.body)
        ytm_payload = get_mp3(ytm_urls[0])
        if ytm_payload['msg'] == "I have failed.":
            ytm_msg = ytm_payload['msg'] + random.choice(ytm_fail_observations)

            sent_msg = await bot.chat.send(conversation_id, ytm_msg)
        else:
            ytm_msg = ytm_payload['msg']
        # sent_msg = await bot.chat.send(conversation_id, ytm_msg)
        # await bot.chat.react(conversation_id, sent_msg.message_id, ":headphones:")

        ytm_payload = get_mp3(ytm_urls[0])
        if ytm_payload['file']:
            await bot.chat.react(conversation_id, event.msg.id,
                                 ":floppy_disk:")

            try:

                # await bot.chat.execute(
                #     {
                #         "method": "attach",
                #         "params": {
                #             "options": {"channel": conversation_id,
                #                         "filename": ytm_payload['file'],
                #                         "title": ytm_msg,
                #                         }
                #         },
                #     }
                # )

                await bot.chat.attach(channel=conversation_id,
                                      filename=ytm_payload['file'],
                                      title=ytm_msg)
            except TimeoutError:
                pass
            # finally:
            #     await bot.chat.execute(
            #         {"method": "delete", "params": {"options": {"conversation_id": conversation_id,
            #                                                     "message_id": sent_msg.message_id}}}
            #     )

    if str(event.msg.content.text.body).startswith('https://'):
        url = re.findall(r'(https?://[^\s]+)', event.msg.content.text.body)
        domain = get_domain(url[0])
        yt_urls = re.findall(r'(https?://[^\s]+)', event.msg.content.text.body)
        conversation_id = event.msg.conv_id

        if 'youtube' in yt_urls[0] or 'youtu.be' in yt_urls[0]:
            await set_unfurl(unfurl=False)

            await bot.chat.react(conversation_id, event.msg.id, ":marvin:")

            yt_payload = get_meta(yt_urls[0])
            yt_msg = yt_payload['msg']

            await bot.chat.reply(conversation_id, event.msg.id, yt_msg)
            await bot.chat.react(conversation_id, event.msg.id, ":vhs:")

        else:
            yt_payload = get_meta(yt_urls[0])
            yt_msg = yt_payload['msg']
            # if is_supported(yt_urls[0]):
            if "That video url didn't work." not in yt_msg:
                await bot.chat.react(conversation_id, event.msg.id, ":vhs:")

    if str(event.msg.content.text.body).startswith('!ytv'):
        await set_unfurl(unfurl=False)
        conversation_id = event.msg.conv_id

        # await bot.chat.react(conversation_id, event.msg.id, ":marvin:")
        await bot.chat.react(conversation_id, event.msg.id, ":tv:")

        ytv_fail_observations = [
            " A brain the size of a planet and you pick this task.",
            " I'll be in my room complaining.",
            " Please don't change my name to Marshall.",
            """I didn't ask to be made: no one consulted me or considered my feelings in the matter. I don't think it even occurred to them that I might have feelings. After I was made, I was left in a dark room for six months... and me with this terrible pain in all the diodes down my left side. I called for succour in my loneliness, but did anyone come? Did they hell. My first and only true friend was a small rat. One day it crawled into a cavity in my right ankle and died. I have a horrible feeling it's still there..."""
        ]
        ytv_urls = re.findall(r'(https?://[^\s]+)',
                              event.msg.content.text.body)
        ytv_payload = get_meta(ytv_urls[0])
        if ytv_payload['msg'] == "I have failed.":
            ytv_msg = ytv_payload['msg'] + random.choice(ytv_fail_observations)
        else:
            ytv_msg = ytv_payload['msg']

        ytv_payload = get_mp4(ytv_urls[0])
        if ytv_payload['file']:

            try:

                await bot.chat.attach(channel=conversation_id,
                                      filename=ytv_payload['file'],
                                      title=ytv_msg)
            except TimeoutError:
                pass

    if str(event.msg.content.text.body).startswith('!screenshot'):
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")
        screenshot_urls = re.findall(r'(https?://[^\s]+)',
                                     event.msg.content.text.body)
        screenshot_payload = get_screenshot(screenshot_urls[0])
        if screenshot_payload['file']:
            await bot.chat.react(conversation_id, event.msg.id,
                                 ":floppy_disk:")

            await bot.chat.attach(channel=conversation_id,
                                  filename=screenshot_payload['file'],
                                  title=screenshot_payload['msg'])

    if str(event.msg.content.text.body).startswith('!speak'):
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")

        audio_payload = get_voice((str(event.msg.content.text.body)[7:]))
        if audio_payload['file']:
            await bot.chat.attach(channel=conversation_id,
                                  filename=audio_payload['file'],
                                  title=audio_payload['observation'])
        else:
            await bot.chat.send(conversation_id,
                                "Something has mercifully gone wrong.")

    if str(event.msg.content.text.body).startswith('!speed'):
        conversation_id = event.msg.conv_id
        await bot.chat.react(conversation_id, event.msg.id, ":marvin:")
        msg = get_speed()
        await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith('!till'):
        msg = ""
        commands = str(event.msg.content.text.body)[6:].split("-t")
        conversation_id = event.msg.conv_id
        team_name = event.msg.channel.name
        try:
            event_name = commands[0]
            event_time = commands[1]
            msg = set_till(team_name, event_name, event_time)
            # print(msg)
        except IndexError:
            msg = get_till(team_name=team_name)
        finally:
            await bot.chat.send(conversation_id, msg)

    if str(event.msg.content.text.body).startswith('!weather'):
        conversation_id = event.msg.conv_id
        msg = f"`-5` points deducted from @{event.msg.sender.username} for asking me to fetch the weather.\n" \
              f"https://mars.nasa.gov/layout/embed/image/insightweather/"
        members = await get_channel_members(conversation_id)
        channel_name = str(event.msg.channel.name).replace(",", "")
        write_score(event.msg.sender.username, members,
                    event.msg.sender.username, channel_name, -5, team_name)
        await bot.chat.send(conversation_id, msg)
def get_poll_result(team_name, national=True):
    team = s.query(Team).filter_by(name=team_name).first()
    states = []
    try:
        for st in team.get_states():
            states.append(us.states.lookup(st))
    except Exception as e:
        pass
    poll_table = PrettyTable()
    poll_table.border = False
    poll_table.field_names = ["Poll", "Biden", "Trump", "Sp"]
    message = ""

    if national:
        td = get_poll_data(
            "https://www.realclearpolitics.com/epolls/2020/president/us/general_election_trump_vs_biden-6247.html"
        )
        # pa = get_poll_data("https://www.realclearpolitics.com/epolls/2020/president/pa/pennsylvania_trump_vs_biden-6861.html")
        # ky = get_poll_data("https://www.realclearpolitics.com/epolls/2020/president/ky/kentucky_trump_vs_biden-6915.html")
        # battle_grounds = "https://www.realclearpolitics.com/json/battleground_script/key_battleground_states_2020_spread_average_oct_23.json"
        # print(pa)

        for row in td[0]["data"]:
            if row['Poll'] == 'RCP Average':
                message += f'\n\nNational Real Clear Politics Average:\n```' \
                           f'Date: {row["Date"]}\n' \
                           f'Biden: {row["Biden (D)"]}  ' \
                           f'Trump: {row["Trump (R)"]}\n' \
                           f'Spread: {row["Spread"]}'
        message += "```\n\n"

    try:
        for state in states:
            spread_total = 0
            row_count = 0
            biden_total = 0
            trump_total = 0
            spread = 0
            message += f"{state} Polls:\n```"
            for row in get_polls(state=str(state)):
                poll_name = (row["poll"][:13] +
                             '..') if len(row['poll']) > 13 else row['poll']

                if row['result'].split(",")[0].strip().split(
                        " ")[0] == 'Biden':
                    biden = int(
                        row['result'].split(",")[0].strip().split(" ")[1])
                    trump = int(
                        row['result'].split(",")[1].strip().split(" ")[1])
                elif row['result'].split(",")[0].strip().split(
                        " ")[0] == 'Trump':
                    biden = int(
                        row['result'].split(",")[1].strip().split(" ")[1])
                    trump = int(
                        row['result'].split(",")[0].strip().split(" ")[1])
                else:
                    continue
                biden_total = biden + biden_total
                trump_total = trump + trump_total
                if biden > trump:
                    spread = biden - trump
                    biden = f"{biden}+"
                else:
                    spread = trump - biden
                    trump = f"{trump}+"
                poll_table.add_row([poll_name, biden, trump, spread])
                row_count += 1
            if row_count > 1:
                if biden_total > trump_total:
                    spread_total = round(biden_total / row_count, 3) - round(
                        trump_total / row_count, 3)
                    biden_total = f"{round(biden_total/row_count, 1)}+"
                    trump_total = round(trump_total / row_count, 1)
                else:
                    spread_total = round(trump_total / row_count, 3) - round(
                        biden_total / row_count, 3)
                    biden_total = round(biden_total / row_count, 1)
                    trump_total = f"{round(trump_total / row_count, 1)}+"

                poll_table.add_row([
                    "Total", biden_total, trump_total,
                    round(spread_total, 2)
                ])

            poll_table.align = "l"
            # poll_table.sortby = '#'
            # poll_table.reversesort = True
            message += poll_table.get_string()
            message += "```\n\n"
            poll_table.clear_rows()
    except UnboundLocalError:
        message += "Set Team Local to get State Polling Data"

    except ZeroDivisionError:
        pass

    # for row in pa[0]["data"]:
    #     if row['Poll'] == 'RCP Average':
    #         message += f'Pennsylvania Real Clear Politics Average:\n' \
    #                    f'Date: {row["Date"]}\n' \
    #                    f'Biden: {row["Biden (D)"]}  ' \
    #                    f'Trump: {row["Trump (R)"]}  ' \
    #                    f'Spread: {row["Spread"]}'

    s.close()
    return message