diff --git a/extendedcommands/__init__.py b/extendedcommands/__init__.py new file mode 100644 index 0000000..3067153 --- /dev/null +++ b/extendedcommands/__init__.py @@ -0,0 +1,4 @@ +from .extendedcommands import ExtendedCommands + +async def setup(bot): + await bot.add_cog(ExtendedCommands(bot)) \ No newline at end of file diff --git a/extendedcommands/extendedcommands.py b/extendedcommands/extendedcommands.py new file mode 100644 index 0000000..069d132 --- /dev/null +++ b/extendedcommands/extendedcommands.py @@ -0,0 +1,114 @@ +from redbot.core import commands, Config +from redbot.core.bot import Red +import discord +import shlex +import asyncio + +class ExtendedCommands(commands.Cog): + """Extended command functionality for Red bots.""" + + def __init__(self, bot: Red): + self.bot = bot + self.config = Config.get_conf(self, identifier=867530998, force_registration=True) + + @commands.Cog.listener() + async def on_message(self, message: discord.Message): + if message.author.bot: + return + + # Check if message starts with any bot prefix + prefixes = await self.bot.get_valid_prefixes(message.guild) + has_prefix = False + content = message.content + + for prefix in prefixes: + if content.startswith(prefix): + has_prefix = True + content = content[len(prefix):] + break + + if not has_prefix: + return + + # Check if message contains && + if "&&" not in content: + return + + # Prevent the original message from triggering commands + await self.bot.process_commands(message) + + # Split commands and execute them in sequence + commands_to_run = [cmd.strip() for cmd in content.split("&&")] + + async with message.channel.typing(): + for cmd in commands_to_run: + if not cmd: # Skip empty commands + continue + + # Create a new message object with the command + new_message = discord.Message( + state=message._state, + channel=message.channel, + data={ + "id": message.id, + "content": prefixes[0] + cmd, # Use first prefix + "author": message.author.to_dict(), + "pinned": False, + "mention_everyone": False, + "tts": False, + "type": 0, + "flags": 0, + } + ) + + try: + # Process the command + await self.bot.process_commands(new_message) + # Add a small delay between commands + await asyncio.sleep(0.5) + except Exception as e: + await message.channel.send(f"Error executing command `{cmd}`: {str(e)}") + break # Stop executing remaining commands on error + + @commands.command() + async def chain(self, ctx: commands.Context, *, commands_str: str): + """Chain multiple commands together using &&. + + Example: + [p]chain ping && info && glb show + """ + if "&&" not in commands_str: + await ctx.send("Please provide multiple commands separated by &&") + return + + commands_to_run = [cmd.strip() for cmd in commands_str.split("&&")] + + async with ctx.typing(): + for cmd in commands_to_run: + if not cmd: # Skip empty commands + continue + + # Create a new message object with the command + new_message = discord.Message( + state=ctx.message._state, + channel=ctx.channel, + data={ + "id": ctx.message.id, + "content": ctx.prefix + cmd, + "author": ctx.author.to_dict(), + "pinned": False, + "mention_everyone": False, + "tts": False, + "type": 0, + "flags": 0, + } + ) + + try: + # Process the command + await self.bot.process_commands(new_message) + # Add a small delay between commands + await asyncio.sleep(0.5) + except Exception as e: + await ctx.send(f"Error executing command `{cmd}`: {str(e)}") + break # Stop executing remaining commands on error \ No newline at end of file diff --git a/extendedcommands/info.json b/extendedcommands/info.json new file mode 100644 index 0000000..41dd3fd --- /dev/null +++ b/extendedcommands/info.json @@ -0,0 +1,19 @@ +{ + "author": [ + "Valerie" + ], + "name": "ExtendedCommands", + "disabled": false, + "short": "Chain multiple commands together using &&", + "description": "Allows users to chain multiple commands together using && operator. For example: [p]ping && info && glb show", + "tags": [ + "utility", + "commands", + "chain", + "extended" + ], + "min_bot_version": "3.5.0", + "hidden": false, + "install_msg": "Thanks for installing ExtendedCommands! You can now chain commands using && (e.g., [p]ping && info). Use [p]help ExtendedCommands for more information.", + "end_user_data_statement": "This cog does not store any user data." +} \ No newline at end of file