我想检查一下我的discord机器人所在的服务器,请注意,我不知道如何使用discord.client(该机器人是用discord.py编写的)
到目前为止我尝试过的代码是:
@bot.command(pass_context=True)
async def servers(ctx):
client = discord.Client()
servers = list(client.servers)
await bot.say("Connected on " + str(len(client.servers)) + " servers:")
for x in range(len(servers)):
await bot.say(" "+servers[x-1].name)
(请注意,缩进正确,但是堆栈溢出未正确显示)
答案 0 :(得分:0)
您可以这样做
@bot.command()
async def servers():
servers = list(bot.servers)
await bot.say(f"Connected on {str(len(servers))} servers:")
await bot.say('\n'.join(server.name for server in servers))
答案 1 :(得分:-1)
这对我有用,来自https://www.devdungeon.com/content/make-discord-bot-python-part-2:
async def list_servers():
await client.wait_until_ready()
while not client.is_closed:
print("Current servers:")
for server in client.servers:
print(server.name)
await asyncio.sleep(600)
client.loop.create_task(list_servers())