<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kapil Gaire</title>
    <description>The latest articles on DEV Community by Kapil Gaire (@kapilgaire312).</description>
    <link>https://dev.to/kapilgaire312</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4036681%2F1885a4c1-8cbf-41fc-9c58-0c80c9b5d71e.jpg</url>
      <title>DEV Community: Kapil Gaire</title>
      <link>https://dev.to/kapilgaire312</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kapilgaire312"/>
    <language>en</language>
    <item>
      <title>Learning Python by Building a Discord Bot</title>
      <dc:creator>Kapil Gaire</dc:creator>
      <pubDate>Sun, 19 Jul 2026 17:57:59 +0000</pubDate>
      <link>https://dev.to/kapilgaire312/learning-python-by-building-a-discord-bot-1ndj</link>
      <guid>https://dev.to/kapilgaire312/learning-python-by-building-a-discord-bot-1ndj</guid>
      <description>&lt;h2&gt;
  
  
  What I built
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhbyia5myfkijat2yrjte.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhbyia5myfkijat2yrjte.png" alt=" " width="800" height="573"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A expense tracking Discord bot for groups. It lets a server team register members, log shared expenses and repayments, inspect balances, view transaction history, and export records to CSV.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I built
&lt;/h2&gt;

&lt;p&gt;The main reason was to get hang of python language. I am someone who learns by doing. So, when I decided to learn backend with python, I wanted to make a project hard enough to challenge me.&lt;/p&gt;

&lt;p&gt;Also, this bot solves one of the problems that my friends and I often face. Whenever we go out, one person usually pays for everything. We always say we'll settle the expenses later, but it often gets forgotten for months. Eventually, we have to go through months of bank statements just to figure out who owes whom.&lt;br&gt;
And, since this is a discord bot, not a webapp, it feels more natural to add the records and view them in the same place where we chat.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tech Stack
&lt;/h2&gt;

&lt;p&gt;I wrote this bot in python using the discord.py library.&lt;br&gt;
For the database, I used PostgreSQL. This was also my first time using Postgres. I wrote the queries in plain SQL instead of using an ORM.&lt;/p&gt;

&lt;h2&gt;
  
  
  Features
&lt;/h2&gt;

&lt;p&gt;This bot uses slash commands to take input from the server members.&lt;br&gt;
Some of the main commands are:&lt;br&gt;
/initialize_bot - to create the database and tables.&lt;br&gt;
/expense - used to record new expense &lt;br&gt;
/repay - used to track when repayment is done.&lt;br&gt;
/history - shows the expenses and repayments after last cleared        timestamp.&lt;br&gt;
/balance - shows how much a member owes to others and others owe them.&lt;br&gt;
/clear_records - resets the timestamp after all records are settled.&lt;br&gt;
/export_transactions - to export all the records as csv.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working
&lt;/h2&gt;

&lt;p&gt;It is a simple implementation where discord.py handles the communication with the members. &lt;br&gt;
The bot simply takes the expense or repayment info and stores it in table.&lt;br&gt;
When showing history, it compares the added date timestamp of expense and repayment to recent  cleared timestamp and shows records after that timestamp.&lt;/p&gt;

&lt;p&gt;Calculating the balance was a bit tricky part. The balance is calculated by summing each participant's share of expenses using sum aggregate function and subtracting sum of repayments they have already made. &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2gstt8y35vfnujm4yp7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2gstt8y35vfnujm4yp7w.png" alt=" " width="796" height="93"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1wo5j9v9s8rkipc9cyn4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1wo5j9v9s8rkipc9cyn4.png" alt=" " width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges
&lt;/h2&gt;

&lt;p&gt;The challenging part was writing SQL queries for calculating balance and history but it was also a exciting part seeing the output table come out as intended after using the query.&lt;/p&gt;

&lt;p&gt;Another issue I realized much later was when running export command, it was throwing an error every time. After a search, I learnt that discord commands expect a response from the server within 3 seconds.&lt;br&gt;
So, it was taking more time to query the database when export command  was used.&lt;br&gt;
This made me to realize how fragile the entire setup was. At this time, I was running the bot server and the databse server locally. But when this gets deployed, this error can happen for all the commands.&lt;br&gt;
To prevent this, we need to "defer" when the command is called and this will give us 15 mins of window to respond. So, I made a custom decorator which defers all the commands when they are called.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;p&gt;First, I became more familiar with the syntax of python. &lt;br&gt;
I learned the use of with ... as ... and decorators in python.&lt;br&gt;
After writing raw SQL, it taught me how ORM works under the hood.&lt;/p&gt;

&lt;p&gt;I deployed the bot in Railway and database in Supabase. Both were quite easy. Railway directly deployed the bot from github just like Vercel.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd improve
&lt;/h2&gt;

&lt;p&gt;The current bot can only run in one server at a time. If used in multiple servers it shouldn't work and even if it did, the records will be mixed in database since there is only one database.&lt;br&gt;
I made it like this because if made for multiple servers, the bot needs a lot of time (hours) to propagate the changes made in development. So, when developing it is made for a single server.&lt;/p&gt;

&lt;p&gt;I also made it for single server but now if I want to make it work accross multiple servers, I will need seperate database for each server, means passing server id to all the database operations which I am too lazy to implement now and I think the goal of this project is fulfilled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Source Code
&lt;/h2&gt;

&lt;p&gt;The complete source code is available on &lt;a href="https://github.com/kapilgaire312/hisab_bot" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>discord</category>
      <category>postgres</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
