-
Notifications
You must be signed in to change notification settings - Fork 10
Optional cog "simplewriter" that enforces xkcd's simple writer in a text channel #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JoshuaS3
wants to merge
19
commits into
strinking:master
Choose a base branch
from
JoshuaS3:simplewriter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2546a86
Begin source for the optional cog 'simplewriter'
JoshuaS3 a78a749
Add 'simplewriter' cog settings to misc/config.toml
JoshuaS3 23769b7
Add compiled list of words to use in simplewriter
JoshuaS3 eb04125
Merge https://github.com/strinking/futaba into simplewriter
JoshuaS3 4d83980
Merge branch 'fix-reloader' into simplewriter
JoshuaS3 1dad752
Merge branch 'master' into simplewriter
JoshuaS3 c21699c
Merge branch 'master' into simplewriter
JoshuaS3 acb73fa
Update words list
JoshuaS3 1cbf577
Bring cog setup/teardown up to date (strinking/futaba#327)
JoshuaS3 df4686c
Finish simplewriter cog
JoshuaS3 3da7740
Fix formatting issues; disable pylint warning on words list
JoshuaS3 967d3c3
Stop adding empty strings to simplewriter's bad words list
JoshuaS3 210e310
Move words list to txt file, read at runtime
JoshuaS3 778b4ce
Run pylint and black on simplewriter cog
JoshuaS3 e03260a
Set default core_words_list value to [] instead of None
JoshuaS3 a9d3462
Make channel_id a member of the cog for easy access
JoshuaS3 09a4b99
Don't filter contractions in simplewriter
JoshuaS3 c8aae0b
Update code formatting, expand journal event
JoshuaS3 33b7c35
Merge listener methods into cog
JoshuaS3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # | ||
| # cogs/optional/simplewriter/__init__.py | ||
| # | ||
| # futaba - A Discord Mod bot for the Programming server | ||
| # Copyright (c) 2017-2020 Jake Richardson, Ammon Smith, jackylam5 | ||
| # | ||
| # futaba is available free of charge under the terms of the MIT | ||
| # License. You are free to redistribute and/or modify it under those | ||
| # terms. It is distributed in the hopes that it will be useful, but | ||
| # WITHOUT ANY WARRANTY. See the LICENSE file for more details. | ||
| # | ||
|
|
||
| from .core import SimplewriterCog | ||
|
|
||
|
|
||
| def setup(bot): | ||
| setup_simplewriter(bot) | ||
|
|
||
|
|
||
| def setup_simplewriter(bot): | ||
| cog = SimplewriterCog(bot) | ||
| bot.add_listener(cog.on_message, "on_message") | ||
| bot.add_listener(cog.on_message_edit, "on_message_edit") | ||
| bot.add_cog(cog) | ||
|
|
||
|
|
||
| def teardown(bot): | ||
| teardown_simplewriter(bot) | ||
|
|
||
|
|
||
| def teardown_simplewriter(bot): | ||
| bot.remove_cog(SimplewriterCog.__name__) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # | ||
| # cogs/optional/simplewriter/core.py | ||
| # | ||
| # futaba - A Discord Mod bot for the Programming server | ||
| # Copyright (c) 2017-2020 Jake Richardson, Ammon Smith, jackylam5 | ||
| # | ||
| # futaba is available free of charge under the terms of the MIT | ||
| # License. You are free to redistribute and/or modify it under those | ||
| # terms. It is distributed in the hopes that it will be useful, but | ||
| # WITHOUT ANY WARRANTY. See the LICENSE file for more details. | ||
| # | ||
|
|
||
| """ | ||
| A thing that lets you write stuff in one word box, but only if it's made up of | ||
| the first ten hundred most used words of the language. | ||
|
|
||
| (https://xkcd.com/simplewriter) | ||
| """ | ||
|
|
||
| from futaba.cogs.abc import AbstractCog | ||
|
|
||
| from .simple_filter import simple_filter | ||
|
|
||
|
|
||
| class SimplewriterCog(AbstractCog): | ||
| __slots__ = ("journal", "channel_id") | ||
|
|
||
| def __init__(self, bot): | ||
| super().__init__(bot) | ||
| self.journal = bot.get_broadcaster("/simplewriter") | ||
| self.channel_id = int(bot.config.optional_cogs["simplewriter"]["channel-id"]) | ||
|
|
||
| def setup(self): | ||
| pass | ||
|
|
||
| def cog_unload(self): | ||
| self.bot.remove_listener(self.on_message, "on_message") | ||
| self.bot.remove_listener(self.on_message_edit, "on_message_edit") | ||
|
|
||
| async def on_message(self, message): | ||
| await simple_filter(self, message) | ||
|
|
||
| async def on_message_edit(self, before, after): | ||
| if after: | ||
| await simple_filter(self, after) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # | ||
| # cogs/optional/simplewriter/simple_filter.py | ||
| # | ||
| # futaba - A Discord Mod bot for the Programming server | ||
| # Copyright (c) 2017-2020 Jake Richardson, Ammon Smith, jackylam5 | ||
| # | ||
| # futaba is available free of charge under the terms of the MIT | ||
| # License. You are free to redistribute and/or modify it under those | ||
| # terms. It is distributed in the hopes that it will be useful, but | ||
| # WITHOUT ANY WARRANTY. See the LICENSE file for more details. | ||
| # | ||
|
|
||
| """ | ||
| Filters messages through the allowed wordlist, deleting unallowed words. | ||
| """ | ||
|
|
||
| import logging | ||
| import re | ||
| from datetime import datetime | ||
|
|
||
| import discord | ||
| from discord import MessageType | ||
|
|
||
| from futaba.utils import plural | ||
|
|
||
| from .words import core_words_list | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| REGEX_SPLIT = r"[^a-zA-Z0-9_']" | ||
|
|
||
| __all__ = ["simple_filter"] | ||
|
|
||
|
|
||
| async def simple_filter(cog, message): | ||
| """ | ||
| Filter Discord messages for the simplewriter channel | ||
| """ | ||
|
|
||
| # Message is in designated channel | ||
| if message.channel.id != cog.channel_id: | ||
| return | ||
|
|
||
| # Don't filter bot messages | ||
| if message.author.id == cog.bot.user.id: | ||
| return | ||
|
|
||
| # Not a special message type | ||
| if message.type != MessageType.default: | ||
| return | ||
|
|
||
| # Make sure we actually can remove this message | ||
| if not message.channel.permissions_for(message.guild.me).manage_messages: | ||
| return | ||
|
|
||
| logger.debug( | ||
| "Checking message id %d (by '%s' (%d)) against simplewriter core words list", | ||
| message.id, | ||
| message.author.name, | ||
| message.author.id, | ||
| ) | ||
|
|
||
| split = re.split(REGEX_SPLIT, message.content) | ||
| bad_words = [] | ||
| for word in split: | ||
| cleaned = word.strip() | ||
| if not cleaned: | ||
| continue | ||
| if not cleaned.lower() in core_words_list: | ||
| if not cleaned in bad_words: | ||
| try: # test to see if input is a number | ||
| int(cleaned) | ||
| except ValueError: | ||
| bad_words.append(cleaned) | ||
|
|
||
| if len(bad_words) > 0: | ||
| await message.delete() | ||
|
|
||
| content = ( | ||
| f"Message id {message.id} (by '{message.author.name}' ({message.author.id})) filtered from " | ||
| "simplewriter channel" | ||
| ) | ||
| cog.journal.send( | ||
| "message/delete", | ||
| message.guild, | ||
| content, | ||
| icon="delete", | ||
| channel=message.channel, | ||
| message=message, | ||
| ) | ||
|
|
||
| bad_words_plural = plural(len(bad_words)) | ||
| article = "some" if len(bad_words) > 1 else "a" | ||
|
|
||
| help_embed = discord.Embed() | ||
| help_embed.color = discord.Color.red() | ||
| help_embed.title = f"You used {article} less simple word{bad_words_plural}." | ||
| help_embed.description = ( | ||
| f"Your message in {message.channel.mention} has been removed.\n" | ||
| "You can use [xkcd's simplewriter](https://xkcd.com/simplewriter) " | ||
| "to see what words are valid in this channel." | ||
| ) | ||
| help_embed.timestamp = datetime.now() | ||
| help_embed.add_field(name="Full message", value=message.content) | ||
| help_embed.add_field( | ||
| name=f"Bad word{bad_words_plural}", value=", ".join(bad_words) | ||
| ) | ||
| help_embed.set_footer(text=message.id) | ||
|
|
||
| dm_channel = message.author.dm_channel or await message.author.create_dm() | ||
| await dm_channel.send(embed=help_embed) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # | ||
| # cogs/optional/simplewriter/words/__init__.py | ||
| # | ||
| # futaba - A Discord Mod bot for the Programming server | ||
| # Copyright (c) 2017-2020 Jake Richardson, Ammon Smith, jackylam5 | ||
| # | ||
| # futaba is available free of charge under the terms of the MIT | ||
| # License. You are free to redistribute and/or modify it under those | ||
| # terms. It is distributed in the hopes that it will be useful, but | ||
| # WITHOUT ANY WARRANTY. See the LICENSE file for more details. | ||
| # | ||
|
|
||
| __all__ = ["core_words_list"] | ||
|
|
||
| from .words import core_words_list |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # | ||
| # cogs/optional/simplewriter/words/words.py | ||
| # | ||
| # futaba - A Discord Mod bot for the Programming server | ||
| # Copyright (c) 2017-2020 Jake Richardson, Ammon Smith, jackylam5 | ||
| # | ||
| # futaba is available free of charge under the terms of the MIT | ||
| # License. You are free to redistribute and/or modify it under those | ||
| # terms. It is distributed in the hopes that it will be useful, but | ||
| # WITHOUT ANY WARRANTY. See the LICENSE file for more details. | ||
| # | ||
|
|
||
| import os | ||
|
|
||
| __all__ = ["core_words_list"] | ||
|
|
||
| core_words_list = [] | ||
| core_words_list_path = os.path.join(os.path.dirname(__file__), "core_words_list.txt") | ||
|
|
||
| with open(core_words_list_path, "r") as f: | ||
| core_words_list = f.read().split(",") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't always expect this directory to be writeable, it might be in
/usror something. Just have a line in the config instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for a read operation of a text file in the same directory as this source file. The text file will not change and is not configurable through means other than a commit due to the static nature of the source.