86 lines
No EOL
2.6 KiB
Python
86 lines
No EOL
2.6 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')
|
|
CHANNEL = os.getenv('BOT_CHANNEL')
|
|
|
|
#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)
|
|
|
|
@bot.event
|
|
async def on_voice_state_update(member, before, after):
|
|
|
|
#Sends a message when someone joins any voice channel.
|
|
if before.channel is None and after.channel is not None:
|
|
channel = discord.utils.get(member.guild.text_channels, name=CHANNEL) #Finds a channel named in the .env file.
|
|
if channel: #If the channel exists.
|
|
await channel.send(f"{member.mention} joined {after.channel.name}!")
|
|
|
|
# Run the bot with your token
|
|
bot.run(TOKEN) |