(core): Improve permission error handling.
Add channel permission wards to `LionModule` pre-command hook. Improve `Forbidden` handling in `embed_reply` and `error_reply` addons.
This commit is contained in:
@@ -85,6 +85,16 @@ class LionModule(Module):
|
|||||||
if ctx.author.id in ctx.client.objects['ignored_members'][ctx.guild.id]:
|
if ctx.author.id in ctx.client.objects['ignored_members'][ctx.guild.id]:
|
||||||
raise SafeCancellation
|
raise SafeCancellation
|
||||||
|
|
||||||
|
# Check channel permissions are sane
|
||||||
|
if not ctx.ch.permissions_for(ctx.guild.me).send_messages:
|
||||||
|
raise SafeCancellation
|
||||||
|
if not ctx.ch.permissions_for(ctx.guild.me).embed_links:
|
||||||
|
await ctx.reply("I need permission to send embeds in this channel before I can run any commands!")
|
||||||
|
raise SafeCancellation
|
||||||
|
|
||||||
|
# Start typing
|
||||||
|
await ctx.ch.trigger_typing()
|
||||||
|
|
||||||
async def on_exception(self, ctx, exception):
|
async def on_exception(self, ctx, exception):
|
||||||
try:
|
try:
|
||||||
raise exception
|
raise exception
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import discord
|
import discord
|
||||||
from cmdClient import Context
|
from cmdClient import Context
|
||||||
|
from cmdClient.lib import SafeCancellation
|
||||||
|
|
||||||
from data import tables
|
from data import tables
|
||||||
from core import Lion
|
from core import Lion
|
||||||
@@ -17,9 +18,11 @@ async def embed_reply(ctx, desc, colour=discord.Colour.orange(), **kwargs):
|
|||||||
"""
|
"""
|
||||||
embed = discord.Embed(description=desc, colour=colour, **kwargs)
|
embed = discord.Embed(description=desc, colour=colour, **kwargs)
|
||||||
try:
|
try:
|
||||||
return await ctx.reply(embed=embed, reference=ctx.msg)
|
return await ctx.reply(embed=embed, reference=ctx.msg.to_reference(fail_if_not_exists=False))
|
||||||
except discord.NotFound:
|
except discord.Forbidden:
|
||||||
return await ctx.reply(embed=embed)
|
if not ctx.guild or ctx.ch.permissions_for(ctx.guild.me).send_mssages:
|
||||||
|
await ctx.reply("Command failed, I don't have permission to send embeds in this channel!")
|
||||||
|
raise SafeCancellation
|
||||||
|
|
||||||
|
|
||||||
@Context.util
|
@Context.util
|
||||||
@@ -34,15 +37,17 @@ async def error_reply(ctx, error_str, **kwargs):
|
|||||||
)
|
)
|
||||||
message = None
|
message = None
|
||||||
try:
|
try:
|
||||||
message = await ctx.ch.send(embed=embed, reference=ctx.msg, **kwargs)
|
message = await ctx.ch.send(
|
||||||
except discord.NotFound:
|
embed=embed,
|
||||||
message = await ctx.ch.send(embed=embed, **kwargs)
|
reference=ctx.msg.to_reference(fail_if_not_exists=False),
|
||||||
except discord.Forbidden:
|
**kwargs
|
||||||
message = await ctx.reply(error_str)
|
)
|
||||||
finally:
|
|
||||||
if message:
|
|
||||||
ctx.sent_messages.append(message)
|
ctx.sent_messages.append(message)
|
||||||
return message
|
return message
|
||||||
|
except discord.Forbidden:
|
||||||
|
if not ctx.guild or ctx.ch.permissions_for(ctx.guild.me).send_mssages:
|
||||||
|
await ctx.reply("Command failed, I don't have permission to send embeds in this channel!")
|
||||||
|
raise SafeCancellation
|
||||||
|
|
||||||
|
|
||||||
@Context.util
|
@Context.util
|
||||||
|
|||||||
Reference in New Issue
Block a user