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,11 +274,11 @@ 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
try:
for tag in tags: for tag in tags:
if tag in blacklists['global_blacklist']: if tag in blacklists['global_blacklist']:
raise exc.TagExists(tag) raise exc.TagExists(tag)
@ -298,6 +292,8 @@ class MsG:
with open('aliases.json', 'w') as outfile: with open('aliases.json', 'w') as outfile:
json.dump(aliases, outfile, indent=4, sort_keys=True) json.dump(aliases, outfile, indent=4, sort_keys=True)
await ctx.send('✅ **Added to global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) 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,6 +303,7 @@ class MsG:
else: else:
guild = ctx.message.channel guild = ctx.message.channel
channel = ctx.message.channel channel = ctx.message.channel
try:
for tag in tags: for tag in tags:
if tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []): if tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []):
raise exc.TagExists(tag) raise exc.TagExists(tag)
@ -320,10 +317,13 @@ class MsG:
with open('aliases.json', 'w') as outfile: with open('aliases.json', 'w') as outfile:
json.dump(aliases, outfile, indent=4, sort_keys=True) 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) 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
try:
for tag in tags: for tag in tags:
if tag in blacklists['user_blacklist'].get(str(user.id), []): if tag in blacklists['user_blacklist'].get(str(user.id), []):
raise exc.TagExists(tag) raise exc.TagExists(tag)
@ -337,16 +337,18 @@ class MsG:
with open('aliases.json', 'w') as outfile: with open('aliases.json', 'w') as outfile:
json.dump(aliases, outfile, indent=4, sort_keys=True) json.dump(aliases, outfile, indent=4, sort_keys=True)
await ctx.send('' + user.mention + ' **added:**\n```' + formatter.tostring(tags) + '```', delete_after=5) 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
try:
for tag in tags: for tag in tags:
if tag in blacklists['global_blacklist']: if tag in blacklists['global_blacklist']:
blacklists['global_blacklist'].remove(tag) blacklists['global_blacklist'].remove(tag)
@ -358,6 +360,8 @@ class MsG:
with open('aliases.json', 'w') as outfile: with open('aliases.json', 'w') as outfile:
json.dump(aliases, outfile, indent=4, sort_keys=True) json.dump(aliases, outfile, indent=4, sort_keys=True)
await ctx.send('✅ **Removed from global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) 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,6 +371,7 @@ class MsG:
else: else:
guild = ctx.message.channel guild = ctx.message.channel
channel = ctx.message.channel channel = ctx.message.channel
try:
for tag in tags: for tag in tags:
if tag in blacklists['guild_blacklist'][str(guild.id)][str(channel.id)]: if tag in blacklists['guild_blacklist'][str(guild.id)][str(channel.id)]:
blacklists['guild_blacklist'][str(guild.id)][str(channel.id)].remove(tag) blacklists['guild_blacklist'][str(guild.id)][str(channel.id)].remove(tag)
@ -378,10 +383,13 @@ class MsG:
with open('aliases.json', 'w') as outfile: with open('aliases.json', 'w') as outfile:
json.dump(aliases, outfile, indent=4, sort_keys=True) 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) 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
try:
for tag in tags: for tag in tags:
if tag in blacklists['user_blacklist'][str(user.id)]: if tag in blacklists['user_blacklist'][str(user.id)]:
blacklists['user_blacklist'][str(user.id)].remove(tag) blacklists['user_blacklist'][str(user.id)].remove(tag)
@ -393,12 +401,13 @@ class MsG:
with open('aliases.json', 'w') as outfile: with open('aliases.json', 'w') as outfile:
json.dump(aliases, outfile, indent=4, sort_keys=True) json.dump(aliases, outfile, indent=4, sort_keys=True)
await ctx.send('' + user.mention + ' **removed:**\n```' + formatter.tostring(tags) + '```', delete_after=5) 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):