简体   繁体   中英

Discord.py - changing status

I want my bot to change status every 5 seconds. When I run the code, it does not display any error. I do not know what is wrong because I use same status changing code for an another bot. I have this code:

import discord
from discord.ext import commands, tasks
from itertools import cycle

client = commands.Bot(command_prefix="?")
status = cycle(["status1", "status2"])

@tasks.loop(seconds=5)
async def changeStatus():
    await client.change_presence(status=discord.Status.do_not_disturb, activity=discord.Activity(type=discord.ActivityType.playing, name=next(status)))

@client.event
async def on_ready():
    notificationChannel = client.get_channel(channel_id)
    await notificationChannel.send("Bot booted up!")

client.run("token")

Thanks in advance.

You forgot to start the task. You should not be doing it every 5 seconds as it may rate limit you. Personally, I change them every 5 minutes.

import discord
from discord.ext import commands, tasks
from itertools import cycle

client = commands.Bot(command_prefix="?")
status = cycle(["status1", "status2"])

@tasks.loop(seconds=5)
async def changeStatus():
    await client.change_presence(status=discord.Status.do_not_disturb, activity=discord.Activity(type=discord.ActivityType.playing, name=next(status)))

@client.event
async def on_ready():
    notificationChannel = client.get_channel(channel_id)
    await notificationChannel.send("Bot booted up!")
    changeStatus.start()

client.run("token")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM