2023-03-30 17:03:17 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import discord
|
|
|
|
from discord.ext import commands
|
2023-06-26 05:47:57 +00:00
|
|
|
from discord.ext.commands import Cog, Context, BucketType, Greedy
|
2023-03-30 17:03:17 +00:00
|
|
|
|
2023-06-09 14:28:11 +00:00
|
|
|
from robocop_ng.helpers.checks import check_if_staff, check_if_staff_or_dm
|
2023-04-01 16:43:56 +00:00
|
|
|
from robocop_ng.helpers.macros import (
|
|
|
|
get_macro,
|
|
|
|
add_macro,
|
|
|
|
edit_macro,
|
|
|
|
remove_macro,
|
2023-04-02 11:56:49 +00:00
|
|
|
get_macros_dict,
|
|
|
|
add_aliases,
|
|
|
|
remove_aliases,
|
|
|
|
clear_aliases,
|
2023-04-01 16:43:56 +00:00
|
|
|
)
|
2023-03-30 17:03:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Macro(Cog):
|
2023-04-05 10:10:18 +00:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
|
2023-03-30 17:03:17 +00:00
|
|
|
@commands.cooldown(3, 30, BucketType.member)
|
|
|
|
@commands.command(aliases=["m"])
|
2023-05-02 19:38:22 +00:00
|
|
|
async def macro(
|
2023-06-26 05:47:57 +00:00
|
|
|
self, ctx: Context, key: str, targets: Greedy[discord.Member] = None
|
2023-05-02 19:38:22 +00:00
|
|
|
):
|
2023-04-02 11:56:49 +00:00
|
|
|
await ctx.message.delete()
|
2023-03-30 17:03:17 +00:00
|
|
|
if len(key) > 0:
|
2023-04-05 10:10:18 +00:00
|
|
|
text = get_macro(self.bot, key)
|
2023-03-30 17:03:17 +00:00
|
|
|
if text is not None:
|
2023-06-26 05:47:57 +00:00
|
|
|
if targets is not None:
|
|
|
|
await ctx.send(
|
|
|
|
f"{', '.join(target.mention for target in targets)}:\n{text}"
|
|
|
|
)
|
2023-03-30 17:03:17 +00:00
|
|
|
else:
|
2023-04-02 11:56:49 +00:00
|
|
|
if ctx.message.reference is not None:
|
|
|
|
await ctx.send(
|
|
|
|
text, reference=ctx.message.reference, mention_author=True
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
await ctx.send(text)
|
2023-03-30 17:03:17 +00:00
|
|
|
else:
|
2023-04-01 16:43:56 +00:00
|
|
|
await ctx.send(
|
|
|
|
f"{ctx.author.mention}: The macro '{key}' doesn't exist."
|
|
|
|
)
|
2023-03-30 17:03:17 +00:00
|
|
|
|
|
|
|
@commands.check(check_if_staff)
|
|
|
|
@commands.command(name="macroadd", aliases=["ma", "addmacro", "add_macro"])
|
|
|
|
async def add_macro(self, ctx: Context, key: str, *, text: str):
|
2023-04-05 10:10:18 +00:00
|
|
|
if add_macro(self.bot, key, text):
|
2023-03-30 17:03:17 +00:00
|
|
|
await ctx.send(f"Macro '{key}' added!")
|
|
|
|
else:
|
|
|
|
await ctx.send(f"Error: Macro '{key}' already exists.")
|
|
|
|
|
2023-04-02 11:56:49 +00:00
|
|
|
@commands.check(check_if_staff)
|
|
|
|
@commands.command(name="aliasadd", aliases=["addalias", "add_alias"])
|
|
|
|
async def add_alias_macro(self, ctx: Context, existing_key: str, *new_keys: str):
|
|
|
|
if len(new_keys) == 0:
|
|
|
|
await ctx.send("Error: You need to add at least one alias.")
|
|
|
|
else:
|
2023-04-05 10:10:18 +00:00
|
|
|
if add_aliases(self.bot, existing_key, list(new_keys)):
|
2023-04-02 11:56:49 +00:00
|
|
|
await ctx.send(
|
|
|
|
f"Added {len(new_keys)} aliases to macro '{existing_key}'!"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
await ctx.send(f"Error: No new and unique aliases found.")
|
|
|
|
|
2023-03-30 17:03:17 +00:00
|
|
|
@commands.check(check_if_staff)
|
|
|
|
@commands.command(name="macroedit", aliases=["me", "editmacro", "edit_macro"])
|
|
|
|
async def edit_macro(self, ctx: Context, key: str, *, text: str):
|
2023-04-05 10:10:18 +00:00
|
|
|
if edit_macro(self.bot, key, text):
|
2023-03-30 17:03:17 +00:00
|
|
|
await ctx.send(f"Macro '{key}' edited!")
|
|
|
|
else:
|
|
|
|
await ctx.send(f"Error: Macro '{key}' not found.")
|
|
|
|
|
2023-04-02 11:56:49 +00:00
|
|
|
@commands.check(check_if_staff)
|
|
|
|
@commands.command(
|
|
|
|
name="aliasremove",
|
|
|
|
aliases=[
|
|
|
|
"aliasdelete",
|
|
|
|
"delalias",
|
|
|
|
"aliasdel",
|
|
|
|
"removealias",
|
|
|
|
"remove_alias",
|
|
|
|
"delete_alias",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def remove_alias_macro(
|
|
|
|
self, ctx: Context, existing_key: str, *remove_keys: str
|
|
|
|
):
|
|
|
|
if len(remove_keys) == 0:
|
|
|
|
await ctx.send("Error: You need to remove at least one alias.")
|
|
|
|
else:
|
2023-04-05 10:10:18 +00:00
|
|
|
if remove_aliases(self.bot, existing_key, list(remove_keys)):
|
2023-04-02 11:56:49 +00:00
|
|
|
await ctx.send(
|
|
|
|
f"Removed {len(remove_keys)} aliases from macro '{existing_key}'!"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
await ctx.send(
|
|
|
|
f"Error: None of the specified aliases were found for macro '{existing_key}'."
|
|
|
|
)
|
|
|
|
|
2023-03-30 17:03:17 +00:00
|
|
|
@commands.check(check_if_staff)
|
2023-04-01 16:43:56 +00:00
|
|
|
@commands.command(
|
|
|
|
name="macroremove",
|
|
|
|
aliases=[
|
|
|
|
"mr",
|
|
|
|
"md",
|
|
|
|
"removemacro",
|
|
|
|
"remove_macro",
|
|
|
|
"macrodel",
|
|
|
|
"delmacro",
|
|
|
|
"delete_macro",
|
|
|
|
],
|
|
|
|
)
|
2023-03-30 17:03:17 +00:00
|
|
|
async def remove_macro(self, ctx: Context, key: str):
|
2023-04-05 10:10:18 +00:00
|
|
|
if remove_macro(self.bot, key):
|
2023-03-30 17:03:17 +00:00
|
|
|
await ctx.send(f"Macro '{key}' removed!")
|
|
|
|
else:
|
|
|
|
await ctx.send(f"Error: Macro '{key}' not found.")
|
|
|
|
|
2023-04-02 11:56:49 +00:00
|
|
|
@commands.check(check_if_staff)
|
|
|
|
@commands.command(name="aliasclear", aliases=["clearalias", "clear_alias"])
|
|
|
|
async def clear_alias_macro(self, ctx: Context, existing_key: str):
|
2023-04-05 10:10:18 +00:00
|
|
|
if clear_aliases(self.bot, existing_key):
|
2023-04-02 11:56:49 +00:00
|
|
|
await ctx.send(f"Removed all aliases of macro '{existing_key}'!")
|
|
|
|
else:
|
|
|
|
await ctx.send(f"Error: No aliases found for macro '{existing_key}'.")
|
|
|
|
|
2023-06-09 14:28:11 +00:00
|
|
|
@commands.check(check_if_staff_or_dm)
|
2023-03-30 17:03:17 +00:00
|
|
|
@commands.cooldown(3, 30, BucketType.channel)
|
|
|
|
@commands.command(name="macros", aliases=["ml", "listmacros", "list_macros"])
|
2023-04-26 18:38:52 +00:00
|
|
|
async def list_macros(self, ctx: Context, macros_only=False):
|
2023-04-05 10:10:18 +00:00
|
|
|
macros = get_macros_dict(self.bot)
|
2023-04-02 11:56:49 +00:00
|
|
|
if len(macros["macros"]) > 0:
|
2023-05-28 08:36:37 +00:00
|
|
|
messages = []
|
2023-06-09 14:28:11 +00:00
|
|
|
macros_formatted = []
|
|
|
|
|
|
|
|
for key in sorted(macros["macros"].keys()):
|
|
|
|
message = f"- {key}"
|
2023-06-12 07:45:51 +00:00
|
|
|
if not macros_only and key in macros["aliases"]:
|
2023-04-26 18:38:52 +00:00
|
|
|
for alias in macros["aliases"][key]:
|
|
|
|
message += f", {alias}"
|
2023-06-09 14:28:11 +00:00
|
|
|
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"
|
2023-04-26 18:38:52 +00:00
|
|
|
|
2023-06-09 14:28:11 +00:00
|
|
|
if message not in messages:
|
|
|
|
# Add the last message as well
|
|
|
|
messages.append(message)
|
2023-05-28 08:36:37 +00:00
|
|
|
|
|
|
|
for msg in messages:
|
|
|
|
await ctx.send(msg)
|
2023-06-09 14:28:11 +00:00
|
|
|
|
2023-03-30 17:03:17 +00:00
|
|
|
else:
|
|
|
|
await ctx.send("Couldn't find any macros.")
|
|
|
|
|
2023-06-09 14:28:11 +00:00
|
|
|
@commands.check(check_if_staff_or_dm)
|
2023-04-02 11:56:49 +00:00
|
|
|
@commands.cooldown(3, 30, BucketType.channel)
|
|
|
|
@commands.command(name="aliases", aliases=["listaliases", "list_aliases"])
|
|
|
|
async def list_aliases(self, ctx: Context, existing_key: str):
|
2023-04-05 10:10:18 +00:00
|
|
|
macros = get_macros_dict(self.bot)
|
2023-04-02 11:56:49 +00:00
|
|
|
existing_key = existing_key.lower()
|
|
|
|
if existing_key in macros["aliases"].keys():
|
|
|
|
message = f"📝 **Aliases for '{existing_key}'**:\n"
|
|
|
|
for alias in sorted(macros["aliases"][existing_key]):
|
|
|
|
message += f"- {alias}\n"
|
|
|
|
await ctx.send(message)
|
|
|
|
else:
|
|
|
|
await ctx.send(f"Couldn't find any aliases for macro '{existing_key}'.")
|
|
|
|
|
2023-03-30 17:03:17 +00:00
|
|
|
|
|
|
|
async def setup(bot):
|
|
|
|
await bot.add_cog(Macro(bot))
|