目前我的Discord机器人的”pi”命令存在问题。

huangapple go评论184阅读模式
英文:

There is currently a problem with my discord bot's pi command

问题

我有一个 Discord 的 Python 机器人,并且我制作了一个类似这样的命令:命令:?pi 100 响应:The value of pi to 100 decimal places is: 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

这是我的代码:

import decimal
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions, MissingPermissions

#token
token = "hidden"

#intents and prefix
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="?", intents=intents)

#basic events
@bot.event
async def on_ready():
  print('We have logged in as {0.user}'.format(bot))

@bot.event
async def on_message(message):
  if message.author == bot.user:
    return
  await bot.process_commands(message)

#fun commands
@bot.command()
async def pi(ctx, num_decimals: int):
  if num_decimals > 10000:
    await ctx.send("Sorry, I can only print up to 10000 decimal places.")
    return
  decimal.getcontext().prec = num_decimals + 1
  pi_value = decimal.Decimal(0)
  for i in range(num_decimals + 1):
    pi_value += (decimal.Decimal(1) / 16 ** i) * ((decimal.Decimal(4) / (8 * i + 1)) - (decimal.Decimal(2) / (8 * i + 4)) - (decimal.Decimal(1) / (8 * i + 5)) - (decimal.Decimal(1) / (8 * i + 6)))
  pi_str = str(pi_value)[:num_decimals + 2]  # Fix: changed to num_decimals + 2
  await ctx.send(f"The value of pi to {num_decimals} decimal places is: {pi_str}")


try:
  bot.run(token)
except discord.HTTPException as e:
  if e.status == 429:
    print("The Discord servers denied the connection for making too many requests")
  else:
    raise e
英文:

I have a discord python bot and I made a command like this: command: ?pi 100 response: The value of pi to 100 decimal places is: 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

Here is my code:

import decimal
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions, MissingPermissions

#token
token = "hidden"

#intents and prefix
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="?", intents=intents)

#basic events
@bot.event
async def on_ready():
  print('We have logged in as {0.user}'.format(bot))

@bot.event
async def on_message(message):
  if message.author == bot.user:
    return
  await bot.process_commands(message)

#fun commands
@bot.command()
async def pi(ctx, num_decimals: int):
  if num_decimals > 10000:
    await ctx.send("Sorry, I can only print up to 10000 decimal places.")
    return
  decimal.getcontext().prec = num_decimals + 1
  pi_value = decimal.Decimal(0)
  for i in range(num_decimals + 1):
    pi_value += (decimal.Decimal(1) / 16 ** i) * ((decimal.Decimal(4) / (8 * i + 1)) - (decimal.Decimal(2) / (8 * i + 4)) - (decimal.Decimal(1) / (8 * i + 5)) - (decimal.Decimal(1) / (8 * i + 6)))
  pi_str = str(pi_value)[:num_decimals + 1]
  await ctx.send(f"The value of pi to {num_decimals} decimal places is: {pi_str}")


try:
  bot.run(token)
except discord.HTTPException as e:
  if e.status == 429:
    print("The Discord servers denied the connection for making too many requests")
  else:
    raise e

The bot works perfectly but the only problem is that, when I type ?pi 100, it only responds with The value of pi to 100 decimal places is: 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067. And the problem is, that is only 99 digits, not 100. Is there a way I can fix it? Any help will be appreciated.

答案1

得分: 1

你可以通过利用 mpmath 库来解决这个问题使你的代码更简洁

```python
from mpmath import mp

@bot.command()
async def pi(ctx, num_decimals: int):
  if num_decimals > 10000:
    await ctx.send("抱歉,我只能显示最多10000位小数。")
  else:
    mp.dps = num_decimals + 1  # 设置小数位数;我们加一是因为第一位不是小数
    await ctx.send(f"π的值,精确到小数点后{num_decimals}位,为:{mp.pi}")
英文:

You can solve this and make your code much shorter by taking advantage of the mpmath library like so:

from mpmath import mp

@bot.command()
async def pi(ctx, num_decimals: int):
  if num_decimals > 10000:
    await ctx.send("Sorry, I can only print up to 10000 decimal places.")
  else:
    mp.dps = num_decimals + 1  # set number of digits; We are adding one beause the first number is not a decimal
    await ctx.send(f"The value of pi to {num_decimals} decimal places is: {mp.pi}")

Note that I'm almost certain that discord only allows the maximum of 2,000 characters per message so the send will fail if you request too many digits

huangapple
  • 本文由 发表于 2023年3月9日 21:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定