Discord.py 提醒

时间:2021-03-01 16:07:27

标签: python discord.py

嗨,最近开始为一款名为 EPIC RPG 的不和谐游戏开发提醒机器人。我目前有工作代码,但我想重写它,所以我不能为提醒使用错误的文本。例如,我有一个包含所有命令及其冷却时间的列表。但是有一些重叠的命令,例如 tr 和 trade 这两个命令都触发了训练或简称为 tr,我解决了这个问题,但我觉得我的代码没有它应该的那么好。

相关列表:

cooldowns = [
  {"command": "daily", "cooldown": 86400, "alias": ""},
  {"command": "weekly", "cooldown": 604800, "alias": ""},
  {"command": "hunt", "cooldown": 60, "alias": ""},
  {"command": "adventure", "cooldown": 3600, "alias": "adv"},
  {"command": "training", "cooldown": 915, "alias": "tr"},
  {"command": "chop", "cooldown": 300, "alias": ""},
  {"command": "fish", "cooldown": 300, "alias": ""},
  {"command": "axe", "cooldown": 300, "alias": ""},
  {"command": "net", "cooldown": 300, "alias": ""},
  {"command": "pickup", "cooldown": 300, "alias": ""},
  {"command": "ladder", "cooldown": 300, "alias": ""},
  {"command": "mine", "cooldown": 300, "alias": ""},
  {"command": "bowsaw", "cooldown": 300, "alias": ""},
  {"command": "boat", "cooldown": 300, "alias": ""},
  {"command": "pickaxe", "cooldown": 300, "alias": ""},
  {"command": "tractor", "cooldown": 300, "alias": ""},
  {"command": "chainsaw", "cooldown": 300, "alias": ""},
  {"command": "bigboat", "cooldown": 300, "alias": ""},
  {"command": "drill", "cooldown": 300, "alias": ""},
  {"command": "greenhouse", "cooldown": 300, "alias": ""},
  {"command": "dynamite", "cooldown": 300, "alias": ""}
]

我的其余代码可能会更好,但这是我现在拥有的

import discord
import os
import math
import asyncio
from keep_alive import keep_alive

eventReduction = 0  # if no event active change this to 0
eventCommand = "" # if no event active change this to empty string

donors = [
  {"name": "◄DEFINED►#0256", "reduction": 35},
  {"name": "Freekikz#0951", "reduction": 35}
]

cooldowns = [
  {"command": "daily", "cooldown": 86400, "alias": ""},
  {"command": "weekly", "cooldown": 604800, "alias": ""},
  {"command": "hunt", "cooldown": 60, "alias": ""},
  {"command": "adventure", "cooldown": 3600, "alias": "adv"},
  {"command": "training", "cooldown": 915, "alias": "tr"},
  {"command": "chop", "cooldown": 300, "alias": ""},
  {"command": "fish", "cooldown": 300, "alias": ""},
  {"command": "axe", "cooldown": 300, "alias": ""},
  {"command": "net", "cooldown": 300, "alias": ""},
  {"command": "pickup", "cooldown": 300, "alias": ""},
  {"command": "ladder", "cooldown": 300, "alias": ""},
  {"command": "mine", "cooldown": 300, "alias": ""},
  {"command": "bowsaw", "cooldown": 300, "alias": ""},
  {"command": "boat", "cooldown": 300, "alias": ""},
  {"command": "pickaxe", "cooldown": 300, "alias": ""},
  {"command": "tractor", "cooldown": 300, "alias": ""},
  {"command": "chainsaw", "cooldown": 300, "alias": ""},
  {"command": "bigboat", "cooldown": 300, "alias": ""},
  {"command": "drill", "cooldown": 300, "alias": ""},
  {"command": "greenhouse", "cooldown": 300, "alias": ""},
  {"command": "dynamite", "cooldown": 300, "alias": ""}
]

client = discord.Client()

async def checkCommands(message):

  for cooldown in cooldowns:
    content = message.content
    dismantleStop = content.find("dismantle")
    tradeStop = content.find("trade")

    if dismantleStop == -1:
      commandMatch = content.find(cooldown["command"])
    else:
      commandMatch = -1
      
    if cooldown["alias"] != "":
      if tradeStop == -1:
        aliasMatch = content.find(cooldown["alias"])
      else:
        aliasMatch = -1
    else:
      aliasMatch = -1

    if commandMatch != -1 or aliasMatch != -1:
      if commandMatch != -1:
        action = content.upper()[4:]
      elif aliasMatch != -1:
        action = content.replace(cooldown["alias"], cooldown["command"]).upper()[4:]

      firstMessage =  message
      channel = message.channel

      def check(m):
        return m.content[2:len(str(firstMessage.author)) - 3] == str(firstMessage.author)[:len(str(firstMessage.author)) - 5] and m.channel == channel
        #return m.content[:12] != ':police_car:' and m.channel == channel


      msg = await client.wait_for('message', check=check)
      print(msg.content)     

      secondint = checkReductions(cooldown["cooldown"], firstMessage.author, cooldown["command"])

      await channel.send(f'Starting Timer\n```\nUSER\n{firstMessage.author}\nCOMMAND\n{firstMessage.content}\nDURATION\n{secondint}```')
      await startTimer(secondint, message, action)

def checkReductions(secondint, author, action):
  secondint = checkDonor(author, secondint)
  secondint = checkEvent(action, secondint)
  return secondint

def checkDonor(author, secondint):
  for donor in donors:
    if str(author) == donor["name"]:
      secondint = secondint - (secondint / 100 * donor["reduction"])

  return math.floor(secondint)

def checkEvent(action, secondint):
  x = action.find(eventCommand)
  if x != -1:
    action = action[x:(x + len(eventCommand))]

  if action == eventCommand:
    secondint = secondint - (secondint / 100 * eventReduction)

  return math.floor(secondint)

async def startTimer(secondint, message, action):
  while True:
      secondint -= 1
      if secondint == 0:
        await message.channel.send('{}'.format(message.author.mention) + ' ' + "**{}**".format(action) + ' is ready!')
        break
      await asyncio.sleep(1)

#Bot Code
@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
    return

  if message.content.startswith('rpg'):
    await checkCommands(message)

  if message.content.startswith('rpg buy * lootbox'): # fix this
    secondint = 10800
    while True:
      secondint -= 1
      if secondint == 0:
        await message.channel.send('{}'.format(message.author.mention) + ' Lootbox is ready!')
        break
      await asyncio.sleep(1)

  if message.content.startswith('rpg guild upgrade') or message.content.startswith('rpg guild raid'):
    #if guild_ready:
      #guild_ready == False
    secondint = 7200
    while True:
      secondint -= 1
      if secondint == 0:
        #guild_ready == True
        await message.channel.send('<@&808321623949443122> cooldown is ready!')
        break
      await asyncio.sleep(1)

keep_alive()
client.run(os.getenv('TOKEN'))
#END Bot Code

我想重写 checkCommands 函数,这样它就不会对列表中的其他内容做出反应。

提前致谢 如果您对我的代码有任何疑问,请尽我所能尽可能多地解释。

0 个答案:

没有答案