G4_Bot/bot.py
2025-04-21 12:40:03 +00:00

76 lines
No EOL
2.1 KiB
Python

#bot.py
##############################
##SETUP##
##############################
#Imports.
import discord, os, json, methods
from discord.ext import commands
from dotenv import load_dotenv
#Load .env file.
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
PREFIX = os.getenv('BOT_PREFIX')
#Sets Discord intents.
intents = discord.Intents.default()
intents.message_content = True
# Create a bot instance with a command prefix of your choice
bot = commands.Bot(command_prefix=PREFIX, intents=intents)
#Load commands to an object named commands_dict.
commands_dict = methods.loadcommands("commands.json")
customcommands_dict = methods.loadcommands("customcommands.json")
##############################
##DEFAULT ACTIONS##
##############################
#Debug message on startup. Not visible to server.
@bot.event
async def on_ready():
print(f'Logged in as {bot.user}!')
#Look for matching commands in JSON file for simple commands.
@bot.event
async def on_message(message):
commands_dict = methods.loadcommands("commands.json")
customcommands_dict = methods.loadcommands("customcommands.json")
if message.author.bot:
return #Ignore any messages from itself.
content = message.content.strip() #Remove the prefix.
if content.startswith(PREFIX):
parts = content[1:].split()
cmd = parts[0]
args = parts[1:]
if cmd in commands_dict:
method_name = commands_dict[cmd]
method = getattr(methods, method_name, None)
if callable(method):
ctx = await bot.get_context(message)
try:
response = await method(ctx, *args)
if response:
await message.channel.send(response)
except TypeError:
await message.channel.send("Invalid number of arguments, or there was an error in the method.")
return
if cmd in customcommands_dict:
response = customcommands_dict[cmd]
response = response.replace("{mention}", message.author.mention)
await message.channel.send(response)
# Run the bot with your token
bot.run(TOKEN)