未超Discord速率限制却遭限流?Discord Bot代码排查求助
问题:Discord Bot修改频道名称触发限流问题
我开发了一款Discord Bot,用于在Minecraft服务器有玩家加入或退出时发送频道重命名请求。如标题所示,我远未触发Discord每秒50次请求的实际速率限制(53秒仅发送3次请求),即便设置了20秒延迟,在第3次请求后仍被限流。
相关代码
@tasks.loop(seconds=1) async def update_channel_name(): global status_check try: status = server.status() channel = bot.get_channel(VOICE_CHANNEL_ID) if channel is None: print("Voice channel not found!") return if status_check != status.players.online: await channel.edit(name=f'Online players: {status.players.online}') print(f"Updated voice channel to: {status.players.online}") status_check = status.players.online else: print("-") except discord.Forbidden: print("Bot lacks permission to edit the voice channel.") except discord.HTTPException as e: print(f"An error occurred : {e}") @bot.event async def on_ready(): print(f'Bot is ready. Logged in as {bot.user}') await bot.get_channel(VOICE_CHANNEL_ID).edit(name=f'Online players: {server.status().players.online}') print(f"At start updated channel to {server.status().players.online}") update_channel_name.start()
错误信息
: discord.http We are being rate limited. PATCH *{Channel_ID_link}* responded with 429. Retrying in 580.74 seconds. Every time i run my code, channel name gets changed about 2 or 3 times and then I get rate limited for 500sec.
请问我的代码是否存在问题?(以上为代码片段,其余多为变量定义)
问题分析与解决办法
核心原因:误解了Discord的速率限制规则
你以为的通用请求限制(每秒50次)不适用于频道名称修改这类操作。Discord对单个频道的名称修改设置了专属低频限制:每10分钟最多允许2次修改。你的启动初始化修改+后续2次循环修改,刚好触发了这个限制,所以会被限流约10分钟(580秒左右)。
代码优化方案
1. 增加修改冷却机制
记录上次修改频道名称的时间,强制两次修改间隔大于10分钟(建议留1分钟缓冲,设为11分钟),避免触发限制。
2. 降低状态检查频率
没必要每秒都查询Minecraft服务器状态,改成10秒一次完全能满足实时性需求,还能减少不必要的服务器请求。
3. 统一状态初始化逻辑
在on_ready中统一获取服务器状态并记录初始化时间,避免重复请求。
修改后的代码示例
import time from discord.ext import tasks # 全局变量 status_check = None last_edit_time = 0 EDIT_COOLDOWN = 660 # 11分钟,单位:秒 VOICE_CHANNEL_ID = 你的频道ID # 替换为实际ID @tasks.loop(seconds=10) # 改为10秒检查一次 async def update_channel_name(): global status_check, last_edit_time try: status = server.status() current_time = time.time() channel = bot.get_channel(VOICE_CHANNEL_ID) if channel is None: print("Voice channel not found!") return # 满足两个条件才修改:玩家数变化 + 超过冷却时间 if status_check != status.players.online and (current_time - last_edit_time) > EDIT_COOLDOWN: await channel.edit(name=f'Online players: {status.players.online}') print(f"Updated voice channel to: {status.players.online}") status_check = status.players.online last_edit_time = current_time else: print("-") except discord.Forbidden: print("Bot lacks permission to edit the voice channel.") except discord.HTTPException as e: print(f"An error occurred : {e}") @bot.event async def on_ready(): global status_check, last_edit_time print(f'Bot is ready. Logged in as {bot.user}') # 统一获取初始状态 status = server.status() channel = bot.get_channel(VOICE_CHANNEL_ID) await channel.edit(name=f'Online players: {status.players.online}') print(f"At start updated channel to {status.players.online}") # 初始化全局变量 status_check = status.players.online last_edit_time = time.time() update_channel_name.start()
内容的提问来源于stack exchange,提问作者akisha 009
相关产品推荐
相关产品推荐

