Fix list_macros command still not working properly (#57)

* Properly split the message

* Apply black formatting
This commit is contained in:
TSRBerry 2023-06-09 16:28:11 +02:00 committed by GitHub
parent 7bcf3c28fe
commit cbd1f55dc3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 23 deletions

View file

@ -4,7 +4,7 @@ import discord
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import Cog, Context, BucketType from discord.ext.commands import Cog, Context, BucketType
from robocop_ng.helpers.checks import check_if_staff from robocop_ng.helpers.checks import check_if_staff, check_if_staff_or_dm
from robocop_ng.helpers.macros import ( from robocop_ng.helpers.macros import (
get_macro, get_macro,
add_macro, add_macro,
@ -127,44 +127,41 @@ class Macro(Cog):
else: else:
await ctx.send(f"Error: No aliases found for macro '{existing_key}'.") await ctx.send(f"Error: No aliases found for macro '{existing_key}'.")
@commands.check(check_if_staff_or_dm)
@commands.cooldown(3, 30, BucketType.channel) @commands.cooldown(3, 30, BucketType.channel)
@commands.command(name="macros", aliases=["ml", "listmacros", "list_macros"]) @commands.command(name="macros", aliases=["ml", "listmacros", "list_macros"])
async def list_macros(self, ctx: Context, macros_only=False): async def list_macros(self, ctx: Context, macros_only=False):
macros = get_macros_dict(self.bot) macros = get_macros_dict(self.bot)
if len(macros["macros"]) > 0: if len(macros["macros"]) > 0:
messages = [] messages = []
num_messages = ( macros_formatted = []
len(macros["macros"]) // 50 if len(macros["macros"]) > 50 else 1
)
message = ""
for index, key in zip( for key in sorted(macros["macros"].keys()):
range(len(macros["macros"])), sorted(macros["macros"].keys()) message = f"- {key}"
): if not macros_only:
if index == 0 or index + 1 % 50 == 0:
if len(message) > 0:
messages.append(message)
message = f"📝 **Macros** ({len(messages) + 1}/{num_messages}):\n"
message += f"- {key}\n"
if not macros_only and key in macros["aliases"].keys():
message += " - __aliases__: "
first_alias = True
for alias in macros["aliases"][key]: for alias in macros["aliases"][key]:
if first_alias:
message += alias
first_alias = False
continue
message += f", {alias}" message += f", {alias}"
message += "\n" macros_formatted.append(message)
message = f"📝 **Macros**:\n"
for macro in macros_formatted:
if len(message) >= 1500:
messages.append(message)
message = f"{macro}\n"
else:
message += f"{macro}\n"
if message not in messages:
# Add the last message as well # Add the last message as well
messages.append(message) messages.append(message)
for msg in messages: for msg in messages:
await ctx.send(msg) await ctx.send(msg)
else: else:
await ctx.send("Couldn't find any macros.") await ctx.send("Couldn't find any macros.")
@commands.check(check_if_staff_or_dm)
@commands.cooldown(3, 30, BucketType.channel) @commands.cooldown(3, 30, BucketType.channel)
@commands.command(name="aliases", aliases=["listaliases", "list_aliases"]) @commands.command(name="aliases", aliases=["listaliases", "list_aliases"])
async def list_aliases(self, ctx: Context, existing_key: str): async def list_aliases(self, ctx: Context, existing_key: str):

View file

@ -22,6 +22,12 @@ def check_if_staff_or_ot(ctx):
return is_ot or is_staff or is_bot_cmds return is_ot or is_staff or is_bot_cmds
def check_if_staff_or_dm(ctx):
if not ctx.guild:
return True
return any(r.id in config.staff_role_ids for r in ctx.author.roles)
def check_if_collaborator(ctx): def check_if_collaborator(ctx):
if not ctx.guild: if not ctx.guild:
return False return False