获取完整服务器列表

时间:2018-05-31 03:15:16

标签: python python-3.x discord discord.py

那么如何获取我的机器人连接或安装的所有服务器列表。我使用下面的代码,但它现在不知道为什么。

servers = list(bot.servers)
print("Connected on " + str(len(bot.servers)) + "servers:")
for x in range(len(servers)):
    print('  ' + servers[x-1].name)

我的bot.py完整代码

token = "This is my Token" # This is what the bot uses to log into Discord.
prefix = "?" # This will be used at the start of commands.

import discord
from discord.ext import commands
from discord.ext.commands import Bot

bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print(discord.__version__)
    print('------')

@bot.command(pass_context=True)
async def ping(ctx):
    msg = 'Pong {0.author.mention}'.format(ctx.message)
    await bot.say(msg)

@bot.command(pass_context=True)
async def hello(ctx):
    msg = 'hello... {0.author.mention}'.format(ctx.message)
    await bot.say(msg)

bot.run(token)

2 个答案:

答案 0 :(得分:2)

不确定您使用代码查找服务器的位置,但在您使用bot.servers的情况下确实会返回机器人可以看到的所有服务器的可迭代内容。

您可以修改on_ready事件以打印出所有服务器名称。

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print(discord.__version__)
    print('------')

    print('Servers connected to:')
    for server in bot.servers:
        print(server.name)

答案 1 :(得分:2)

我对代码有一点乐趣。这是当前工作的版本。

@bot.event
async def on_ready():
print(f'Currently at {len(bot.guilds)} servers!')
print('Servers connected to:')
print('')
for server in bot.guilds:
    print(server.name)

这对您来说应该很好。

相关问题