我希望当有人使用任何命令时,但仅当消息作者ID不在我的名为results的列表中时,我的Discord机器人才能进行冷却。y =您可以使用该命令多少次。z =冷却时间(以秒为单位)。不知何故它根本不起作用。当我输入$ lucky时,根本没有响应。提前致谢
bot = commands.Bot(command_prefix='$')
y = 0
z = 0
@bot.event
async def on_message(message):
with open(r'path') as f:
content = f.readlines()
content = [x.strip() for x in content]
results = list(map(int, content))
user_id = message.author.id
if user_id in results:
y = 99999
z = 1
return (y,z)
else:
y = 1
z = 10
return (y, z)
await bot.process_commands(message)
@bot.command()
@commands.cooldown(y, z, commands.BucketType.user)
async def lucky(message):
...
它根本不起作用,因为您没有从on_message
事件中检索信息,也无法获得事件返回的值。我的建议-使用global
关键字并编辑全局变量
y = 0
z = 0
@bot.event
async def on_message(message):
global y, z
with open(r'path') as f:
content = f.readlines()
content = [x.strip() for x in content]
results = list(map(int, content))
user_id = message.author.id
if user_id in results:
y = 99999
z = 1
else:
y = 1
z = 10
await bot.process_commands(message)
我也不确定这是否行得通,一旦设置了冷却时间,就无法动态更改它。
编辑:
删除某些用户的冷却时间
@bot.command()
@commands.cooldown(1, 10.0, commands.BucketType.user)
async def foo(ctx):
await ctx.send("Foo")
@bot.event
async def on_message(message):
# Check if the user is in the file and shouldn't have a cooldown
if "user is in file":
context = await bot.get_context(message)
# Check if the command is in cooldown, optional step
if foo.get_cooldown_retry_after(context): # `foo` is the command
# Reseting the cooldown
foo.reset_cooldown(context)
await bot.process_commands(message)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句