cq1v

make me a bot with !messageall and ensure the bot says Watching Over Jackson State

Kodari AI
KodariAug 4 at 12:37 am 1
JSUBroadcaster
A Discord bot for Jackson State University community, featuring a !messageall command that broadcasts a message to all members in the server. The bot also sets its presence to 'Watching Over Jackson State' to reflect its purpose. Built with discord.py, it provides a simple and reliable way to communicate with the entire server, ensuring no member misses important announcements. The bot is easy to configure and can be used by moderators to send mass messages quickly. It includes error handling for missing arguments and permission checks to prevent misuse. The bot is designed to be lightweight and efficient, making it ideal for campus-wide notifications.

Created a Discord bot with:

  • !messageall <message> for server-wide announcements

  • Administrator-only access

  • Presence set to Watching Over Jackson State

  • .env token loading

  • Permission checks and error handling

  • Setup documentation and dependencies

Kodari can make mistakes. Review important code before using.
Files
bot.py
README.md
requirements.txt
Code
3 111
bot.py
import logging
import os

import discord
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
logger = logging.getLogger("jackson-state-bot")

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

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


@bot.event
async def on_ready():
    activity = discord.Activity(
        type=discord.ActivityType.watching,
        name="Over Jackson State",
    )
    await bot.change_presence(activity=activity)
    logger.info("Logged in as %s", bot.user)


@bot.command(name="messageall")
@commands.guild_only()
@commands.has_guild_permissions(administrator=True)
async def messageall(ctx: commands.Context, *, message: str):
    sent = 0
    skipped = 0

    for guild in bot.guilds:
        bot_member = guild.me
        for channel in guild.text_channels:
            permissions = channel.permissions_for(bot_member)
            if not permissions.view_channel or not permissions.send_messages:
                skipped += 1
                continue

            try:
                await channel.send(message)
                sent += 1
            except (discord.Forbidden, discord.HTTPException):
                skipped += 1

    await ctx.send(f"Message sent to {sent} channel(s). Skipped {skipped} channel(s).")


@messageall.error
async def messageall_error(ctx: commands.Context, error: commands.CommandError):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You need Administrator permission to use this command.")
    elif isinstance(error, commands.MissingRequiredArgument):
        await ctx.send("Usage: `!messageall <message>`")
    elif isinstance(error, commands.NoPrivateMessage):
        await ctx.send("This command can only be used in a server.")
    else:
        logger.exception("Error while running !messageall", exc_info=error)
        await ctx.send("Something went wrong while sending the message.")


token = os.getenv("DISCORD_TOKEN")
if not token:
    raise RuntimeError("DISCORD_TOKEN is not set in the environment.")

bot.run(token)