1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-11-01 21:02:38 +00:00

Added try/except blocks to subcommands

This commit is contained in:
Myned 2017-10-02 19:37:58 -04:00
parent fd464d948f
commit c8571d1c7a

View file

@ -27,6 +27,7 @@ import requests
import traceback import traceback
from discord import reaction from discord import reaction
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import errors as errext
from discord import errors as err from discord import errors as err
from cogs import tools from cogs import tools
from misc import checks from misc import checks
@ -212,15 +213,10 @@ class MsG:
async def blacklist(self, ctx): async def blacklist(self, ctx):
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
await ctx.send('❌ **Use a flag to manage blacklists.**\n*Type* `' + ctx.prefix + 'help bl` *for more info.*', delete_after=10) await ctx.send('❌ **Use a flag to manage blacklists.**\n*Type* `' + ctx.prefix + 'help bl` *for more info.*', delete_after=10)
@blacklist.error @blacklist.error
async def blacklist_error(self, ctx, error): async def blacklist_error(self, ctx, error):
if isinstance(error, commands.CheckFailure): if isinstance(error, commands.CheckFailure):
return await ctx.send('❌ **Insufficient permissions.**') return await ctx.send('❌ **Insufficient permissions.**')
if isinstance(error, exc.TagExists):
return await ctx.send('❌ `' + str(exc.TagExists) + '` **already in blacklist.**', delete_after=10)
if isinstance(error, exc.TagError):
return await ctx.send('❌ `' + str(exc.TagError) + '` **not in blacklist.**', delete_after=10)
if isinstance(error, KeyError): if isinstance(error, KeyError):
return await ctx.send('❌ **Blacklist does not exist.**', delete_after=10) return await ctx.send('❌ **Blacklist does not exist.**', delete_after=10)
@ -228,7 +224,6 @@ class MsG:
async def _get_blacklist(self, ctx): async def _get_blacklist(self, ctx):
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
await ctx.send('❌ **Invalid blacklist.**') await ctx.send('❌ **Invalid blacklist.**')
@_get_blacklist.command(name='global', aliases=['gl', 'g']) @_get_blacklist.command(name='global', aliases=['gl', 'g'])
async def __get_global_blacklist(self, ctx): async def __get_global_blacklist(self, ctx):
global blacklists global blacklists
@ -260,7 +255,6 @@ class MsG:
async def __get_all_blacklists(self, ctx): async def __get_all_blacklists(self, ctx):
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
await ctx.send('❌ **Invalid blacklist.**') await ctx.send('❌ **Invalid blacklist.**')
@__get_all_blacklists.command(name='guild', aliases=['g']) @__get_all_blacklists.command(name='guild', aliases=['g'])
@commands.has_permissions(manage_channels=True) @commands.has_permissions(manage_channels=True)
async def ___get_all_guild_blacklists(self, ctx): async def ___get_all_guild_blacklists(self, ctx):
@ -280,24 +274,26 @@ class MsG:
async def _add_tags(self, ctx): async def _add_tags(self, ctx):
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
await ctx.send('❌ **Invalid blacklist.**') await ctx.send('❌ **Invalid blacklist.**')
@_add_tags.command(name='global', aliases=['gl', 'g']) @_add_tags.command(name='global', aliases=['gl', 'g'])
@commands.is_owner() @commands.is_owner()
async def __add_global_tags(self, ctx, *tags): async def __add_global_tags(self, ctx, *tags):
global blacklists, aliases, headers global blacklists, aliases, headers
for tag in tags: try:
if tag in blacklists['global_blacklist']: for tag in tags:
raise exc.TagExists(tag) if tag in blacklists['global_blacklist']:
blacklists['global_blacklist'].extend(tags) raise exc.TagExists(tag)
for tag in tags: blacklists['global_blacklist'].extend(tags)
alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() for tag in tags:
for dic in alias_request: alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json()
aliases['global_blacklist'].setdefault(tag, []).append(dic['name']) for dic in alias_request:
with open('blacklists.json', 'w') as outfile: aliases['global_blacklist'].setdefault(tag, []).append(dic['name'])
json.dump(blacklists, outfile, indent=4, sort_keys=True) with open('blacklists.json', 'w') as outfile:
with open('aliases.json', 'w') as outfile: json.dump(blacklists, outfile, indent=4, sort_keys=True)
json.dump(aliases, outfile, indent=4, sort_keys=True) with open('aliases.json', 'w') as outfile:
await ctx.send('✅ **Added to global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) json.dump(aliases, outfile, indent=4, sort_keys=True)
await ctx.send('✅ **Added to global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5)
except exc.TagExists as e:
await ctx.send('❌ `' + str(e) + '` **already in blacklist.**', delete_after=10)
@_add_tags.command(name='channel', aliases=['ch', 'c']) @_add_tags.command(name='channel', aliases=['ch', 'c'])
@commands.has_permissions(manage_channels=True) @commands.has_permissions(manage_channels=True)
async def __add_channel_tags(self, ctx, *tags): async def __add_channel_tags(self, ctx, *tags):
@ -307,57 +303,65 @@ class MsG:
else: else:
guild = ctx.message.channel guild = ctx.message.channel
channel = ctx.message.channel channel = ctx.message.channel
for tag in tags: try:
if tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []): for tag in tags:
raise exc.TagExists(tag) if tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []):
blacklists['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), []).extend(tags) raise exc.TagExists(tag)
for tag in tags: blacklists['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), []).extend(tags)
alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() for tag in tags:
for dic in alias_request: alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json()
aliases['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), {}).setdefault(tag, []).append(dic['name']) for dic in alias_request:
with open('blacklists.json', 'w') as outfile: aliases['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), {}).setdefault(tag, []).append(dic['name'])
json.dump(blacklists, outfile, indent=4, sort_keys=True) with open('blacklists.json', 'w') as outfile:
with open('aliases.json', 'w') as outfile: json.dump(blacklists, outfile, indent=4, sort_keys=True)
json.dump(aliases, outfile, indent=4, sort_keys=True) with open('aliases.json', 'w') as outfile:
await ctx.send('✅ **Added to** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) json.dump(aliases, outfile, indent=4, sort_keys=True)
await ctx.send('✅ **Added to** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5)
except exc.TagExists as e:
await ctx.send('❌ `' + str(e) + '` **already in blacklist.**', delete_after=10)
@_add_tags.command(name='me', aliases=['m']) @_add_tags.command(name='me', aliases=['m'])
async def __add_user_tags(self, ctx, *tags): async def __add_user_tags(self, ctx, *tags):
global blacklists, aliases, headers global blacklists, aliases, headers
user = ctx.message.author user = ctx.message.author
for tag in tags: try:
if tag in blacklists['user_blacklist'].get(str(user.id), []): for tag in tags:
raise exc.TagExists(tag) if tag in blacklists['user_blacklist'].get(str(user.id), []):
blacklists['user_blacklist'].setdefault(str(user.id), []).extend(tags) raise exc.TagExists(tag)
for tag in tags: blacklists['user_blacklist'].setdefault(str(user.id), []).extend(tags)
alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() for tag in tags:
for dic in alias_request: alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json()
aliases['user_blacklist'].setdefault(str(user.id), {}).setdefault(tag, []).append(dic['name']) for dic in alias_request:
with open('blacklists.json', 'w') as outfile: aliases['user_blacklist'].setdefault(str(user.id), {}).setdefault(tag, []).append(dic['name'])
json.dump(blacklists, outfile, indent=4, sort_keys=True) with open('blacklists.json', 'w') as outfile:
with open('aliases.json', 'w') as outfile: json.dump(blacklists, outfile, indent=4, sort_keys=True)
json.dump(aliases, outfile, indent=4, sort_keys=True) with open('aliases.json', 'w') as outfile:
await ctx.send('' + user.mention + ' **added:**\n```' + formatter.tostring(tags) + '```', delete_after=5) json.dump(aliases, outfile, indent=4, sort_keys=True)
await ctx.send('' + user.mention + ' **added:**\n```' + formatter.tostring(tags) + '```', delete_after=5)
except exc.TagExists as e:
await ctx.send('❌ `' + str(e) + '` **already in blacklist.**', delete_after=10)
@blacklist.group(name='remove', aliases=['rm', 'r']) @blacklist.group(name='remove', aliases=['rm', 'r'])
async def _remove_tags(self, ctx): async def _remove_tags(self, ctx):
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
await ctx.send('❌ **Invalid blacklist.**') await ctx.send('❌ **Invalid blacklist.**')
@_remove_tags.command(name='global', aliases=['gl', 'g']) @_remove_tags.command(name='global', aliases=['gl', 'g'])
@commands.is_owner() @commands.is_owner()
async def __remove_global_tags(self, ctx, *tags): async def __remove_global_tags(self, ctx, *tags):
global blacklists, aliases global blacklists, aliases
for tag in tags: try:
if tag in blacklists['global_blacklist']: for tag in tags:
blacklists['global_blacklist'].remove(tag) if tag in blacklists['global_blacklist']:
del aliases['global_blacklist'][tag] blacklists['global_blacklist'].remove(tag)
else: del aliases['global_blacklist'][tag]
raise exc.TagError(tag) else:
with open('blacklists.json', 'w') as outfile: raise exc.TagError(tag)
json.dump(blacklists, outfile, indent=4, sort_keys=True) with open('blacklists.json', 'w') as outfile:
with open('aliases.json', 'w') as outfile: json.dump(blacklists, outfile, indent=4, sort_keys=True)
json.dump(aliases, outfile, indent=4, sort_keys=True) with open('aliases.json', 'w') as outfile:
await ctx.send('✅ **Removed from global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) json.dump(aliases, outfile, indent=4, sort_keys=True)
await ctx.send('✅ **Removed from global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5)
except exc.TagError as e:
await ctx.send('❌ `' + str(e) + '` **not in blacklist.**', delete_after=10)
@_remove_tags.command(name='channel', aliases=['ch', 'c']) @_remove_tags.command(name='channel', aliases=['ch', 'c'])
@commands.has_permissions(manage_channels=True) @commands.has_permissions(manage_channels=True)
async def __remove_channel_tags(self, ctx, *tags): async def __remove_channel_tags(self, ctx, *tags):
@ -367,38 +371,43 @@ class MsG:
else: else:
guild = ctx.message.channel guild = ctx.message.channel
channel = ctx.message.channel channel = ctx.message.channel
for tag in tags: try:
if tag in blacklists['guild_blacklist'][str(guild.id)][str(channel.id)]: for tag in tags:
blacklists['guild_blacklist'][str(guild.id)][str(channel.id)].remove(tag) if tag in blacklists['guild_blacklist'][str(guild.id)][str(channel.id)]:
del aliases['guild_blacklist'][str(guild.id)][str(channel.id)][tag] blacklists['guild_blacklist'][str(guild.id)][str(channel.id)].remove(tag)
else: del aliases['guild_blacklist'][str(guild.id)][str(channel.id)][tag]
raise exc.TagError(tag) else:
with open('blacklists.json', 'w') as outfile: raise exc.TagError(tag)
json.dump(blacklists, outfile, indent=4, sort_keys=True) with open('blacklists.json', 'w') as outfile:
with open('aliases.json', 'w') as outfile: json.dump(blacklists, outfile, indent=4, sort_keys=True)
json.dump(aliases, outfile, indent=4, sort_keys=True) with open('aliases.json', 'w') as outfile:
await ctx.send('✅ **Removed from** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) json.dump(aliases, outfile, indent=4, sort_keys=True)
await ctx.send('✅ **Removed from** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5)
except exc.TagError as e:
await ctx.send('❌ `' + str(e) + '` **not in blacklist.**', delete_after=10)
@_remove_tags.command(name='me', aliases=['m']) @_remove_tags.command(name='me', aliases=['m'])
async def __remove_user_tags(self, ctx, *tags): async def __remove_user_tags(self, ctx, *tags):
global blacklists, aliases global blacklists, aliases
user = ctx.message.author user = ctx.message.author
for tag in tags: try:
if tag in blacklists['user_blacklist'][str(user.id)]: for tag in tags:
blacklists['user_blacklist'][str(user.id)].remove(tag) if tag in blacklists['user_blacklist'][str(user.id)]:
del aliases['user_blacklist'][str(user.id)][tag] blacklists['user_blacklist'][str(user.id)].remove(tag)
else: del aliases['user_blacklist'][str(user.id)][tag]
raise exc.TagError(tag) else:
with open('blacklists.json', 'w') as outfile: raise exc.TagError(tag)
json.dump(blacklists, outfile, indent=4, sort_keys=True) with open('blacklists.json', 'w') as outfile:
with open('aliases.json', 'w') as outfile: json.dump(blacklists, outfile, indent=4, sort_keys=True)
json.dump(aliases, outfile, indent=4, sort_keys=True) with open('aliases.json', 'w') as outfile:
await ctx.send('' + user.mention + ' **removed:**\n```' + formatter.tostring(tags) + '```', delete_after=5) json.dump(aliases, outfile, indent=4, sort_keys=True)
await ctx.send('' + user.mention + ' **removed:**\n```' + formatter.tostring(tags) + '```', delete_after=5)
except exc.TagError as e:
await ctx.send('❌ `' + str(e) + '` **not in blacklist.**', delete_after=10)
@blacklist.group(name='clear', aliases=['cl', 'c']) @blacklist.group(name='clear', aliases=['cl', 'c'])
async def _clear_blacklist(self, ctx): async def _clear_blacklist(self, ctx):
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
await ctx.send('❌ **Invalid blacklist.**') await ctx.send('❌ **Invalid blacklist.**')
@_clear_blacklist.command(name='global', aliases=['gl', 'g']) @_clear_blacklist.command(name='global', aliases=['gl', 'g'])
@commands.is_owner() @commands.is_owner()
async def __clear_global_blacklist(self, ctx): async def __clear_global_blacklist(self, ctx):