Files
bot.py
noctis.py
requirements.txt
storage.py
Code
4 796
bot.py
import logging
import os
from typing import Optional
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv
from noctis import NoctisEngine
from storage import ArchiveStore
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)
logger = logging.getLogger("noctis")
intents = discord.Intents.default()
intents.message_content = True
store = ArchiveStore("data/noctis.json")
class NoctisBot(commands.Bot):
def __init__(self) -> None:
super().__init__(
command_prefix=commands.when_mentioned_or("!"),
intents=intents,
help_command=None,
)
self.engine = NoctisEngine(self, store)
async def setup_hook(self) -> None:
self.engine.start_scheduler()
await self.tree.sync()
logger.info("Noctis slash commands synchronized.")
async def on_ready(self) -> None:
await self.change_presence(
status=discord.Status.idle,
activity=discord.Game("the lights"),
)
logger.info("Noctis is online as %s.", self.user)
async def on_message(self, message: discord.Message) -> None:
if message.author.bot:
return
response = self.engine.observe_message(message.guild_id, message.content)
if response is not None:
await message.reply(
response,
mention_author=False,
allowed_mentions=discord.AllowedMentions.none(),
)
await self.process_commands(message)
bot = NoctisBot()
noctis = app_commands.Group(
name="noctis",
description="Interact with the presence in the archive.",
)
bot.tree.add_command(noctis)
def guild_only(interaction: discord.Interaction) -> bool:
return interaction.guild_id is not None
@noctis.command(name="start", description="Awaken Noctis in this channel.")
async def start(interaction: discord.Interaction) -> None:
if not guild_only(interaction):
await interaction.response.send_message(
"Noctis does not answer in private conversations.",
ephemeral=True,
)
return
channel_id = interaction.channel_id
if channel_id is None:
await interaction.response.send_message(
"No channel could be recorded.",
ephemeral=True,
)
return
first_start = bot.engine.start_guild(interaction.guild_id, channel_id)
if first_start:
embed = discord.Embed(
title="NOCTIS // CONNECTION ESTABLISHED",
description=(
"The archive is open.\n\n"
"Noctis will remain quiet for a while. Do not assume that "
"silence means it has left."
),
color=0x6E0F17,
)
embed.set_footer(text="Chapter I — The Unlit Corridor")
else:
embed = discord.Embed(
title="NOCTIS // ALREADY AWAKE",
description=(
"The archive is already listening in this server. "
"Changing the channel will not make it forget."
),
color=0x303030,
)
await interaction.response.send_message(embed=embed)
@noctis.command(name="archive", description="Read the latest entries in the archive.")
@app_commands.describe(limit="Number of recent entries to display.")
async def archive(
interaction: discord.Interaction,
limit: app_commands.Range[int, 1, 15] = 8,
) -> None:
if not guild_only(interaction):
await interaction.response.send_message(
"The archive has no private shelves.",
ephemeral=True,
)
return
entries = bot.engine.get_archive(interaction.guild_id, limit)
if not entries:
await interaction.response.send_message(
"The archive is empty. It is waiting for something to happen.",
ephemeral=True,
)
return
lines = []
for entry in entries:
timestamp = entry["timestamp"].replace("T", " ")[:16]
lines.append(f"`{timestamp}` — {entry['text']}")
embed = discord.Embed(
title="NOCTIS // FORGOTTEN ARCHIVE",
description="\n".join(lines),
color=0x242424,
)
embed.set_footer(text="Some entries may have been written before you arrived.")
await interaction.response.send_message(embed=embed)
@noctis.command(name="chapter", description="Reveal the current chapter of the story.")
async def chapter(interaction: discord.Interaction) -> None:
if not guild_only(interaction):
await interaction.response.send_message(
"There are no chapters here.",
ephemeral=True,
)
return
details = bot.engine.get_chapter(interaction.guild_id)
embed = discord.Embed(
title=f"CHAPTER {details['number']} // {details['title']}",
description=details["description"],
color=0x6E0F17,
)
embed.add_field(name="Current trace", value=details["trace"], inline=False)
embed.set_footer(text="The story changes when someone chooses to look closer.")
await interaction.response.send_message(embed=embed)
@noctis.command(name="murmur", description="Ask Noctis for a brief message.")
async def murmur(interaction: discord.Interaction) -> None:
if not guild_only(interaction):
await interaction.response.send_message(
"The murmur cannot cross that distance.",
ephemeral=True,
)
return
text = bot.engine.create_murmur(interaction.guild_id)
embed = discord.Embed(
title="NOCTIS // MURMUR",
description=f"“{text}”",
color=0x3D0B10,
)
await interaction.response.send_message(embed=embed)
@noctis.command(name="puzzle", description="Inspect or answer the current occult puzzle.")
@app_commands.describe(answer="Your answer to the current puzzle.")
async def puzzle(
interaction: discord.Interaction,
answer: Optional[str] = None,
) -> None:
if not guild_only(interaction):
await interaction.response.send_message(
"The symbols refuse to appear here.",
ephemeral=True,
)