python - 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)
解决方案
您的代码中有一些错误:
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:为什么要在命令中创建一个类,为什么需要它?
推荐阅读
- swift - 将 vapor 链接到自己的 openssl 编译
- c++ - 它在VS中不起作用,但是当我运行它时它在CLion中起作用
- python - 是否可以在python中绘制一个正则表达式来提取所有字符串的属性值
- c - 自引用结构,包含三个指针(称为左、右和父)
- javascript - 如何在 ES6 中重写 Headless Autodesk Forge Viewer
- java - 如何使用 Java 在 Midi 文件中列出乐器名称?
- css - 嵌套类和标签
- php - 如何在 PHP 中匹配和替换图像 URL?
- amazon-web-services - 适用于对 AWS IoT 的 HTTP 请求的 AWS Signature v4
- node.js - 如何将 MongoDB 错误从 Express 传递给 React