-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Message (anti-spam) constraints (#594)
* Message constraints * Documentation * Fix documentation
- Loading branch information
1 parent
e871f05
commit 72fc2f3
Showing
9 changed files
with
165 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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
Binary file modified
BIN
-18.9 KB
(28%)
docs/source/guide/core/images/message_definition_example_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 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 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 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 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,46 @@ | ||
""" | ||
Implements additional message constraints, that are required to pass | ||
for a message to be sent. | ||
.. versionadded:: 4.1 | ||
""" | ||
from __future__ import annotations | ||
from abc import abstractmethod, ABC | ||
from _discord import TextChannel | ||
|
||
from .autochannel import AutoCHANNEL | ||
from ..misc import doc_category | ||
|
||
|
||
__all__ = ("AntiSpamMessageConstraint",) | ||
|
||
|
||
class BaseMessageConstraint(ABC): | ||
@abstractmethod | ||
def check(self, channels: list[TextChannel]) -> list[TextChannel]: | ||
""" | ||
Checks if the message can be sent based on the configured check. | ||
""" | ||
|
||
|
||
@doc_category("Message constraints") | ||
class AntiSpamMessageConstraint(BaseMessageConstraint): | ||
""" | ||
Prevents a new message to be sent if the last message in the same channel was | ||
sent by us, thus preventing spam on inactivate channels. | ||
.. versionadded:: 4.1 | ||
""" | ||
def __init__(self, per_channel: bool = True) -> None: | ||
self.per_channel = per_channel | ||
super().__init__() | ||
|
||
def check(self, channels: list[TextChannel | AutoCHANNEL]) -> list[TextChannel]: | ||
allowed = list(filter( | ||
lambda channel: channel.last_message is None or | ||
channel.last_message.author.id != channel._state.user.id, channels | ||
)) | ||
if not self.per_channel and len(allowed) != len(channels): # In global mode, only allow to all channels | ||
return [] | ||
|
||
return allowed |
This file contains 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 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