faucet/cogs/listener.py

54 lines
1.8 KiB
Python
Raw Normal View History

import discord
from discord.ext import commands
import aiohttp
import os
from dotenv import load_dotenv
load_dotenv()
class MessageLogger(commands.Cog):
def __init__(self, bot):
self.bot = bot
# Load webhook URL
self.webhook_url = os.getenv("WEBHOOK_URL")
if not self.webhook_url:
raise ValueError("WEBHOOK_URL environment variable not set.")
# Load ignored user IDs
ignored_ids_str = os.getenv("IGNORED_USER_IDS", "")
self.ignored_user_ids = [int(uid) for uid in ignored_ids_str.split(",") if uid.strip().isdigit()]
# Load allowed channel IDs
channel_ids_str = os.getenv("CHANNEL_IDS", "")
self.allowed_channel_ids = [int(cid) for cid in channel_ids_str.split(",") if cid.strip().isdigit()]
@commands.Cog.listener()
async def on_message(self, message):
if message.author == self.bot.user:
return
if message.author.id in self.ignored_user_ids:
return
if message.channel.id not in self.allowed_channel_ids:
return
embed = {
"title": "Message Log",
"description": message.content,
"color": 3426654,
"timestamp": message.created_at.isoformat(),
"author": {
"name": message.author.name,
"icon_url": message.author.avatar.url if message.author.avatar else ""
},
"fields": [
{"name": "Channel", "value": message.channel.mention, "inline": False},
{"name": "User ID", "value": str(message.author.id), "inline": False}
]
}
async with aiohttp.ClientSession() as session:
await session.post(self.webhook_url, json={"embeds": [embed]})