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

如何实现Discord机器人状态循环?要求每5-10秒切换一次状态

Fixing Discord Bot Presence Cycling (Every 5-10 Seconds)

Got it, let's get your bot cycling through presence statuses automatically! The problem with your current code is that the presenced() function only runs once—we need to turn it into a persistent loop that waits a random 5-10 seconds between each status switch. Here's how to do it right:

Step-by-Step Fix & Full Code

First, make sure you've imported asyncio and random (you probably already have discord and discord.ext.commands). Then adjust your presence task to run in an infinite loop with proper delays:

import discord
from discord.ext import commands
import random
import asyncio

client = commands.Bot(command_prefix=commands.when_mentioned_or(","), intents=discord.Intents.default())

async def cycle_presence():
    # Wait until the bot is fully ready before starting the loop
    await client.wait_until_ready()
    
    presences = [
        "Prefix: ,", 
        "Over people!",
        "Add more statuses here!",
        "Another fun status"
    ]
    
    # Keep running until the bot shuts down
    while not client.is_closed():
        # Pick a random presence from the list
        selected_presence = random.choice(presences)
        # Set the activity (using "watching" type as you had before)
        activity = discord.Activity(name=selected_presence, type=discord.ActivityType.watching)
        await client.change_presence(activity=activity)
        
        # Wait 5-10 seconds before switching again
        await asyncio.sleep(random.randint(5, 10))

# Start the presence cycling task
client.loop.create_task(cycle_presence())

# Don't forget your on_ready event to confirm it's running!
@client.event
async def on_ready():
    print(f'Logged in as {client.user} (ID: {client.user.id})')
    print('------')

# Run your bot with your token
client.run('YOUR_BOT_TOKEN_HERE')

Key Notes:

  • wait_until_ready(): This ensures we don't try to set the presence before the bot has finished connecting to Discord—prevents weird errors.
  • Infinite Loop with is_closed(): The loop runs as long as the bot is online. When you shut down the bot, it exits gracefully.
  • asyncio.sleep(): Never use time.sleep() in async Discord code—it blocks the entire bot! asyncio.sleep() is non-blocking, so your bot can still respond to commands while waiting.
  • Random Delay: random.randint(5,10) gives you that 5-10 second random interval you wanted.
  • Extensible Presence List: Just add more strings to the presences list to expand your status options.

Troubleshooting Tip:

If you run into issues, make sure your bot has the Presence Intent enabled in the Discord Developer Portal under your bot's "Privileged Gateway Intents" section—this is required to change presence statuses.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 16:22:40