【问题标题】:How can I correctly implement a random integer command in a Discord bot with discord.py?如何使用 discord.py 在 Discord 机器人中正确实现随机整数命令?
【发布时间】:2021-06-01 13:29:51
【问题描述】:

我最近决定尝试使用 discord.py 在 Discord 中制作一个机器人。机器人本身已成功创建并集成到公会中,但我在执行某些命令时遇到了一些问题。

import discord
from discord.ext import commands
import os
import random

bot = commands.Bot(command_prefix = "?")

@bot.command()
async def ping(ctx): 
      await ctx.channel.send("pong")

@bot.command()
async def rand(ctx, arg1, arg2):
      await ctx.channel.send(random.randint(arg1,arg2))

bot.run(os.getenv('TOKEN'))

当我在我的公会中输入“?ping”时,机器人会回复“pong”,但是当我输入“?rand a b”时,a

如何使用 bot 命令正确实现 randint() 函数?

【问题讨论】:

    标签: python discord command discord.py


    【解决方案1】:

    将 arg1 和 arg2 从字符串转换为整数,然后再将它们传递给 randint。

    【讨论】:

      猜你喜欢
      • 2021-01-01
      • 2023-01-26
      • 2020-12-24
      • 2018-12-01
      • 2018-05-06
      • 2021-10-06
      • 2021-08-14
      • 2019-12-14
      • 2020-07-09
      相关资源
      最近更新 更多