async def on_message_without_command(self, message: discord.Message): """ Credit to https://github.com/Twentysix26/26-Cogs/blob/master/cleverbot/cleverbot.py for on_message recognition of @bot Credit to: https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/redbot/cogs/customcom/customcom.py#L508 for the message filtering """ ########### if len(message.content) < 2 or message.author.bot: return guild: discord.Guild = getattr(message, "guild", None) if await self.bot.cog_disabled_in_guild(self, guild): return ctx: commands.Context = await self.bot.get_context(message) if ctx.prefix is not None: # Probably unnecessary, we're in on_message_without_command return ########### # Thank you Cog-Creators def my_local_get_prefix(prefixes, content): for p in prefixes: if content.startswith(p): return p return None when_mentionables = commands.when_mentioned(self.bot, message) prefix = my_local_get_prefix(when_mentionables, message.content) if prefix is None: # print("not mentioned") return channel: discord.TextChannel = message.channel message.content = message.content.replace(prefix, "", 1) text = message.clean_content async with channel.typing(): future = await self.loop.run_in_executor(None, self.chatbot.get_response, text) if future and str(future): await channel.send(str(future)) else: await channel.send(":thinking:")
async def on_message_without_command(self, message: discord.Message): """ Credit to https://github.com/Twentysix26/26-Cogs/blob/master/cleverbot/cleverbot.py for on_message recognition of @bot Credit to: https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/redbot/cogs/customcom/customcom.py#L508 for the message filtering """ ########### is_private = isinstance(message.channel, discord.abc.PrivateChannel) # user_allowed check, will be replaced with self.bot.user_allowed or # something similar once it's added user_allowed = True if len(message.content) < 2 or is_private or not user_allowed or message.author.bot: return ctx: commands.Context = await self.bot.get_context(message) if ctx.prefix is not None: return ########### # Thank you Cog-Creators def my_local_get_prefix(prefixes, content): for p in prefixes: if content.startswith(p): return p return None when_mentionables = commands.when_mentioned(self.bot, message) prefix = my_local_get_prefix(when_mentionables, message.content) if prefix is None: # print("not mentioned") return author = message.author guild: discord.Guild = message.guild channel: discord.TextChannel = message.channel # if author.id != self.bot.user.id: # if guild is None: # to_strip = "@" + channel.me.display_name + " " # else: # to_strip = "@" + guild.me.display_name + " " # text = message.clean_content # if not text.startswith(to_strip): # return # text = text.replace(to_strip, "", 1) # A bit more aggressive, could remove two mentions # Or might not work at all, since mentionables are pre-cleaned_content message.content = message.content.replace(prefix, "", 1) text = message.clean_content async with channel.typing(): future = await self.loop.run_in_executor(None, self.chatbot.get_response, text) if future and str(future): await channel.send(str(future)) else: await channel.send(":thinking:")
async def on_message_without_command(self, message: discord.Message): """ Credit to https://github.com/Twentysix26/26-Cogs/blob/master/cleverbot/cleverbot.py for on_message recognition of @bot Credit to: https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/redbot/cogs/customcom/customcom.py#L508 for the message filtering """ ########### if len(message.content) < 2 or message.author.bot: return guild: discord.Guild = getattr(message, "guild", None) if guild is None or await self.bot.cog_disabled_in_guild(self, guild): return ctx: commands.Context = await self.bot.get_context(message) if ctx.prefix is not None: # Probably unnecessary, we're in on_message_without_command return ########### # Thank you Cog-Creators channel: discord.TextChannel = message.channel if not self._guild_cache[guild.id]: self._guild_cache[guild.id] = await self.config.guild(guild).all() is_reply = False # this is only useful with in_response_to if (message.reference is not None and isinstance(message.reference.resolved, discord.Message) and message.reference.resolved.author.id == self.bot.user.id): is_reply = True # this is only useful with in_response_to pass # this is a reply to the bot, good to go elif guild is not None and channel.id == self._guild_cache[ guild.id]["chatchannel"]: pass # good to go else: when_mentionables = commands.when_mentioned(self.bot, message) prefix = my_local_get_prefix(when_mentionables, message.content) if prefix is None: # print("not mentioned") return message.content = message.content.replace(prefix, "", 1) text = message.clean_content async with ctx.typing(): if is_reply: in_response_to = message.reference.resolved.content elif self._last_message_per_channel[ctx.channel.id] is not None: last_m: discord.Message = self._last_message_per_channel[ ctx.channel.id] minutes = self._guild_cache[ctx.guild.id]["convo_delta"] if (datetime.utcnow() - last_m.created_at).seconds > minutes * 60: in_response_to = None else: in_response_to = last_m.content else: in_response_to = None # Always use generate reponse # Chatterbot tries to learn based on the result it comes up with, which is dumb log.debug("Generating response") Statement = self.chatbot.storage.get_object("statement") future = await self.loop.run_in_executor( None, self.chatbot.generate_response, Statement(text)) if not self._global_cache: self._global_cache = await self.config.all() if in_response_to is not None and self._global_cache["learning"]: log.debug("learning response") await self.loop.run_in_executor( None, partial( self.chatbot.learn_response, Statement(text), previous_statement=in_response_to, ), ) replying = None if ("reply" not in self._guild_cache[guild.id] and self.default_guild["reply"] ) or self._guild_cache[guild.id]["reply"]: if message != ctx.channel.last_message: replying = message if future and str(future): self._last_message_per_channel[ ctx.channel.id] = await channel.send(str(future), reference=replying) else: await ctx.send(":thinking:")
async def on_message_without_command(self, message: discord.Message): """ Credit to https://github.com/Twentysix26/26-Cogs/blob/master/cleverbot/cleverbot.py for on_message recognition of @bot Credit to: https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/redbot/cogs/customcom/customcom.py#L508 for the message filtering """ ########### if len(message.content) < 2 or message.author.bot: return guild: discord.Guild = getattr(message, "guild", None) if await self.bot.cog_disabled_in_guild(self, guild): return ctx: commands.Context = await self.bot.get_context(message) if ctx.prefix is not None: # Probably unnecessary, we're in on_message_without_command return ########### # Thank you Cog-Creators channel: discord.TextChannel = message.channel # is_reply = False # this is only useful with in_response_to if (message.reference is not None and isinstance(message.reference.resolved, discord.Message) and message.reference.resolved.author.id == self.bot.user.id): # is_reply = True # this is only useful with in_response_to pass # this is a reply to the bot, good to go elif guild is not None and channel.id == await self.config.guild( guild).chatchannel(): pass # good to go else: when_mentionables = commands.when_mentioned(self.bot, message) prefix = my_local_get_prefix(when_mentionables, message.content) if prefix is None: # print("not mentioned") return message.content = message.content.replace(prefix, "", 1) text = message.clean_content async with channel.typing(): future = await self.loop.run_in_executor(None, self.chatbot.get_response, text) replying = None if await self.config.guild(guild).reply(): if message != ctx.channel.last_message: replying = message if future and str(future): await channel.send(str(future), reference=replying) else: await channel.send(":thinking:")