From 4098da204b32206d61c268836c9feb401a9b5d7c Mon Sep 17 00:00:00 2001 From: Bobloy Date: Fri, 18 May 2018 11:34:30 -0400 Subject: [PATCH] TriggerCmd initial commit, still developing --- triggercmd/__init__.py | 5 ++ triggercmd/info..json | 18 +++++ triggercmd/triggercmd.py | 145 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 triggercmd/__init__.py create mode 100644 triggercmd/info..json create mode 100644 triggercmd/triggercmd.py diff --git a/triggercmd/__init__.py b/triggercmd/__init__.py new file mode 100644 index 0000000..91b145f --- /dev/null +++ b/triggercmd/__init__.py @@ -0,0 +1,5 @@ +from .triggercmd import TriggerCmd + + +def setup(bot): + bot.add_cog(TriggerCmd(bot)) diff --git a/triggercmd/info..json b/triggercmd/info..json new file mode 100644 index 0000000..c762df6 --- /dev/null +++ b/triggercmd/info..json @@ -0,0 +1,18 @@ +{ + "author": [ + "Bobloy" + ], + "bot_version": [ + 3, + 0, + 0 + ], + "description": "Cog Template", + "hidden": true, + "install_msg": "Thank you for installing MyCog", + "requirements": [], + "short": "Cog Template", + "tags": [ + "bobloy" + ] +} \ No newline at end of file diff --git a/triggercmd/triggercmd.py b/triggercmd/triggercmd.py new file mode 100644 index 0000000..fa49ddb --- /dev/null +++ b/triggercmd/triggercmd.py @@ -0,0 +1,145 @@ +import asyncio + +import discord + +from redbot.core import Config, checks, commands + +from redbot.core.bot import Red + + +class TriggerCmd: + """ + Trigger cog to end all trigger cogs + """ + + def __init__(self, bot: Red): + self.bot = bot + self.config = Config.get_conf(self, identifier=9811198108111121, force_registration=True) + + default_global = { + "triggers": [] + } + default_guild = { + "triggers": [] + } + default_role = { + "triggers": [] + } + default_channel = { + "triggers": [] + } + default_user = { + "triggers": [] + } + default_member = { + "triggers": [] + } + + self.config.register_global(**default_global) + self.config.register_guild(**default_guild) + self.config.register_role(**default_role) + self.config.register_channel(**default_channel) + self.config.register_user(**default_user) + self.config.register_member(**default_member) + + + @commands.group() + async def trigger(self, ctx: commands.Context): + """ + My custom cog + + Extra information goes here + """ + if ctx.invoked_subcommand is None: + await ctx.send_help() + + @trigger.command(name="add") + async def _trigger_add(self, ctx: commands.Context, *, trigger: str): + """ + Add a new trigger + + Trigger can be a word, phrase, or regex + This will begin the setup process for configuring the triggers + """ + guild = ctx.guild + author = ctx.author + channel = ctx.channel + + def check(m): + return m.author == author and m.channel == channel and m.content.upper() in ["Y","N","YES","NO"] + + await ctx.send("Is `{}` a regex trigger? (Y\\N)".format(trigger)) + try: + answer = await self.bot.wait_for('message', timeout=30, check=check) + except asyncio.TimeoutError: + await ctx.send("Timed out, canceling") + return + + is_regex = answer.content.upper() in ["Y", "YES"] + + await ctx.send("Regex processing for this trigger is **{}**".format("Enabled" if is_regex else "Disabled")) + + + await ctx.send("Should this trigger apply to all guilds?") + try: + answer = await self.bot.wait_for('message', timeout=30, check=check) + except asyncio.TimeoutError: + await ctx.send("Timed out, canceling") + return + + is_global = answer.content.upper() in ["Y", "YES"] + + await ctx.send("Should this trigger apply to only one user?") + try: + answer = await self.bot.wait_for('message', timeout=30, check=check) + except asyncio.TimeoutError: + await ctx.send("Timed out, canceling") + return + if not is_global: # Options: Guild only, Channel only, Member only, Role only + pass + + + + + async def on_message(self, message): + pass + + +class Trigger: + """ + Trigger class + """ + + def __init__(self, trigger: str, is_regex: bool, scope: str, response_text: str, *response_code: str): + self.trigger = trigger + self.is_regex = is_regex + self.scope = scope.lower() + self.response_text = response_text + self.response_code = response_code + + async def save(self, config, obj=None): + if self.scope == "global": + self.config = config + elif self.scope == "guild": + self.config = config.guild(obj) + elif self.scope == "role": + self.config = config.role(obj) + elif self.scope == "channel": + self.config = config.channel(obj) + elif self.scope == "user": + self.config = config.user(obj) + elif self.scope == "member": + self.config = config.member(obj) + else: + # Failed to save + return + + async with self.config.triggers() as tr: + tr.append( + { + "trigger": self.trigger, + "is_regex": self.is_regex, + "response_text": self.response_text, + "response_code": self.response_code + } + ) \ No newline at end of file