简体   繁体   中英

Discord.py get names of all guilds bot is in into an 1 embed/message

I want to make totalguilds command into 1 msg instead of many of them. How do I do that?

@bot.command()
async def showguilds(ctx):
    
    for guild in bot.guilds:
         await ctx.send(guild.name)

This can be done by going through the bot.guilds and adding it to a variable.

@bot.command()
async def showguilds(ctx):
    message = ""
    for guild in bot.guilds:
         message += f"{guild.name}\n"
    await ctx.send(message)

Alternatively you could use a list:

@bot.command()
async def showguilds(ctx):
    messages = []
    for guild in bot.guilds:
         messages.append(f"{guild.name}")
    await ctx.send("\n".join(messages))

If you want all the guilds in 1 message in 1 line then Łukasz Kwieciński's solution:

@bot.command()
async def showguilds(ctx):
    await ctx.send("\n".join(bot.guilds))

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