This is my first trying creating a discord bot. I want it to work like this:
User:
/roll 2d20+4
Then my bot will roll 2d20, save the sum and the bigger. Finally, it will print like this:
Output:
Bigger: 16+4=20 | Sum: 23+4=27.
This is the code I have:
import hikari
import lightbulb
import random
disc_token="..."
server_id="..."
bot=lightbulb.BotApp(
token=disc_token,
default_enabled_guilds=int(server_id)
)
@bot.command
@lightbulb.command('roll',"Role dados (exemplo: 2d20+5)")
@lightbulb.implements(lightbulb.SlashCommand)
async def roll(ctx,play):
die_split=play.split("d")
final_split=die_split[1].split("+")
n_die=int(die_split[0])
die=int(final_split[0])
extra=int(final_split[1])
play=[n_die,die,extra]
best_roll=0
sum_roll=0
for i in range(n_die):
actual_roll=random.randint(1,die)
sum_roll=sum_roll+actual_roll
if(actual_roll>best_roll):
best_roll=actual_roll
sum_roll=sum_roll+extra
extra_roll=best_roll+extra
play_print='Bigger: '+str(best_roll)+'+'+str(extra)+'='+str(extra_roll)+' | Sum: '+str(sum_roll)
await ctx.respond(play_print)
bot.run()
What is happening:
The bot is working (I tested some basic commands, like "Hello World" and works fine), but when I try to run the /roll command, for the discord user it shows: "! The app did not answered". At my terminal, the error was displayed like this:
Traceback (most recent call last): File "C:\Users\MateusRochaQSOFT\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\lightbulb\app.py", line 1163, in invoke_application_command await context.invoke() File "C:\Users\MateusRochaQSOFT\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\lightbulb\context\base.py", line 328, in invoke await self.command.invoke(self) File "C:\Users\MateusRochaQSOFT\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ligh raise new_exc lightbulb.errors.CommandInvocationError: An error occurred during command 'roll' invocation
What could be wrong?
1条答案
按热度按时间2ul0zpep1#
我认为你在使用
lightbulb
包时定义了错误的选项。你必须使用@lightbulb.option
装饰器来定义选项,然后才能通过ctx.options
访问它们。其中选项的名称是你能得到的属性。可在此处的文档中找到。您可以使用
ctx.options.play
访问play
变量。我还格式化了代码以使用PEP8 formatting,并且使用f-strings代替了您正在进行的字符串连接。
您还可以使用
@lightbulb.command
中的pass_options
参数将所有定义的选项(通过装饰器)作为关键字参数传递。就像这样:
这样就不需要执行
play = ctx.options.play
。