You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

仅每分钟1次请求仍遭遇Discord Rate Limiting问题求助

问题分析与解决方案

原因

你遇到的429限流是因为Discord API对**频道编辑(PATCH /channels/{id})**设置了全局应用层限流规则:每600秒(10分钟)最多允许5次请求。你的代码虽然每60秒检查一次人数,但如果短时间内玩家人数频繁变化(比如10分钟内超过5次),就会触发这个限制。

解决方案

主动添加冷却时间控制,确保两次频道名称更新的间隔不低于120秒(10分钟5次的平均间隔),即使人数变化,也要等冷却时间到后再执行更新。修改后的代码如下:

import discord
from discord.ext import tasks
import datetime

client = discord.Client(intents=discord.Intents.default())

LastUpdate = -1
last_edit_time = datetime.datetime.min  # 初始化上次编辑时间

@client.event
async def on_ready():
    channel = client.get_channel(int(CHANNEL_ID))
    update_info.start(channel)

@tasks.loop(seconds=60)
async def update_info(channel):
    global LastUpdate, last_edit_time
    players_online = get_info(HOST, PORT)['players']['online']
    current_time = datetime.datetime.now()
    
    # 同时满足两个条件才更新:人数有变化,且距离上次编辑至少120秒
    if players_online != LastUpdate and (current_time - last_edit_time).total_seconds() >= 120:
        print("updating")
        await channel.edit(name=f"Users Online:  {players_online}")
        LastUpdate = players_online
        last_edit_time = current_time
    elif players_online != LastUpdate:
        print("人数变化但未到冷却时间,暂不更新")

client.run(BOT_TOKEN)

额外说明

  • 这个方案保留了60秒一次的人数检查频率,同时避免触发限流。如果人数持续变化,冷却时间到后会自动更新到最新数值。
  • discord.py本身会对429错误进行自动重试,但主动控制请求频率是更稳妥的做法,能避免不必要的延迟和API资源浪费。

内容的提问来源于stack exchange,提问作者SebastiaanTheCoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 09:02:59