我如何制作一个机器人状态来显示机器人正在玩的所有成员?
How do i make a bot status that shows all the members the bot is playing with?
我目前正尝试在 discord python 重写中为我的 discord 机器人创建一个状态循环命令。我已经发出了状态命令,但是获取状态以显示机器人所在的所有服务器中的所有用户的数量不起作用。当我尝试打印代码时,它工作得很好。当我尝试 运行 它作为状态时,我一直显示与 0 个成员一起玩。我不知道如何解决这个问题。
代码:
from discord.ext import commands, tasks
import asyncio
import os
import random
from itertools import cycle
client = commands.Bot(command_prefix = '?')
status = cycle(["?help", "Welcoming People", f"Playing with {len(set(client.users))} users"])
@client.remove_command('help')
@client.event
async def on_ready():
change_status.start()
print ('Bot online')
print (f"Playing with {len(client.users)} users")
@tasks.loop(seconds=10)
async def change_status():
await client.change_presence(status=discord.Status.idle, activity=discord.Game(next(status)))```
它没有按预期工作的原因如下:
status = cycle(["?help", "Welcoming People", f"Playing with {len(set(client.users))} users"])
我们用一个没有更新的数组启动一个循环。为了解释让我们输入它(我们感兴趣的字符串):
f"Playing with {len(set(client.users))} users"
在创建字符串的那一刻,我们输入了那一刻的值。这变成:
"Playing with 0 users"
这将在数组循环保持时使用。因为循环只知道这个字符串,所以它只会使用这个字符串。
如果你想更新字符串,你必须手动更改字符串并在每次我们需要时更新它。
我建议您创建自己的循环变体。哪个会更新字符串。一种方法:
last_string = 0
@tasks.loop(seconds=10)
async def change_status():
# Update the member count string
list[str_member_index] = f"Playing with {len(set(client.users))} users"
status_str = list[last_string]
await client.change_presence(status=discord.Status.idle, activity=discord.Game(status_str))
last_string += 1
if last_string == len(list):
last_string = 0
此代码不完整,因为您仍然需要分配 str_member_index
等。但它会遍历你的状态。并更新会员计数状态。
我目前正尝试在 discord python 重写中为我的 discord 机器人创建一个状态循环命令。我已经发出了状态命令,但是获取状态以显示机器人所在的所有服务器中的所有用户的数量不起作用。当我尝试打印代码时,它工作得很好。当我尝试 运行 它作为状态时,我一直显示与 0 个成员一起玩。我不知道如何解决这个问题。
代码:
from discord.ext import commands, tasks
import asyncio
import os
import random
from itertools import cycle
client = commands.Bot(command_prefix = '?')
status = cycle(["?help", "Welcoming People", f"Playing with {len(set(client.users))} users"])
@client.remove_command('help')
@client.event
async def on_ready():
change_status.start()
print ('Bot online')
print (f"Playing with {len(client.users)} users")
@tasks.loop(seconds=10)
async def change_status():
await client.change_presence(status=discord.Status.idle, activity=discord.Game(next(status)))```
它没有按预期工作的原因如下:
status = cycle(["?help", "Welcoming People", f"Playing with {len(set(client.users))} users"])
我们用一个没有更新的数组启动一个循环。为了解释让我们输入它(我们感兴趣的字符串):
f"Playing with {len(set(client.users))} users"
在创建字符串的那一刻,我们输入了那一刻的值。这变成:
"Playing with 0 users"
这将在数组循环保持时使用。因为循环只知道这个字符串,所以它只会使用这个字符串。 如果你想更新字符串,你必须手动更改字符串并在每次我们需要时更新它。
我建议您创建自己的循环变体。哪个会更新字符串。一种方法:
last_string = 0
@tasks.loop(seconds=10)
async def change_status():
# Update the member count string
list[str_member_index] = f"Playing with {len(set(client.users))} users"
status_str = list[last_string]
await client.change_presence(status=discord.Status.idle, activity=discord.Game(status_str))
last_string += 1
if last_string == len(list):
last_string = 0
此代码不完整,因为您仍然需要分配 str_member_index
等。但它会遍历你的状态。并更新会员计数状态。