diff options
| -rw-r--r-- | bot/cogs/bot.py | 46 | ||||
| -rw-r--r-- | bot/constants.py | 4 |
2 files changed, 48 insertions, 2 deletions
diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py index 9351da472..9d8ee52aa 100644 --- a/bot/cogs/bot.py +++ b/bot/cogs/bot.py @@ -1,10 +1,14 @@ # coding=utf-8 -from discord import Embed +import ast +import time + +from discord import Embed, Message from discord.ext.commands import AutoShardedBot, Context, command, group from dulwich.repo import Repo -from bot.constants import PYTHON_GUILD, VERIFIED_ROLE +from bot.constants import (HELP1_CHANNEL, HELP2_CHANNEL, HELP3_CHANNEL, + PYTHON_CHANNEL, PYTHON_GUILD, VERIFIED_ROLE) from bot.decorators import with_role @@ -16,6 +20,13 @@ class Bot: def __init__(self, bot: AutoShardedBot): self.bot = bot + # Stores allowed channels plus unix timestamp from last call + self.code_block_channels = {HELP1_CHANNEL: 0, + HELP2_CHANNEL: 0, + HELP3_CHANNEL: 0, + PYTHON_CHANNEL: 0 + } # noqa. E124 + @group(invoke_without_command=True, name="bot", hidden=True) @with_role(VERIFIED_ROLE) async def bot_group(self, ctx: Context): @@ -60,6 +71,37 @@ class Bot: await ctx.invoke(self.info) + async def on_message(self, msg: Message): + if msg.channel.id in self.code_block_channels: + if self.code_block_channels[msg.channel.id]-time.time() > 300: + if msg.content.count("\n") >= 3: + try: + tree = ast.parse(msg.content) + + # Attempts to parse the message into an AST node. + # Invalid Python code will raise a SyntaxError. + if not all(isinstance(node, ast.Expr) for node in tree.body): + + # Multiple lines of single words could be interpreted as expressions. + # This check is to avoid all nodes being parsed as expressions. + # (e.g. words over multiple lines) + howto = ("Please use syntax highlighted blocks, as it makes" + "your code more legible for other users.\n" + "\nTo do this, you should input your content like this:\n" + "\n\`\`\`python\n" + "print(\"Hello world!\")\n" + "\`\`\`\n" + "\nThis will result in the following:\n" + "```\n" + "print(\"Hello world!\")" + "```" + ) # noqa. E124 + information = Embed(title="Code formatting", description=howto) + await msg.channel.send(embed=information) + self.code_block_channels[msg.channel.id] = time.time() + except SyntaxError: + pass + def setup(bot): bot.add_cog(Bot(bot)) diff --git a/bot/constants.py b/bot/constants.py index 582bca2fa..aa546b66b 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -5,6 +5,10 @@ import os PYTHON_GUILD = 267624335836053506 BOT_CHANNEL = 267659945086812160 +HELP1_CHANNEL = 303906576991780866 +HELP2_CHANNEL = 303906556754395136 +HELP3_CHANNEL = 303906514266226689 +PYTHON_CHANNEL = 267624335836053506 DEVLOG_CHANNEL = 409308876241108992 VERIFICATION_CHANNEL = 352442727016693763 |