first commit
This commit is contained in:
104
bot/bot.py
104
bot/bot.py
@@ -19,6 +19,7 @@ import bcrypt
|
||||
import pickle
|
||||
|
||||
from bot.command_registry import get_handler, list_registered
|
||||
import bot.prefix_command_registry as prefix_registry
|
||||
import ai.parser as ai_parser
|
||||
|
||||
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
|
||||
@@ -32,6 +33,7 @@ CACHE_FILE = "/app/user_cache.pkl"
|
||||
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
intents.voice_states = True
|
||||
|
||||
client = discord.Client(intents=intents)
|
||||
|
||||
@@ -181,7 +183,7 @@ async def handleLoginStep(message):
|
||||
|
||||
async def sendHelpMessage(message):
|
||||
registered = list_registered()
|
||||
help_msg = f"**Available Modules:**\n{chr(10).join(f'- {m}' for m in registered) if registered else '- No modules registered'}\n\nJust talk naturally and I'll help you out!"
|
||||
help_msg = f"**Available Modules:**\n{chr(10).join(f'- {m}' for m in registered) if registered else '- No modules registered'}\n\nJust talk naturally and I'll try to help you out!"
|
||||
await message.channel.send(help_msg)
|
||||
|
||||
|
||||
@@ -190,41 +192,71 @@ async def routeCommand(message):
|
||||
session = user_sessions[discord_id]
|
||||
user_input = message.content.lower()
|
||||
|
||||
if "help" in user_input or "what can i say" in user_input:
|
||||
await sendHelpMessage(message)
|
||||
return
|
||||
# if "help" in user_input in user_input:
|
||||
# await sendHelpMessage(message)
|
||||
# return
|
||||
if isinstance(message.channel, discord.DMChannel):
|
||||
ADMIN_IDS = [
|
||||
int(uid.strip())
|
||||
for uid in os.getenv("ADMIN_USER_IDS", "").split(",")
|
||||
if uid.strip()
|
||||
]
|
||||
if discord_id in ADMIN_IDS:
|
||||
async with message.channel.typing():
|
||||
history = message_history.get(discord_id, [])
|
||||
parsed = ai_parser.parse(
|
||||
message.content, "command_parser", history=history
|
||||
)
|
||||
|
||||
async with message.channel.typing():
|
||||
history = message_history.get(discord_id, [])
|
||||
parsed = ai_parser.parse(message.content, "command_parser", history=history)
|
||||
if discord_id not in message_history:
|
||||
message_history[discord_id] = []
|
||||
message_history[discord_id].append((message.content, parsed))
|
||||
message_history[discord_id] = message_history[discord_id][-5:]
|
||||
|
||||
if discord_id not in message_history:
|
||||
message_history[discord_id] = []
|
||||
message_history[discord_id].append((message.content, parsed))
|
||||
message_history[discord_id] = message_history[discord_id][-5:]
|
||||
if "needs_clarification" in parsed:
|
||||
await message.channel.send(
|
||||
f"I'm not quite sure what you mean. {parsed['needs_clarification']}"
|
||||
)
|
||||
return
|
||||
|
||||
if "needs_clarification" in parsed:
|
||||
await message.channel.send(
|
||||
f"I'm not quite sure what you mean. {parsed['needs_clarification']}"
|
||||
)
|
||||
return
|
||||
if "error" in parsed:
|
||||
await message.channel.send(
|
||||
f"I had trouble understanding that: {parsed['error']}"
|
||||
)
|
||||
return
|
||||
|
||||
if "error" in parsed:
|
||||
await message.channel.send(
|
||||
f"I had trouble understanding that: {parsed['error']}"
|
||||
)
|
||||
return
|
||||
interaction_type = parsed.get("interaction_type")
|
||||
handler = get_handler(interaction_type)
|
||||
|
||||
interaction_type = parsed.get("interaction_type")
|
||||
handler = get_handler(interaction_type)
|
||||
if handler:
|
||||
await handler(message, session, parsed)
|
||||
else:
|
||||
registered = ", ".join(list_registered()) or "none"
|
||||
await message.channel.send(
|
||||
f"Unknown command type '{interaction_type}'. Registered modules: {registered}"
|
||||
)
|
||||
else:
|
||||
await message.channel.send("You don't have permission to use this bot.")
|
||||
|
||||
if handler:
|
||||
await handler(message, session, parsed)
|
||||
else:
|
||||
registered = ", ".join(list_registered()) or "none"
|
||||
await message.channel.send(
|
||||
f"Unknown command type '{interaction_type}'. Registered modules: {registered}"
|
||||
)
|
||||
elif hasattr(message.channel, "guild") and message.channel.guild is not None:
|
||||
# we are in a guild channel, now we need to determine which
|
||||
if message.channel.id == int(os.getenv("COMMAND_CHANNEL_ID", 0)):
|
||||
content = message.content.strip()
|
||||
if not content.startswith("!"):
|
||||
return
|
||||
|
||||
parts = content[1:].split()
|
||||
if not parts:
|
||||
return
|
||||
|
||||
command_name = parts[0].lower()
|
||||
args = parts[1:]
|
||||
|
||||
handler = prefix_registry.get_prefix_handler(command_name)
|
||||
if handler:
|
||||
await handler(message, args)
|
||||
else:
|
||||
await message.channel.send(f"Unknown command '{command_name}'.")
|
||||
|
||||
|
||||
@client.event
|
||||
@@ -267,4 +299,16 @@ async def beforeBackgroundLoop():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from bot.commands import load_commands
|
||||
|
||||
load_commands()
|
||||
|
||||
# Optional: load general plugins
|
||||
try:
|
||||
from plugins import load_plugins
|
||||
|
||||
load_plugins()
|
||||
except ImportError:
|
||||
pass # plugins folder not present
|
||||
|
||||
client.run(DISCORD_BOT_TOKEN)
|
||||
|
||||
Reference in New Issue
Block a user