如何添加空白命令?(不和谐.py)

2024-06-17 09:29:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在正在创建一个骰子机器人,想创建一个灵活的命令。例如,'!1d6'(!XdY公司)。 另外,我想创建一个命令,比如'![游戏名][命令]'。例如,'!Cthulhu CBR(50,20)'(因为有很多游戏名,手动添加很困难)。你知道吗

但都有信心,可以实现同样的方式,不知道的方式。 我知道如何添加消息,但我不知道如何添加@client.command命令(). 你知道吗

请告诉我。你知道吗

[我用谷歌翻译。]


Tags: 命令client游戏消息方式机器人公司手动
1条回答
网友
1楼 · 发布于 2024-06-17 09:29:40
import random

@client.event
async def on_message(message):
  # Takes in format dice!xdy i.e dice!3d7 rolls 3 d7 dice
  if (message.content.startswith == 'dice!'):
    await client.send_message(message.channel, 'Rolling...')
    diceArr = message.content[5:].split("d")
    totRolled = 0
    for x in range(int(diceArr[0])):
      totRolled = random.randrange(1, int(diceArr[1]), 1) + totRolled
    await client.send_message(message.channel, 'Rolled', totRolled)

这样就可以了。我还没有测试它,因为我的Linux笔记本电脑已经死了

非冲突版本:

import random

# Takes in format dice!xdy i.e dice!3d7 rolls 3 d7 dice
def roll(stri):
  print('Rolling...')
  diceArr = stri[5:].split("d")
  totRolled = 0
  for x in range(int(diceArr[0])):
    totRolled = random.randrange(1, int(diceArr[1]), 1) + totRolled
  print('Rolled', str(totRolled))

roll("dice!3d7")

相关问题 更多 >