如何使不和秒表机器人?



我是一个立方体和一个程序员,我正在帮助一个朋友提出一个想法,使立方体机器人!所以我们开始对不同的立方体进行排序,然后我们试图为机器人制作一个秒表,但是当我们在通道中运行!stop时,它不会停止while循环,而是继续运行!

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"))

这是我们正在努力的部分

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

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

让我们看看这个:

@client.event
async def on_message(message):

在发送消息时调用。message作为参数传递,然后您可以使用它。

现在,假设用户呼叫"!start"

然后输入if语句:

if message.content.startswith("!start"):

在一些代码之后,这将运行:

while timer < limit:
#some code
if message.content.startswith("!stop"):
c = await message.channel.send(timer)
break

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

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

await message.channel.send(timer)

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

这意味着,调用"!stop"不会像在单独的协程中那样中断循环,尽管它会打印出timer的内容。


您可以尝试几种方法来修复此问题。你可以为计时器的while循环打开一个新线程,并在计时器结束时终止该线程。

据我所知,这是最优雅的方式,尽管我很高兴看到人们能在评论中提出什么。我会把这些加到我的答案里。

相关内容

  • 没有找到相关文章

最新更新