Reputation: 27
i I want to write a command that outputs information about the server. I ran into the error
My code:
@client.command( pass_context = True )
async def server( ctx ): #server
emb = discord.Embed( title = '<:rightgrey:927573354863542292> Kuromi | Информация', description = f'<:ccrown:928503495361982605> **Владелец:** {ctx.guild.owner.mention} | `{ctx.guild.owner.name}`', colour = 0x2f3136, url = '' )
emb.add_field(name=f"Пользователей: {ctx.guild.member_count}", value=f"Участников: {ctx.guild.members}\nБотов:")
await ctx.send( embed = emb )
#{ctx.guild.member_count}
client.run('') #твой токен бота
Именно часть с {ctx.guild.members}
отказывается работать, выводя такую ошибку:
Traceback (most recent call last):
File "C:\Users\vlad\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\vlad\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\vlad\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In embed: Embed size exceeds maximum size of 6000
and I don't really understand how to display the display of the number of bots!
Upvotes: 2
Views: 87
Reputation: 149
This should work:
# I Transtalted from english to russian using Google Translator, It might not be perfect
# Learn more about textwrap from the two links below
# 1. https: // docs.python.org/3/library/textwrap.html
# 2. https://www.geeksforgeeks.org/textwrap-text-wrapping-filling-python/
import textwrap
# Code for a basic discord bot
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="--")
# Command
@client.command(pass_context=True)
@commands.guild_only() # to make sure that this command will only work on a discord server
async def server(ctx):
# Send this embed first with the basic facts
emb = discord.Embed(title='<:rightgrey:927573354863542292> Kuromi | Информация',
description=f'<:ccrown:928503495361982605> **Владелец:** {ctx.guild.owner.mention} | `{ctx.guild.owner.name}` \n Пользователи: {ctx.guild.member_count}\nВесь список участников будет отправлен вам в директ',
color=0x2f3136) # its `color`, not `colour`
await ctx.send(embed=emb)
# Creating a string with the member names of the discord server seperated by a newline("\n")
all_members = "\n".join(ctx.guild.members)
# Creating chunks of texts with the maximum amount of characters of 4096
# and using each chunk as the description of the embed
# This will create a huge amount of embeds if the amount of members are high
# This could be used to spam the server channel
for chunk in textwrap.wrap(all_members, 4096, replace_whitespace=False):
embed = discord.Embed(
title="<:rightgrey:927573354863542292> Kuromi | Список членов",
description=chunk,
color=0x2f3136
)
# Send the member list embed messages to the user who ran the command
# To prevent spam in the channel
# Change `ctx.author.send` to `ctx.send` to send to the channel instead of DM
await ctx.author.send(embed=embed)
# Run the bot
client.run("TOKEN")
Upvotes: 0
Reputation: 2917
ctx.guild.members
returns full list of all members. Therefore a size of your embed is very big and Discord can't display it.
If you want to display member count use ctx.guild.member_count
instead of ctx.guild.members
.
To display number of bots use something like this:
sum(member.bot for member in ctx.guild.members)
Upvotes: 1