我想要创建一个具有垃圾邮件功能的机器人,但条件是它每个周期都要检查停止消息。我试过寻找其他职位,但他们都没有真正的帮助或工作。检查似乎没有做任何事情,好像代码没有到达它。我试着找出我输入到代码中要显示在控制台中的任何“检查”消息,但是它们没有被打印出来,因此我怀疑。
我在控制台中没有错误,输入‘$stop’什么也不做。知道为什么吗?谢谢。
async def bot_spam(message):
try:
msg = message.content.split(' ')
if msg[2] == '1':
await message.channel.send(f"You will ping {msg[1]}, once.")
elif msg[2] == '0':
await message.channel.send("You cant ping 0 times.")
return
else:
await message.channel.send(f"You will ping {msg[1]}, {msg[2]} times.")
time.sleep(1)
msgr = int(msg[2])
global spam
spam = True
while spam is True and msgr > 0:
def check1(msg1: discord.Message):
print("checked")
if msg1.content == "$stop":
return False
return True
try:
if await bot.wait_for('message', check=check1, timeout=1.5):
print("checked 2")
await message.channel.send("Stopping.")
spam = False
except asyncio.TimeoutError:
await message.channel.send(f"{msg[1]}")
msgr -= 1
await message.channel.send("Done.")
except IndexError:
await message.channel.send("Your arguments don't make sense \n It's '$spam @someguy timesyouwantpinged'")发布于 2022-05-11 12:17:05
这是一个逻辑问题。
def check1(msg1: discord.Message):
print("checked")
if msg1.content == "$stop":
return False
return True键入“False”时将返回$stop,因此bot一直在等待消息,直到check返回True
发布于 2022-05-11 19:31:41
创建了一个变量,它使用循环之外的$stop来检查on_message,而不是在循环中。
async def on_message(message):
if message.author == client.user:
return
else:
if message.content == "$stop":
stop = True
return..。
while stop is False and msgr > 0:
await message.channel.send(f"{msg[1]}")
time.sleep(0.5)
msgr -= 1
if stop is True:
await message.channel.send("Stopped.")
else:
await message.channel.send("Done.")https://stackoverflow.com/questions/72199310
复制相似问题