123 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			123 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| import discord
 | |
| from discord import app_commands
 | |
| import requests
 | |
| import random
 | |
| from datetime import datetime
 | |
| from dotenv import load_dotenv
 | |
| import os
 | |
| 
 | |
| # Load environment variables from .env
 | |
| load_dotenv()
 | |
| 
 | |
| # Fetch token and URL from .env
 | |
| DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
 | |
| QUOTES_URL = os.getenv('QUOTES_URL')
 | |
| 
 | |
| # Function to fetch quotes from the URL
 | |
| def fetch_quotes():
 | |
|     response = requests.get(QUOTES_URL)
 | |
|     if response.status_code == 200:
 | |
|         return response.json()
 | |
|     return None
 | |
| 
 | |
| # Function to get all quotes from the fetched data
 | |
| def get_all_quotes(data):
 | |
|     all_quotes = []  # List to hold all quotes
 | |
|     for entry in data['cytaty']:
 | |
|         for quote in entry['quotes']:
 | |
|             all_quotes.append({
 | |
|                 'quote': quote['quote'],
 | |
|                 'user': quote['user'],
 | |
|                 'author': quote.get('author', "Unknown"),
 | |
|                 'date': entry['date']
 | |
|             })
 | |
|     return all_quotes
 | |
| 
 | |
| # Bot setup
 | |
| intents = discord.Intents.default()
 | |
| client = discord.Client(intents=intents)
 | |
| tree = app_commands.CommandTree(client)
 | |
| 
 | |
| # Load quotes when the bot starts
 | |
| all_quotes = []
 | |
| 
 | |
| # Event when bot is ready
 | |
| @client.event
 | |
| async def on_ready():
 | |
|     global all_quotes
 | |
| 
 | |
| 
 | |
|     # Sync commands again
 | |
|     await tree.sync()  # Sync global commands
 | |
|     for guild in client.guilds:
 | |
|         await tree.sync(guild=discord.Object(id=guild.id))  # Sync guild-specific commands if any
 | |
| 
 | |
|     data = fetch_quotes()
 | |
|     if data:
 | |
|         all_quotes = get_all_quotes(data)
 | |
|         print(f'Logged in as {client.user}')
 | |
|         print("Commands synced")
 | |
|     else:
 | |
|         print("Failed to fetch quotes on startup")
 | |
| 
 | |
| # Slash command for fetching the quote of the day
 | |
| @tree.command(name="quote", description="Get the quote of the day based on the current date")
 | |
| async def fetch_quote(interaction: discord.Interaction):
 | |
|     global all_quotes
 | |
| 
 | |
|     # Acknowledge the interaction immediately
 | |
|     await interaction.response.defer(thinking=True)
 | |
| 
 | |
|     if all_quotes:
 | |
|         total_quotes = len(all_quotes)
 | |
|         
 | |
|         # Calculate the index based on the current date
 | |
|         current_date = datetime.now()
 | |
|         epoch_start = datetime(1970, 1, 1)  # Unix epoch start date
 | |
|         days_since_epoch = (current_date - epoch_start).days
 | |
|         
 | |
|         # Calculate the quote index
 | |
|         quote_index = days_since_epoch % total_quotes
 | |
| 
 | |
|         quote_data = all_quotes[quote_index]
 | |
|         user = quote_data['user']
 | |
|         quote = quote_data['quote'].replace("<br>", "\n")  # Replace <br> with new lines
 | |
|         author = quote_data['author']
 | |
|         date = quote_data['date']
 | |
| 
 | |
|         # Format response message
 | |
|         response_message = (
 | |
|             f"**Quote of the Day**:\n> {quote}\n- Autor tego cuda: {author} (Wysłane przez: {user})\n*Data: {date}*"
 | |
|         )
 | |
| 
 | |
|         await interaction.followup.send(response_message)
 | |
|     else:
 | |
|         await interaction.followup.send("No quotes available.")
 | |
| 
 | |
| # Slash command for fetching a random quote
 | |
| @tree.command(name="random_quote", description="Get a random quote")
 | |
| async def random_quote(interaction: discord.Interaction):
 | |
|     global all_quotes
 | |
| 
 | |
|     # Acknowledge the interaction immediately
 | |
|     await interaction.response.defer(thinking=True)
 | |
| 
 | |
|     if all_quotes:
 | |
|         quote_data = random.choice(all_quotes)  # Select a random quote
 | |
|         user = quote_data['user']
 | |
|         quote = quote_data['quote'].replace("<br>", "\n")  # Replace <br> with new lines
 | |
|         author = quote_data['author']
 | |
|         date = quote_data['date']
 | |
| 
 | |
|         # Format response message
 | |
|         response_message = (
 | |
|             f"**Random Quote**:\n> {quote}\n- Autor tego cuda: {author} (Wysłane przez: {user})\n*Data: {date}*"
 | |
|         )
 | |
| 
 | |
|         await interaction.followup.send(response_message)
 | |
|     else:
 | |
|         await interaction.followup.send("No quotes available.")
 | |
| 
 | |
| # Run the bot with the token from the .env file
 | |
| client.run(DISCORD_TOKEN)
 |