PiggyBank Discord Bot
import io
import aioschedule
from crud import delete_wishlist_item, edit_subscription, delete_subscription, create_subscription, view_recurring_subscriptions, view_non_recurring_subscriptions, set_account_amount, get_wishlist, update_wishlist_item, set_investment_value, delete_investment, get_investments_for_user, update_tag, get_transactions_for_user, create_investment, create_wishlist_item, create_transaction, get_user, get_all_tags, create_account, get_account, get_all_accounts, delete_account, update_account_amount, create_tag, delete_tag, get_budget, update_budget
from fetch_currencies import fetch_currencies, convert_currency
from analytics import all_in_one_analysis, get_investment_statistics, generate_spending_report
from flask import Flask
from app_init import app, tree, bot, bot_token
import discord
from discord import app_commands, Interaction
from sqlalchemy.exc import IntegrityError
import os
from discord.ui import Select, View
import asyncio
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from database import db
from accounts import *
from stats import *
from tags import *
from investments import *
from subscriptions import *
from wishlist import *
from budgets import *
scheduler = AsyncIOScheduler()
Frequently Asked Questions
How can the PiggyBank Discord bot help businesses manage their finances?
The PiggyBank Discord bot can be a valuable tool for businesses to track and manage their finances. It allows for creating multiple accounts, categorizing expenses with tags, and monitoring investments. This can help businesses keep a close eye on their cash flow, budget for different departments or projects, and make informed financial decisions. The bot's ability to generate reports and provide analytics can also assist in financial planning and forecasting.
Can the PiggyBank bot be used for personal finance management as well?
Absolutely! While the PiggyBank Discord bot can be used for business purposes, it's equally effective for personal finance management. Users can track their personal accounts, set budgets for different spending categories, manage subscriptions, and even create wishlists. The bot's features like expense tracking, investment monitoring, and financial reporting make it a comprehensive tool for individuals looking to improve their financial health and achieve their financial goals.
How does the PiggyBank bot handle multi-currency transactions?
The PiggyBank bot is designed to handle multi-currency transactions seamlessly. It uses an external API to fetch current exchange rates and can convert amounts between different currencies. This is particularly useful for businesses or individuals dealing with international transactions or managing accounts in multiple currencies. For example, when adding a new account or recording a transaction, users can specify the currency, and the bot will handle the conversions when generating reports or calculating net worth.
Can you explain how the PiggyBank bot implements the command for creating a new account?
Certainly! The PiggyBank bot implements the new account command using Discord's slash command feature. Here's a simplified version of the code:
```python @tree.command(name='new_account') async def new_account_command(interaction, account_name: str, amount: float = 0.0): currencies = fetch_currencies() select = Select(placeholder="Select a currency...", options=currencies)
async def select_callback(interaction):
user_id = interaction.user.id
try:
create_account(user_id, account_name, amount, select.values[0])
await interaction.response.send_message(f"New account '{account_name}' created with {amount} {select.values[0]}")
except IntegrityError:
await interaction.response.send_message(f"Account '{account_name}' already exists")
select.callback = select_callback
view = View()
view.add_item(select)
await interaction.response.send_message("Choose a currency for your account", view=view)
```
This code defines a slash command that prompts the user for an account name and initial amount, then allows them to select a currency from a dropdown menu before creating the account.
How does the PiggyBank bot handle scheduled tasks like sending weekly reports?
The PiggyBank bot uses the APScheduler library to handle scheduled tasks. Here's an example of how it schedules the weekly report:
```python from apscheduler.schedulers.asyncio import AsyncIOScheduler
scheduler = AsyncIOScheduler()
async def send_weekly_report(): # Code to generate and send the report
def schedule_reports(): scheduler.add_job(send_weekly_report, 'cron', day_of_week='sun', hour=12, minute=0) scheduler.start()
# This function is called when the bot starts def main(): schedule_reports() bot.run(bot_token) ```
This code sets up a scheduler that runs the send_weekly_report
function every Sunday at 12:00 PM. The scheduler is started when the bot initializes, ensuring that reports are sent automatically at the specified time.
Created: | Last Updated:
Here's a step-by-step guide for using the PiggyBank Discord Bot template:
Introduction
The PiggyBank Discord Bot is a powerful financial tracking tool that integrates with Discord. It allows users to manage various aspects of their finances, including accounts, spending categories, wishlist items, transactions, investments, and more. This bot can help Discord users keep track of their financial health directly within their Discord server.
Getting Started
To begin using this template:
- Click the "Start with this Template" button in the Lazy Builder interface.
Initial Setup
Before testing the bot, you need to set up a Discord bot and obtain its token. Follow these steps:
- Go to the Discord Developer Portal (https://discord.com/developers/applications).
- Click "New Application" and give your application a name.
- Navigate to the "Bot" section in the left sidebar.
- Click "Add Bot" to create a bot user for your application.
- Under the "TOKEN" section, click "Reset Token" to generate a new token. Confirm by clicking "Yes, do it!"
- Copy the generated token.
- In the Lazy Builder, go to the "Environment Secrets" tab.
- Add a new secret with the key
DISCORD_BOT_TOKEN
and paste your bot token as the value.
Additionally, you need to set up an API key for currency conversion:
- Go to https://www.exchangerate-api.com/
- Sign up for a free account.
- Once logged in, copy your API key from the dashboard.
- In the Lazy Builder's "Environment Secrets" tab, add another secret with the key
EXCHANGE_RATE_API_KEY
and paste your API key as the value.
Testing the Bot
After setting up the environment secrets:
- Click the "Test" button in the Lazy Builder interface to start the deployment process.
Using the Bot
Once the bot is deployed, you can invite it to your Discord server:
- Go back to the Discord Developer Portal and select your application.
- Navigate to the "OAuth2" section in the left sidebar.
- In the "Scopes" section, select "bot".
- In the "Bot Permissions" section, select the permissions your bot needs (at minimum: Read Messages/View Channels, Send Messages, Use Slash Commands).
- Copy the generated OAuth2 URL.
- Open this URL in a new browser tab and select the Discord server where you want to add the bot.
After adding the bot to your server, you can start using its commands. Here are some example commands:
/new_account
: Create a new financial account/spent
: Record an expense/money_in
: Record income/view_transactions
: View your transaction history/new_tag
: Create a new spending category/new_investment
: Add a new investment/new_subscription
: Add a recurring subscription/view_wishlist
: View your wishlist items/net_worth
: Calculate your net worth
Each command will guide you through the process of entering the required information.
Integrating the Bot
This Discord bot is designed to work within Discord servers. There's no additional integration required beyond adding it to your desired Discord server and using its commands.
Remember to keep your bot token and API keys secure. Never share them publicly or commit them to version control systems.
Template Benefits
-
Personal Finance Management: This Discord bot provides a comprehensive platform for users to track and manage their personal finances, including accounts, transactions, investments, and budgets, all within the familiar Discord interface.
-
Financial Education and Awareness: By offering features like budget tracking, investment monitoring, and spending analysis, the bot helps users become more aware of their financial habits and learn better money management skills.
-
Community Engagement: As a Discord bot, this tool can be integrated into financial communities or servers, fostering discussions about personal finance and encouraging users to share tips and strategies.
-
Customizable Financial Tracking: The bot allows users to create custom tags for transactions, set up personalized budgets, and manage multiple accounts and currencies, catering to diverse financial situations and goals.
-
Data-Driven Insights: With features like spending reports, investment statistics, and budget analysis, the bot provides valuable insights that can help users make informed financial decisions and improve their overall financial health.