Skip to content

Commit

Permalink
feat: Spam Detection (#83)
Browse files Browse the repository at this point in the history
* feat: Add basic spam detection and handling

* feat: Add spam logging

* chore: Run Black

* chore: Update known spam messages

* chore: Update deploy workflow with LOG_CHANNEL_ID environment variable

* chore: Update spam log message layout

* chore: Add SPAM_CHECK_MIN_MSG and improve log messages
  • Loading branch information
phoenixpereira authored Nov 21, 2024
1 parent 74d271f commit da027a5
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 4 deletions.
1 change: 1 addition & 0 deletions .example.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ TENOR_API_KEY="TENOR_API_KEY"
GEMINI_API_KEY="GEMINI_API_KEY"
REQUESTS_PER_MINUTE=3
LIMIT_WINDOW=60
LOG_CHANNEL_ID=LOG_CHANNEL_ID
2 changes: 2 additions & 0 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ jobs:
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
REQUESTS_PER_MINUTE: ${{ secrets.REQUESTS_PER_MINUTE }}
LIMIT_WINDOW: ${{ secrets.LIMIT_WINDOW }}
LOG_CHANNEL_ID: ${{ secrets.LOG_CHANNEL_ID }}
run: |
echo "$KEY" > private_key && chmod 600 private_key
ssh -v -o StrictHostKeyChecking=no -i private_key ${USER}@${HOSTNAME} '
Expand All @@ -98,6 +99,7 @@ jobs:
echo GEMINI_API_KEY=${{ secrets.GEMINI_API_KEY }} >> .env
echo REQUESTS_PER_MINUTE=${{ secrets.REQUESTS_PER_MINUTE }} >> .env
echo LIMIT_WINDOW=${{ secrets.LIMIT_WINDOW }} >> .env
echo LOG_CHANNEL_ID=${{ secrets.LOG_CHANNEL_ID }} >> .env
docker load -i duckbot.tar.gz
docker compose up -d
'
102 changes: 101 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ aiosqlite = "^0.20.0"
schedule = "^1.2.2"
pytz = "^2024.1"
matplotlib = "^3.9.2"
levenshtein = "^0.26.1"

[build-system]
requires = ["poetry-core"]
Expand Down
17 changes: 14 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from constants.colours import LIGHT_YELLOW
from commands import gemini, skullboard, help_menu
from utils import time
from utils import time, spam_detection

# Load environment variables from .env file
load_dotenv()
Expand All @@ -32,6 +32,7 @@
SKULLBOARD_CHANNEL_ID = int(os.environ["SKULLBOARD_CHANNEL_ID"])
TENOR_API_KEY = os.environ["TENOR_API_KEY"]
GEMINI_API_KEY = os.environ["GEMINI_API_KEY"]
SPAM_CHECK_MIN_MSG = 3

# Load the permissions the bot has been granted in the previous configuration
intents = Intents.default()
Expand Down Expand Up @@ -183,10 +184,20 @@ async def help(interaction: Interaction):
# Ignore non-slash commands
@client.event
async def on_message(message: Message):
# Ignore DMs by checking if the message was sent in a server
if message.guild is None:
# Ignore DMs and bot's own messages
if message.guild is None or message.author.bot:
return

# Count user's messages in channel
count = 0
async for msg in message.channel.history(limit=None):
if msg.author.id == message.author.id:
count += 1

# If the user has sent less than SPAM_CHECK_MIN_MSG messages in the channel, check for spam
if count < SPAM_CHECK_MIN_MSG:
await spam_detection.check_spam(message)

if (
client.user.mentioned_in(message) or "d.chat" in message.clean_content
) and message.author != client.user:
Expand Down
Loading

0 comments on commit da027a5

Please sign in to comment.