diff --git a/bot.py b/bot.py index 483c6520..b78657df 100644 --- a/bot.py +++ b/bot.py @@ -2,6 +2,7 @@ import constants import sqlalchemy import nextcord +import random from nextcord.ext import commands from sqlalchemy import insert from sqlalchemy.orm import Session @@ -12,6 +13,7 @@ import database # noqa: E402 from utils import logging_utils # noqa: E402 +from constants import CUSTOM_RESPONSE_PREFIX # noqa: E402 def get_prefix(client, message): @@ -153,6 +155,15 @@ async def on_message(message: nextcord.Message): # Command found in cache if command_return is not None: + # Check for random response and send in plain text + if command_return.startswith(CUSTOM_RESPONSE_PREFIX): + responses = command_return[len(CUSTOM_RESPONSE_PREFIX) :].split( + ", " + ) + command_return = random.choice(responses) + await message.channel.send(command_return) + return + # Image, so we use normal text. if database.CUSTOM_COMMANDS[message.guild.id][command_name][1]: await message.channel.send(command_return) @@ -186,6 +197,14 @@ async def on_message(message: nextcord.Message): .first() ) if result is not None: + # Pick random and send in plain text + if result.command_return.startswith(CUSTOM_RESPONSE_PREFIX): + responses = result.command_return[ + len(CUSTOM_RESPONSE_PREFIX) : + ].split(", ") + to_send = random.choice(responses) + await message.channel.send(to_send) + if result.image: await message.channel.send(result.command_return) return diff --git a/constants.py b/constants.py index e519bfb8..5118a223 100644 --- a/constants.py +++ b/constants.py @@ -3,7 +3,7 @@ ############### DEFAULT_BOT_PREFIX = "~" EMBED_COLOR = 0xD4E4FF - +CUSTOM_RESPONSE_PREFIX = "RANDOM_RESPONSE:" DEFAULT_COMMANDS = [] CUSTOM_COMMANDS = {} diff --git a/modules/custom_command/cog.py b/modules/custom_command/cog.py index 56c29c32..84ace0da 100644 --- a/modules/custom_command/cog.py +++ b/modules/custom_command/cog.py @@ -1,10 +1,12 @@ import database import constants import os +import shlex from utils import discord_utils, logging_utils, command_predicates from nextcord.ext import commands from sqlalchemy.orm import Session from sqlalchemy import insert +from constants import CUSTOM_RESPONSE_PREFIX """ Custom command module. Allows users to set their own "custom command" of saveable image/embed/messages making a lot of easy to retrieve utility. @@ -363,6 +365,36 @@ async def rmcustomcommand(self, ctx, command_name: str): ) await discord_utils.send_message(ctx, embed) + @command_predicates.is_trusted() + @commands.command( + name="addcresponse", + aliases=["addcustomresponse", "addembedresponse", "customresponse", "addcr"], + ) + async def addcustomresponse(self, ctx, command_name: str, *args): + """A custom command that picks a random response from a list of responses. + Arguments must be delimited by spaces/gropued together in quotes. + Permission Category : Trusted Roles only. + Usage: `~addcustomresponse command_name "Response 1" "Response 2" "Response 3"` + """ + + await logging_utils.log_command( + "addcustomresponse", ctx.guild, ctx.channel, ctx.author + ) + embed = discord_utils.create_embed() + + args_list = shlex.split(" ".join(args)) + + if len(args_list) < 2: + embed = discord_utils.create_no_argument_embed("At least two responses are required") + await discord_utils.send_message(ctx, embed) + return + + command_name = command_name.lower() + command_return = CUSTOM_RESPONSE_PREFIX +", ".join(args_list) # Use || as a delimiter for responses + + await self.add_cc_generic( + ctx, command_name, command_return, is_image=False, is_global=False + ) def setup(bot): bot.add_cog(CustomCommandCog(bot))