Skip to content

Commit

Permalink
Change the black line length to default 88
Browse files Browse the repository at this point in the history
  • Loading branch information
joinemm committed Jul 26, 2023
1 parent d4888a3 commit 4bffa1d
Show file tree
Hide file tree
Showing 33 changed files with 1,204 additions and 408 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
repos:
- repo: https://github.com/ambv/black
rev: 23.3.0
rev: 23.7.0
hooks:
- id: black
args: [--line-length=100]
args: [--line-length=88]
language_version: python3

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.270
rev: v0.0.280
hooks:
- id: ruff
args: [--line-length=100, --ignore=E501]
Expand Down
155 changes: 117 additions & 38 deletions cogs/configuration.py

Large diffs are not rendered by default.

57 changes: 42 additions & 15 deletions cogs/customcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ async def custom_command_list(self, guild_id, match=""):
"SELECT command_trigger FROM custom_command WHERE guild_id = %s", guild_id
)
return {
command_trigger for command_trigger in data if match == "" or match in command_trigger
command_trigger
for command_trigger in data
if match == "" or match in command_trigger
}

@commands.Cog.listener()
Expand Down Expand Up @@ -107,14 +109,19 @@ async def add(self, ctx: commands.Context, name, *, response):
if ctx.guild is None:
raise exceptions.CommandError("Unable to get current guild")

if not ctx.author.guild_permissions.manage_guild and await self.bot.db.fetch_value(
"SELECT restrict_custom_commands FROM guild_settings WHERE guild_id = %s",
ctx.guild.id,
if (
not ctx.author.guild_permissions.manage_guild
and await self.bot.db.fetch_value(
"SELECT restrict_custom_commands FROM guild_settings WHERE guild_id = %s",
ctx.guild.id,
)
):
raise commands.MissingPermissions(["manage_server"])

if name in self.bot_command_list():
raise exceptions.CommandWarning(f"`{ctx.prefix}{name}` is already a built in command!")
raise exceptions.CommandWarning(
f"`{ctx.prefix}{name}` is already a built in command!"
)
if await self.bot.db.fetch_value(
"SELECT content FROM custom_command WHERE guild_id = %s AND command_trigger = %s",
ctx.guild.id,
Expand Down Expand Up @@ -153,7 +160,9 @@ async def command_remove(self, ctx: commands.Context, name):
ctx.guild.id,
)
if not owner_id:
raise exceptions.CommandWarning(f"Custom command `{ctx.prefix}{name}` does not exist")
raise exceptions.CommandWarning(
f"Custom command `{ctx.prefix}{name}` does not exist"
)

owner = ctx.guild.get_member(owner_id)
if (
Expand All @@ -173,7 +182,9 @@ async def command_remove(self, ctx: commands.Context, name):
ctx.guild.id,
name,
)
await util.send_success(ctx, f"Custom command `{ctx.prefix}{name}` has been deleted")
await util.send_success(
ctx, f"Custom command `{ctx.prefix}{name}` has been deleted"
)

@command.command(name="search")
async def command_search(self, ctx: commands.Context, name):
Expand Down Expand Up @@ -207,13 +218,16 @@ async def command_list(self, ctx: commands.Context):
raise exceptions.CommandError("Unable to get current guild")

rows = [
f"{ctx.prefix}{command}" for command in await self.custom_command_list(ctx.guild.id)
f"{ctx.prefix}{command}"
for command in await self.custom_command_list(ctx.guild.id)
]
if rows:
content = discord.Embed(title=f"{ctx.guild.name} custom commands")
await util.send_as_pages(ctx, content, rows)
else:
raise exceptions.CommandInfo("No custom commands have been added on this server yet")
raise exceptions.CommandInfo(
"No custom commands have been added on this server yet"
)

@command.command(name="import")
@commands.has_permissions(manage_guild=True)
Expand All @@ -233,7 +247,9 @@ async def command_import(self, ctx: commands.Context):
```
"""
if not ctx.message.attachments:
raise exceptions.CommandWarning("Please attach a `.json` file to the message")
raise exceptions.CommandWarning(
"Please attach a `.json` file to the message"
)

jsonfile = ctx.message.attachments[0]
imported = json.loads(await jsonfile.read())
Expand Down Expand Up @@ -264,7 +280,9 @@ async def command_export(self, ctx: commands.Context):
ctx.guild.id,
)
if not data:
raise exceptions.CommandInfo("No custom commands have been added on this server yet")
raise exceptions.CommandInfo(
"No custom commands have been added on this server yet"
)

jsondata = [
{
Expand Down Expand Up @@ -299,7 +317,10 @@ async def import_command(self, ctx: commands.Context, command: dict):
ctx.guild.id,
name,
):
return False, f"Custom command `{ctx.prefix}{name}` already exists on this server!"
return (
False,
f"Custom command `{ctx.prefix}{name}` already exists on this server!",
)

await self.bot.db.execute(
"INSERT INTO custom_command VALUES(%s, %s, %s, %s, %s)",
Expand All @@ -315,7 +336,9 @@ async def import_command(self, ctx: commands.Context, command: dict):
@commands.has_permissions(manage_guild=True)
async def command_restrict(self, ctx: commands.Context, value: bool):
"""Restrict command management to only people with manage_server permission"""
await queries.update_setting(ctx, "guild_settings", "restrict_custom_commands", value)
await queries.update_setting(
ctx, "guild_settings", "restrict_custom_commands", value
)
if value:
await util.send_success(
ctx, "Adding custom commands is now restricted to server managers."
Expand Down Expand Up @@ -345,7 +368,9 @@ async def command_clear(self, ctx: commands.Context):
if count < 1:
raise exceptions.CommandWarning("This server has no custom commands yet!")

content = discord.Embed(title=":warning: Are you sure?", color=int("ffcc4d", 16))
content = discord.Embed(
title=":warning: Are you sure?", color=int("ffcc4d", 16)
)
content.description = (
f"This action will delete all **{count}** custom commands on "
"this server and is **irreversible**."
Expand All @@ -370,7 +395,9 @@ async def cancel():

functions = {"✅": confirm, "❌": cancel}
asyncio.ensure_future(
util.reaction_buttons(ctx, msg, functions, only_author=True, single_use=True)
util.reaction_buttons(
ctx, msg, functions, only_author=True, single_use=True
)
)


Expand Down
60 changes: 45 additions & 15 deletions cogs/errorhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@
import discord
from discord.ext import commands
from loguru import logger

from modules import emojis, exceptions, queries, util
from modules.instagram import InstagramError
from modules.misobot import MisoBot
from modules.tiktok import TiktokError

from modules import emojis, exceptions, queries, util


@dataclass
class ErrorMessages:
disabled_command = "This command is temporarily disabled, sorry for the inconvenience!"
disabled_command = (
"This command is temporarily disabled, sorry for the inconvenience!"
)
no_private_message = "This command cannot be used in a DM!"
missing_permissions = "You require {0} permission to use this command!"
bot_missing_permissions = "Unable execute command due to missing permissions! (I need {0})"
bot_missing_permissions = (
"Unable execute command due to missing permissions! (I need {0})"
)
not_donator = "This command is exclusive to Miso Bot donators! Consider donating to get access"
server_too_big = "This command cannot be used in large servers for performance reasons!"
server_too_big = (
"This command cannot be used in large servers for performance reasons!"
)
not_allowed = "You cannot use this command."
max_concurrency = "Stop spamming! >:("
command_on_cooldown = "You are on cooldown! Please wait `{0:.0f} seconds.`"
Expand All @@ -35,7 +41,9 @@ def __init__(self, bot):
self.bot: MisoBot = bot

@staticmethod
def log_format(ctx: commands.Context, error: Exception | None, message: str | None = None):
def log_format(
ctx: commands.Context, error: Exception | None, message: str | None = None
):
return (
f"{ctx.guild} @ {ctx.author} : {ctx.message.content} "
f"=> {type(error).__name__}: {message or str(error)}"
Expand Down Expand Up @@ -68,16 +76,26 @@ async def send_embed(
**kwargs,
)
except discord.Forbidden:
logger.warning(f"403 Forbidden when trying to send error message : {message}")
logger.warning(
f"403 Forbidden when trying to send error message : {message}"
)

async def send_info(
self, ctx: commands.Context, message: str, error: Exception | None = None, **kwargs
self,
ctx: commands.Context,
message: str,
error: Exception | None = None,
**kwargs,
):
logger.info(self.log_format(ctx, error, message))
await self.send_embed(ctx, message, ":information_source:", "3b88c3", **kwargs)

async def send_warning(
self, ctx: commands.Context, message: str, error: Exception | None = None, **kwargs
self,
ctx: commands.Context,
message: str,
error: Exception | None = None,
**kwargs,
):
logger.warning(self.log_format(ctx, error, message))
await self.send_embed(ctx, message, ":warning:", "ffcc4d", **kwargs)
Expand All @@ -91,9 +109,13 @@ async def send_error(
**kwargs,
):
logger.error(self.log_format(ctx, error, message))
await self.send_embed(ctx, f"```{language}\n{message}```", color="be1931", **kwargs)
await self.send_embed(
ctx, f"```{language}\n{message}```", color="be1931", **kwargs
)

async def send_lastfm_error(self, ctx: commands.Context, error: exceptions.LastFMError):
async def send_lastfm_error(
self, ctx: commands.Context, error: exceptions.LastFMError
):
match error.error_code:
case 8:
message = (
Expand All @@ -112,7 +134,9 @@ async def send_lastfm_error(self, ctx: commands.Context, error: exceptions.LastF

await self.send_embed(ctx, message, emojis.LASTFM, "b90000")

async def handle_blacklist(self, ctx: commands.Context, error: exceptions.Blacklist):
async def handle_blacklist(
self, ctx: commands.Context, error: exceptions.Blacklist
):
if ctx.author.id == ctx.bot.owner_id or (
isinstance(
error,
Expand Down Expand Up @@ -145,7 +169,9 @@ async def handle_blacklist(self, ctx: commands.Context, error: exceptions.Blackl
await asyncio.sleep(5)
await ctx.message.delete()

async def handle_cooldown(self, ctx: commands.Context, error: commands.CommandOnCooldown):
async def handle_cooldown(
self, ctx: commands.Context, error: commands.CommandOnCooldown
):
if (
ctx.author.id == ctx.bot.owner_id
or await queries.is_donator(ctx, ctx.author, 2)
Expand All @@ -161,7 +187,9 @@ async def handle_cooldown(self, ctx: commands.Context, error: commands.CommandOn
)

@commands.Cog.listener()
async def on_command_error(self, ctx: commands.Context, error_wrapper: commands.CommandError):
async def on_command_error(
self, ctx: commands.Context, error_wrapper: commands.CommandError
):
"""The event triggered when an error is raised while invoking a command"""

# extract the original error from the CommandError wrapper
Expand Down Expand Up @@ -255,7 +283,9 @@ async def on_command_error(self, ctx: commands.Context, error_wrapper: commands.
await self.send_warning(ctx, error.message)

case _:
await self.send_error(ctx, f"{type(error).__name__}: {error}", error, language="ex")
await self.send_error(
ctx, f"{type(error).__name__}: {error}", error, language="ex"
)
logger.opt(exception=error).error("Unhandled exception traceback:")


Expand Down
Loading

0 comments on commit 4bffa1d

Please sign in to comment.