diff --git a/.env b/.env new file mode 100644 index 0000000..ebd8301 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +DISCORD_TOKEN= +QUOTES_URL= \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8287c81 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +# Use the official Python image +FROM python:3.10-slim + +# Set the working directory +WORKDIR /app + +# Copy the bot script and requirements file to the working directory +COPY bot.py ./ +COPY requirements.txt ./ + + +# Install dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Expose no ports, but needed for convention +EXPOSE 8000 + +# Command to run the bot +CMD ["python", "bot.py"] diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..4398093 --- /dev/null +++ b/bot.py @@ -0,0 +1,122 @@ +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("
", "\n") # Replace
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("
", "\n") # Replace
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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8e03de9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +discord.py +python-dotenv +requests