首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我该怎么做秒表来防止不和谐的机器人?

我该怎么做秒表来防止不和谐的机器人?
EN

Stack Overflow用户
提问于 2022-07-06 16:21:14
回答 1查看 74关注 0票数 -1

我是一个库珀和一个程序员,我在帮助一个朋友谁提出了一个想法,在制造一个小机器人!因此,我们开始对不同的立方体进行争夺,然后我们试图为机器人做一个秒表,但是当我们运行的时候!停在通道上,它不会停止时间循环,相反,它只是继续前进!

代码语言:javascript
复制
import random
import discord
import os
import re
import keep_alive
import time

client = discord.Client()

@client.event
async def on_ready():
  print("Bot is ready")

@client.event
async def on_message(message):
  
  timer = 0.97
  limit = 600.00
  
  if message.author == client.user:
    return
  
  if message.content.startswith("!3X3"):

    num = 0
    Scramble = []
    num2 = random.randint(15,26)
  
    while num < num2:
        notation = random.randint(1,18)
        num += 1
      
        if notation == 1:
          Scramble.append("U ")
        elif notation == 2:
          Scramble.append("U' ")
        elif notation == 3:
          Scramble.append("U2 ")
        elif notation == 4:
          Scramble.append("D ")
        elif notation == 5:
          Scramble.append("D' ")
        elif notation == 6:
          Scramble.append("D2 ")
        elif notation == 7:
          Scramble.append("R ")
        elif notation == 8:
          Scramble.append("R' ")
        elif notation == 9:
          Scramble.append("R2 ")
        elif notation == 10:
          Scramble.append("L ")
        elif notation == 11:
          Scramble.append("L' ")
        elif notation == 12:
          Scramble.append("L2 ")
        elif notation == 13:
          Scramble.append("F ")
        elif notation == 14:
          Scramble.append("F' ")
        elif notation == 15:
          Scramble.append("F2 ")
        elif notation == 16:
          Scramble.append("B ")
        elif notation == 17:
          Scramble.append("B' ")
        else: 
          Scramble.append("B2 ")
    
    ScrambleBR = (','.join(Scramble))
    ScrambleAF = re.sub(",","",ScrambleBR)

    ctx1 = await message.channel.send("`Generating 3X3 scramble . . .`")
    
    time.sleep(0.2)
    
    ScrambleRRL = re.sub("l' l'" or "l l","l2",ScrambleAF)
    time.sleep(0.2)
    ScrambleRRLL = re.sub("L' L'" or "L L","L2",ScrambleRRL)
    time.sleep(0.2)
    ScrambleRRLLL = re.sub("l2 l2" or "L2 L2" or "l' l' l' l'" or "L' L' L' L'" or "l l l l" or "L L L L" "l' l' l2" or "l2 l' l'" or "l l l2" or "l2 l l" or "L' L' L2" or "L2 L' L'" or "L L L2" or "L2 L L","",ScrambleRRLL)

    time.sleep(0.2)
  
    ScrambleRRR = re.sub("r' r'" or "r r","r2",ScrambleRRLLL)
    time.sleep(0.2)
    ScrambleRRRR = re.sub("R' R'" or "R R","R2",ScrambleRRR)
    time.sleep(0.2)
    ScrambleRRRRR = re.sub("r2 r2" or "R2 R2" or "r' r' r' r'" or "R' R' R' R'" or "r r r r" or "R R R R" "r' r' r2" or "r2 r' r'" or "r r r2" or "r2 r r" or "R' R' R2" or "R2 R' R'" or "R R R2" or "R2 R R","",ScrambleRRRR)

    time.sleep(0.2)
  
    ScrambleRRF = re.sub("f' f'" or "f f","f2",ScrambleRRRRR)
    time.sleep(0.2)
    ScrambleRRFF = re.sub("F' F'" or "F F","F2",ScrambleRRF)
    time.sleep(0.2)
    ScrambleRRFFF = re.sub("f2 f2" or "F2 F2" or "f' f' f' f'" or "F' F' F' F'" or "f f f f" or "F F F F" or "f' f' f2" or "f2 f' f'" or "f f f2" or "f2 f f" "F' F' F2" or "F2 F' F'" or "F F F2" or "F2 F F","",ScrambleRRFF)

    time.sleep(0.2)

    ScrambleRRD = re.sub("D' D'" or "D D","D2",ScrambleRRFFF)
    time.sleep(0.2)
    ScrambleRRDD = re.sub("D' D' D' D'" or "D D D D","",ScrambleRRD)

    time.sleep(0.2)
  
    ScrambleRRBB = re.sub("B' B'" or "B B","B2",ScrambleRRDD)
    time.sleep(0.2)
    ScrambleFinal = re.sub("B2 B2" or "B' B' B' B'" or "B B B B" or "B' B' B2" or "B2 B' B'" or "B B B2" or "B2 B B","",ScrambleRRBB)
    
    await ctx1.edit(content = ScrambleFinal)

  if message.content.startswith("!2X2"):

    numm = 0
    Scramble2X2 = []
    numm2 = random.randint(8,10)
  
    while numm < numm2:
        notationn = random.randint(1,18)
      
        if notationn == 1:
          Scramble2X2.append("U ")
        elif notationn == 2:
          Scramble2X2.append("U' ")
        elif notationn == 3:
          Scramble2X2.append("U2 ")
        elif notationn == 4:
          Scramble2X2.append("D ")
        elif notationn == 5:
          Scramble2X2.append("D' ")
        elif notationn == 6:
          Scramble2X2.append("D2 ")
        elif notationn == 7:
          Scramble2X2.append("R ")
        elif notationn == 8:
          Scramble2X2.append("R' ")
        elif notationn == 9:
          Scramble2X2.append("R2 ")
        elif notationn == 10:
          Scramble2X2.append("L ")
        elif notationn == 11:
          Scramble2X2.append("L' ")
        elif notationn == 12:
          Scramble2X2.append("L2 ")
        elif notationn == 13:
          Scramble2X2.append("F ")
        elif notationn == 14:
          Scramble2X2.append("F' ")
        elif notationn == 15:
          Scramble2X2.append("F2 ")
        elif notationn == 16:
          Scramble2X2.append("B ")
        elif notationn == 17:
          Scramble2X2.append("B' ")
        else: 
          Scramble2X2.append("B2 ")
        numm += 1

    ScrambleBRR = (','.join(Scramble2X2))
    ScrambleAF2 = re.sub(",","",ScrambleBRR)
    ctx1 = await message.channel.send("`Generating 2X2 scramble . . .`")
    
    time.sleep(0.2)
    
    ScrambleRRL2 = re.sub("l' l'" or "l l","l2",ScrambleAF2)
    time.sleep(0.2)
    ScrambleRRLL2 = re.sub("L' L'" or "L L","L2",ScrambleRRL2)
    time.sleep(0.2)
    ScrambleRRLLL2 = re.sub("l2 l2" or "L2 L2" or "l' l' l' l'" or "L' L' L' L'" or "l l l l" or "L L L L" "l' l' l2" or "l2 l' l'" or "l l l2" or "l2 l l" or "L' L' L2" or "L2 L' L'" or "L L L2" or "L2 L L","",ScrambleRRLL2)

    time.sleep(0.2)
  
    ScrambleRRR2 = re.sub("r' r'" or "r r","r2",ScrambleRRLLL2)
    time.sleep(0.2)
    ScrambleRRRR2 = re.sub("R' R'" or "R R","R2",ScrambleRRR2)
    time.sleep(0.2)
    ScrambleRRRRR2 = re.sub("r2 r2" or "R2 R2" or "r' r' r' r'" or "R' R' R' R'" or "r r r r" or "R R R R" "r' r' r2" or "r2 r' r'" or "r r r2" or "r2 r r" or "R' R' R2" or "R2 R' R'" or "R R R2" or "R2 R R","",ScrambleRRRR2)

    time.sleep(0.2)
  
    ScrambleRRF2 = re.sub("f' f'" or "f f","f2",ScrambleRRRRR2)
    time.sleep(0.2)
    ScrambleRRFF2 = re.sub("F' F'" or "F F","F2",ScrambleRRF2)
    time.sleep(0.2)
    ScrambleRRFFF2 = re.sub("f2 f2" or "F2 F2" or "f' f' f' f'" or "F' F' F' F'" or "f f f f" or "F F F F" or "f' f' f2" or "f2 f' f'" or "f f f2" or "f2 f f" "F' F' F2" or "F2 F' F'" or "F F F2" or "F2 F F","",ScrambleRRFF2)

    time.sleep(0.2)

    ScrambleRRD2 = re.sub("D' D'" or "D D","D2",ScrambleRRFFF2)
    time.sleep(0.2)
    ScrambleRRDD2 = re.sub("D' D' D' D'" or "D D D D","",ScrambleRRD2)

    time.sleep(0.2)
  
    ScrambleRRBB2 = re.sub("B' B'" or "B B","B2",ScrambleRRDD2)
    time.sleep(0.2)
    ScrambleFinal2 = re.sub("B2 B2" or "B' B' B' B'" or "B B B B" or "B' B' B2" or "B2 B' B'" or "B B B2" or "B2 B B","",ScrambleRRBB2)

    await ctx1.edit(content = ScrambleFinal2)

  if message.content.startswith("!4X4"):

    num4 = 0
    Scramble4X4 = []
    num24 = random.randint(43,47)
  
    while num4 < num24:
        notation4 = random.randint(1,36)
        num4 += 1
      
        if notation4 == 1:
          Scramble4X4.append("U ")
        elif notation4 == 2:
          Scramble4X4.append("U' ")
        elif notation4 == 3:
          Scramble4X4.append("U2 ")
        elif notation4 == 4:
          Scramble4X4.append("Uw ")
        elif notation4 == 5:
          Scramble4X4.append("Uw' ")
        elif notation4 == 6:
          Scramble4X4.append("Uw2 ")
        elif notation4 == 7:
          Scramble4X4.append("D ")
        elif notation4 == 8:
          Scramble4X4.append("D' ")
        elif notation4 == 9:
          Scramble4X4.append("D2 ")
        elif notation4 == 10:
          Scramble4X4.append("Dw ")
        elif notation4 == 11:
          Scramble4X4.append("Dw' ")
        elif notation4 == 12:
          Scramble4X4.append("Dw2 ")
        elif notation4 == 13:
          Scramble4X4.append("R ")
        elif notation4 == 14:
          Scramble4X4.append("R' ")
        elif notation4 == 15:
          Scramble4X4.append("R2 ")
        elif notation4 == 16:
          Scramble4X4.append("Rw ")
        elif notation4 == 17:
          Scramble4X4.append("Rw' ")
        elif notation4 == 18:
          Scramble4X4.append("Rw2 ")
        elif notation4 == 19:
          Scramble4X4.append("L ")
        elif notation4 == 20:
          Scramble4X4.append("L' ")
        elif notation4 == 21:
          Scramble4X4.append("L2 ")
        elif notation4 == 22:
          Scramble4X4.append("Lw ")
        elif notation4 == 23:
          Scramble4X4.append("Lw' ")
        elif notation4 == 24:
          Scramble4X4.append("Lw2 ")
        elif notation4 == 25:
          Scramble4X4.append("F ")
        elif notation4 == 26:
          Scramble4X4.append("F' ")
        elif notation4 == 27:
          Scramble4X4.append("F2 ")
        elif notation4 == 28:
          Scramble4X4.append("Fw ")
        elif notation4 == 29:
          Scramble4X4.append("Fw' ")
        elif notation4 == 30:
          Scramble4X4.append("Fw2 ")
        elif notation4 == 31:
          Scramble4X4.append("B' ")
        elif notation4 == 32:
          Scramble4X4.append("B ")
        elif notation4 == 33: 
          Scramble4X4.append("B2 ")  
        elif notation4 == 34:
          Scramble4X4.append("Bw ")
        elif notation4 == 35:
          Scramble4X4.append("Bw' ")
        else:
          Scramble4X4.append("Bw2 ")
    
    ScrambleBR4 = (','.join(Scramble4X4))
    ScrambleAR4 = re.sub(",","",ScrambleBR4)
    ctx4 = await message.channel.send("`Generating 4X4 scramble . . .`")

    time.sleep(0.2)
    
    ScrambleRRL4 = re.sub("l' l'" or "l l","l2",ScrambleAR4)
    time.sleep(0.2)
    ScrambleRRLL4 = re.sub("L' L'" or "L L","L2",ScrambleRRL4)
    time.sleep(0.2)
    ScrambleRRLLL4 = re.sub("l2 l2" or "L2 L2" or "l' l' l' l'" or "L' L' L' L'" or "l l l l" or "L L L L" "l' l' l2" or "l2 l' l'" or "l l l2" or "l2 l l" or "L' L' L2" or "L2 L' L'" or "L L L2" or "L2 L L","",ScrambleRRLL4)

    time.sleep(0.2)
  
    ScrambleRRR4 = re.sub("r' r'" or "r r","r2",ScrambleRRLLL4)
    time.sleep(0.2)
    ScrambleRRRR4 = re.sub("R' R'" or "R R","R2",ScrambleRRR4)
    time.sleep(0.2)
    ScrambleRRRRR4 = re.sub("r2 r2" or "R2 R2" or "r' r' r' r'" or "R' R' R' R'" or "r r r r" or "R R R R" "r' r' r2" or "r2 r' r'" or "r r r2" or "r2 r r" or "R' R' R2" or "R2 R' R'" or "R R R2" or "R2 R R","",ScrambleRRRR4)

    time.sleep(0.2)
  
    ScrambleRRF4 = re.sub("f' f'" or "f f","f2",ScrambleRRRRR4)
    time.sleep(0.2)
    ScrambleRRFF4 = re.sub("F' F'" or "F F","F2",ScrambleRRF4)
    time.sleep(0.2)
    ScrambleRRFFF4 = re.sub("f2 f2" or "F2 F2" or "f' f' f' f'" or "F' F' F' F'" or "f f f f" or "F F F F" or "f' f' f2" or "f2 f' f'" or "f f f2" or "f2 f f" "F' F' F2" or "F2 F' F'" or "F F F2" or "F2 F F","",ScrambleRRFF4)

    time.sleep(0.2)

    ScrambleRRD4 = re.sub("D' D'" or "D D","D2",ScrambleRRFFF4)
    time.sleep(0.2)
    ScrambleRRDD4 = re.sub("D' D' D' D'" or "D D D D","",ScrambleRRD4)

    time.sleep(0.2)
  
    ScrambleRRBB4 = re.sub("B' B'" or "B B","B2",ScrambleRRDD4)
    time.sleep(0.2)
    ScrambleFinal4 = re.sub("B2 B2" or "B' B' B' B'" or "B B B B" or "B' B' B2" or "B2 B' B'" or "B B B2" or "B2 B B","",ScrambleRRBB4)
    await ctx4.edit(content = ScrambleFinal4)

  if message.content.startswith("!pyra"):
    nump = 0
    ScramblePyra = []
    nump2 = random.randint(10,12)
  
    while nump < nump2:
      notationp = random.randint(1,18)
      
      if notationp == 1:
        ScramblePyra.append("U ")
      elif notationp == 2:
        ScramblePyra.append("U' ")
      elif notationp == 3:
        ScramblePyra.append("U2 ")
      elif notationp == 4:
        ScramblePyra.append("R ")
      elif notationp == 5:
        ScramblePyra.append("R' ")
      elif notationp == 6:
        ScramblePyra.append("R2 ")
      elif notationp == 7:
        ScramblePyra.append("L ")
      elif notationp == 8:
        ScramblePyra.append("L' ")
      elif notationp == 9:
        ScramblePyra.append("L2 ")
      elif notationp == 10:
        ScramblePyra.append("r ")
      elif notationp == 11:
        ScramblePyra.append("r' ")
      elif notationp == 12:
        ScramblePyra.append("l ")
      elif notationp == 13:
        ScramblePyra.append("l' ")
      elif notationp == 14:
        ScramblePyra.append("b ")
      elif notationp == 15:  
        ScramblePyra.append("b' ")
      elif notationp == 16:
        ScramblePyra.append("B' ")
      elif notationp == 17:
        ScramblePyra.append("B ")
      else: 
        ScramblePyra.append("B2 ")
      nump += 1

    ScrambleBRRp = (','.join(ScramblePyra))
    ScrambleFinal3 = re.sub(",","",ScrambleBRRp)
    await message.channel.send(ScrambleFinal3)

  if message.content.startswith("!stop"):
      await message.channel.send(timer)
  
  if message.content.startswith("!start"):

    timerm = await message.channel.send("`Time will be here`")
    
    ready = await message.channel.send("**3**")
    time.sleep(1)
    set = await message.channel.send("**2**")
    time.sleep(1)
    go = await message.channel.send("**1**")
    time.sleep(1)
    GO = await message.channel.send("**GO!**")
    time.sleep(0.97)
    
    while timer < limit:
      time.sleep(0.01)
      timer += 0.01
      print(timer) #for testing if stopwatch works
      if message.content.startswith("!stop"):
        c = await message.channel.send(timer)
        break
      
keep_alive.keep_alive()
client.run(os.getenv("Token"))

这是我们正在努力解决的问题。

代码语言:javascript
复制
  if message.content.startswith("!stop"):
      await message.channel.send(timer)
  
  if message.content.startswith("!start"):

    timerm = await message.channel.send("`Time will be here`")
    
    ready = await message.channel.send("**3**")
    time.sleep(1)
    set = await message.channel.send("**2**")
    time.sleep(1)
    go = await message.channel.send("**1**")
    time.sleep(1)
    GO = await message.channel.send("**GO!**")
    time.sleep(0.97)
    
    while timer < limit:
      time.sleep(0.01)
      timer += 0.01
      print(timer) #for testing if stopwatch works
      if message.content.startswith("!stop"):
        c = await message.channel.send(timer)
        break

我一直在网上寻找答案,有些是类似的,但不是我想要的。我希望有人能解决这个问题,因为你可能永远改变机器人!

EN

回答 1

Stack Overflow用户

发布于 2022-07-06 16:31:11

让我们来看看这个:

代码语言:javascript
复制
@client.event
async def on_message(message):

无论何时发送消息,都会调用此命令。message作为参数传递,然后可以使用它。

现在,假设用户调用"!start"

然后我们将输入以下if-语句:

代码语言:javascript
复制
if message.content.startswith("!start"):

在编写了更多代码之后,将运行以下代码:

代码语言:javascript
复制
while timer < limit:
      #some code
      if message.content.startswith("!stop"):
        c = await message.channel.send(timer)
        break

这里的问题在于if message.content.startswith("!stop"):。实际上,没有读取一条新消息;这个if语句是用同一个message运行的,该message"!start"开头。在这个协同过程中,消息以"!start"开头,因此我们永远不会输入这个if语句,因为它肯定不是以"!stop"开头的。

现在,当"!stop"实际被调用时,它处于一个新的on_message(message)协同线中,这意味着它不与旧的while循环交互。相反,在新的协同线中运行此代码:

代码语言:javascript
复制
await message.channel.send(timer)

while循环继续进行,这意味着您的计时器将继续运行。

这意味着,调用"!stop"永远不会中断循环,因为它是在单独的协同线中完成的,尽管它会打印出timer的内容。

你可以尝试几种方法来解决这个问题。您可以为计时器的while循环打开一个新的线程,在计时器结束时打开切断线

据我所知,这是最优雅的方式,虽然我很兴奋看到人们可以在评论中得出什么。我会把这些加到我的答案里。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72886915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档