cube._007

create a discord bot to make rules

Kodari AI
KodariJun 26 at 7:32 am 1

I’ll create DiscordRuleManager — a Discord bot that lets server administrators create, manage, and display server rules with user agreement tracking.

DiscordRuleManager
A Discord bot for server administrators to create, manage, and display server rules. Features include adding/removing/editing rules, numbered rule displays in embeds, dedicated rules channel configuration, user agreement with auto-role assignment, persistent JSON storage, permission-restricted admin commands, and a comprehensive help system.

All files are created. Here’s a summary:

Project Structure

Plain Text
DiscordRuleManager/
├── .env                    # Bot token (edit this!)
├── requirements.txt        # Dependencies
├── config.json             # Bot configuration
├── main.py                 # Entry point
├── utils/
│   ├── __init__.py
│   └── rules_manager.py    # JSON-based data persistence
└── cogs/
    ├── __init__.py
    └── rules.py            # All commands

Features

Command
Purpose
!rules
Display all rules in a clean embed
!rule <n>
Show a single rule
!accept
Accept rules → get assigned role
!ruleset
Admin command panel
!ruleset add <text>
Add a rule
!ruleset remove <n>
Remove a rule (auto-renumbers)
!ruleset edit <n> <text>
Edit a rule
!ruleset channel #ch
Set dedicated rules channel
!ruleset role @role
Set role given on acceptance
!ruleset agree on/off
Toggle acceptance system
!ruleset reload
Reload data from file

To run

  1. Edit .env — replace your_discord_bot_token_here with your actual token

  2. pip install -r requirements.txt

  3. python main.py

Kodari can make mistakes. Review important code before using.
Files
cogs
utils
config.json
main.py
requirements.txt
Code
7 412
main.py
import os
import asyncio
import discord
from discord.ext import commands
from dotenv import load_dotenv
from utils import rules_manager

load_dotenv()

TOKEN = os.getenv("DISCORD_TOKEN")
if not TOKEN or TOKEN == "your_discord_bot_token_here":
    print("❌ ERROR: No DISCORD_TOKEN found in .env file.")
    print("  1. Open the .env file")
    print("  2. Replace 'your_discord_bot_token_here' with your actual bot token")
    print("  3. Save the file and run again")
    exit(1)

intents = discord.Intents.all()

bot = commands.Bot(
    command_prefix="!",
    intents=intents,
    help_command=None
)


@bot.event
async def on_ready():
    print(f"✅ Logged in as {bot.user} (ID: {bot.user.id})")
    print(f"🌐 Serving {len(bot.guilds)} guild(s)")
    print("─" * 40)


@bot.event
async def on_guild_join(guild):
    rules_manager.get_guild_data(guild.id)
    channel = guild.system_channel
    if channel and channel.permissions_for(guild.me).send_messages:
        embed = discord.Embed(
            title="👋 Thanks for adding me!",
            description=(
                "I help manage server rules. Here's how to get started:\n\n"
                "1. **Add rules** — `!ruleset add <rule text>`\n"
                "2. **View rules** — `!rules`\n"
                "3. **Set a channel** — `!ruleset channel #channel`\n"
                "4. **Set agreement role** — `!ruleset role @role`\n"
                "5. **Enable acceptance** — `!ruleset agree on`\n\n"
                "Use `!help` for all commands."
            ),
            color=discord.Color.green()
        )
        await channel.send(embed=embed)


@bot.command(name="help")
async def help_command(ctx):
    embed = discord.Embed(
        title="📜 DiscordRuleManager — Commands",
        description="Manage and display server rules easily.",
        color=discord.Color.green()
    )
    embed.add_field(name="!rules", value="Show all server rules", inline=False)
    embed.add_field(name="!rule <number>", value="Show a specific rule", inline=False)
    embed.add_field(name="!accept", value="Accept the server rules", inline=False)
    embed.add_field(name="!ruleset", value="Admin rule management panel", inline=False)
    embed.add_field(name="!ruleset add <text>", value="Add a new rule (Admin)", inline=False)
    embed.add_field(name="!ruleset remove <num>", value="Remove a rule (Admin)", inline=False)
    embed.add_field(name="!ruleset edit <num> <text>", value="Edit a rule (Admin)", inline=False)
    embed.add_field(name="!ruleset channel <#ch>", value="Set rules channel (Admin)", inline=False)
    embed.add_field(name="!ruleset role <@role>", value="Set accept role (Admin)", inline=False)
    embed.add_field(name="!ruleset agree on/off", value="Toggle acceptance (Admin)", inline=False)
    embed.add_field(name="!ruleset reload", value="Reload from file (Admin)", inline=False)
    embed.set_footer(text=f"{bot.user.name} • Made with discord.py")
    await ctx.send(embed=embed)


@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandNotFound):
        return
    if isinstance(error, commands.NotOwner):
        return
    raise error


async def main():
    await bot.load_extension("cogs.rules")
    async with bot:
        await bot.start(TOKEN)


if __name__ == "__main__":
    asyncio.run(main())