PlasticFantastic
PlasticFantastic

Reputation: 21

How do I create a command using discord.py?

I am creating a command to calculate time needed to get "Familiars" in a particular server, but I am having troubles actually making the command itself. This is what I have done so far to get my grounds on making a command yet when I run test, nothing happens. Can anyone help?

import discord
from discord.ext import commands

client = discord.Client()

bot = commands.Bot(command_prefix='//')


@bot.command()
async def test(ctx):
    await ctx.send('test')
#edited in 'await' above ^
#Familiars Calculator Portion:
import discord
from discord.ext import commands

from Commands import bot


@bot.command
async def calculator():
class calculate:
    def values(self):
        fam = 35353
        xp: int = int(input("Enter your current xp: "))
        msg = 1
        while xp < fam:
            xp += 15
            msg += 1
            days_to_fam: int = round(msg * 2 / (60 * 24))
            hours_to_fam: int = round(msg * 2 / 60)
            hours_to_fam %= 24
            minutes_to_fam: int = round(msg * 2)
            minutes_to_fam %= 60

            embed = discord.Embed(title="Statistics needed to reach fam:",color=0xffffff))
            embed = discord.Embed(description="Messages: ", int(msg), " (roughly).", "\nDays: ", days_to_fam, "\nHours: ", hours_to_fam, "\nMinutes:", minutes_to_fam)

        print('\nStatistics needed to reach fam: ', "\nMessages:", int(msg), "(roughly)", "\nDays:", days_to_fam,
          "\nHours:", hours_to_fam, "\nMinutes:", minutes_to_fam)

Upvotes: 2

Views: 4636

Answers (2)

Spy Yolo
Spy Yolo

Reputation: 1

just do that it is very simple and easy


@bot.command(pass_context=True)
async def add(ctx, a:int, b:int):
    await ctx.send(f"The calculation result is:\n***{a+b}***")
    
@bot.command(pass_context=True)
async def subtract(ctx, a:int, b:int):
    await ctx.send(f"The calculation result is:\n***{a-b}***")
    
@bot.command(aliases=["calmultiply"])
async def multiply(ctx, a:int, b:int):
    await ctx.send(f"The calculation result is:\n***{a*b}***")
    
@bot.command(pass_context=True)
async def devide(ctx, a:int, b:int):
    await ctx.send(f"The calculation result is:\n***{a/b}***")

Upvotes: 0

MrSpaar
MrSpaar

Reputation: 3994

There are some errors in your code:

  • client = discord.Client(), you don't need it so you can delete it.
  • @bot.command() #You forgot to put parentheses
    async def calculator(ctx): #ctx MUST be the fist argument
    
  • Your embed definition is wrong, as in your code, it will just be a default embed with a custom description field.
    #Change
    embed = discord.Embed(title="Statistics needed to reach fam:",color=0xffffff))
    embed = discord.Embed(description="Messages: ", int(msg), " (roughly).", "\nDays: ", days_to_fam, "\nHours: ", hours_to_fam, "\nMinutes:", minutes_to_fam)
    #to
    embed = discord.Embed(title="Statistics needed to reach fam:",
                          description=f"Messages: {int(msg)} (roughly).\nDays: {days_to_fam}\nHours: {hours_to_fam}\nMinutes: {minutes_to_fam}"
                          color=0xffffff,
    )
    
  • You don't need to import Bot (from Commands import Bot). Also, you don't need to import the same things twice, import what you need at the beginning of your code.
  • To send a message, use ctx.send(). How to use it: await ctx.send("Your message") or await ctx.send(embed=embed)

PS: Why would you create a class in your command, why do you need it?

Upvotes: 1

Related Questions