Exemple #1
0
    def clean_msg(self, contents: str, message: discord.Message) -> str:
        """cleans up an incoming message to be processed by pyborg"""
        # first fix up any nickname flagged mentions, as fixed versions are needed for `_replace_mentions`
        incoming_message = self._fix_mentions(contents)
        # replace all emojis with their text versions
        incoming_message = self._replace_emojis(incoming_message, message)
        # and all mentions with text versions
        incoming_message = self._replace_mentions(incoming_message, message)
        # normalize awoos
        incoming_message = normalize_awoos(incoming_message)

        return incoming_message
Exemple #2
0
 def test_mixed_awoo(self):
     out = normalize_awoos("real Awoooooo hours who up?")
     self.assertEqual(out, "real awoo hours who up?")
Exemple #3
0
 def test_uppercase_awoo(self):
     out = normalize_awoos("real AWOOOOOOOO hours who up?")
     self.assertEqual(out, "real awoo hours who up?")
Exemple #4
0
 def test_real_world(self):
     out = normalize_awoos("real awooooo hours who up?")
     self.assertEqual(out, "real awoo hours who up?")
Exemple #5
0
 def test_2_awoo(self):
     out = normalize_awoos("awooooooo awoooooooooooooooo")
     self.assertEqual(out, "awoo awoo")
Exemple #6
0
 def test_1_awoo(self):
     out = normalize_awoos("awooooooo")
     self.assertEqual(out, "awoo")
Exemple #7
0
 def test_mixed_awoo(self):
     out = normalize_awoos("real Awoooooo hours who up?")
     self.assertEqual(out, "real awoo hours who up?")
Exemple #8
0
 def test_uppercase_awoo(self):
     out = normalize_awoos("real AWOOOOOOOO hours who up?")
     self.assertEqual(out, "real awoo hours who up?")
Exemple #9
0
 def test_real_world(self):
     out = normalize_awoos("real awooooo hours who up?")
     self.assertEqual(out, "real awoo hours who up?")
Exemple #10
0
    async def on_message(self, message: discord.Message) -> None:
        """message.content  ~= <@221134985560588289> you should play dota"""
        logger.debug(message.content)
        if message.content and message.content[0] == "!":
            command_name = message.content.split()[0][1:]
            if command_name in ["list", "help"]:
                help_text = "I have a bunch of commands:"
                for k, _ in self.registry.registered.items():
                    help_text += " !{}".format(k)
                await message.channel.send(help_text)
            else:
                if command_name in self.registry.registered:
                    command = self.registry.registered[command_name]
                    logger.debug("cmd: Running command %s", command)
                    logger.debug("cmd: pass message?: %s", command.pass_msg)
                    if command.pass_msg:
                        await message.channel.send(command(msg=message.content)
                                                   )
                    else:
                        await message.channel.send(command())
        if message.author == self.user:
            logger.debug("Not learning/responding to self")
            return

        if self.save_status_count % 5:
            async with self.aio_session.get(
                    f"http://{self.multi_server}:{self.multi_port}/meta/status.json",
                    raise_for_status=True) as ret_status:
                data = await ret_status.json()
                if data["status"]:
                    await self.change_presence(
                        activity=discord.Game("Saving brain..."))
                else:
                    await self.change_presence(
                        activity=discord.Game("hack the planet"))

        self.save_status_count += 1

        # Custom Emoji handling here
        # DEBUG:pyborg.mod.mod_discord:<:weedminion:392111795642433556>
        # is this cached? is this fast? who the f**k knows
        if "<:" in message.content:
            emojis_possible = message.guild.emojis
            server_emojis = [x.name for x in emojis_possible]
            logger.debug("got server emojis as: %s", str(server_emojis))
            incoming_message = self._extract_emoji(message.content,
                                                   server_emojis)
        else:
            incoming_message = message.content

        # Strip nicknames for pyborg
        line_list = list()
        for chunk in incoming_message.split():
            if chunk.startswith("<@!"):
                chunk = "#nick"
            line_list.append(chunk)

        line = " ".join(line_list)
        try:
            if self.settings["discord"]["plaintext_ping"]:
                exp = re.compile(message.guild.me.display_name, re.IGNORECASE)
                line = exp.sub("#nick", line)
        except KeyError:
            pass

        logger.debug("post nick replace: %s", line)
        line = normalize_awoos(line)

        if self.settings['discord']['learning']:
            await self.learn(line)

        if self.user.mentioned_in(message) or self._plaintext_name(message):
            async with message.channel.typing():
                msg = await self.reply(line)
                logger.debug("on message: %s", msg)
                if msg:
                    logger.debug("Sending message...")
                    # if custom emoji: replace to <:weedminion:392111795642433556>
                    # message.server map to full custom emoji
                    # TODO: measure time spent here?
                    emoji_map = {x.name: x for x in message.guild.emojis}
                    for word in msg.split():
                        if word in emoji_map:
                            e = emoji_map[word]
                            msg = msg.replace(word,
                                              "<:{}:{}>".format(e.name, e.id))
                    msg = msg.replace("#nick", str(message.author.mention))
                    msg = msg.replace("@everyone", "`@everyone`")
                    msg = msg.replace("@here", "`@here`")
                    await message.channel.send(msg)
Exemple #11
0
    async def on_message(self, message: discord.Message) -> None:
        """message.content  ~= <@221134985560588289> you should play dota"""
        logger.debug(message.content)
        if message.content and message.content[0] == "!":
            command_name = message.content.split()[0][1:]
            if command_name in ["list", "help"]:
                help_text = "I have a bunch of commands:"
                for k, v in self.registry.registered.items():
                    help_text += " !{}".format(k)
                await self.send_message(message.channel, help_text)
            else:
                if command_name in self.registry.registered:
                    command = self.registry.registered[command_name]
                    logger.info("Running command %s", command)
                    logger.info("pass message?: %s", command.pass_msg)
                    if command.pass_msg:
                        await self.send_message(message.channel,
                                                command(msg=message.content))
                    else:
                        await self.send_message(message.channel, command())
        if message.author == self.user:
            logger.info("Not learning/responding to self")
            return

        # Custom Emoji handling here
        # DEBUG:pyborg.mod.mod_discord:<:weedminion:392111795642433556>
        # is this cached? is this fast? who the f**k knows
        if "<:" in message.content:
            e = message.server.emojis
            server_emojis = [x.name for x in e]
            logger.debug("got server emojis as: %s", str(server_emojis))
            incoming_message = self._extract_emoji(message.content,
                                                   server_emojis)

        else:
            incoming_message = message.content

        # Strip nicknames for pyborg
        line_list = list()
        for x in incoming_message.split():
            if x.startswith("<@!"):
                x = "#nick"
            line_list.append(x)

        logger.debug("post nick replace: %s", str(line_list))
        line = " ".join(line_list)
        line = normalize_awoos(line)

        if self.settings['discord']['learning']:
            self.learn(line)

        if self.user.mentioned_in(message):
            await self.send_typing(message.channel)
            msg = self.reply(line)
            logger.debug("on message: %s" % msg)
            if msg:
                logger.debug("Sending message...")
                # if custom emoji: replace to <:weedminion:392111795642433556>
                # message.server map to full custom emoji
                emoji_map = {x.name: x for x in message.server.emojis}
                for word in msg.split():
                    if word in emoji_map:
                        e = emoji_map[word]
                        msg = msg.replace(word,
                                          "<:{}:{}>".format(e.name, e.id))
                msg = msg.replace("#nick", str(message.author.mention))
                msg = msg.replace("@everyone", "`@everyone`")
                msg = msg.replace("@here", "`@here`")
                await self.send_message(message.channel, msg)
Exemple #12
0
    async def on_message(self, message: discord.Message) -> None:
        """message.content  ~= <@221134985560588289> you should play dota"""
        logger.debug(message.content)
        if message.content and message.content[0] == "!":
            command_name = message.content.split()[0][1:]
            if command_name in ["list", "help"]:
                help_text = "I have a bunch of commands:"
                for k, v in self.registry.registered.items():
                    help_text += " !{}".format(k)
                await message.channel.send(help_text)
            else:
                if command_name in self.registry.registered:
                    command = self.registry.registered[command_name]
                    logger.info("Running command %s", command)
                    logger.info("pass message?: %s", command.pass_msg)
                    if command.pass_msg:
                        await message.channel.send(command(msg=message.content))
                    else:
                        await message.channel.send(command())
        if message.author == self.user:
            logger.info("Not learning/responding to self")
            return

        # Custom Emoji handling here
        # DEBUG:pyborg.mod.mod_discord:<:weedminion:392111795642433556>
        # is this cached? is this fast? who the f**k knows
        if "<:" in message.content:
            e = message.guild.emojis
            server_emojis = [x.name for x in e]
            logger.debug("got server emojis as: %s", str(server_emojis))
            incoming_message = self._extract_emoji(message.content, server_emojis)

        else:
            incoming_message = message.content

        # Strip nicknames for pyborg
        line_list = list()
        for x in incoming_message.split():
            if x.startswith("<@!"):
                x = "#nick"
            line_list.append(x)

        line = " ".join(line_list)
        try:
            if self.settings["discord"]["plaintext_ping"]:
                line = line.replace(message.guild.me.display_name, "#nick")
                line = line.replace(message.guild.me.display_name.lower(), "#nick")
        except KeyError:
            pass

        logger.debug("post nick replace: %s", line)
        line = normalize_awoos(line)

        if self.settings['discord']['learning']:
            self.learn(line)

        if self.user.mentioned_in(message) or self._plaintext_name(message):
            async with message.channel.typing():
                msg = self.reply(line)
                logger.debug("on message: %s" % msg)
                if msg:
                    logger.debug("Sending message...")
                    # if custom emoji: replace to <:weedminion:392111795642433556>
                    # message.server map to full custom emoji
                    emoji_map = {x.name: x for x in message.guild.emojis}
                    for word in msg.split():
                        if word in emoji_map:
                            e = emoji_map[word]
                            msg = msg.replace(word, "<:{}:{}>".format(e.name, e.id))
                    msg = msg.replace("#nick", str(message.author.mention))
                    msg = msg.replace("@everyone", "`@everyone`")
                    msg = msg.replace("@here", "`@here`")
                    await message.channel.send(msg)