机器人应该:
输入字符串以识别多个骰子卷(例如1d20,2d6)。
将每个组的骰子填充并计算总数。 最终计算中的包括常数(例如,+10)。
混合操作员(+, - )。 我目前拥有的代码:
import random
from discord.ext import commands
from discord import Intents
import re
intents = Intents.default()
intents.message_content = True # Enables reading the content of messages
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.command(name='s')
async def soma(ctx, a: float, b: float):
resultado = int(a + b)
await ctx.reply(f"```➕ The sum of {int(a)} and {int(b)} is {resultado}.```")
@bot.command(name='m')
async def multiplicacao(ctx, a: float, b: float):
resultado = int(a * b)
await ctx.reply(f"```✖️ The multiplication of {int(a)} and {int(b)} is {resultado}.```")
@bot.command(name='soma_dados')
async def soma_dados(ctx, *, dados: str):
try:
padrao = re.compile(r'(\d+)d(\d+)|(\d+)')
partes = padrao.findall(dados)
total = 0
detalhes = []
for parte in partes:
if parte[0] and parte[1]: # is a dice, e.g., 1d20
num_dice = int(parte[0])
dice_type = int(parte[1])
rolls = [random.randint(1, dice_type) for _ in range(num_dice)]
subtotal = sum(rolls)
total += subtotal
detalhes.append(f"{num_dice}d{dice_type}: {rolls} (Subtotal: {subtotal})")
else: # is a flat number
total += int(parte[2])
detalhes.append(f"{parte[2]}")
await ctx.reply(f"```🎲 You rolled: {', '.join(detalhes)} (Total: {total})```")
except Exception as e:
await ctx.reply(f"```Error in expression: {str(e)}```")
@bot.command(name='r')
async def rolar_dado(ctx, expression: str):
try:
if '+' in expression:
dice_part, modifier_part = expression.split('+')
modifier = int(modifier_part)
elif '-' in expression:
dice_part, modifier_part = expression.split('-')
modifier = -int(modifier_part)
else:
dice_part = expression
modifier = 0
num_dice, dice_type = map(int, dice_part.split('d'))
rolls = [random.randint(1, dice_type) for _ in range(num_dice)]
total = sum(rolls) + modifier
await ctx.reply(f"```🎲 You rolled: {rolls} (Modifier: {modifier}) (Total: {total})```")
except Exception as e:
await ctx.reply(f"```Error in expression: {str(e)}```")
@bot.command(name='p')
async def porcentagem(ctx, parte: float, total: float):
if total == 0:
await ctx.reply("```Error: Total cannot be zero.```")
else:
resultado = (parte / total) * 100
await ctx.reply(f"```📊 {parte} is {resultado:.2f}% of {total}```")
bot.run('Bot token :p')
如何对此进行修改以处理输入字符串中的多个骰子组和操作?
I只需使用内置的python方法拆分即可将输入字符串变成列表,然后循环通过该列表,使骰子滚动
import random
# test input
input_string = "1d20 + 2d6 + 1d10"
# spliting the inputs into a list of strings
dice_strings = input_string.split(" + ")
# setting up string we are gonna print/send back
final_string = "🎲 You rolled: "
# loop through list of strings we made
for s in dice_strings:
# first number will always be the amount of dice we are rolling
first_num = int(s[0])
# find the num after the d and take that as the die we are rolling
second_index = s.index("d")
second_num = int(s[second_index+1:])
# loops the amount of times specifized and adds up the total
total = 0
for i in range(first_num):
total += random.randrange(1,second_num)
#adds the current die to the final string
final_string += f'{s}({total}) '
# finally we print the string
# this all assumes proper input
print(final_string)
输出:🎲滚动:1d20(14)2d6(6)1d10(9)