Discord Activity Rank & Monitor Bot That Kicks Inactive Members
import discord
from discord.ext import commands
import os
from datetime import datetime, timedelta
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Retrieve the Discord bot token from the environment variable
BOT_TOKEN = os.environ['DISCORD_BOT_TOKEN']
# Define intents
intents = discord.Intents.default()
intents.messages = True # Subscribe to the messages intent to track last message activity.
intents.members = True # Subscribe to the privileged members intent.
bot = commands.Bot(command_prefix='!', intents=intents)
# In-memory storage for inactivity criteria and channel message history
inactivity_criteria = {}
channel_message_history = {}
Created: | Last Updated:
Here's a step-by-step guide for using the Discord Activity Rank & Monitor Bot template that kicks inactive members:
Introduction
This template provides a Discord bot that monitors member activity within a server. It allows administrators to set inactivity criteria and kick inactive members based on those criteria.
Getting Started
- Click "Start with this Template" to begin using this template in the Lazy Builder interface.
Initial Setup
- Set up the required environment secret:
- In the Lazy Builder, navigate to the Environment Secrets tab.
- Add a new secret with the key
DISCORD_BOT_TOKEN
. - To obtain the Discord Bot Token:
- Go to the Discord Developer Portal (https://discord.com/developers/applications).
- Create a new application or select an existing one.
- Navigate to the "Bot" section and click "Add Bot" if you haven't already.
- Under the bot's token, click "Copy" to copy the token.
-
Paste the copied token as the value for the
DISCORD_BOT_TOKEN
secret in Lazy Builder. -
Set up bot permissions on Discord:
- In the Discord Developer Portal, go to your application's "OAuth2" section.
- Under "Scopes", select "bot".
- Under "Bot Permissions", select the following:
- Read Message History
- Kick Members
- Read Messages / View Channels
- Copy the generated OAuth2 URL and use it to invite the bot to your Discord server.
Test the Bot
- Click the "Test" button in the Lazy Builder to deploy and start the bot.
Using the Bot
Once the bot is running, you can use the following commands in your Discord server:
- Set inactivity criteria:
- Use the command
!setinactivity <period> <unit>
where:<period>
is a number<unit>
is either "days" or "minutes"
-
Example:
!setinactivity 3 days
or!setinactivity 30 minutes
-
Kick inactive members:
- Use the command
!kickinactive
to kick members who meet the inactivity criteria.
Note: Only users with administrator permissions in the Discord server can use these commands.
Integrating the Bot
This bot runs as a standalone application within your Discord server. There are no additional integration steps required beyond inviting the bot to your server and using its commands.