initial commit

This commit is contained in:
user
2025-07-15 22:40:51 -04:00
commit 98db3a2cc8
6 changed files with 2058 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
venv
.env
server_settings.json

9
LICENSE Normal file
View File

@@ -0,0 +1,9 @@
MIT License
Copyright (c) 2025 amrfti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

23
README.md Normal file
View File

@@ -0,0 +1,23 @@
# linkbomber
Discord bot that cleans tracking elements from messages
You can invite the bot to your server [here](https://discord.com/oauth2/authorize?client_id=1336105313589395616&scope=bot&permissions=274877990912).
## Usage
By default, the bot will automatically strip tracking elements from any URL it supports,
this feature can be disabled with the command: `/toggle`. Then, to use the functionality
manually, just use the command `/bomb` which will strip the latest link posted in a channel
of its tracking elements.
## Setup
1. Clone the repository
2. Create a file named `.env` in the same directory as the bot
3. Add the following contents to the file
```
DISCORD_BOT_TOKEN=your_bot_token_here
```
4. Install the dependencies with `pip install -r requirements.txt`
5. Run the bot with `python main.py`

128
main.py Normal file
View File

@@ -0,0 +1,128 @@
import discord
import json
import re
import os
from urllib.parse import urlparse, parse_qs, urlencode, urlunparse
from discord import app_commands
from dotenv import load_dotenv
load_dotenv()
def load_json(filename):
try:
with open(filename, "r", encoding="utf-8") as f:
return json.load(f)
except FileNotFoundError:
return {}
def save_json(data, filename):
with open(filename, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
rules = load_json("url_rules.json")
server_settings = load_json("server_settings.json")
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
def clean_url(url):
parsed_url = urlparse(url)
providers = [p for p in rules["providers"].items() if p[0] != "globalRules"] + [
("globalRules", rules["providers"].get("globalRules", {}))
]
for _, data in providers:
if re.match(data.get("urlPattern", ""), url):
query_params = parse_qs(parsed_url.query, keep_blank_values=True)
filtered_params = {
k: v
for k, v in query_params.items()
if not any(re.fullmatch(rule, k) for rule in data.get("rules", []))
}
cleaned_query = urlencode(filtered_params, doseq=True)
return (
urlunparse((
parsed_url.scheme,
parsed_url.netloc,
parsed_url.path,
parsed_url.params,
cleaned_query,
parsed_url.fragment,
))
if cleaned_query != parsed_url.query
else None
)
return None
@client.event
async def on_ready():
await tree.sync()
@client.event
async def on_message(message):
if message.author == client.user or server_settings.get(
str(message.guild.id), {}
).get("disabled", False):
return
for word in message.content.split():
if word.startswith(("http://", "https://")) and (cleaned := clean_url(word)):
await message.channel.send(f"Cleaned link: {cleaned}")
break
@tree.command(
name="toggle",
description="Toggle automatic link cleaning for this server (requires Manage Messages permission).",
)
async def toggle(interaction: discord.Interaction):
if not interaction.user.guild_permissions.manage_messages:
await interaction.response.send_message(
"You need 'Manage Messages' permission to use this command.", ephemeral=True
)
return
guild_id = str(interaction.guild.id)
server_settings[guild_id] = {
"disabled": not server_settings.get(guild_id, {}).get("disabled", False)
}
save_json(server_settings, "server_settings.json")
await interaction.response.send_message(
f"Link cleaning {'disabled' if server_settings[guild_id]['disabled'] else 'enabled'}."
)
@tree.command(
name="bomb", description="Cleans the first link found in the last 20 messages."
)
async def bomb(interaction: discord.Interaction):
async for msg in interaction.channel.history(limit=20):
for word in msg.content.split():
if word.startswith(("http://", "https://")) and (
cleaned := clean_url(word)
):
await interaction.response.send_message(f"Cleaned link: {cleaned}")
return
await interaction.response.send_message("No links found.")
@tree.command(
name="about",
description="Explains the purpose of the bot and its privacy features.",
)
async def about(interaction: discord.Interaction):
await interaction.response.send_message(
"This bot removes tracking parameters from links to improve privacy. Use `/toggle` to enable/disable auto-cleaning, `/bomb` to clean a recent link manually, and `/about` for info."
)
TOKEN = os.getenv("DISCORD_BOT_TOKEN")
client.run(TOKEN)

2
requirements.txt Normal file
View File

@@ -0,0 +1,2 @@
discord
python-dotenv

1893
url_rules.json Normal file

File diff suppressed because it is too large Load Diff