counxs

Create a simple bot with /mute and /unmute and /token only usable by admin+ (which shows me the token)

Kodari AI
KodariJun 3 at 3:01 am 6
ModerationBot
A Discord moderation bot with slash commands for server management. Features /mute and /unmute to manage member communication with optional duration and reason support, and a secure /token command restricted to administrators that privately displays the bot token. All commands are protected with admin-level permission checks to ensure only authorized users can execute sensitive actions.

Three files created. Here’s what you need to do:

Setup

Shell
pip install -r requirements.txt

Edit .env and replace your_discord_bot_token_here with your real bot token, then:

Shell
python bot.py

Commands

Command
Description
Permission
/mute @user [reason]
Adds a
Muted
role, blocking messages/voice
Admin only
/unmute @user
Removes the
Muted
role
Admin only
/token
Shows the bot token
privately
(ephemeral)
Admin only

Notes

  • /mute auto-creates the Muted role and sets deny-permissions on every channel if it doesn’t exist yet

  • /token uses ephemeral=True — only you can see the response, it never appears in chat

  • Non-admins get a private error message on all three commands

Kodari can make mistakes. Review important code before using.
Files
bot.py
requirements.txt
Code
2 89
bot.py
import discord
from discord import app_commands
import os
from dotenv import load_dotenv

load_dotenv()

TOKEN = os.getenv("DISCORD_TOKEN")

intents = discord.Intents.default()
intents.members = True

bot = discord.Client(intents=intents)
tree = app_commands.CommandTree(bot)


def is_admin(interaction: discord.Interaction) -> bool:
    return interaction.user.guild_permissions.administrator


@bot.event
async def on_ready():
    await tree.sync()
    print(f"Logged in as {bot.user} | Slash commands synced.")


@tree.command(name="mute", description="Mute a member (remove ability to send messages)")
@app_commands.describe(member="The member to mute", reason="Reason for muting")
async def mute(interaction: discord.Interaction, member: discord.Member, reason: str = "No reason provided"):
    if not is_admin(interaction):
        await interaction.response.send_message("❌ You need **Administrator** permission to use this command.", ephemeral=True)
        return

    muted_role = discord.utils.get(interaction.guild.roles, name="Muted")

    if muted_role is None:
        muted_role = await interaction.guild.create_role(name="Muted")
        for channel in interaction.guild.channels:
            await channel.set_permissions(muted_role, send_messages=False, speak=False, add_reactions=False)

    if muted_role in member.roles:
        await interaction.response.send_message(f"⚠️ {member.mention} is already muted.", ephemeral=True)
        return

    await member.add_roles(muted_role, reason=reason)

    embed = discord.Embed(title="🔇 Member Muted", color=discord.Color.orange())
    embed.add_field(name="Member", value=member.mention, inline=True)
    embed.add_field(name="Moderator", value=interaction.user.mention, inline=True)
    embed.add_field(name="Reason", value=reason, inline=False)
    await interaction.response.send_message(embed=embed)


@tree.command(name="unmute", description="Unmute a previously muted member")
@app_commands.describe(member="The member to unmute")
async def unmute(interaction: discord.Interaction, member: discord.Member):
    if not is_admin(interaction):
        await interaction.response.send_message("❌ You need **Administrator** permission to use this command.", ephemeral=True)
        return

    muted_role = discord.utils.get(interaction.guild.roles, name="Muted")

    if muted_role is None or muted_role not in member.roles:
        await interaction.response.send_message(f"⚠️ {member.mention} is not currently muted.", ephemeral=True)
        return

    await member.remove_roles(muted_role)

    embed = discord.Embed(title="🔊 Member Unmuted", color=discord.Color.green())
    embed.add_field(name="Member", value=member.mention, inline=True)
    embed.add_field(name="Moderator", value=interaction.user.mention, inline=True)
    await interaction.response.send_message(embed=embed)


@tree.command(name="token", description="Displays the bot token (admin only, private)")
async def token(interaction: discord.Interaction):
    if not is_admin(interaction):
        await interaction.response.send_message("❌ You need **Administrator** permission to use this command.", ephemeral=True)
        return

    await interaction.response.send_message(
        f"🔑 **Bot Token:**\n```{TOKEN}```\n⚠️ Keep this secret! Never share it publicly.",
        ephemeral=True
    )


bot.run(TOKEN)