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

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

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

这是我的代码:

  1. import decimal
  2. import discord
  3. from discord.ext import commands
  4. from discord.ext.commands import has_permissions, MissingPermissions
  5. #token
  6. token = "hidden"
  7. #intents and prefix
  8. intents = discord.Intents.default()
  9. bot = commands.Bot(command_prefix="?", intents=intents)
  10. #basic events
  11. @bot.event
  12. async def on_ready():
  13. print('We have logged in as {0.user}'.format(bot))
  14. @bot.event
  15. async def on_message(message):
  16. if message.author == bot.user:
  17. return
  18. await bot.process_commands(message)
  19. #fun commands
  20. @bot.command()
  21. async def pi(ctx, num_decimals: int):
  22. if num_decimals > 10000:
  23. await ctx.send("Sorry, I can only print up to 10000 decimal places.")
  24. return
  25. decimal.getcontext().prec = num_decimals + 1
  26. pi_value = decimal.Decimal(0)
  27. for i in range(num_decimals + 1):
  28. 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)))
  29. pi_str = str(pi_value)[:num_decimals + 2] # Fix: changed to num_decimals + 2
  30. await ctx.send(f"The value of pi to {num_decimals} decimal places is: {pi_str}")
  31. try:
  32. bot.run(token)
  33. except discord.HTTPException as e:
  34. if e.status == 429:
  35. print("The Discord servers denied the connection for making too many requests")
  36. else:
  37. 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:

  1. import decimal
  2. import discord
  3. from discord.ext import commands
  4. from discord.ext.commands import has_permissions, MissingPermissions
  5. #token
  6. token = "hidden"
  7. #intents and prefix
  8. intents = discord.Intents.default()
  9. bot = commands.Bot(command_prefix="?", intents=intents)
  10. #basic events
  11. @bot.event
  12. async def on_ready():
  13. print('We have logged in as {0.user}'.format(bot))
  14. @bot.event
  15. async def on_message(message):
  16. if message.author == bot.user:
  17. return
  18. await bot.process_commands(message)
  19. #fun commands
  20. @bot.command()
  21. async def pi(ctx, num_decimals: int):
  22. if num_decimals > 10000:
  23. await ctx.send("Sorry, I can only print up to 10000 decimal places.")
  24. return
  25. decimal.getcontext().prec = num_decimals + 1
  26. pi_value = decimal.Decimal(0)
  27. for i in range(num_decimals + 1):
  28. 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)))
  29. pi_str = str(pi_value)[:num_decimals + 1]
  30. await ctx.send(f"The value of pi to {num_decimals} decimal places is: {pi_str}")
  31. try:
  32. bot.run(token)
  33. except discord.HTTPException as e:
  34. if e.status == 429:
  35. print("The Discord servers denied the connection for making too many requests")
  36. else:
  37. 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

  1. 你可以通过利用 mpmath 库来解决这个问题使你的代码更简洁
  2. ```python
  3. from mpmath import mp
  4. @bot.command()
  5. async def pi(ctx, num_decimals: int):
  6. if num_decimals > 10000:
  7. await ctx.send("抱歉,我只能显示最多10000位小数。")
  8. else:
  9. mp.dps = num_decimals + 1 # 设置小数位数;我们加一是因为第一位不是小数
  10. 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:

  1. from mpmath import mp
  2. @bot.command()
  3. async def pi(ctx, num_decimals: int):
  4. if num_decimals > 10000:
  5. await ctx.send("Sorry, I can only print up to 10000 decimal places.")
  6. else:
  7. mp.dps = num_decimals + 1 # set number of digits; We are adding one beause the first number is not a decimal
  8. 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:

确定