diff options
| author | Nathan Perry <nathan@meta.sc> | 2016-04-10 04:42:50 -0400 |
|---|---|---|
| committer | Nathan Perry <nathan@meta.sc> | 2016-04-10 04:42:50 -0400 |
| commit | fde945e7d693244efaf642459e3d7d822715be9e (patch) | |
| tree | a11ef3f3a8c596093b2f24deff9deb184991e36f | |
| parent | 38b1e8ffb033ba7bcbf4eab3a2f19dba51de4e77 (diff) | |
simplify functions
| -rw-r--r-- | srv.py | 84 |
1 files changed, 42 insertions, 42 deletions
@@ -15,8 +15,8 @@ with open('config.yml') as f: config = yaml.load(f) client = discord.Client() -client.queue = Queue(maxsize=config.get('queue_size', 0)) -client.current_player = None +queue = Queue(maxsize=config.get('queue_size', 0)) +current_player = None reg = re.compile(r'^(?:!|\/){} (.*)$'.format(config['trigger'])) @@ -60,14 +60,14 @@ def on_message(message): 'sudoku': stop_client, 'pause': pause, 'resume': resume, - 'list': partial(list_queued, channel=message.channel), - 'queue': partial(list_queued, channel=message.channel), + 'list': list_queued, + 'queue': list_queued, } if command in cmd_map: if author_id == config['admin'] or config['op_role'] in [role.name for role in message.author.roles]: logger.info('running command \'{}\''.format(command)) - async(cmd_map[command](client)) + async(cmd_map[command]()) return logger.info('unauthorized command \'{}\' from member \'{}\' ({})'.format(command, message.author.name, message.author.id)) @@ -83,67 +83,67 @@ def on_message(message): url = url.geturl() logger.debug('playing video from url \'{}\''.format(url)) - async(enqueue_video((url, message), client, message.channel)) + async(enqueue_video((url, message))) @coroutine -def pause(client): - if not client.current_player: +def pause(): + if not current_player: return - client.current_player.pause() + current_player.pause() @coroutine -def resume(client): - if not client.current_player: +def resume(): + if not current_player: return - client.current_player.resume() + current_player.resume() @coroutine -def stop_player(client): - if not client.current_player: +def stop_player(): + if not current_player: return - client.current_player.stop() - client.current_player = None + current_player.stop() + current_player = None @coroutine -def stop_client(client): - if not client.current_player: +def stop_client(): + if not current_player: return while True: try: - client.queue.get_nowait() + queue.get_nowait() except QueueEmpty: break - yield from async(stop_player(client)) + yield from async(stop_player()) @coroutine -def enqueue_video(pair, client, channel): +def enqueue_video(pair): global config - yield from async(connect_voice(client)) + yield from async(connect_voice()) if not client.is_voice_connected(): - async(client.send_message(channel, 'go fuck yourself. voice isn\'t working.', tts=True)) + async(client.send_message(pair[1].channel, 'go fuck yourself. voice isn\'t working.', tts=True)) return - if client.queue.full(): - async(client.send_message(channel, 'fuck you. wait for the other videos.', tts=True)) + if queue.full(): + async(client.send_message(pair[1].channel, 'fuck you. wait for the other videos.', tts=True)) return - async(client.queue.put(pair)) + async(queue.put(pair)) @coroutine -def connect_voice(client): +def connect_voice(): if not client.is_voice_connected(): server = discord.utils.find(lambda x: x.name == config['server'], client.servers) voice_chan = discord.utils.find(lambda x: x.name == config['voice_channel'] and x.type is discord.ChannelType.voice, @@ -152,7 +152,7 @@ def connect_voice(client): @coroutine -def list_queued(client, channel): +def list_queued(channel): s = '' count = 0 @@ -167,13 +167,13 @@ def list_queued(client, channel): async(client.send_message(channel, s.strip())) - if client.current_player and not client.current_player.is_done(): - s += '**{}**: {}\n\n'.format('playing' if client.current_player.is_playing() else 'paused', client.current_player.title) + if current_player and not current_player.is_done(): + s += '**{}**: {}\n\n'.format('playing' if current_player.is_playing() else 'paused', current_player.title) pairs = [] while True: try: - pairs.append(client.queue.get_nowait()) + pairs.append(queue.get_nowait()) except QueueEmpty: break @@ -182,13 +182,13 @@ def list_queued(client, channel): list_resp(s, count) return - yield from async(connect_voice(client)) + yield from async(connect_voice()) if not client.is_voice_connected(): async(client.send_message(channel, 'go fuck yourself. couldn\'t check stored videos', tts=True)) logger.error('unable to connect to voice!') for pair in pairs: - yield from client.queue.put(pair) + yield from queue.put(pair) return for (url, msg) in pairs: @@ -202,18 +202,18 @@ def list_queued(client, channel): s += '{}\n'.format(name if name and name != '' else '(Unknown)') count += 1 - yield from client.queue.put((url, msg)) + yield from queue.put((url, msg)) list_resp(s, count) @coroutine -def run_video(client): +def run_video(): vid_logger = logger.getChild('video_scheduler') vid_logger.debug('entering run_video') - (url, _) = yield from client.queue.get() - yield from connect_voice(client) + (url, _) = yield from queue.get() + yield from connect_voice() if not client.is_voice_connected(): raise Exception('unable to connect to voice!') @@ -221,13 +221,13 @@ def run_video(client): vid_logger.info('voice reconnected') vid_logger.debug('starting playback') - client.current_player = yield from client.voice.create_ytdl_player(url) - client.current_player.start() + current_player = yield from client.voice.create_ytdl_player(url) + current_player.start() vid_logger.debug('playback started') has_slept = False while True: - if not client.current_player or client.current_player.is_done(): + if not current_player or current_player.is_done(): break if not has_slept: @@ -239,8 +239,8 @@ def run_video(client): if has_slept: vid_logger.debug('awoken') - async(run_video(client)) + async(run_video()) -async(run_video(client)) +async(run_video()) client.run(config['username'], config['password']) |
