discord.py 如何从消息中获取整数
discord.py how to get the integer from a message
所以我正在使用 discord.py 制作一个机器人,我正在为它制作一个投币功能,但我想使用
if message.content == "g! coinflip":
await message.channel.send("<the coinflip function>")
而不是
@bot.command()
async def coinflip():
await message.channel.send("<the coinflip function>")
因为它的前缀是 g!
并且在 discord 中我希望命令看起来像这样 g! coinflip 10000
但显然用户会选择他们想要赌博的金额,这就是它的原因很难,但是是的,如果你能帮忙,请帮忙
如果您坚持使用 on_message
事件,您可以尝试的一种方法是使用 .split()
方法。然后,您可以检查是否有足够的参数。之后,您可以将最后一个参数(即数字)转换为整数。尽管此处未显示,但您应该首先检查最后一个参数是否实际上是一个整数,否则您将收到错误消息。代码如下所示。
if message.content.lower() == "g! coinflip":
con = message.content.lower().split(" ")
# returned con example: ["g!", "coinflip", "3"]
if len(con) < 3:
await message.channel.send("Not enough arguments! Please give a number") # send the necessary feedback
return # end this function
elif len(con) > 3:
await message.channel.send("Too many arguments! Please only give a number")
return
num = int(con[-1]) # turn the last number into an integer
# then use this number for the rest of your function
所以我正在使用 discord.py 制作一个机器人,我正在为它制作一个投币功能,但我想使用
if message.content == "g! coinflip":
await message.channel.send("<the coinflip function>")
而不是
@bot.command()
async def coinflip():
await message.channel.send("<the coinflip function>")
因为它的前缀是 g!
并且在 discord 中我希望命令看起来像这样 g! coinflip 10000
但显然用户会选择他们想要赌博的金额,这就是它的原因很难,但是是的,如果你能帮忙,请帮忙
如果您坚持使用 on_message
事件,您可以尝试的一种方法是使用 .split()
方法。然后,您可以检查是否有足够的参数。之后,您可以将最后一个参数(即数字)转换为整数。尽管此处未显示,但您应该首先检查最后一个参数是否实际上是一个整数,否则您将收到错误消息。代码如下所示。
if message.content.lower() == "g! coinflip":
con = message.content.lower().split(" ")
# returned con example: ["g!", "coinflip", "3"]
if len(con) < 3:
await message.channel.send("Not enough arguments! Please give a number") # send the necessary feedback
return # end this function
elif len(con) > 3:
await message.channel.send("Too many arguments! Please only give a number")
return
num = int(con[-1]) # turn the last number into an integer
# then use this number for the rest of your function