From c897cebcd58fbf25c49a325fc106d2c35510b2c1 Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Tue, 3 Nov 2020 16:06:08 +0100 Subject: [PATCH 1/2] Make prefixes case insensitive, make Hmm? easter egg work with all prefixes --- cogs/events.py | 13 +++++++------ data/constants.py | 3 +++ didier.py | 9 ++------- functions/easterEggResponses.py | 13 ++++++------- functions/prefixes.py | 28 ++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 functions/prefixes.py diff --git a/cogs/events.py b/cogs/events.py index 909f9e5..aa4979b 100644 --- a/cogs/events.py +++ b/cogs/events.py @@ -67,18 +67,19 @@ class Events(commands.Cog): await self.failedChecksCog.freeGames(message) # Log commands in terminal - if any(message.content.lower().startswith(pre) for pre in self.client.prefixes): - DM = message.guild is None - print("{} in {}: {}".format(message.author.display_name, - "DM" if DM else "{} ({})".format(message.channel.name, message.guild.name), - message.content)) + # TODO move to on_command + # if any(message.content.lower().startswith(pre) for pre in self.client.prefixes): + # DM = message.guild is None + # print("{} in {}: {}".format(message.author.display_name, + # "DM" if DM else "{} ({})".format(message.channel.name, message.guild.name), + # message.content)) # Boos React to people that call him Dider if "dider" in message.content.lower() and str(message.author.id) not in [constants.myId, constants.didierId]: await message.add_reaction("<:boos:629603785840263179>") # Check for other easter eggs - eER = easterEggResponses.control(message) + eER = easterEggResponses.control(self.client, message) if eER: await message.channel.send(eER) diff --git a/data/constants.py b/data/constants.py index 9f71825..09da550 100644 --- a/data/constants.py +++ b/data/constants.py @@ -2,6 +2,7 @@ from enum import Enum myId = "171671190631481345" didierId = "680510935164911730" +coolerDidierId = "728361496874057812" botIDs = [ "155149108183695360", "234395307759108106", @@ -43,6 +44,8 @@ creationDate = 1582243200 holidayAPIKey = "af4e1ebe-465d-4b93-a828-b95df18e6424" +prefixes = ["big d", "didier"] + class Live(Enum): CallOfCode = "626699611192688641" diff --git a/didier.py b/didier.py index 78eed74..89bb5cb 100644 --- a/didier.py +++ b/didier.py @@ -1,23 +1,18 @@ import discord from discord.ext import commands from dotenv import load_dotenv +from functions.prefixes import get_prefix import os load_dotenv(verbose=True) -# All possible prefixes -# When_mentioned_or doesn't count for spaces so can't use that -prefixes = ["didier ", "Didier ", "DIDIER ", "big d ", "Big d ", "Big D ", "BIG D ", "<@!680510935164911730> ", - "didier", "Didier", "DIDIER", "big d", "Big d", "Big D", "BIG D", "<@!680510935164911730>"] - # Configure intents (1.5.0) intents = discord.Intents.default() intents.members = True -client = commands.Bot(command_prefix=prefixes, case_insensitive=True, intents=intents) -client.prefixes = prefixes +client = commands.Bot(command_prefix=get_prefix, case_insensitive=True, intents=intents) # Remove default help because it sucks & I made my own client.remove_command("help") diff --git a/functions/easterEggResponses.py b/functions/easterEggResponses.py index f9998fe..ab73efd 100644 --- a/functions/easterEggResponses.py +++ b/functions/easterEggResponses.py @@ -1,16 +1,15 @@ +from data import constants import random -from data import constants - -def control(message): +def control(bot, message): if str(message.author.id) == constants.didierId: return "" - elif didier(message.content): + elif didier(bot, message.content): return "Hmm?" elif any(term in message.content for term in ["gib dink", "gib donk"]): return "No." - elif didier(message.content.split(" ")[0]) and any(term in message.content.lower() for term in ["are you sure", "are u sure"]): + elif didier(bot, message.content.split(" ")[0]) and any(term in message.content.lower() for term in ["are you sure", "are u sure"]): return "I'm not just sure, I'm HIV Positive." elif any(message.content.lower().startswith(term) for term in ["is this", "isthis", "isdis", "is dis"]): res = random.randint(0, 100) @@ -24,6 +23,6 @@ def control(message): return "" -def didier(message): +def didier(bot, message): ml = message.lower() - return ml == "didier" or ml == "<@!{}>".format(constants.didierId) + return ml in constants.prefixes or ml == "<@!{}>".format(bot.user.id) diff --git a/functions/prefixes.py b/functions/prefixes.py new file mode 100644 index 0000000..4624d3b --- /dev/null +++ b/functions/prefixes.py @@ -0,0 +1,28 @@ +from data.constants import prefixes +from discord.ext import commands +import os + + +fallback = os.urandom(32).hex() + + +def get_prefix(bot: commands.Bot, message): + content = message.content.lower() + mention = "<@!{}>".format(bot.user.id) + + # Used @Didier + if content.startswith(mention): + if content.startswith(mention + " "): + return mention + " " + return mention + + # Used a prefix + for prefix in prefixes: + # Find correct prefix + if content.startswith(prefix): + # Check if a space has to be added to invoke commands + if content.startswith(prefix + " "): + return prefix + " " + return prefix + + return fallback From 08b574283003d4fbfb4e06d275cad80594409939 Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Tue, 3 Nov 2020 16:09:22 +0100 Subject: [PATCH 2/2] Move command logging to on_command --- cogs/events.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/cogs/events.py b/cogs/events.py index aa4979b..e9ca378 100644 --- a/cogs/events.py +++ b/cogs/events.py @@ -66,16 +66,8 @@ class Events(commands.Cog): if not checks.freeGamesCheck(message): await self.failedChecksCog.freeGames(message) - # Log commands in terminal - # TODO move to on_command - # if any(message.content.lower().startswith(pre) for pre in self.client.prefixes): - # DM = message.guild is None - # print("{} in {}: {}".format(message.author.display_name, - # "DM" if DM else "{} ({})".format(message.channel.name, message.guild.name), - # message.content)) - # Boos React to people that call him Dider - if "dider" in message.content.lower() and str(message.author.id) not in [constants.myId, constants.didierId]: + if "dider" in message.content.lower() and str(message.author.id) not in [constants.myId, constants.didierId, constants.coolerDidierId]: await message.add_reaction("<:boos:629603785840263179>") # Check for other easter eggs @@ -86,6 +78,18 @@ class Events(commands.Cog): # Earn XP & Message count stats.sentMessage(message) + @commands.Cog.listener() + async def on_command(self, ctx): + """ + Function called whenever someone invokes a command. + Logs commands in your terminal. + :param ctx: Discord Context + """ + DM = ctx.guild is None + print("{} in {}: {}".format(ctx.author.display_name, + "DM" if DM else "{} ({})".format(ctx.channel.name, ctx.guild.name), + ctx.message.content)) + @commands.Cog.listener() async def on_command_error(self, ctx, err): """