Error handling, API caching (thanks @GlitterButts)

This commit is contained in:
Zeva Rose 2021-06-30 13:07:23 -06:00
parent 81b1b705f6
commit c8ee10b24c
5 changed files with 34 additions and 28 deletions

View file

@ -17,7 +17,7 @@ class AdminCog(commands.Cog):
self.db = DBManager(config.mongo)
@commands.command(name="ban")
@commands.has_permissions(administrator=True)
@commands.has_permissions(ban_members=True)
async def _ban(self, ctx, user: User = None, reason=None):
if not user or user == ctx.message.author:
await ctx.send("You cannot ban yourself.")
@ -36,7 +36,7 @@ class AdminCog(commands.Cog):
)
@commands.command(name="kick")
@commands.has_permissions(administrator=True)
@commands.has_permissions(kick_members=True)
async def _kick(self, ctx, user: User = None, reason=None):
if not user or user == ctx.message.author:
await ctx.send("You cannot kick yourself.")
@ -50,15 +50,10 @@ class AdminCog(commands.Cog):
)
await ctx.guild.kick(user, reason=reason)
await ctx.send(
f"{user.name} has been banned from {guild_name}."
f"{user.name} has been kicked from {guild_name}."
+ f"Reason:\n{reason}"
)
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.errors.MissingPermissions):
await ctx.send("I'm afraid I can't let you do that.")
def setup(bot):
bot.add_cog(AdminCog(bot))

View file

@ -22,6 +22,7 @@ class DbrandCog(commands.Cog):
self._session = aiohttp.ClientSession()
self._session.headers.update({"Content-Type": "application/json"})
self.api_url = get_config().urls["dbrand_shipping"]
self.cache = {}
@commands.group(name="db", aliases=["dbrand"], pass_context=True)
async def _db(self, ctx):
@ -72,16 +73,16 @@ class DbrandCog(commands.Cog):
if len(matches) > 0:
search = matches[0]
dest = search
if not self.cache.contains(dest):
api_link = self.api_url + dest
try:
data = await self._session.get(api_link)
except Exception as e:
print(e)
fields = None
if 200 <= data.status < 400:
data = await data.json()
else:
data = None
self.cache.update(dest)
data = self.cache.get(dest)
fields = None
if (
data is not None
and data["is_valid"]

View file

@ -300,13 +300,6 @@ class DevCog(commands.Cog):
del args, code
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.errors.MissingPermissions):
await ctx.send("I'm afraid I can't let you do that.")
else:
await ctx.send(f"Error processing command:\n```{error}```")
def setup(bot):
bot.add_cog(DevCog(bot))

22
jarvis/cogs/error.py Normal file
View file

@ -0,0 +1,22 @@
import jarvis
from discord.ext import commands
class ErrorHandlerCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.errors.MissingPermissions):
await ctx.send("I'm afraid I can't let you do that.")
elif isinstance(error, commands.errors.CommandNotFound):
await ctx.send(
"Command does not exist. Run `>help` to get a list of commands"
)
else:
await ctx.send(f"Error processing command:\n```{error}```")
def setup(bot):
bot.add_cog(ErrorHandlerCog(bot))

View file

@ -150,11 +150,6 @@ class OwnerCog(commands.Cog):
reload_config()
await ctx.send("System refreshed")
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.errors.MissingPermissions):
await ctx.send("I'm afraid I can't let you do that.")
def setup(bot):
bot.add_cog(OwnerCog(bot))