discord.py-倒计时命令协助

时间:2018-10-12 02:01:49

标签: python-3.x variables time discord.py

我正在尝试为Discord Bot创建一个结婚命令(不要问我为什么),而且我不知道如何使它倒计时30秒以接收提到的人的输入。我想要的概括:

用户在聊天中输入!婚姻,并提及他们希望“结婚”的人。

在启动的30秒计时器结束之前,该命令将不再可用于其他任何人。

提到的人必须输入“ y”或“ n”以接受建议或在30秒倒计时之内拒绝建议。

如果提及的人执行了上述任何一项操作,则倒计时将停止,出现一条消息,并且该命令将再次可用。

如果所提到的人在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秒结束,它永远都不会离开该命令,而且我也不能在“ marriage”中放置“ y”或“ n”命令因为提到的人不会选择它们,只有最初发起命令的作者才能选择。

如果有更好的方法可以解决此问题或对我的问题进行简单的修复,则非常感谢您的帮助! :-)

1 个答案:

答案 0 :(得分:0)

我们可以使用commands extension清理很多东西。我们将使用wait_for_message等待消息

from discord.ext import commands
import discord

bot = commands.Bot(command_prefix='!')  # Use Bot instead of Client

@bot.event
async def on_message(message):
    await bot.process_commands(message)  # You need this line if you have an on_message

bot.marriage_active = False
@bot.command(pass_context=True)
async def marriage(ctx, member: discord.Member):
    if bot.marriage_active:
        return  # Do nothing
    bot.marriage_active = True
    await bot.say("{} wanna get hitched?".format(member.mention))
    reply = await bot.wait_for_message(author=member, channel=ctx.message.channel, timeout=30)
    if not reply or reply.content.lower() not in ("y", "yes", "yeah"):
        await bot.say("Too bad")
    else:
        await bot.say("Mazel Tov!")
    bot.marriage_active = False

bot.run("Token")

您可能必须将bot.marriage_active替换为global变量。

要仅接受yn,我们将在check中包含一个wait_for_message,以查找该内容

    def check(message):
       return message.content in ('y', 'n')

    reply = await bot.wait_for_message(author=member, channel=ctx.message.channel, 
                                       timeout=30, check=check)
    if not reply or reply.content == 'n':
        await bot.say("Too bad")
    else:
        await bot.say("Mazel Tov!")