Skip to content

Dependencies

Liam Ramirez-Guess edited this page Oct 17, 2024 · 1 revision

This is a list of the dependencies needed to run the project and why!


Requirements.txt

To easily install this project's dependencies, I have included a requirements.txt in the Project Files under the Server folder.

It looks like this:

discord.py==2.3.2
discord-py-slash-command==4.2.1
requests==2.32.2
Datetime==5.2
pytz==2023.3.post1
python-dotenv==1.0.1
flask==3.0.3

To install the project dependencies using the txt, run pip install -r Server/requirements.txt


Imports Section

In code, the imports section looks like this:

import discord
from discord import app_commands
from discord.ui import Select, View
from discord.ext import tasks, commands
import pytz
from pytz import all_timezones
import requests
import datetime
from dotenv import load_dotenv
import os
import json 

Discord.py

Static Badge

The discord.py library is used to interact with the Discord API, allowing the bot to perform various actions within Discord servers. In the program, discord.py is used for the following purposes:

  1. Creating Commands: The bot defines several commands using the @bot.tree.command decorator, such as /weather, /forecast, and /airquality. These commands allow users to interact with the bot by typing specific commands in Discord.

  2. Handling Interactions: Handling Interactions: The bot uses discord.interactions to handle user interactions, such as selecting options from dropdown menus or responding to commands

  3. Creating Views and Selects: The bot uses discord.ui.View and discord.ui.Select to create interactive components like dropdown menus. For example, DateSelectView and DateSelect are used to allow users to select dates for weather forecasts.

  4. Sending Messages and Embeds: The bot sends messages and embeds to Discord channels using interaction.response.edit_message and discord.Embed. This allows the bot to display weather information in a formatted way.

  5. Managing Events: The bot handles events such as on_ready and on_disconnect to perform actions when the bot connects to or disconnects from Discord.

  6. Task Scheduling: The bot uses tasks.loop to schedule periodic tasks, such as sending daily weather updates.


Discord-py-slash-command

Static Badge

The discord-py-slash-command library is an extension for discord.py that provides support for creating and handling slash commands (also known as application commands) in Discord. Slash commands are a type of command that users can invoke by typing a forward slash (/) followed by the command name. These commands provide a more user-friendly and interactive way to interact with bots.

Here are some key features and functionalities provided by discord-py-slash-command:

  1. Slash command registration: It allows the bot to register slash commands with Discord so that they appear in the command suggestions when users type /.

  2. Command Handling: It provides decorators and methods to define and handle slash commands, similar to how you define regular commands in discord.py.

  3. Autocomplete: It supports autocomplete for command options, making it easier for users to fill in command parameters.

  4. Subcommands and Groups: It allows the bot to create subcommands and command groups for better organization and structure of commands.

  5. Permissions: It provides options to set permissions for commands, restricting who can use certain commands based on roles or user IDs.

  6. Interaction Handling: It handles interactions from users, such as button clicks or dropdown selections, within the context of slash commands.


Requests

Static Badge

The requests library is a popular Python library used for making HTTP requests. It simplifies the process of sending HTTP requests and handling responses. In the context of the Discord bot, requests is used to interact with external APIs, such as weather services, to fetch data that the bot can then process and present to users.

  1. Fetching Weather data: requests is used to call the OpenWeatherMap API to get current weather conditions, forecasts, air quality data, etc.

  2. Handling API Responses: requests makes it easy to handle JSON responses from APIs, which can then be parsed and used within the bot.

  3. Error Handling: requests provides mechanisms to handle HTTP errors, timeouts, and retries, ensuring that the bot can gracefully handle issues with external services.


Datetime

Static Badge

The datetime module in Python provides classes for manipulating dates and times. It is part of the Python standard library, so you don't need to install it separately. Here's how it can be used in your Discord bot:

  1. Getting the Current Date and Time: datetime can be used to get the current date and time, which can be useful for timestamping messages or scheduling tasks.

  2. Formatting Dates and Times: datetime provides methods to format dates and times in various ways, making it easier to present information to users.

  3. Time calculations: It can perform arithmetic operations on datetime objects to calculate differences between dates and times, add or subtract time intervals, etc.


pytz

Static Badge

The pytz library is a Python package that provides accurate and up-to-date time zone information. It allows the bot to work with time zones and convert between different time zones easily. This is particularly useful in applications where it is needed to handle dates and times across multiple time zones, such as scheduling events, logging timestamps, or displaying local times to users.

  1. Convert UTC to Local Time: Convert UTC times to the local time zones of users.

  2. Handle Time Zone Aware Datetimes: Create and manipulate time zone-aware datetime objects.

  3. List Available Time Zones: Provide a list of available time zones for users to select from.


python-dotenv

Static Badge

The python-dotenv library is used to read key-value pairs from a .env file and set them as environment variables. This is particularly useful for managing configuration settings and sensitive information, such as API keys, database credentials, and other environment-specific variables, without hardcoding them into your source code.

  1. Load Environment Variables: Automatically load environment variables from a .env file into the environment.

  2. Keep Sensitive Information Secure: Store sensitive information in a separate file that can be excluded from version control.

  3. Easy Configuration Management: Simplify the management of configuration settings across different environments (development, testing, production).


os

Static Badge

The os module in Python provides a way of using operating system-dependent functionality, such as reading or writing to the file system, managing environment variables, and interacting with the operating system in various ways. In the weatherbot code, the os module is used for the following purposes:

  1. Determining the Base Directory:

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

This line sets BASE_DIR to the directory where the script is located. It uses os.path.abspath(__file__) to get the absolute path of the script and os.path.dirname() to get the directory name of that path.

  1. Defining the Path to the Data File:

DATA_FILE = os.path.join(BASE_DIR, '../Server/user_data.json')

This line constructs the path to the user_data.json file by joining the BASE_DIR with a relative path (../Server/user_data.json). The os.path.join() function ensures that the path is constructed correctly for the operating system being used.

  1. Loading Environment Variables:
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
OPENWEATHERMAP_API_KEY = os.getenv('OPENWEATHERMAP_API_KEY')

These lines retrieve the values of the DISCORD_TOKEN and OPENWEATHERMAP_API_KEY environment variables using os.getenv(). This is useful for keeping sensitive information like API keys and tokens out of the source code.


json

Static Badge

The json module in Python is used for parsing JSON (JavaScript Object Notation) data. JSON is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. The json module provides methods to convert Python objects to JSON strings and vice versa.

  1. Read Data from JSON Files: Load user data, configuration settings, or other information stored in JSON format.

  2. Write Data to JSON Files: Save user data, logs, or other information in JSON format.

  3. Parse JSON Responses: Handle JSON responses from APIs, such as weather data from an external service.


Please check the above resources to find info about the Weatherbot!

Clone this wiki locally