Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
###############
DEFAULT_BOT_PREFIX = "~"
EMBED_COLOR = 0xD4E4FF

CUSTOM_RESPONSE_PREFIX = "RANDOM_RESPONSE:"

DEFAULT_COMMANDS = []
CUSTOM_COMMANDS = {}
Expand Down
32 changes: 32 additions & 0 deletions modules/custom_command/cog.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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))