python - 有没有办法阻止已经在执行的命令在 discord.py 中完成?
问题描述
如果用户按照.stoprepeat
.
import discord
from discord.ext import commands, tasks
import asyncio
import string
import time
import random
import datetime
from datetime import datetime
import os
class Botting(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
print("Botting Cog is online and connected to Discord")
@commands.command()
async def repeat(self, ctx, times: int, *, content):
"""Repeats a message multiple times."""
for i in range(times):
await ctx.send(content)
await asyncio.sleep(0.75)
def setup(client):
client.add_cog(Botting(client))
解决方案
你可以通过使用asyncio.Task
一个单独定义的协程来做到这一点。
您必须将 分配给Task
cog 的属性并使用它在另一个命令中取消它。
如果您预计该命令会同时运行多次,您可以存储一个Task
s 列表并在另一个命令中取消每个。