如何使用discord.py创建命令?

时间:2020-07-16 01:22:38

标签: python discord discord.py

我正在创建一个命令来计算在特定服务器上获取“ Famiiliars”所需的时间,但是我在实际制作命令本身时遇到了麻烦。到目前为止,这是我做基础的一切,但是当我运行test时,什么也没发生。有人可以帮忙吗?

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)

2 个答案:

答案 0 :(得分:1)

您的代码中有一些错误:

  • 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,
    )
    
  • 您不需要导入Botfrom Commands import Bot)。另外,您不需要两次导入相同的内容,也不需要在代码开始时导入所需的内容。
  • 要发送消息,请使用ctx.send()。使用方法:await ctx.send("Your message")await ctx.send(embed=embed)

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

答案 1 :(得分:0)

做起来很简单很容易


@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}***")

相关问题