【问题标题】:discord.py - Assistance with Count-Down Commanddiscord.py - 倒计时命令的帮助
【发布时间】: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


    【解决方案1】:

    我们可以通过使用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,我们将在wait_for_message 中包含一个check 以查找该内容

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

    【讨论】:

    • 好的,但我该怎么做才能收到“否”?如果他们键入“是”或“否”以外的其他内容,我怎样才能做到这一点,以免超时中断?
    • 基本上我想要它,所以代码会忽略除“y”或“n”之外的任何其他内容并继续运行超时。
    • 非常感谢!我需要了解有关命令扩展的更多信息,以显示我不知道多少。再次感谢! :-D
    • 你可以看看重写文档。有一些细微的 APi 差异,但它应该让您了解可能的情况:discordpy.readthedocs.io/en/rewrite/ext/commands/commands.html
    猜你喜欢
    • 2020-11-06
    • 2021-09-16
    • 2019-07-16
    • 2021-01-01
    • 2020-10-19
    • 1970-01-01
    • 2021-05-07
    • 2020-03-31
    • 2021-04-21
    相关资源
    最近更新 更多