如何在Python Discord Bot中制作一个停止倒计时的命令?

问题描述 投票:2回答:1

我想让这个停止,我的意思是一个单独的命令(~cdstop)将取消倒计时。

import discord
import asyncio

counter_channel = None

async def ex(args, message, client, invoke):

  global counter_channel
  if counter_channel is not None:
    await client.send_message(message.channel, embed=discord.Embed(color=discord.Color.red(), "There is a counter in {}".format(counter_channel.mention)))
    return

  counter_channel = message.channel

  await client.send_message(message.channel, "5 Minutes")
  await asyncio.sleep(60)
  await client.send_message(message.channel, "4 Minutes")
  await asyncio.sleep(60)
  await client.send_message(message.channel, "3 Minutes")
  await asyncio.sleep(60)
  await client.send_message(message.channel, "2 Minutes")
  await asyncio.sleep(60)
  await client.send_message(message.channel, "1 Minutes")
  await asyncio.sleep(30)
  await client.send_message(message.channel, "30 Seconds")
  await asyncio.sleep(15)
  await client.send_message(message.channel, "15 Seconds")
  await asyncio.sleep(10)
  await client.send_message(message.channel, "5 Seconds")

  counter_channel = None

我的想法是,在每个print语句之后,它将检查用户是否键入了~cdstop然后从函数中断。我认为它会起作用,但我不知道它是否有效,因为我知道它会弄乱双重倒计时检查。

编辑

调用文件的代码:

import discord
from discord import Game, Embed

import SECRETS
import STATICS
from commands import cmd_ping, cmd_clear, cmd_help, cmd_userinfo, cmd_cdstart

client = discord.Client()


commands = {

    "ping": cmd_ping,
    "clear": cmd_clear,
    "help": cmd_help,
    "userinfo": cmd_userinfo,
    "cdstart": cmd_cdstart,

}


@client.event
async def on_ready():
    print("Bot is logged in successfully. Running on servers:\n")
    [(lambda s: print("  - %s (%s)" % (s.name, s.id)))(s) for s in client.servers]
    await client.change_presence(game=Game(name="~help"))


@client.event
async def on_message(message):
    if message.content.startswith(STATICS.PREFIX):
        invoke = message.content[len(STATICS.PREFIX):].split(" ")[0]
        args = message.content.split(" ")[1:]
        if commands.__contains__(invoke):
            await commands.get(invoke).ex(args, message, client, invoke)
        else:
            await client.send_message(message.channel, embed=Embed(color=discord.Color.red(), description=("The command `%s` is not valid!" % invoke)))

client.run(SECRETS.TOKEN)
python python-3.x discord.py
1个回答
0
投票

这是一个解决方案,使用AbstractEventLoop.create_task创建一个Task对象,然后你可以随时Task.cancel

bot = commands.Bot(command_prefix="!")
counter_channel = None
task = None

async def ex(message):

    global counter_channel
    if counter_channel is not None:
        await bot.send_message(
            message.channel,
            embed=discord.Embed("There is a counter in {}".format(counter_channel.mention), color=discord.Color.red() ))
        return

    counter_channel = message.channel
    # reduced waits for easy testing
    await bot.send_message(message.channel, "5 Minutes")
    await asyncio.sleep(5)
    await bot.send_message(message.channel, "4 Minutes")
    await asyncio.sleep(5)
    await bot.send_message(message.channel, "3 Minutes")
    await asyncio.sleep(5)
    await bot.send_message(message.channel, "2 Minutes")
    await asyncio.sleep(5)
    counter_channel = None

@bot.command(pass_context=True)
async def timer(ctx):
    global task
    task = bot.loop.create_task(ex(ctx.message))

@bot.command(pass_context=True)
async def cancel(ctx):
    global task, counter_channel
    task.cancel()
    task = None
    counter_channel = None
© www.soinside.com 2019 - 2024. All rights reserved.