Files
BaseballBot/src/main/discord_module/bot_runner.py

161 lines
6.1 KiB
Python
Raw Normal View History

2019-09-08 18:51:44 -05:00
import sys
import discord
import uuid
import datetime
from src.main.configs import Configs
2020-11-29 11:51:10 -06:00
from src.main.database_module.guess_dao import GuessDAO, GUESSED_NUMBER, MEMBER_ID, MEMBER_NAME, DIFFERENCE
2019-09-08 18:51:44 -05:00
from src.main.database_module.play_dao import PlayDAO, PLAY_ID, CREATION_DATE
from src.main.db_session import DatabaseSession
from src.main.discord_module.leaderboard_config import LeaderboardConfig
play_dao = None
guess_dao = None
bot = discord.Client()
2020-11-29 11:51:10 -06:00
2019-09-08 18:51:44 -05:00
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
2020-11-29 11:51:10 -06:00
2019-09-08 18:51:44 -05:00
@bot.event
async def on_message(message):
if message.author == bot.user:
return
content = message.content
'''
Sets up the next set of guesses.
'''
if content.startswith('!ghostball'):
if play_dao.is_active_play():
2020-11-27 22:42:13 -06:00
await message.channel.send("There's already an active play. Could you close that one first, please?")
2019-09-08 18:51:44 -05:00
else:
2020-11-29 11:51:10 -06:00
play_object = {PLAY_ID: uuid.uuid4(), CREATION_DATE: datetime.datetime.now()}
2019-09-08 18:51:44 -05:00
play_dao.insert(play_object)
2020-11-29 11:51:10 -06:00
await message.channel.send("@flappyball, pitch is in! Send me your guesses with a !guess command.")
2019-09-08 18:51:44 -05:00
if content.startswith("!guess"):
guess_value = __parse_guess__(content)
if not play_dao.is_active_play():
2020-11-29 11:51:10 -06:00
await message.channel.send("Hey, there's no active play! Start one up first with !ghostball.")
2019-09-08 18:51:44 -05:00
else:
play = play_dao.get_active_play()
guess_object = {PLAY_ID: play['play_id'],
MEMBER_ID: str(message.author.id),
GUESSED_NUMBER: guess_value,
MEMBER_NAME: str(message.author.name)}
if guess_dao.insert(guess_object):
2020-11-27 22:42:13 -06:00
await message.add_reaction(emoji="\N{THUMBS UP SIGN}")
2019-09-08 18:51:44 -05:00
# Closes off the active play to be ready for the next set
if content.startswith('!resolve'):
# try:
2020-11-27 22:42:13 -06:00
pitch_value = __parse_resolve_play__(content)
if pitch_value is None:
2020-11-29 11:51:10 -06:00
await message.channel.send("Hey " + "<@" + str(message.author.id) + ">, I'm not sure what you meant. "
"You need real, numeric, values for this command to work. "
"Use !resolve <pitch number> and try again.")
2019-09-08 18:51:44 -05:00
# Check if we have an active play
if not play_dao.is_active_play():
2020-11-29 11:51:10 -06:00
await message.channel.send("You confused me. There's no active play so I have nothing to close!")
2019-09-08 18:51:44 -05:00
else:
2020-11-27 22:42:13 -06:00
play = play_dao.resolve_play(pitch_value)
2019-09-08 19:54:18 -05:00
guess_dao.set_differences(pitch_value, play['play_id'])
2020-11-29 11:51:10 -06:00
closest_guess = guess_dao.get_closest_on_play(play['play_id'])
2019-09-08 18:51:44 -05:00
2020-11-27 22:42:13 -06:00
await message.channel.send(
2020-11-29 11:51:10 -06:00
"Closed this play! " + "<@" + str(closest_guess[MEMBER_ID]) +
"> was the closest with a guess of " + closest_guess[GUESSED_NUMBER] +
" resulting in a difference of " + closest_guess[DIFFERENCE] + ".")
2019-09-08 18:51:44 -05:00
# Likely due to too few parameters but could be any number of things
# except :
2020-11-27 22:42:13 -06:00
# await message.channel.send( "Hey " + "<@" + str(message.author.id) + ">, you confused me with that message. "
2019-09-08 18:51:44 -05:00
# "To close an active pitch, the proper command is !resolve <pitch number> <swing_number>. "
# "Use that format and try again, ok?")
if content.startswith('!leaderboard'):
leaderboard_config = __parse_leaderboard_message__(content)
if leaderboard_config.should_sort_by_pure_closest():
values = guess_dao.fetch_closest(10)
string_to_send = ''
for i, value in enumerate(values):
string_to_send += str(i + 1) + ': ' + value['member_name'] + ', ' + value['difference'] + '\n'
2020-11-29 11:51:10 -06:00
await message.channel.send(string_to_send)
2019-09-08 18:51:44 -05:00
elif leaderboard_config.should_sort_by_best_average():
pass
else:
2020-11-29 11:51:10 -06:00
await message.channel.send(
"I don't understand that leaderboard command, sorry! I know it's a little confusing, so send me"
" a !help message if you want the full rundown for how to make this work!")
if content.startswith("!points"):
pass #TODO
2019-09-08 18:51:44 -05:00
if content.startswith('!help'):
help_message = __get_help_message__()
2020-11-27 22:42:13 -06:00
recipient = await bot.fetch_user(message.author.id)
await recipient.send(help_message)
2019-09-08 18:51:44 -05:00
2020-11-29 11:51:10 -06:00
2019-09-08 18:51:44 -05:00
def __get_help_message__():
# Start message with person who asked for help
2020-11-29 11:51:10 -06:00
help_message = "Hey! I can be instructed to do any number of things! Use the following commands: \n" \
"!guess <NUMBER> --- This will add your guess to the currently active play. " \
"I will give you a thumbs up if everything worked!\n" \
"!ghostball --- Starts a new play. I'll let you know if this didn't work for some reason!\n" \
"!help --- You just asked for this. If you ask for it again, I'll repeat myself.\n" \
"!resolve <PITCH_NUMBER> --- Uses the pitch number and real swing number " \
"to figure out who was closest and ends the active play.\n" \
"<HELP MESSAGE NEEDS DOCUMENTATION FOR LEADERBOARD COMMAND! PING KALI IF YOU'RE ANGRY!>\n"
2019-09-08 18:51:44 -05:00
return help_message
def __parse_leaderboard_message__(message_content):
return LeaderboardConfig(message_content)
2020-11-29 11:51:10 -06:00
2019-09-08 18:51:44 -05:00
def __parse_guess__(message_content):
pieces = message_content.split(' ')
try:
return pieces[1]
except TypeError:
return None
2020-11-29 11:51:10 -06:00
2019-09-08 18:51:44 -05:00
def __parse_resolve_play__(message_content):
pieces = message_content.split(' ')
try:
2020-11-27 22:42:13 -06:00
return pieces[1]
2019-09-08 18:51:44 -05:00
except TypeError:
return None
2020-11-29 11:51:10 -06:00
2019-09-08 18:51:44 -05:00
if __name__ == '__main__':
args = sys.argv
token = args[1]
file_path = args[2]
configs = Configs(file_path)
databaseSession = DatabaseSession()
2020-11-27 22:42:13 -06:00
2019-09-08 18:51:44 -05:00
play_dao = PlayDAO()
guess_dao = GuessDAO()
2020-11-29 11:51:10 -06:00
bot.run(token)