只需以某种方式存储一个计数器并使用random 模块来选择一个随机提示
如果对命令使用函数:
import random
hi_counter = 0
hi_tips = [
"You can also use kick and ban commands. Use `-help` for more info.",
"a tip",
"another tip",
"yet another tip",
]
@bot.command('hi')
async def hi(ctx):
global hi_counter, hi_tips
await ctx.send('hi')
hi_counter += 1
if hi_counter % 5 == 0:
await asyncio.sleep(1)
await ctx.send(random.choice(hi_tips))
如果您将 Cog 用于命令:
import random
class hi(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.counter = 0
self.tips = [
"You can also use kick and ban commands. Use `-help` for more info.",
"a tip",
"another tip",
"yet another tip",
]
@commands.command('hi')
async def hi(self, ctx):
await ctx.send('hi')
self.counter += 1
if self.counter % 5 == 0:
await asyncio.sleep(1)
await ctx.send(random.choice(self.tips))
def setup(bot):
bot.add_cog(hi(bot))
如果你想为此做一个装饰器,你也可以这样做:
import random
def tips(times: int, tips: List[str]):
counter = 0
def decorator(func):
nonlocal counter, tips
async def wrapper(*args, **kwargs):
nonlocal counter, tips
ctx = func(*args, **kwargs)
counter += 1
if counter % times == 0:
await asyncio.sleep(1)
await ctx.send(random.choice(tips))
return wrapper
return decorator
# Use it like:
@bot.command('hi')
@tips(5, [
"You can also use kick and ban commands. Use `-help` for more info.",
"a tip",
"another tip",
"yet another tip",
])
async def hi(ctx):
global hi_counter, hi_tips
await ctx.send('hi')
return ctx # MAKE SURE TO RETURN `ctx` OR THE DECORATOR WON'T WORK