DEV Community

Cover image for Building a Simple Calculator Bot with Discord.py - A Beginner's Guide
Vanshaj Sharma
Vanshaj Sharma

Posted on

Building a Simple Calculator Bot with Discord.py - A Beginner's Guide

Step 1: Setting Up the Project:

To get started, create a new directory for your project. Inside this directory, create a Python script (e.g., math_bot.py) using your preferred text editor. Now, let's begin coding the math bot.

Step 2:

Installing Discord.py and Importing Modules:
In your Python script, start by importing the necessary modules:

import math
import discord
from discord.ext import commands
import random

Step 3: Creating the Bot and Math Functions:

Next, create the Discord bot and define the math functions. The math functions will handle different arithmetic operations like addition, subtraction, multiplication, division, generating random numbers, and calculating square roots.

Image description

Step 4: Defining Command Functions:

Now, we'll define command functions that will allow users to perform math operations through Discord commands. For each operation, we'll calculate the result using the corresponding math function and send it back to the Discord channel.

@client.command()
async def mathadd(ctx, x: float, y: float):
res = add(x, y)
await ctx.send(res)

@client.command()
async def mathsub(ctx, x: float, y: float):
res = sub(x, y)
await ctx.send(res)

@client.command()
async def mathdiv(ctx, x: float, y: float):
res = div(x, y)
await ctx.send(res)

@client.command()
async def mathmul(ctx, x: float, y: float):
res = mul(x, y)
await ctx.send(res)

@client.command()
async def mathrandom(ctx, x: int, y: int):
res = rando(x, y)
await ctx.send(res)

@client.command()
async def mathsqrt(ctx, x: float):
res = sqrt(x)
await ctx.send(res)

Step 5: Discord Bot Event and Running the Bot:

Lastly, we'll add an event that will be triggered when the bot connects to the Discord server. In this event, we'll set the bot's presence status to "Watching Calculating."

@client.event
async def on_ready():
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='Calculating'))
print(f'{client.user.name} is online')

--Replace "YOUR_BOT_TOKEN" with your actual bot token from the Discord Developer Portal
client.run("YOUR_BOT_TOKEN")

Conclusion:

Congratulations! You've successfully created a simple Discord bot capable of performing various math operations. Through this guide, you've learned how to use Python and the Discord.py library to interact with the Discord API, define commands, and handle user inputs. As you continue to explore bot development, you can expand upon this project, add error handling, and even integrate more advanced features to make your bot even more functional and interactive.

Remember to keep your bot token secure and refrain from sharing it with others, as it grants access to your bot's capabilities within the Discord ecosystem. Happy bot building!

The Code is available on the following link:

https://github.com/vanshaj8/Discord.py/blob/main/calculator.py

Top comments (0)