python - Discord Bot 在第二次执行时需要很长时间
问题描述
我有一个奇怪的问题,我会尽力解释它,所以我们开始吧,
我正在使用 Python 编写一个 Discord 机器人。我做了一个只能在成员位于 discord.VoiceChannel 时执行的命令。它将消息发送到通道并编辑语音通道:
事件:
@bot.command(name="lft", pass_context=True)
async def lft_command(ctx):
await lft.lft(ctx, bot)
第一种方法(lft):
async def lft(ctx, bot):
if ctx.channel == bot.get_channel(806109172336689162):
dcUser = ctx.author
if dcUser.voice is not None:
if dcUser.voice.channel.category == bot.get_channel(809430391177084969).category:
await methods.set_lft(dcUser, bot)
else:
await bot.get_channel(806112383693094942).send(
ctx.author.mention + ", you have to be in a temporary channel to use this command.",
delete_after=30)
else:
await bot.get_channel(806112383693094942).send(
ctx.author.mention + ", you have to be in a temporary channel to use this command.", delete_after=30)
else:
await bot.get_channel(806112383693094942).send(
ctx.author.mention + ", you can't use this command here, got to " + bot.get_channel(
806109172336689162).mention, delete_after=30)
第二种方法(set_lft):
async def set_lft(executor, bot):
channel = executor.voice.channel
lft_channel = bot.get_channel(806109172336689162)
user_role = await get_rank(executor)
print("b")
await channel.set_permissions(get(executor.guild.roles, id=806081402407092295), connect=False)
print("c")
await channel.edit(name="Looking for mates", user_limit=5)
msg = await lft_channel.send(
content=executor.mention + " is looking for teammates for ranked, he is " + user_role.name + ". Join a channel and react to the message to join the channel. There are currently " + str(
len(executor.voice.channel.members)) + "/5 player in the channel.",
delete_after=900)
await msg.add_reaction('✅')
lft_data[executor.id] = ["placeholder", msg, channel]
lft_data[msg.id] = [executor, "placeholder", channel]
lft_data[channel.id] = [executor, msg, "placeholder"]
如果执行该命令的成员离开语音通道,语音通道将再次变回正常语音通道:
async def set_casual(channel):
msg = get_msg(channel)
executor = get_executor(channel)
await msg.delete()
if len(channel.members) != 0:
await channel.set_permissions(get(executor.guild.roles, id=806081402407092295), connect=True)
await channel.edit(name=channel.members[0].nick + "'s channel", limit=None)
[lft_data.pop(x, None) for x in [msg.id, channel.id, executor.id]]
另一个用户可以执行该命令,但如果该命令被执行,它不会被调用或需要大约 5 分钟才能执行。是否有一个循环运行时间过长,或者根本没有停止?
先感谢您
PS: Github 如果您需要更多代码:
解决方案
你得到速率限制。问题,产生错误的唯一方法是购买为您的命令添加全局冷却时间,或者每当有人使用该命令时创建一个新频道。
@discord.ext.commands.cooldown(rate=2, per=600) #bucketType defaults to global
@bot.command()
async def lft_command(ctx):
# other stuff here
@lft_command.error()
async def lft_error(ctx, error): #handling cooldown errors
if isinstance(error, commands.CommandOnCooldown):
await ctx.send("The command is on cooldown")
参考:
推荐阅读
- javascript - 将参数从 Javascript 传递到 Controller 中的 API
- javascript - TypeError: undefined is not an object (evalating 'this.state.Gas') in React Native
- django - 如何将 Django Rest Framework API 视图转换为异步视图?
- python - For循环在多个熊猫数据框中添加列
- spring-boot - OKTA登录成功后spring boot应用进入死循环
- c++ - 将句子拆分为单词时缺少字符串的最后一个单词
- firebase - 我们可以在 Flutter Web 中使用云存储吗?
- scala - 如何使用火花将列添加到json字段中
- angular - EventSource 动态绑定变量
- c - malloc 和 memcpy 的漏洞