async def play_sound_tag(server_id, voice_channel_id, sound_tag_name, user_id): """Plays the sound from the given sound tag if it is available.""" try: if servermanager.is_muted(server_id, voice_channel_id): raise bot_exception(EXCEPT_TYPE, "The bot is muted in this voice channel") except KeyError: raise bot_exception(EXCEPT_TYPE, "You are not in a voice channel (are you perhaps on a different server?)") sound_tag_data = get_sound_tag_data(server_id, sound_tag_name) check_sound_tag_access(server_id, sound_tag_data, user_id, need_owner=False) update_sound_tag(server_id, sound_tag_name, increment_hits=True) # Increment hits from jshbot.botmanager import client from jshbot.botmanager import voice_player global timeout_goal channel = botmanager.get_voice_channel(server_id, voice_channel_id) if client.voice == None or client.voice.channel != channel or not client.voice.is_connected(): # Connect to channel if client.voice: # Disconnect from old channel await client.voice.disconnect() client.voice = await client.join_voice_channel(channel) voice_player.server_id = server_id if voice_player.player is not None and voice_player.player.is_playing(): # Stop if playing voice_player.player.stop() if sound_tag_data['type'] == 'YouTube': # To prevent playlist downloads # 'noplaylist': True voice_player.player = await client.voice.create_ytdl_player(sound_tag_data['url']) else: # Direct download (or stream? Not sure) try: # One day, I will figure out how to stream this crap. But today is not that day. #response = urllib.request.urlopen(sound_tag_data['url']) #voice_player.player = client.voice.create_ffmpeg_player(response, use_avconv=True) urllib.request.urlretrieve (sound_tag_data['url'], '{}/tempsound'.format(configmanager.data_directory)) voice_player.player = client.voice.create_ffmpeg_player('{}/tempsound'.format(configmanager.data_directory)) except: raise bot_exception(EXCEPT_TYPE, "An error occurred when downloading the sound file") voice_player.player.start() timeout = configmanager.config['voice_timeout'] if timeout >= 0: timeout_reset_lock.acquire() timeout_goal = time.time() + ((timeout*60) if timeout > 0 else (sound_tag_data['length']+1)) timeout_reset_lock.release() await timeout_disconnect()
async def get_response(message, send_typing=True): """Gets a response. Split up so messages can be edited.""" # Check if the message is a valid command and the server or channel is not muted if (message.content and message.content[0] in configmanager.config['command_invokers'] and message.author.id != client.user.id): # Valid invoker from not the bot is_private = (type(message.channel) is discord.PrivateChannel) if parser.is_command(message.content.partition(' ')[0][1:].lower(), private=is_private): # Valid command if is_private or ( not is_private and # Private - skip checks. Not private - do server checks. not servermanager.is_banned(server_id=message.server.id, user_id=message.author.id) and # Author is not banned ( not servermanager.is_muted(server_id=message.server.id, channel_id=message.channel.id) or # Server or channel is not muted or (servermanager.is_admin(message.server.id, message.author.id) and message.content[1:].startswith('admin'))) ): # Admin is unmuting bot try: global last_responses_dictionary if send_typing: await client.send_typing(message.channel) return await parser.parse( message.content, message.server.id if not is_private else '0', message.channel.id if not is_private else '0', message.author.id if not is_private else '0', message.author.voice_channel.id if (not is_private and message.author.voice_channel) else 0, # Previously type None is_private) except bot_exception as e: # Something bad happened return [str(e), False] return ['', False]
async def get_response(message, send_typing=True): """Gets a response. Split up so messages can be edited.""" # Check if the message is a valid command and the server or channel is not muted if (message.content and message.content[0] in configmanager.config['command_invokers'] and message.author.id != client.user.id): # Valid invoker from not the bot is_private = (type(message.channel) is discord.PrivateChannel) if parser.is_command(message.content.partition(' ')[0][1:].lower(), private=is_private): # Valid command if is_private or (not is_private and # Private - skip checks. Not private - do server checks. not servermanager.is_banned(server_id=message.server.id, user_id=message.author.id) and # Author is not banned (not servermanager.is_muted(server_id=message.server.id, channel_id=message.channel.id) or # Server or channel is not muted or (servermanager.is_admin(message.server.id, message.author.id) and message.content[1:].startswith('admin')))): # Admin is unmuting bot try: global last_responses_dictionary if send_typing: await client.send_typing(message.channel) return await parser.parse( message.content, message.server.id if not is_private else '0', message.channel.id if not is_private else '0', message.author.id if not is_private else '0', message.author.voice_channel.id if (not is_private and message.author.voice_channel) else 0, # Previously type None is_private) except bot_exception as e: # Something bad happened return [str(e), False] return ['', False]
async def play_sound_tag(server_id, voice_channel_id, sound_tag_name, user_id): """Plays the sound from the given sound tag if it is available.""" try: if servermanager.is_muted(server_id, voice_channel_id): raise bot_exception(EXCEPT_TYPE, "The bot is muted in this voice channel") except KeyError: raise bot_exception( EXCEPT_TYPE, "You are not in a voice channel (are you perhaps on a different server?)" ) sound_tag_data = get_sound_tag_data(server_id, sound_tag_name) check_sound_tag_access(server_id, sound_tag_data, user_id, need_owner=False) update_sound_tag(server_id, sound_tag_name, increment_hits=True) # Increment hits from jshbot.botmanager import client from jshbot.botmanager import voice_player global timeout_goal channel = botmanager.get_voice_channel(server_id, voice_channel_id) if client.voice == None or client.voice.channel != channel or not client.voice.is_connected( ): # Connect to channel if client.voice: # Disconnect from old channel await client.voice.disconnect() client.voice = await client.join_voice_channel(channel) voice_player.server_id = server_id if voice_player.player is not None and voice_player.player.is_playing( ): # Stop if playing voice_player.player.stop() if sound_tag_data['type'] == 'YouTube': # To prevent playlist downloads # 'noplaylist': True voice_player.player = await client.voice.create_ytdl_player( sound_tag_data['url']) else: # Direct download (or stream? Not sure) try: # One day, I will figure out how to stream this crap. But today is not that day. #response = urllib.request.urlopen(sound_tag_data['url']) #voice_player.player = client.voice.create_ffmpeg_player(response, use_avconv=True) urllib.request.urlretrieve( sound_tag_data['url'], '{}/tempsound'.format(configmanager.data_directory)) voice_player.player = client.voice.create_ffmpeg_player( '{}/tempsound'.format(configmanager.data_directory)) except: raise bot_exception( EXCEPT_TYPE, "An error occurred when downloading the sound file") voice_player.player.start() timeout = configmanager.config['voice_timeout'] if timeout >= 0: timeout_reset_lock.acquire() timeout_goal = time.time() + ((timeout * 60) if timeout > 0 else (sound_tag_data['length'] + 1)) timeout_reset_lock.release() await timeout_disconnect()