Merge pull request #3 from bobloy/coglint

Coglint Initial
pull/6/head
bobloy 7 years ago committed by GitHub
commit 5dd803faa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,6 +6,7 @@ Cog Function
| --- | --- | --- |
| ccrole | **Beta** | <details><summary>Create custom commands that also assign roles</summary>May have some bugs, please create an issue if you find any</details> |
| chatter | **Alpha** | <details><summary>Chat-bot trained to talk like your guild</summary>Missing some key features, but currently functional</details> |
| coglint | **Alpha** | <details><summary>Error check code in python syntax posted to discord</summary>Works, but probably needs more turning to work for cogs</details> |
| fight | **Incomplete** | <details><summary>Organize bracket tournaments within discord</summary>Still in-progress, a massive project</details> |
| flag | **Incomplete** | <details><summary>Create temporary marks on users that expire after specified time</summary>Not yet ported to v3</details> |
| hangman | **Alpha** | <details><summary>Play a game of hangman</summary>Some visual glitches and needs more customization</details> |

@ -0,0 +1,5 @@
from .coglint import CogLint
def setup(bot):
bot.add_cog(CogLint(bot))

@ -0,0 +1,88 @@
import discord
from discord.ext import commands
from redbot.core import Config, checks, RedContext
from redbot.core.bot import Red
from pylint import epylint as lint
from redbot.core.data_manager import cog_data_path
class CogLint:
"""
V3 Cog Template
"""
def __init__(self, bot: Red):
self.bot = bot
self.config = Config.get_conf(self, identifier=9811198108111121, force_registration=True)
default_global = {
"lint": True
}
default_guild = {}
self.path = str(cog_data_path(self)).replace('\\', '/')
self.do_lint = None
self.counter = 0
# self.answer_path = self.path + "/tmpfile.py"
self.config.register_global(**default_global)
self.config.register_guild(**default_guild)
@commands.command()
async def autolint(self, ctx: RedContext):
"""Toggles automatically linting code"""
curr = await self.config.lint()
self.do_lint = not curr
await self.config.lint.set(not curr)
await ctx.send("Autolinting is now set to {}".format(not curr))
@commands.command()
async def lint(self, ctx: RedContext, *, code):
"""Lint python code
Toggle autolinting with `[p]autolint`
"""
await self.lint_message(ctx.message)
await ctx.send("Hello World")
async def lint_code(self, code):
self.counter += 1
path = self.path + "/{}.py".format(self.counter)
with open(path, 'w') as codefile:
codefile.write(code)
future = await self.bot.loop.run_in_executor(None, lint.py_run, path, 'return_std=True')
if future:
(pylint_stdout, pylint_stderr) = future
else:
(pylint_stdout, pylint_stderr) = None, None
# print(pylint_stderr)
# print(pylint_stdout)
return pylint_stdout, pylint_stderr
async def lint_message(self, message):
if self.do_lint is None:
self.do_lint = await self.config.lint()
if not self.do_lint:
return
code_blocks = message.content.split('```')[1::2]
for c in code_blocks:
is_python, code = c.split(None, 1)
is_python = is_python.lower() == 'python'
if is_python: # Then we're in business
linted, errors = await self.lint_code(code)
linted = linted.getvalue()
errors = errors.getvalue()
await message.channel.send(linted)
# await message.channel.send(errors)
async def on_message(self, message: discord.Message):
await self.lint_message(message)

@ -0,0 +1,20 @@
{
"author": [
"Bobloy"
],
"bot_version": [
3,
0,
0
],
"description": "Lint python code posted in chat",
"hidden": true,
"install_msg": "Thank you for installing CogLint",
"requirements": [],
"short": "Python cog linter",
"tags": [
"bobloy",
"utils",
"tools"
]
}
Loading…
Cancel
Save