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)