async def ai_move(client: discord.Client, message: discord.Message): game = get_user_game_info(message.author.mention) if game is None: await message.channel.send('Game not found') return if game.check_terminal() is not None: await message.channel.send('Game has ended') return # generate game string url = game.string_serialise() res = await request_pos_info(game.string_serialise()) if not res['success'] or not res['moves']: await message.channel.send('Search failure') return move_data = res['moves'] move_data.sort(key=lambda x: x['visits'], reverse=True) best_move = move_data[0]['move'] game.play_move(best_move) serialised = game.serialise() db_data = json.loads(REDISDB.get('con4').decode()) db_data[message.author.mention] = serialised REDISDB.set('con4', json.dumps(db_data).encode()) move_str = ':regional_indicator_' + 'abcdefg'[best_move] + ':' await message.channel.send(f'Search complete! Moved to {move_str}\n' + game.discord_message())
async def autoyeet_loop(client: discord.Client, message: discord.Message): yeetdb = json.loads(REDISDB.get('AUTOYEET').decode()) yeetdb[message.channel.id] = True REDISDB.set('AUTOYEET', json.dumps(yeetdb)) while True: await asyncio.sleep(random.randint(5, 120)) yeetdb = json.loads(REDISDB.get('AUTOYEET').decode()) if not yeetdb[message.channel.id]: break await client.send_message(message.channel, 'YEET!')
async def new_game(client: discord.Client, message: discord.Message): """ User wants to start a new game with the bot """ game = C4Game() serialised = game.serialise() db_data = json.loads(REDISDB.get('con4').decode()) db_data[message.author.mention] = serialised REDISDB.set('con4', json.dumps(db_data).encode()) await message.channel.send('New game!\n' + game.discord_message())
async def autoyeet_toggle(client: discord.Client, message: discord.Message): yeetdb = json.loads(REDISDB.get('AUTOYEET').decode()) if message.channel.id not in yeetdb: yeetdb[message.channel.id] = False yeetdb[message.channel.id] = not yeetdb[message.channel.id] REDISDB.set('AUTOYEET', json.dumps(yeetdb)) if yeetdb[message.channel.id]: # yeeting on await client.send_message(message.channel, 'Autoyeet on') # start the yeet loop await autoyeet_loop(client, message) else: # yeeting off await client.send_message(message.channel, 'Autoyeet off')
def get_user_game_info(user_id): # user_id obtained from author.mention data = json.loads(REDISDB.get('con4').decode()) game = data.get(user_id) if game is None: return return C4Game.deserialise(game)
async def process_move(client: discord.Client, message: discord.Message): # user made a move now we pwn them game = get_user_game_info(message.author.mention) if game is None: await message.channel.send('Game not found') return re_match = re.match(r'^\$c4 move ([A-Ga-g])$', message.content) move = re_match.group(1).lower() move = 'abcdefg'.index(move) legals = game.legal_moves() if not legals[move]: await message.channel.send('Bad move') return game.play_move(move) serialised = game.serialise() db_data = json.loads(REDISDB.get('con4').decode()) db_data[message.author.mention] = serialised REDISDB.set('con4', json.dumps(db_data).encode()) await message.channel.send(f'You move to column {move + 1}\n' + game.discord_message())
async def on_ready(): print('Logged in as') print(client.user.name) print(client.user.id) print('------') await client.edit_profile(username='******') # TODO: Resume any polls, yeeting yeetdb = json.loads(REDISDB.get('AUTOYEET').decode()) yeet_coros = [] for entry in yeetdb: if yeetdb[entry]: yeet_coros.append(botutils.autoyeet_loop_channelid(client, entry)) await asyncio.gather(*yeet_coros)
""" Discord bot - Luke """ import os import json import asyncio import discord from routing.mapper import automatch from msgs import msgpatterns from redisdb import REDISDB import botutils REDISDB_CONF = {'AUTOYEET': dict()} for conf in REDISDB_CONF: if not REDISDB.get(conf): REDISDB.set(conf, json.dumps(REDISDB_CONF[conf])) loop = asyncio.get_event_loop() client = discord.Client(loop=loop) @client.event async def on_ready(): print('Logged in as') print(client.user.name) print(client.user.id) print('------') await client.edit_profile(username='******') # TODO: Resume any polls, yeeting yeetdb = json.loads(REDISDB.get('AUTOYEET').decode()) yeet_coros = []