如何使用python制作不和谐的倒计时机器人

时间:2020-05-21 19:24:23

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

我正在尝试制作一个机器人,如果您告诉它将倒计时到 7:00 pm ,它将倒计时到该时间,然后发送一条消息,例如 @每个人的倒数计时结束,如果用户输入“ cancel”,倒数计时会取消

请注意,我希望它像闹钟一样,而不是计时器

除了倒计时和倒数计时后的消息外,我已经完成了所有操作

这是现在的代码

import discord
from discord.ext import commands
from discord.ext import tasks

client = commands.Bot(command_prefix = "m!")

@client.command(aliases = ["countdown"])
async def countdown_at(ctx, time):
    await ctx.send(f"OK\countdown made to {time}")

2 个答案:

答案 0 :(得分:0)

在等待取消消息的同时,您可以将wait_for上的@bot.command() async def ex(ctx, time: int): await ctx.send("Countdown started") def check(message): return message.channel == ctx.channel and message.author == ctx.author and message.content.lower() == "cancel" try: m = await bot.wait_for("message", check=check, timeout=time) await ctx.send("Countdown cancelled") except asyncio.TimeoutError: await ctx.send(f"{ctx.guild.default_role} countdown finished") 用作计时器:

implicit any

答案 1 :(得分:0)

我想出了制作方法, 在这里:

now = datetime.now()
    mtimeA = time
    mtimeB = mtimeA.split(":")
    hr = int(mtimeB[0])
    min = int(mtimeB[1])
    secsleft = int((timedelta(hours=24) - (now - now.replace(hour=hr, minute=min, second=0, microsecond=0))).total_seconds() % (24 * 3600))
    await ctx.send(f"OK\nAlarm set to {time}")
    def check(message):
        return message.author == ctx.author and message.content.lower() == "cancel alarm"
    try:
        await client.wait_for("message", check=check, timeout=secsleft)
        await ctx.send("Alarm cancelled")
    except:
        await ctx.send(f"{ctx.message.author.mention} alarm finished")