async def awoo_toggle(bot, context): """Toggles awoo detection for either the guild or the given channel.""" guild_awoo_data = data.get( bot, __name__, None, guild_id=context.guild.id, default={}, create=True) # Channel if context.arguments[0]: changes = [] for channel in context.arguments: if channel.id in guild_awoo_data.get('disabled_channels', []): action = 'is now' data.list_data_remove( bot, __name__, 'disabled_channels', value=channel.id, guild_id=context.guild.id) else: action = 'is no longer' data.list_data_append( bot, __name__, 'disabled_channels', channel.id, guild_id=context.guild.id) changes.append('{} {} being monitored.'.format(channel.mention, action)) return Response(content='\n'.join(changes)) # Guild else: guild_awoo_data['enabled'] = not guild_awoo_data.get('enabled', False) return Response(content='Detection is now {}abled'.format( 'en' if guild_awoo_data['enabled'] else 'dis'))
async def awoo_whitelist(bot, context): """(De)whitelists the given user.""" user = context.arguments[0] whitelist = data.get(bot, __name__, 'whitelist', guild_id=context.guild.id, default=[]) # (De)whitelist user if user: if user.id in whitelist: action = 'removed from' data.list_data_remove(bot, __name__, 'whitelist', value=user.id, guild_id=context.guild.id) else: action = 'added to' data.list_data_append(bot, __name__, 'whitelist', user.id, guild_id=context.guild.id) return Response(content="User {} the whitelist.".format(action)) # Show whitelisted users else: if not whitelist: raise CBException("There are no whitelisted users.") users = [(data.get_member(bot, it, attribute='mention') or 'Unknown ({})'.format(it)) for it in whitelist] return Response(embed=discord.Embed(title="Whitelisted users", description=', '.join(users)))
def _check_roles(bot, guild): """Checks/ensures the validity of available self-assignable roles in the guild.""" available_role_ids = data.get(bot, __name__, 'roles', guild_id=guild.id, default=[]) guild_roles = dict((it.id, it) for it in guild.roles) top_role = guild.me.top_role remaining_roles = [] for role_id in available_role_ids: if role_id not in guild_roles or guild_roles[role_id] > top_role: data.list_data_remove(bot, __name__, 'roles', role_id, guild_id=guild.id) else: remaining_roles.append(guild_roles[role_id]) return remaining_roles
async def botowner_wrapper(bot, message, base, blueprint_index, options, arguments, keywords, cleaned_content): response, tts, message_type, extra = ('', False, 0, None) if blueprint_index == 0: # Halt await bot.send_message(message.channel, "Going down...") bot.shutdown() elif blueprint_index == 1: # Restart await bot.send_message(message.channel, "Restarting...") bot.restart() elif blueprint_index == 2: # Reload response = "Reloading..." message_type = 3 extra = ('reload', ) elif blueprint_index == 3: # IP s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 80)) # Thanks Google ip = s.getsockname()[0] s.close() response = "Local IP: " + ip elif blueprint_index == 4: # Backup utilities.make_backup(bot) await bot.send_file(message.channel, '{}/temp/backup1.zip'.format(bot.path)) elif blueprint_index == 5: # Blacklist blacklist = data.get(bot, 'base', 'blacklist', default=[]) if not arguments[0]: response = "Blacklisted entries: {}".format(blacklist) else: user_id = arguments[0] if user_id in blacklist: data.list_data_remove(bot, 'base', 'blacklist', user_id) response = "User removed from blacklist." else: data.list_data_append(bot, 'base', 'blacklist', user_id) response = "User added to blacklist." elif blueprint_index == 6: # Toggle feedback status = data.get(bot, 'base', 'feedbackdisabled', default=False) action = "enabled" if status else "disabled" data.add(bot, 'base', 'feedbackdisabled', not status) response = "Feedback has been {}.".format(action) elif blueprint_index == 7: # Announcement if arguments[0]: text = '{0}:\n{1}'.format(time.strftime('%c'), arguments[0]) data.add(bot, 'base', 'announcement', text) response = "Announcement set!" else: data.add(bot, 'base', 'announcement', '') response = "Announcement cleared!" return (response, tts, message_type, extra)
async def _delete_session(bot, guild): """Deletes the session for the given guild.""" session_data = data.remove(bot, __name__, 'data', guild_id=guild.id, safe=True) if not session_data: raise CBException("Session does not exist.") channel_id, webhook_id = session_data['channel'], session_data['webhook'] channel = data.get_channel(bot, channel_id, safe=True) webhooks = await channel.webhooks() for webhook in webhooks: if webhook.id == webhook_id: await webhook.delete() break else: logger.warn('Webhook to delete (%s) not found!', webhook_id) try: WEBHOOK_SET.remove(webhook_id) except KeyError: logger.warn("Webhook not found in WEBHOOK_SET") data.list_data_remove(bot, __name__, 'webhooks', value=webhook_id, safe=True) if guild.voice_client and guild.voice_client.channel.id == session_data['voice_channel']: await utilities.stop_audio(bot, guild)
async def awoo_whitelist(bot, context): """(De)whitelists the given user.""" user = context.arguments[0] whitelist = data.get(bot, __name__, 'whitelist', guild_id=context.guild.id, default=[]) # (De)whitelist user if user: if user.id in whitelist: action = 'removed from' data.list_data_remove(bot, __name__, 'whitelist', value=user.id, guild_id=context.guild.id) else: action = 'added to' data.list_data_append(bot, __name__, 'whitelist', user.id, guild_id=context.guild.id) return Response(content="User {} the whitelist.".format(action)) # Show whitelisted users else: if not whitelist: raise CBException("There are no whitelisted users.") users = [ (data.get_member(bot, it, attribute='mention') or 'Unknown ({})'.format(it)) for it in whitelist] return Response( embed=discord.Embed(title="Whitelisted users", description=', '.join(users)))
async def awoo_toggle(bot, context): """Toggles awoo detection for either the guild or the given channel.""" guild_awoo_data = data.get(bot, __name__, None, guild_id=context.guild.id, default={}, create=True) # Channel if context.arguments[0]: changes = [] for channel in context.arguments: if channel.id in guild_awoo_data.get('disabled_channels', []): action = 'is now' data.list_data_remove(bot, __name__, 'disabled_channels', value=channel.id, guild_id=context.guild.id) else: action = 'is no longer' data.list_data_append(bot, __name__, 'disabled_channels', channel.id, guild_id=context.guild.id) changes.append('{} {} being monitored.'.format( channel.mention, action)) return Response(content='\n'.join(changes)) # Guild else: guild_awoo_data['enabled'] = not guild_awoo_data.get('enabled', False) return Response(content='Detection is now {}abled'.format( 'en' if guild_awoo_data['enabled'] else 'dis'))
async def owner_wrapper(bot, message, base, blueprint_index, options, arguments, keywords, cleaned_content): response, tts, message_type, extra = ('', False, 0, None) mod_action = '' send_notifications = data.get(bot, 'base', 'notifications', server_id=message.server.id, default=True) if blueprint_index in (0, 1): # Add or remove moderator user = data.get_member(bot, arguments[0], server=message.server) user_is_mod = data.is_mod(bot, message.server, user.id, strict=True) user_is_elevated = data.is_mod(bot, message.server, user.id) blocked = data.is_blocked(bot, message.server, user.id, strict=True) mod_action = 'Added {}' if blueprint_index == 0 else 'Removed {}' mod_action = mod_action.format( '{0} ({0.id}) as a moderator'.format(user)) if blocked: raise BotException(EXCEPTION, "User is blocked.") elif blueprint_index == 0: # add if user_is_mod or user_is_elevated: raise BotException(EXCEPTION, "User is already a moderator.") else: data.list_data_append(bot, 'base', 'moderators', user.id, server_id=message.server.id) response = "User is now a moderator." else: # remove if not user_is_mod: raise BotException(EXCEPTION, "User is not in the moderators list.") else: data.list_data_remove(bot, 'base', 'moderators', user.id, server_id=message.server.id) response = "User is no longer a moderator." elif blueprint_index == 2: # Send feedback if data.get(bot, 'base', 'feedbackdisabled', default=False): response = ("Feedback has been temporarily disabled, probably " "due to some troll spammers.") else: text = arguments[0] if len(text) > 1500: raise BotException( EXCEPTION, "Whoa! That's a lot of feedback. " "1500 characters or fewer, please.") text = ('{0} ({0.id}) on {1.timestamp}:' '\n\t{2}').format(message.author, message, text) await utilities.notify_owners(bot, text, user_id=message.author.id) response = "Message sent to bot owners." elif blueprint_index == 3: # Toggle notifications response = ("Bot moderator activity notifications are now turned " "{}").format("OFF." if send_notifications else "ON.") data.add(bot, 'base', 'notifications', not send_notifications, server_id=message.server.id) # Send notification if configured if mod_action and send_notifications: if message.edited_timestamp: timestamp = message.edited_timestamp else: timestamp = message.timestamp notification = 'From {0.server} on {1}, you:\n\t{2}'.format( message.author, timestamp, mod_action) logs = await utilities.get_log_text(bot, message.channel, limit=20, before=message) logs += '\n{}'.format(utilities.get_formatted_message(message)) await bot.send_message(message.server.owner, notification) await utilities.send_text_as_file(bot, message.server.owner, logs, 'context') return (response, tts, message_type, extra)
async def mod_wrapper(bot, message, base, blueprint_index, options, arguments, keywords, cleaned_content): response, tts, message_type, extra = ('', False, 0, None) mod_action = '' if blueprint_index == 0: # info server_data = data.get(bot, 'base', None, server_id=message.server.id, default={}) disabled_commands = server_data.get('disabled', []) display_list = [] for disabled_command in disabled_commands: display_list.append('{0} ({1})'.format( disabled_command[0], 'all' if disabled_command[1] == -1 else disabled_command[1] + 1)) response = ('```\n' 'Information for server {0}\n' 'ID: {0.id}\n' 'Owner: {0.owner.id}\n' 'Moderators: {1}\n' 'Blocked users: {2}\n' 'Muted: {3}\n' 'Muted channels: {4}\n' 'Command invoker: {5}\n' 'Mention mode: {6}\n' 'Disabled commands: {7}```').format( message.server, server_data.get('moderators', []), server_data.get('blocked', []), server_data.get('muted', []), server_data.get('muted_channels', []), server_data.get('command_invoker', None), server_data.get('mention_mode', False), display_list) elif blueprint_index == 1: # Toggle command try: # Explicit index split_arguments = arguments[0].split() command = bot.commands[split_arguments[0]] guess = [command.base, int(split_arguments[1]) - 1] assert -1 < guess[1] < len(command.blueprints) except IndexError: # No index guess = [command.base, -1] except: # Guess the help index guess = list(parser.guess_index(bot, arguments[0])) if guess[0] is None: raise BotException(EXCEPTION, "Invalid base.") command = bot.commands[guess[0]] if command.plugin is bot.commands['base'].plugin: raise BotException(EXCEPTION, "The base commands cannot be disabled.") pass_in = (bot, 'base', 'disabled', guess) pass_in_keywords = {'server_id': message.server.id} disabled_commands = data.get(*pass_in[:-1], **pass_in_keywords, default=[]) if guess in disabled_commands: data.list_data_remove(*pass_in, **pass_in_keywords) response = "Enabled" else: data.list_data_append(*pass_in, **pass_in_keywords) response = "Disabled" response += " the `{0}` command {1}.".format( guess[0], "and all associated subcommands" if guess[1] == -1 else "(subcommand {})".format(guess[1] + 1)) mod_action = response elif blueprint_index in (2, 3): # Block or unblock user = data.get_member(bot, arguments[0], message.server) block = blueprint_index == 2 mod_action = 'Blocked {}' if block else 'Unblocked {}' mod_action = mod_action.format('{0} ({0.id})'.format(user)) blocked = data.is_blocked(bot, message.server, user.id, strict=True) mod = data.is_mod(bot, message.server, user.id) if mod: raise BotException(EXCEPTION, "Cannot block or unblock a moderator.") elif block: if blocked: raise BotException(EXCEPTION, "User is already blocked.") else: data.list_data_append(bot, 'base', 'blocked', user.id, server_id=message.server.id) response = "User is now blocked." else: if not blocked: raise BotException(EXCEPTION, "User is already unblocked.") else: data.list_data_remove(bot, 'base', 'blocked', user.id, server_id=message.server.id) response = "User is now unblocked." elif blueprint_index == 4: # Clear response = ('' + '\n' * 80 + "The chat was pushed up by a bot moderator.") elif blueprint_index in (5, 6): # Mute or unmute server_id = message.server.id mute = blueprint_index == 5 mod_action = 'Muted {}' if mute else 'Unmuted {}' if arguments[0]: channel = data.get_channel(bot, arguments[0], message.server) muted = channel.id in data.get(bot, 'base', 'muted_channels', server_id=server_id, default=[]) mod_action = mod_action.format(channel.name) if mute: if muted: raise BotException(EXCEPTION, "Channel is already muted.") else: data.list_data_append(bot, 'base', 'muted_channels', channel.id, server_id=server_id) if str(channel.type) == 'voice': # disconnect await utilities.leave_and_stop(bot, message.server) response = "Channel muted." else: # unmute if not muted: raise BotException(EXCEPTION, "Channel is already unmuted.") else: data.list_data_remove(bot, 'base', 'muted_channels', channel.id, server_id=server_id) response = "Channel unmuted." else: # server mod_action = mod_action.format('the server') muted = data.get(bot, 'base', 'muted', server_id=server_id, default=False) if not (muted ^ mute): response = "Server is already {}muted.".format( '' if muted else 'un') raise BotException(EXCEPTION, response) else: data.add(bot, 'base', 'muted', mute, server_id=server_id) response = "Server {}muted.".format('' if mute else 'un') elif blueprint_index == 7: # Invoker if len(arguments[0]) > 10: raise BotException( EXCEPTION, "The invoker can be a maximum of 10 characters long.") data.add(bot, 'base', 'command_invoker', arguments[0] if arguments[0] else None, server_id=message.server.id) response = "Custom command invoker {}.".format( 'set' if arguments[0] else 'cleared') if arguments[0]: response = "Custom command invoker set." mod_action = "Set the server command invoker to '{}'.".format( arguments[0]) else: response = "Custom command invoker cleared." mod_action = "Removed the custom command invoker." elif blueprint_index == 8: # Mention current_mode = data.get(bot, 'base', 'mention_mode', server_id=message.server.id, default=False) data.add(bot, 'base', 'mention_mode', not current_mode, server_id=message.server.id) response = "Mention mode {}activated.".format( 'de' if current_mode else '') mod_action = "{}activated mention mode.".format( 'de' if current_mode else '').capitalize() # Send notification if configured send_notifications = data.get(bot, 'base', 'notifications', server_id=message.server.id, default=True) if mod_action and send_notifications: if message.edited_timestamp: timestamp = message.edited_timestamp else: timestamp = message.timestamp notification = ('Moderator {0} ({0.id}) from {0.server} on {1}:\n\t' '{2}').format(message.author, timestamp, mod_action) logs = await utilities.get_log_text(bot, message.channel, limit=20, before=message) logs += '\n{}'.format(utilities.get_formatted_message(message)) await bot.send_message(message.server.owner, notification) await utilities.send_text_as_file(bot, message.server.owner, logs, 'context') return (response, tts, message_type, extra)