Files
bot.py
config.json
requirements.txt
Code
6 248
bot.py
import discord
from discord import app_commands
from discord.ext import tasks
import os
import json
from dotenv import load_dotenv
load_dotenv()
CONFIG_FILE = "config.json"
def load_config():
with open(CONFIG_FILE, "r") as f:
return json.load(f)
def save_config(data):
with open(CONFIG_FILE, "w") as f:
json.dump(data, f, indent=4)
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
@client.event
async def on_ready():
print(f"Logged in as {client.user}")
try:
synced = await tree.sync()
print(f"Synced {len(synced)} commands globally")
except Exception as e:
print(f"Sync error: {e}")
config = load_config()
if config.get("running"):
auto_send.start()
@tasks.loop()
async def auto_send():
config = load_config()
cooldown = config.get("cooldown", 60)
auto_send.change_interval(seconds=cooldown)
if not config.get("running"):
auto_send.stop()
return
message = config.get("message", "Hello!")
owner_id = (await client.application_info()).owner.id
user = await client.fetch_user(owner_id)
if user:
try:
await user.send(message)
except discord.Forbidden:
print("Cannot DM the owner — DMs may be closed.")
@tree.command(name="setmessage", description="Set the auto-send message")
@app_commands.describe(message="The message to send")
async def setmessage(interaction: discord.Interaction, message: str):
if interaction.user.id != (await client.application_info()).owner.id:
await interaction.response.send_message("Only the bot owner can use this.", ephemeral=True)
return
config = load_config()
config["message"] = message
save_config(config)
await interaction.response.send_message(f"Message set to: {message}", ephemeral=True)
@tree.command(name="setcooldown", description="Set the interval in seconds")
@app_commands.describe(seconds="Number of seconds between messages")
async def setcooldown(interaction: discord.Interaction, seconds: int):
if interaction.user.id != (await client.application_info()).owner.id:
await interaction.response.send_message("Only the bot owner can use this.", ephemeral=True)
return
if seconds < 1:
await interaction.response.send_message("Cooldown must be at least 1 second.", ephemeral=True)
return
config = load_config()
config["cooldown"] = seconds
save_config(config)
if auto_send.is_running():
auto_send.restart()
await interaction.response.send_message(f"Cooldown set to {seconds} seconds.", ephemeral=True)
@tree.command(name="start", description="Start the auto-messaging loop")
async def start(interaction: discord.Interaction):
if interaction.user.id != (await client.application_info()).owner.id:
await interaction.response.send_message("Only the bot owner can use this.", ephemeral=True)
return
config = load_config()
config["running"] = True
save_config(config)
if not auto_send.is_running():
auto_send.start()
await interaction.response.send_message("Auto-messaging started.", ephemeral=True)
else:
await interaction.response.send_message("Already running.", ephemeral=True)
@tree.command(name="stop", description="Stop the auto-messaging loop")
async def stop(interaction: discord.Interaction):
if interaction.user.id != (await client.application_info()).owner.id:
await interaction.response.send_message("Only the bot owner can use this.", ephemeral=True)
return
config = load_config()
config["running"] = False
save_config(config)
if auto_send.is_running():
auto_send.stop()
await interaction.response.send_message("Auto-messaging stopped.", ephemeral=True)
else:
await interaction.response.send_message("Already stopped.", ephemeral=True)
client.run(os.getenv("DISCORD_TOKEN"))