【发布时间】:2019-03-17 04:37:16
【问题描述】:
我正在尝试为 Discord Bot 创建一个婚姻命令(不要问我为什么),但我不知道如何让它倒计时 30 秒以接收来自提到的人的输入。我想要的概括:
用户在聊天中输入 !marriage 并提及他们希望“结婚”的人。
在启动的 30 秒计时器结束之前,其他任何人都无法再使用该命令。
被提及的人必须在 30 秒倒计时内输入“y”或“n”以接受或拒绝该提议。
如果提到的人执行了上述任一操作,倒计时将停止,出现一条消息,该命令再次可用。
如果提及的人未能在这 30 秒内回答,则会出现一条消息并且该命令可用。
我已经解决了大部分问题,但我对 Python 还是有点陌生,我根本无法让它工作,而且我觉得我编写的代码既乏味又草率。这里是:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot(command_prefix="!")
@client.event
async def on_message(message):
author = message.author.mention
user_mentioned = message.mentions
if message.content == '!marriage':
await client.send_message(message.channel, author + " you must tag another user!")
if message.content.startswith('!marriage '):
if message.channel.name == "restricted-area":
if len(user_mentioned) == 1:
if message.mentions[0].mention == author:
await client.send_message(message.channel, author + " you cannot tag yourself!")
else:
# if time_up + 30.0 <= time.time() or marriage_active == 1:
global marriage_active
marriage_active = 1
if marriage_active == 1:
global marriage_mention
global marriage_author
marriage_mention = message.mentions[0].id[:]
marriage_author = message.author.id[:]
msg = await client.send_message(message.channel, author + " has asked for " + message.mentions[0].mention + "'s hand in marriage!")
time.sleep(0.3)
await client.edit_message(msg, author + " has asked for " + message.mentions[0].mention + "'s hand in marriage!\n" + message.mentions[0].mention + " if you wish to either accept or deny the request, simply type 'y' for yes or 'n' for no!")
time.sleep(0.3)
await client.edit_message(msg, author + " has asked for " + message.mentions[0].mention + "'s hand in marriage!\n" + message.mentions[0].mention + " if you wish to either accept or deny the request, simply type 'y' for yes or 'n' for no!\n You have 30 seconds to reply...")
marriage_active = 0
global time_up
time_up = time.time()
else:
await client.send_message(message.channel, author + " please wait until the current request is finished!")
else:
await client.send_message(message.channel, author + " this command is currently in the works!")
if message.content == "y":
if message.author.id == marriage_author and marriage_active == 0:
if time_up + 30.0 > time.time():
await client.send_message(message.channel, author + " said yes! :heart:")
marriage_active = 1
else:
marriage_active = 1
if message.content == "n":
if message.author.id == marriage_author and marriage_active == 0:
if time_up + 30.0 > time.time():
await client.send_message(message.channel, author + " denied <@" + marriage_author + ">'s proposal. :cry:")
marriage_active = 1
else:
marriage_active = 1
我不能在 !marriage 命令中使用 while 循环,因为它在 30 秒结束之前永远不会离开命令,而且我也不能将“y”或“n”命令放入 !marriage因为他们不会被提到的人捡到,只会被最初发起命令的作者捡到。
如果有更好的方法来解决我的问题或简单地解决我的问题,非常感谢任何帮助! :-)
【问题讨论】:
标签: python-3.x variables time discord.py