by Lazy Sloth
Discord Server Stats Bot
import os
import discord
from discord.ext import commands
from datetime import datetime, timedelta
from collections import Counter
import pytz
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.command()
async def serverstats(ctx):
# Total number of members in the server.
total_members = ctx.guild.member_count
progress_message = await ctx.reply('Gathering stats...')
await progress_message.edit(content='Gathered total members...')
# Number of online members.
online_members = len([member for member in ctx.guild.members if member.status == discord.Status.online])
await progress_message.edit(content='Gathered online members...')
# Number of offline members.
offline_members = len([member for member in ctx.guild.members if member.status == discord.Status.offline])
Discord Server Stats Bot
Created: | Last Updated:
Introduction to the Discord Server Stats Bot Template
Welcome to the step-by-step guide on how to set up and use the Discord Server Stats Bot. This bot is designed to provide you with comprehensive statistics about your Discord server, including member counts, channel information, and activity metrics. Whether you're a server administrator looking to monitor growth or a curious member interested in server dynamics, this bot will serve your needs without the hassle of manual data collection.
To begin using this template, simply click on "Start with this Template" on the Lazy platform. This will set up the template in your Lazy Builder interface, and you can proceed with the following steps to get your bot up and running.
Setting Environment Secrets
Before you can use the bot, you need to provide a Discord bot token. This token allows your bot to interact with the Discord API and is essential for the bot's functionality. Here's how to set it up:
- Go to the Discord Developer Portal and create a new application.
- Within your application, navigate to the "Bot" tab and create a new bot.
- Copy the bot token provided.
- Back in the Lazy Builder interface, locate the Environment Secrets tab.
- Create a new secret with the name 'DISCORD_BOT_TOKEN' and paste your copied bot token as the value.
With the environment secret set, your bot is now ready to be deployed.
External Integrations
For the bot to function correctly, you need to invite it to your Discord server and ensure it has the necessary permissions. Follow these steps:
- In the Discord Developer Portal, under your bot's application, navigate to the "OAuth2" tab.
- Under "SCOPES," select "bot" and then choose the permissions your bot will need, such as reading message history, sending messages, and reacting to messages.
- Use the generated URL to invite your bot to the desired Discord server.
Ensure that your bot has been added to the server and has the appropriate roles and permissions to access the channels and information it needs to generate statistics.
Using the Test Button
Once you have set up the environment secret and invited the bot to your server, you can deploy the bot using the Test button on the Lazy platform. This will launch the Lazy CLI, and the bot will start running. No further user input is required at this stage unless you want to interact with the bot commands within your Discord server.
To use the bot, simply type the command '!serverstats' in any text channel where the bot has permission to read and send messages. The bot will begin compiling server statistics and will provide you with a detailed report. If you want to get channel-specific statistics, use the command '!channelstats ' by replacing with the actual name of the channel you're interested in.
Interacting with the Bot
After deploying the bot, you can interact with it directly on your Discord server. Here are some commands you can use:
To get general server statistics:
!serverstats
To get statistics for a specific channel:
!channelstats channel-name
Replace 'channel-name' with the name of the channel you want to analyze. The bot will respond with the requested statistics.
By following these steps, you should have a fully functional Discord Server Stats Bot running on your server. Enjoy the insights and convenience this bot brings to managing and understanding your Discord community!
Technologies
![Discord](https://media.pocketgamer.biz/2021/5/110514/discord-new-logo-2021-r225x225.jpg)