Reputation: 186
I want to make a command that will show most useful server stats. I tried to make something like this:
@bot.command()
async def serverstats(ctx):
embed=discord.Embed(title=f"Statystyki serwera {ctx.guild.name}")
embed.add_field(name="Users:", value=ctx.guild.members, inline=False)
embed.add_field(name="Channels:", value=ctx.guild.channels, inline=False)
embed.add_field(name="Messages sent:", value=???, inline=False)
await ctx.send(embed=embed)
But i don't know how can i make messages counter, and other counters return not correct or strange data
Upvotes: 2
Views: 5585
Reputation: 23
For users, you can try using ctx.message.guild.member_count
. I am not sure about channels and messages.
Upvotes: 0
Reputation: 191
For the users and channel counter, you would use the len()
function to count how many there are. If you don't have the members intent enabled, you can also use ctx.guild.member_count
.
# Using len()...
embed.add_field(name="Users:", value=ctx.guild.member_count, inline=False)
embed.add_field(name="Channels:", value=len(ctx.guild.channels), inline=False)
For messages however, you will need to use a database or global variable and manually increase the entry each time a message is sent. A global variable is much simpler to use than a database, however it will be flushed whenever the script ends or is restarted.
On a side note, if you want to use a database anyway, I recommend you check out dataset.
# Using a global variable (dictionary)
# Somewhere in your code...
messagecounts = {}
...
@bot.event
async def on_message(message):
if message.guild.id not in messagecounts.keys(): # Make sure there's already an entry... If not, add one!
messagecounts[message.guild.id] = 0
messagecounts[message.guild.id] += 1 # Sets the new count to 1 greater than the original.
Then, to retrieve this amount, you would add the dictionary entry. Final Code:
# Somewhere above the command...
messagecounts = {}
@bot.command()
async def serverstats(ctx):
embed=discord.Embed(title=f"Statystyki serwera {ctx.guild.name}")
embed.add_field(name="Users:", value=ctx.guild.member_count, inline=False)
embed.add_field(name="Channels:", value=len(ctx.guild.channels), inline=False)
embed.add_field(name="Messages sent:", value=messagecounts[ctx.guild.id], inline=False)
await ctx.send(embed=embed)
@bot.event
async def on_message(message):
if message.guild.id not in messagecounts.keys():
messagecounts[message.guild.id] = 0
messagecounts[message.guild.id] += 1
Upvotes: 2