Browse Source

added standardised strings and new error ux to reddit cog. Also changed wordings and standardists error handling strings.

tags/v2.0.0
Roxie Gibson 5 years ago
parent
commit
6e1e6d3668
2 changed files with 30 additions and 14 deletions
  1. +8
    -3
      roxbot/cogs/reddit.py
  2. +22
    -11
      roxbot/err_handle.py

+ 8
- 3
roxbot/cogs/reddit.py View File

@@ -169,6 +169,11 @@ class Scrapper:


class Reddit:

SUB_NOT_FOUND = "Error ;-; That subreddit probably doesn't exist. Please check your spelling"
NO_IMAGES = "I couldn't find any images/videos from that subreddit."
NSFW_FAIL = "This channel isn't marked NSFW and therefore I can't post NSFW content. The subreddit given or all posts found are NSFW."

def __init__(self, bot_client):
self.bot = bot_client
self.scrapper = Scrapper()
@@ -192,7 +197,7 @@ class Reddit:
posts = await self.scrapper.sub_request(subreddit)

if not posts:
return await ctx.send("Error ;-; That subreddit probably doesn't exist. Please check your spelling")
raise roxbot.UserError(self.SUB_NOT_FOUND)

if isinstance(ctx.channel, discord.TextChannel):
nsfw_allowed = ctx.channel.is_nsfw()
@@ -202,9 +207,9 @@ class Reddit:
choice = await self.scrapper.random(posts["children"], cache_id, nsfw_allowed)

if not choice:
return await ctx.send("I couldn't find any images from that subreddit.")
raise commands.CommandError(self.NO_IMAGES)
elif choice.get("success", True) is False:
return await ctx.send("This channel isn't marked NSFW. The subreddit given or all posts found are NSFW.")
raise roxbot.UserError(self.NSFW_FAIL)

title = "**{}** \nby /u/{} from /r/{}\n".format(unescape(choice["title"]), unescape(choice["author"]), subreddit)
url = str(choice["url"])

+ 22
- 11
roxbot/err_handle.py View File

@@ -38,6 +38,17 @@ from roxbot import guild_settings


class ErrHandle:

COMMANDNOTFOUND = "That Command doesn't exist."
COMMANDONCOOLDOWN = "This command is on cooldown, please wait {:.2f} seconds before trying again."
CHECKFAILURE = "You do not have permission to do this. Back off, thot!"
TOOMANYARGS = "Too many arguments given."
DISABLEDCOMMAND = "This command is disabled."
COGSETTINGDISABLED = "{} is disabled on this server."
NODMS = "This command cannot be used in private messages."

YTDLDOWNLOADERROR = "Video could not be downloaded: {}"

def __init__(self, bot_client):
self.bot = bot_client
self.dev = roxbot.dev_mode
@@ -59,17 +70,17 @@ class ErrHandle:
if isinstance(error, (commands.MissingRequiredArgument, commands.BadArgument, roxbot.UserError)):
embed.description = error.args[0]
elif isinstance(error, commands.TooManyArguments):
embed.description = "Too many arguments given."
embed.description = self.TOOMANYARGS
return await ctx.send(embed=embed)

# ActualErrorHandling
embed = discord.Embed()
if isinstance(error, commands.NoPrivateMessage):
embed.description = "This command cannot be used in private messages."
embed.description = self.NODMS
elif isinstance(error, commands.DisabledCommand):
embed.description = "This command is disabled."
embed.description = self.DISABLEDCOMMAND
elif isinstance(error, roxbot.CogSettingDisabled):
embed.description = "The following is not enabled on this server: {}".format(error.args[0])
embed.description = self.COGSETTINGDISABLED.format(error.args[0])
elif isinstance(error, commands.CommandNotFound):
try:
# Sadly this is the only part that makes a cog not modular. I have tried my best though to make it usable without the cog.
@@ -80,25 +91,25 @@ class ErrHandle:
if is_custom_command or is_emoticon_face or is_too_short:
embed = None
else:
embed.description = "That Command doesn't exist."
embed.description = self.COMMANDNOTFOUND
except (KeyError, AttributeError):
# KeyError for cog missing, AttributeError if a command invoked via DM
embed.description = "That Command doesn't exist."
embed.description = self.COMMANDNOTFOUND
elif isinstance(error, commands.BotMissingPermissions):
embed.description = "{}".format(error.args[0].replace("Bot", "Roxbot"))
elif isinstance(error, commands.MissingPermissions):
embed.description = "{}".format(error.args[0])
elif isinstance(error, commands.CommandOnCooldown):
embed.description = "This command is on cooldown, please wait {:.2f} seconds before trying again.".format(error.retry_after)
embed.description = self.COMMANDONCOOLDOWN.format(error.retry_after)
elif isinstance(error, (commands.CheckFailure, commands.NotOwner)):
embed.description = "You do not have permission to do this. Back off, thot!"
embed.description = self.CHECKFAILURE

elif isinstance(error, commands.CommandInvokeError):
# YOUTUBE_DL ERROR HANDLING
if isinstance(error.original, youtube_dl.utils.GeoRestrictedError):
embed.description = "Video is GeoRestricted. Cannot download."
embed.description = self.YTDLDOWNLOADERROR.format("Video is GeoRestricted.")
elif isinstance(error.original, youtube_dl.utils.DownloadError):
embed.description = "Video could not be downloaded: {}".format(error.original.exc_info[1])
embed.description = self.YTDLDOWNLOADERROR.format(error.original.exc_info[1])

# Final catches for errors undocumented.
else:
@@ -109,7 +120,7 @@ class ErrHandle:
embed.add_field(name='Message', value=ctx.message.content)
embed.timestamp = datetime.datetime.utcnow()
elif isinstance(error, commands.CommandError):
embed.description = "{}".format(error.args[0])
embed.description = "Error: {}".format(error.args[0])
else:
raise error
if embed:

Loading…
Cancel
Save