#π Verifying image urls
20 messages Β· Page 1 of 1 (latest)
@marsh cloud
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
hi
greetings
as a cogs data or in your main.py?
ok
if os.path.exists(path)
and using if statement check the ending like .png or .jpg
but probably their is more easier way about what i donβt know
I'm pretty new at coding -- what exactly can a json file store?
watch i created an example:
import discord
from discord.ext import commands
import requests
import json
# Loading data from JSON file
try:
with open('allowed_images.json', 'r') as file:
allowed_images = json.load(file)
except FileNotFoundError:
allowed_images = []
# Function to save data to JSON file
def save_allowed_images():
with open('allowed_images.json', 'w') as file:
json.dump(allowed_images, file, indent=4)
@bot.command()
async def addimage(ctx, image_url: str):
# List of allowed prefixes for the URL
allowed_prefixes = ["https://", "http://"]
# Checking if the link starts with an allowed prefix
if not any(image_url.startswith(prefix) for prefix in allowed_prefixes):
await ctx.send("The image URL must start with 'https://' or 'http://'.")
return
# Checking if the URL is already in the list
if image_url in allowed_images:
await ctx.send("The image URL is already allowed and was not saved again.")
else:
# The URL is valid, saving it to the database
allowed_images.append(image_url)
save_allowed_images()
await ctx.send("The image URL has been successfully saved!")
i made notes above the codes #
please take this as an exercise and correct some information by yourself!
π
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.