first commit

This commit is contained in:
Zastian Pretorius
2025-08-28 00:21:50 +01:00
commit 3971a76d02
5 changed files with 167 additions and 0 deletions

39
bot.py Normal file
View File

@@ -0,0 +1,39 @@
import discord
from discord.ext import commands
import re
import os
# Set up intents explicitly
intents = discord.Intents.default()
intents.message_content = True # Enable message content access
# Set up the bot
bot = commands.Bot(command_prefix='!', intents=intents)
# Event: Bot is ready and connected to Discord
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
# Event: Triggered when a message is sent in a channel the bot can see
@bot.event
async def on_message(message):
# Ignore messages sent by the bot itself to prevent loops
if message.author == bot.user:
return
# Check if the message contains "nixos" (case-insensitive, as a whole word)
if re.search(r'\bnixos\b', message.content, re.IGNORECASE):
# Replace "nixos" with "fagos" (preserving case of the first letter)
new_content = re.sub(
r'\b(nixos)\b',
lambda m: 'Fagos' if m.group(1)[0].isupper() else 'fagos',
message.content,
flags=re.IGNORECASE
)
# Send the modified message
await message.channel.send(f'{new_content}')
# Allow other commands to process (if any)
await bot.process_commands(message)
# Run the bot using the token from the environment variable
bot.run(os.getenv('FAGOS_TOKEN'))

25
flake.lock generated Normal file
View File

@@ -0,0 +1,25 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1746904237,
"narHash": "sha256-3e+AVBczosP5dCLQmMoMEogM57gmZ2qrVSrmq9aResQ=",
"rev": "d89fc19e405cb2d55ce7cc114356846a0ee5e956",
"revCount": 797896,
"type": "tarball",
"url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.1.797896%2Brev-d89fc19e405cb2d55ce7cc114356846a0ee5e956/0196c1a7-7ad3-74a9-9d50-1b854aca6d6c/source.tar.gz"
},
"original": {
"type": "tarball",
"url": "https://flakehub.com/f/NixOS/nixpkgs/0.1"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

96
flake.nix Normal file
View File

@@ -0,0 +1,96 @@
{
description = "A Nix-flake-based Python development environment for the Fagos Discord bot";
inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.1";
outputs = inputs:
let
supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
forEachSupportedSystem = f: inputs.nixpkgs.lib.genAttrs supportedSystems (system: f {
pkgs = import inputs.nixpkgs { inherit system; };
});
version = "3.13";
in
{
# Development shell for `nix develop`
devShells = forEachSupportedSystem ({ pkgs }:
let
concatMajorMinor = v:
pkgs.lib.pipe v [
pkgs.lib.versions.splitVersion
(pkgs.lib.sublist 0 2)
pkgs.lib.concatStrings
];
python = pkgs."python${concatMajorMinor version}";
in
{
default = pkgs.mkShell {
venvDir = ".venv";
postShellHook = ''
venvVersionWarn() {
local venvVersion
venvVersion="$("$venvDir/bin/python" -c 'import platform; print(platform.python_version())')"
[[ "$venvVersion" == "${python.version}" ]] && return
cat <<EOF
Warning: Python version mismatch: [$venvVersion (venv)] != [${python.version}]
Delete '$venvDir' and reload to rebuild for version ${python.version}
EOF
}
venvVersionWarn
'';
packages = with python.pkgs; [
venvShellHook
pip
discordpy
];
};
});
# Packages for `nix build`
packages = forEachSupportedSystem ({ pkgs }:
let
concatMajorMinor = v:
pkgs.lib.pipe v [
pkgs.lib.versions.splitVersion
(pkgs.lib.sublist 0 2)
pkgs.lib.concatStrings
];
python = pkgs."python${concatMajorMinor version}";
# Define the bot package using buildPythonApplication
fagos = python.pkgs.buildPythonApplication {
pname = "fagos";
version = "0.1.0";
src = ./.;
format = "pyproject"; # Remove if no pyproject.toml
nativeBuildInputs = with python.pkgs; [ setuptools ];
propagatedBuildInputs = with python.pkgs; [ discordpy ];
doCheck = false;
};
in
{
default = fagos;
});
# Apps for `nix run`
apps = forEachSupportedSystem ({ pkgs }:
let
concatMajorMinor = v:
pkgs.lib.pipe v [
pkgs.lib.versions.splitVersion
(pkgs.lib.sublist 0 2)
pkgs.lib.concatStrings
];
python = pkgs."python${concatMajorMinor version}";
# Create a Python environment with discordpy
myPython = python.withPackages (ps: with ps; [ discordpy ]);
# Create a shell script to run bot.py
fagosApp = pkgs.writeShellScriptBin "fagos" ''
exec ${myPython}/bin/python ${./bot.py} "$@"
'';
in
{
default = {
type = "app";
program = "${fagosApp}/bin/fagos";
};
});
};
}

6
pyproject.toml Normal file
View File

@@ -0,0 +1,6 @@
[project]
name = "fagos"
version = "0.1.0"
dependencies = [
"discord.py>=2.0.0"
]

1
result Symbolic link
View File

@@ -0,0 +1 @@
/nix/store/3afn3mm8r72bwgni7hlgfx39bqb7scj7-python3.13-fagos-0.1.0