From 0538bca8f32789a9f90a37329d55c4b9efaa1348 Mon Sep 17 00:00:00 2001 From: Henrik Böving Date: Mon, 26 Feb 2018 02:09:20 +0100 Subject: Adding autocodeblocking (#15) * autocodeblocking * ast fix better type annotations * ci there you go * line too long ==> remove comment * deleting message + lowercase python * comments * remove docstrings * you may * not sure what you mean with move to a new line maybe this? * descriptive enough? * better explanation of text filtering * rewrite * fstring * pep8 * better comment * discord message length * ok shady * ok shady * "This is still async discord.py" * didnt see josephs comment * minor comment changes * blank and typo * typo.. * embed sending * adding waiting time + embed dont know if this is the correct way to store the field string * forgot await * but now i know * i heard him * pls * more descriptive variable name * comment * and better string formatting * approve me lemon * here we go aperture * there we go again * name error..... damn linter plugins * you get it aperture * ok lemon * ok aperture * constants instead of plain channel ID * forgot one * constants in bot.py * lemons way ftw * flake8 is weird man * aperture 1 * next aperture * dunno * imports * next lemon --- bot/cogs/bot.py | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 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 -- cgit v1.2.3