Skip to content

Commit 64bf0bf

Browse files
refactor: remove fetchurl command and related utilities
- Deleted the `fetchurl` command and its associated helper functions from the admin cog. - Updated the help documentation to remove references to the `fetchurl` command. - Added URL validation for the `applytemplate_prefix` and `applytemplate` commands to only allow GitHub repository URLs. These changes streamline the admin commands and enhance URL handling for template applications.
1 parent 5ecb581 commit 64bf0bf

File tree

4 files changed

+10
-260
lines changed

4 files changed

+10
-260
lines changed

src/gitcord/cogs/admin.py

Lines changed: 9 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
"""
55

66
import discord
7-
import requests
8-
from bs4 import BeautifulSoup
97
from discord import app_commands
108
from discord.ext import commands
119

@@ -14,6 +12,7 @@
1412
import os
1513
import shutil
1614
from urllib.parse import urlparse
15+
import re
1716

1817
from .base_cog import BaseCog
1918
from ..utils.helpers import (
@@ -33,117 +32,6 @@ def __init__(self, bot: commands.Bot):
3332
super().__init__(bot)
3433
self.logger.info("Admin cog loaded")
3534

36-
async def _fetch_url_content(self, url: str) -> str:
37-
"""Fetch and clean content from a URL."""
38-
# Validate URL format
39-
if not url.startswith(("http://", "https://")):
40-
url = "https://" + url
41-
42-
# Fetch the webpage
43-
headers = {
44-
"User-Agent": (
45-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
46-
"(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
47-
)
48-
}
49-
response = requests.get(url, headers=headers, timeout=10)
50-
response.raise_for_status()
51-
52-
# Parse HTML content
53-
soup = BeautifulSoup(response.content, "html.parser")
54-
55-
# Remove script and style elements
56-
for script in soup(["script", "style"]):
57-
script.decompose()
58-
59-
# Get text content and clean it
60-
text = soup.get_text()
61-
return clean_webpage_text(text)
62-
63-
@commands.command(name="fetchurl")
64-
@commands.has_permissions(administrator=True)
65-
async def fetchurl_prefix(self, ctx: commands.Context, url: str) -> None:
66-
"""Prefix command to fetch text content from a URL."""
67-
try:
68-
# Send initial response
69-
await ctx.send("🔄 Fetching content from URL...")
70-
71-
# Fetch and process content
72-
text = await self._fetch_url_content(url)
73-
74-
if not text.strip():
75-
await self.send_error(
76-
ctx,
77-
"❌ No Content Found",
78-
"No readable text content was found on the provided URL.",
79-
)
80-
return
81-
82-
# Create embed with the content
83-
embed = create_embed(
84-
title=f"📄 Content from {url}",
85-
description=f"```\n{text}\n```",
86-
color=discord.Color.blue(),
87-
footer=f"Fetched from {url}",
88-
)
89-
90-
await ctx.send(embed=embed)
91-
92-
except requests.exceptions.RequestException as e:
93-
await self.send_error(
94-
ctx, "❌ Fetch Error", f"Failed to fetch content from the URL: {str(e)}"
95-
)
96-
except (discord.DiscordException, OSError) as e:
97-
self.logger.error("Error in fetchurl command: %s", e)
98-
await self.send_error(
99-
ctx, "❌ Unexpected Error", f"An unexpected error occurred: {str(e)}"
100-
)
101-
102-
@app_commands.command(
103-
name="fetchurl", description="Fetch and display text content from a URL"
104-
)
105-
@app_commands.describe(url="The URL to fetch text content from")
106-
@app_commands.checks.has_permissions(administrator=True)
107-
async def fetchurl(self, interaction: discord.Interaction, url: str) -> None:
108-
"""Slash command to fetch text content from a URL."""
109-
await interaction.response.defer()
110-
111-
try:
112-
# Fetch and process content
113-
text = await self._fetch_url_content(url)
114-
115-
if not text.strip():
116-
await self.send_interaction_error(
117-
interaction,
118-
"❌ No Content Found",
119-
"No readable text content was found on the provided URL.",
120-
)
121-
return
122-
123-
# Create embed with the content
124-
embed = create_embed(
125-
title=f"📄 Content from {url}",
126-
description=f"```\n{text}\n```",
127-
color=discord.Color.blue(),
128-
footer=f"Fetched from {url}",
129-
)
130-
131-
await interaction.followup.send(embed=embed)
132-
133-
except requests.exceptions.RequestException as e:
134-
await self.send_interaction_error(
135-
interaction,
136-
"❌ Fetch Error",
137-
f"Failed to fetch content from the URL: {str(e)}",
138-
)
139-
except (discord.DiscordException, OSError) as e:
140-
self.logger.error("Error in fetchurl command: %s", e)
141-
await self.send_interaction_error(
142-
interaction,
143-
"❌ Unexpected Error",
144-
f"An unexpected error occurred: {str(e)}",
145-
)
146-
14735
@app_commands.command(
14836
name="synccommands", description="Manually sync slash commands"
14937
)
@@ -201,6 +89,10 @@ async def synccommands_prefix(self, ctx: commands.Context) -> None:
20189
async def applytemplate_prefix(
20290
self, ctx: commands.Context, url: str, folder: str = None, branch: str = "main"
20391
) -> None:
92+
# Sanitize URL: only allow github.com/*
93+
if not re.match(r"^https?://github\.com/[^/]+/[^/]+", url):
94+
await ctx.send("❌ Only direct github.com repository URLs are supported.")
95+
return
20496
self.logger.info(
20597
f"[applytemplate_prefix] User: {ctx.author}, URL: {url}, folder: {folder}, branch: {branch}"
20698
)
@@ -236,6 +128,10 @@ async def applytemplate(
236128
folder: str = None,
237129
branch: str = "main",
238130
) -> None:
131+
# Sanitize URL: only allow github.com/*
132+
if not re.match(r"^https?://github\.com/[^/]+/[^/]+", url):
133+
await interaction.response.send_message("❌ Only direct github.com repository URLs are supported.", ephemeral=True)
134+
return
239135
self.logger.info(
240136
f"[applytemplate_slash] User: {interaction.user}, URL: {url}, folder: {folder}, branch: {branch}"
241137
)

src/gitcord/cogs/help.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ async def help_prefix(self, ctx: commands.Context) -> None:
4444
value="• `!hello` - Get a friendly greeting\n"
4545
"• `!help` - Show help information and link to the wiki (***YOU ARE HERE***)\n"
4646
"• `!ping` - Check bot latency\n"
47-
"• `!fetchurl <url>` - Fetch content from a URL (Admin only)\n"
4847
"• `!createchannel` - Create a channel from YAML (Manage Channels)\n"
4948
"• `!createcategory` - Create a category from YAML (Manage Channels)\n"
5049
"• `!synccommands` - Sync slash commands (Admin only)",
@@ -54,7 +53,6 @@ async def help_prefix(self, ctx: commands.Context) -> None:
5453
embed.add_field(
5554
name="⚡ Slash Commands",
5655
value="• `/slashping` - Check bot latency\n"
57-
"• `/fetchurl <url>` - Fetch content from a URL (Admin only)\n"
5856
"• `/createcategory [yaml_path]` - Create category from YAML (Manage Channels)\n"
5957
"• `/synccommands` - Sync slash commands (Admin only)",
6058
inline=False,
@@ -101,7 +99,6 @@ async def help_slash(self, interaction: discord.Interaction) -> None:
10199
value="• `!hello` - Get a friendly greeting\n"
102100
"• `!help` - Show help information and link to the wiki (***YOU ARE HERE***)\n"
103101
"• `!ping` - Check bot latency\n"
104-
"• `!fetchurl <url>` - Fetch content from a URL (Admin only)\n"
105102
"• `!createchannel` - Create a channel from YAML (Manage Channels)\n"
106103
"• `!createcategory` - Create a category from YAML (Manage Channels)\n"
107104
"• `!synccommands` - Sync slash commands (Admin only)",
@@ -111,7 +108,6 @@ async def help_slash(self, interaction: discord.Interaction) -> None:
111108
embed.add_field(
112109
name="⚡ Slash Commands",
113110
value="• `/slashping` - Check bot latency\n"
114-
"• `/fetchurl <url>` - Fetch content from a URL (Admin only)\n"
115111
"• `/createcategory [yaml_path]` - Create category from YAML (Manage Channels)\n"
116112
"• `/synccommands` - Sync slash commands (Admin only)",
117113
inline=False,

src/gitcord/constants/messages.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
)
1313
ERR_DISCORD_API = "Discord API error: {error}"
1414
ERR_UNEXPECTED = "An unexpected error occurred: {error}"
15-
ERR_NO_CONTENT_FOUND = "No readable text content was found on the provided URL."
16-
ERR_FETCH_ERROR = "Failed to fetch content from the URL: {error}"
1715

1816
# Success messages
1917
SUCCESS_CATEGORY_CREATED = "Successfully created category: **{name}**"
@@ -32,25 +30,7 @@
3230
"• `!hello` - Get a friendly greeting\n"
3331
"• `!help` - Show help information and link to the wiki (***YOU ARE HERE***)\n"
3432
"• `!ping` - Check bot latency\n"
35-
"• `!fetchurl <url>` - Fetch content from a URL (Admin only)\n"
3633
"• `!createchannel` - Create a channel from YAML (Manage Channels)\n"
3734
"• `!createcategory` - Create a category from YAML (Manage Channels)\n"
3835
"• `!synccommands` - Sync slash commands (Admin only)"
39-
)
40-
HELP_SLASH_COMMANDS = (
41-
"• `/slashping` - Check bot latency\n"
42-
"• `/fetchurl <url>` - Fetch content from a URL (Admin only)\n"
43-
"• `/createcategory [yaml_path]` - Create category from YAML (Manage Channels)\n"
44-
"• `/synccommands` - Sync slash commands (Admin only)"
45-
)
46-
HELP_LINKS = (
47-
"• [GitHub Repository](https://github.com/evolvewithevan/gitcord)\n"
48-
"• [Github Project](https://github.com/users/evolvewithevan/projects/4/views/1)\n"
49-
"• [Roadmap](https://github.com/users/evolvewithevan/projects/4/views/3)\n"
50-
"• [Wiki Documentation](https://github.com/evolvewithevan/gitcord/wiki)\n"
51-
"• [Security Policy](https://github.com/evolvewithevan/gitcord/blob/main/SECURITY.md)"
52-
)
53-
HELP_FOOTER = "GitCord - Discord bot for GitOps based server structure management"
54-
55-
# UI button labels
56-
DELETE_EXTRA_CHANNELS_LABEL = "Delete Extra Channel(s)"
36+
)

src/gitcord/utils/url_helpers.py

Lines changed: 0 additions & 122 deletions
This file was deleted.

0 commit comments

Comments
 (0)