[Python] My discord bot keeps getting rate limited

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    My bot is supposed to fetch all the messages sent in specific channels within a specific time period and copy them to another channel using webhooks. However, it constantly gets rate limited while it is fetching the messages.

    target_channel_id = 1314447264759939123
    fetch_channel_ids = [1302779220191219772, 1302784279725735986]
    fetch_category_ids = [1278825246824337428]

    start_channel_id = 1302784279725735986
    start_message_id = 1302786994027626602
    to_date = datetime(2024, 11, 6, tzinfo=pytz.UTC)

    start_channel = bot.get_channel(start_channel_id)
    start_message = await start_channel.fetch_message(start_message_id)

    for channel in ctx.guild.channels:
    if isinstance(channel, discord.TextChannel) and channel.category and ((channel.id in fetch_channel_ids) or (channel.category.id in fetch_category_ids)):
    print(f"Fetching messages from {channel.name}")
    async for message in channel.history(limit=None, before=to_date, after=start_message, oldest_first=True):
    await asyncio.sleep(0.3)

    if message.webhook_id:
    display_name = message.author.name
    avatar_url = message.author.avatar.url if message.author.avatar else message.author.default_avatar.url
    user_id = message.webhook_id
    else:
    user_id = message.author.id
    display_name = message.author.display_name
    avatar_url = message.author.avatar.url if message.author.avatar else message.author.default_avatar.url

    if not message.webhook_id:
    try:
    user = await bot.fetch_user(user_id)
    webauthorname = user.display_name
    webauthoravatar = user.avatar.url if user.avatar else user.default_avatar.url
    except discord.errors.NotFound:
    print(f"Could not fetch user {user_id}")
    webauthorname = display_name
    webauthoravatar = avatar_url
    else:
    webauthorname = display_name
    webauthoravatar = avatar_url

    if user_id not in proxy_profiles:
    proxy_profiles[user_id] = [{
    "name": display_name,
    "avatar": avatar_url
    }]

    messagecontent = message.content

    attachments = [attachment.url for attachment in message.attachments]

    messages_to_mimic.append({
    "content": messagecontent,
    "attachments": attachments,
    "author_name": display_name,
    "author_avatar": avatar_url,
    "channel_name": channel.name,
    "original_channel_id": channel.id,
    "timestamp": message.created_at,
    "user_id": user_id,
    "message_id": message.id
    })


    I was expecting the bot to fetch the messages as it intended, but instead it stops in the middle and spams "We have been rate limited!" in the terminal. I've tried adding a delay between getting each message, using parameters like (limit, before, after and oldest_first) to make the message pool smaller, error handling rate limits, and limiting the message fetching to only the channels needed. None of these efforts worked.

    Continue reading...

Compartilhe esta Página