Skip to content

Instantly share code, notes, and snippets.

@dxsmiley
Created October 26, 2017 04:49
Show Gist options
  • Select an option

  • Save dxsmiley/fd3b6537606249573fc1aa23a27a0b51 to your computer and use it in GitHub Desktop.

Select an option

Save dxsmiley/fd3b6537606249573fc1aa23a27a0b51 to your computer and use it in GitHub Desktop.
A discord bot that corrects your writing and makes suggestions.
'''
grammar_bot.py
A discord bot that unrelentingly corrects your
writing and makes suggestions.
This is silly. Don't actually use it in anything
serious.
Srsly.
'''
import discord
import aiohttp
import json
API_URL = 'https://api.textgears.com/check.php'
TEXTGEARS_KEY = '...'
DISCORD_TOKEN = '...'
LENGTH_LIMIT = 100
RESPONSE_TEMPLATE = '''\
Your writing could be improved!
```
{}
{}
```
Suggested alternatives: {}
'''
client = discord.Client()
@client.event
async def on_message(message):
if not message.author.bot and len(message.clean_content) <= LENGTH_LIMIT:
print('Checking', message.clean_content)
jdata = None
async with aiohttp.ClientSession() as session:
params = {
'text': message.clean_content,
'key': TEXTGEARS_KEY
}
async with session.get(API_URL, params = params) as response:
response.raise_for_status()
jdata = await response.json()
print('Server gave\n', json.dumps(jdata, indent = 4))
if len(jdata.get('errors', [])) > 0:
error = jdata['errors'][0]
response = RESPONSE_TEMPLATE.format(
message.clean_content.replace('\n', ' '),
' ' * error['offset'] + '^' * error['length'],
', '.join(f'`{i}`' for i in error['better'])
)
await client.send_message(message.channel, response)
client.run(DISCORD_TOKEN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment