discord.py 每 60 秒更改一次机器人状态
discord.py change bot status every 60 sec
我正在尝试编写每 60 秒更改一次机器人状态的代码,但我无法让它工作。我搜索了如何制作它,但似乎没有人尝试过这样做。或者至少我找不到类似的东西。
我试过这段代码,但没有用,机器人永远不会改变到第二个状态=/
# Bot joins server/auto msg
@bot.event
async def on_ready():
log_channel = bot.get_channel(log_id)
join_embed = discord.Embed(title='Rosy is back online', color=0xd3d3d3)
join_embed.set_author(name='')
await log_channel.send(embed=join_embed)
while True:
await bot.change_presence(activity=discord.Game(name='?help if you are wondering anything')) # First status
time.sleep(60) # Wait 60 seconds
await bot.change_presence(activity=discord.Game(name='TEST!')) # Show second status then repeat
我还想要第二个状态显示discord服务器中的总成员...
感谢您的帮助!
编辑:
@tasks.loop(seconds=10.0)
async def my_background_task():
"""Will loop every 60 seconds and change the bots presence"""
await bot.change_presence(activity=discord.Game(name='?help if you are wondering anything'))
await bot.change_presence(activity=discord.Game(name='TEST!'))
@bot.event
async def on_ready():
# Waiting until the bot is ready
await bot.wait_until_ready()
# Starting the loop
my_background_task.start()
您可以使用 tasks.loop
,方法如下:
from discord.ext import tasks
@tasks.loop(seconds=60.0)
async def my_background_task():
"""Will loop every 60 seconds and change the bots presence"""
await bot.change_presence(...)
@bot.event
async def on_ready():
# Waiting until the bot is ready
await bot.wait_until_ready()
# Starting the loop
my_background_task.start()
编辑:
正在回复您的评论:
total_members = []
for guild in bot.guilds:
total_members.expand(guild.members)
# Number of total members the bot is `serving` in all guilds, without duplicates
total_members_count = len(set(total_members))
编辑 2:
await bot.change_presence(activity=discord.Game(name=f'Member count: {total_member_count}'))
我找到了这个解决方案,它完美无缺,它会随机更改状态。
自定义机器人状态
@tasks.loop(seconds=40) # How often the bot should change status, mine is set on every 40 seconds
async def changepresence():
global x
game = iter(
[
"Status 1",
"Status 2",
"Status 3",
"Status 4",
"Status 5?",
"Status 6",
]
) # Every line above ^^ is one new status the bot can have
for x in range(random.randint(1, 6)): # Here you write the total of different status you have for the bot, I have 6 and that's why I have number 6 there. This makes it a 16.666% chance to change status every 40 second
x = next(game)
await bot.change_presence(activity=discord.Game(name=x))
@tasks.loop(seconds=40) # 机器人应该多久改变一次状态,我的是每 40 秒设置一次
async def changepresence():
全局 x
game = iter(
[
"Status 1",
"Status 2",
"Status 3",
"Status 4",
"Status 5?",
"Status 6",
]
) # Every line above ^^ is one new status the bot can have
for x in range(random.randint(1, 6)): # Here you write the total of different status you have for the bot, I have 6 and that's why I have number 6 there. This makes it a 16.666% chance to change status every 40 second
x = next(game)
await bot.change_presence(activity=discord.Game(name=x))
我正在尝试编写每 60 秒更改一次机器人状态的代码,但我无法让它工作。我搜索了如何制作它,但似乎没有人尝试过这样做。或者至少我找不到类似的东西。
我试过这段代码,但没有用,机器人永远不会改变到第二个状态=/
# Bot joins server/auto msg
@bot.event
async def on_ready():
log_channel = bot.get_channel(log_id)
join_embed = discord.Embed(title='Rosy is back online', color=0xd3d3d3)
join_embed.set_author(name='')
await log_channel.send(embed=join_embed)
while True:
await bot.change_presence(activity=discord.Game(name='?help if you are wondering anything')) # First status
time.sleep(60) # Wait 60 seconds
await bot.change_presence(activity=discord.Game(name='TEST!')) # Show second status then repeat
我还想要第二个状态显示discord服务器中的总成员...
感谢您的帮助!
编辑:
@tasks.loop(seconds=10.0)
async def my_background_task():
"""Will loop every 60 seconds and change the bots presence"""
await bot.change_presence(activity=discord.Game(name='?help if you are wondering anything'))
await bot.change_presence(activity=discord.Game(name='TEST!'))
@bot.event
async def on_ready():
# Waiting until the bot is ready
await bot.wait_until_ready()
# Starting the loop
my_background_task.start()
您可以使用 tasks.loop
,方法如下:
from discord.ext import tasks
@tasks.loop(seconds=60.0)
async def my_background_task():
"""Will loop every 60 seconds and change the bots presence"""
await bot.change_presence(...)
@bot.event
async def on_ready():
# Waiting until the bot is ready
await bot.wait_until_ready()
# Starting the loop
my_background_task.start()
编辑:
正在回复您的评论:
total_members = []
for guild in bot.guilds:
total_members.expand(guild.members)
# Number of total members the bot is `serving` in all guilds, without duplicates
total_members_count = len(set(total_members))
编辑 2:
await bot.change_presence(activity=discord.Game(name=f'Member count: {total_member_count}'))
我找到了这个解决方案,它完美无缺,它会随机更改状态。
自定义机器人状态
@tasks.loop(seconds=40) # How often the bot should change status, mine is set on every 40 seconds
async def changepresence():
global x
game = iter(
[
"Status 1",
"Status 2",
"Status 3",
"Status 4",
"Status 5?",
"Status 6",
]
) # Every line above ^^ is one new status the bot can have
for x in range(random.randint(1, 6)): # Here you write the total of different status you have for the bot, I have 6 and that's why I have number 6 there. This makes it a 16.666% chance to change status every 40 second
x = next(game)
await bot.change_presence(activity=discord.Game(name=x))
@tasks.loop(seconds=40) # 机器人应该多久改变一次状态,我的是每 40 秒设置一次 async def changepresence(): 全局 x
game = iter(
[
"Status 1",
"Status 2",
"Status 3",
"Status 4",
"Status 5?",
"Status 6",
]
) # Every line above ^^ is one new status the bot can have
for x in range(random.randint(1, 6)): # Here you write the total of different status you have for the bot, I have 6 and that's why I have number 6 there. This makes it a 16.666% chance to change status every 40 second
x = next(game)
await bot.change_presence(activity=discord.Game(name=x))