首页 > 解决方案 > 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)

标签: pythondiscorddiscord.py

解决方案


您的代码中有一些错误:

  • client = discord.Client(),你不需要它,所以你可以删除它。
  • @bot.command() #You forgot to put parentheses
    async def calculator(ctx): #ctx MUST be the fist argument
    
  • 您的嵌入定义是错误的,因为在您的代码中,它只是一个带有自定义描述字段的默认嵌入。
    #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,
    )
    
  • 您不需要导入Bot( from Commands import Bot)。此外,您不需要两次导入相同的内容,只需在代码开头导入您需要的内容。
  • 要发送消息,请使用ctx.send()。如何使用它:await ctx.send("Your message")await ctx.send(embed=embed)

PS:为什么要在命令中创建一个类,为什么需要它?


推荐阅读