add actual logging instead of just print
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
# Generate Token in Fluxer Settings under "Developer"
|
# Generate Token in Fluxer Settings under "Developer"
|
||||||
FLUXER_TOKEN=
|
FLUXER_TOKEN=
|
||||||
|
|
||||||
|
LOG_LEVEL=INFO # DEBUG, INFO, WARNING, ERROR, CRITICAL
|
||||||
INTERVAL=3600 # 1 hour
|
INTERVAL=3600 # 1 hour
|
||||||
SUBREDDIT=ich_iel
|
SUBREDDIT=ich_iel
|
||||||
POST_LIMIT=20 # How many posts to fetch from reddit each time
|
POST_LIMIT=20 # How many posts to fetch from reddit each time
|
||||||
60
main.py
60
main.py
@@ -6,6 +6,7 @@ from dotenv import load_dotenv
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import logging
|
||||||
|
|
||||||
# this is a bot which posts the latest image post from ich_iel
|
# this is a bot which posts the latest image post from ich_iel
|
||||||
# the code probably sucks, but it works, so I don't care
|
# the code probably sucks, but it works, so I don't care
|
||||||
@@ -14,9 +15,26 @@ import re
|
|||||||
bot = fluxer.Bot(command_prefix="/", intents=fluxer.Intents.GUILD_MESSAGES | fluxer.Intents.GUILDS)
|
bot = fluxer.Bot(command_prefix="/", intents=fluxer.Intents.GUILD_MESSAGES | fluxer.Intents.GUILDS)
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
|
||||||
|
log_formatter = logging.Formatter(
|
||||||
|
fmt="%(asctime)s [%(levelname)s] %(message)s",
|
||||||
|
datefmt="%Y-%m-%d %H:%M:%S",
|
||||||
|
)
|
||||||
|
|
||||||
|
file_handler = logging.FileHandler("data/bot.log", encoding="utf-8")
|
||||||
|
file_handler.setFormatter(log_formatter)
|
||||||
|
|
||||||
|
console_handler = logging.StreamHandler()
|
||||||
|
console_handler.setFormatter(log_formatter)
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=getattr(logging, log_level, logging.INFO),
|
||||||
|
handlers=[file_handler, console_handler],
|
||||||
|
)
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print(f"Logged in as {bot.user}")
|
logging.info(f"Logged in as {bot.user}")
|
||||||
asyncio.create_task(post_reddit_periodically())
|
asyncio.create_task(post_reddit_periodically())
|
||||||
|
|
||||||
async def post_reddit_periodically():
|
async def post_reddit_periodically():
|
||||||
@@ -33,23 +51,24 @@ async def get_latest_post(subreddit):
|
|||||||
if response.headers.get("Content-Type", "").startswith("application/json"):
|
if response.headers.get("Content-Type", "").startswith("application/json"):
|
||||||
try:
|
try:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
print(f"Fetched data from Reddit: {data}")
|
logging.debug(f"Fetched data from Reddit: {data}")
|
||||||
posts = []
|
posts = []
|
||||||
if data["data"]["children"]:
|
if data["data"]["children"]:
|
||||||
for child in data["data"]["children"]:
|
for child in data["data"]["children"]:
|
||||||
post = child["data"]
|
post = child["data"]
|
||||||
if post.get("post_hint") == "image" and post.get("url", "").endswith((".jpg", ".png", ".jpeg", ".gif")):
|
if post.get("post_hint") == "image" and post.get("url", "").endswith((".jpg", ".png", ".jpeg", ".gif")):
|
||||||
print(f"Found image post: {post['title']} - {post['url']}")
|
logging.info(f"Found image post: {post['title']} - {post['url']}")
|
||||||
posts.append((post["title"], post["url"]))
|
posts.append((post["title"], post["url"]))
|
||||||
return posts
|
return posts
|
||||||
except (KeyError, json.JSONDecodeError):
|
except (KeyError, json.JSONDecodeError):
|
||||||
|
logging.error(f"Error parsing Reddit response: {response.text}")
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
print(f"Unexpected content type from Reddit: {response.headers.get('Content-Type')}")
|
logging.warning(f"Unexpected content type from Reddit: {response.headers.get('Content-Type')}")
|
||||||
print(f"Response content: {response.text}")
|
logging.warning(f"Response content: {response.text}")
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
print(f"Failed to fetch Reddit data (maybe a block?): {response.status_code}")
|
logging.error(f"Failed to fetch Reddit data (maybe a block?): {response.status_code}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
async def init_db():
|
async def init_db():
|
||||||
@@ -59,9 +78,9 @@ async def init_db():
|
|||||||
cur.execute("CREATE TABLE IF NOT EXISTS channels (guild_id INTEGER PRIMARY KEY, channel_id INTEGER)")
|
cur.execute("CREATE TABLE IF NOT EXISTS channels (guild_id INTEGER PRIMARY KEY, channel_id INTEGER)")
|
||||||
cur.execute("CREATE TABLE IF NOT EXISTS posted (guild_id INTEGER, post_id VARCHAR(255), PRIMARY KEY (guild_id, post_id))")
|
cur.execute("CREATE TABLE IF NOT EXISTS posted (guild_id INTEGER, post_id VARCHAR(255), PRIMARY KEY (guild_id, post_id))")
|
||||||
con.commit()
|
con.commit()
|
||||||
print("Database initialized successfully")
|
logging.info("Database initialized successfully")
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
print(f"Database initialization error: {e}")
|
logging.error(f"Database initialization error: {e}")
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def setChannel(message):
|
async def setChannel(message):
|
||||||
@@ -76,7 +95,7 @@ async def setChannel(message):
|
|||||||
channel_id = int(args[1])
|
channel_id = int(args[1])
|
||||||
channel = await bot.fetch_channel(channel_id)
|
channel = await bot.fetch_channel(channel_id)
|
||||||
guild_id = getattr(channel, "guild_id", None) or getattr(channel, "guild", {}).get("id", None)
|
guild_id = getattr(channel, "guild_id", None) or getattr(channel, "guild", {}).get("id", None)
|
||||||
print(f"Setting channel to {channel_id} for guild {guild_id}")
|
logging.info(f"Setting channel to {channel_id} for guild {guild_id}")
|
||||||
try:
|
try:
|
||||||
con = sqlite3.connect('data/ich_iel-bot.db')
|
con = sqlite3.connect('data/ich_iel-bot.db')
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
@@ -84,6 +103,7 @@ async def setChannel(message):
|
|||||||
con.commit()
|
con.commit()
|
||||||
await message.channel.send(f"Channel set to {channel_id}")
|
await message.channel.send(f"Channel set to {channel_id}")
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
|
logging.error(f"Database error: {e}")
|
||||||
await message.channel.send(f"Database error: {e}")
|
await message.channel.send(f"Database error: {e}")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
await message.channel.send("Invalid channel ID")
|
await message.channel.send("Invalid channel ID")
|
||||||
@@ -98,7 +118,7 @@ async def post_reddit():
|
|||||||
subreddit = os.getenv("SUBREDDIT", "ich_iel")
|
subreddit = os.getenv("SUBREDDIT", "ich_iel")
|
||||||
posts = await get_latest_post(subreddit)
|
posts = await get_latest_post(subreddit)
|
||||||
if not posts:
|
if not posts:
|
||||||
print("No image posts found.")
|
logging.info("No image posts found.")
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
con = sqlite3.connect('data/ich_iel-bot.db')
|
con = sqlite3.connect('data/ich_iel-bot.db')
|
||||||
@@ -106,10 +126,10 @@ async def post_reddit():
|
|||||||
cur.execute("SELECT guild_id, channel_id FROM channels")
|
cur.execute("SELECT guild_id, channel_id FROM channels")
|
||||||
rows = cur.fetchall()
|
rows = cur.fetchall()
|
||||||
if not rows:
|
if not rows:
|
||||||
print("No channels set")
|
logging.info("No channels set")
|
||||||
return
|
return
|
||||||
for guild_id, channel_id in rows:
|
for guild_id, channel_id in rows:
|
||||||
print(f"Processing guild {guild_id}")
|
logging.info(f"Processing guild {guild_id}")
|
||||||
for title, image_url in posts:
|
for title, image_url in posts:
|
||||||
post_id_match = re.search(r"\/([^\/]+)\.(jpg|png|jpeg|gif)$", image_url)
|
post_id_match = re.search(r"\/([^\/]+)\.(jpg|png|jpeg|gif)$", image_url)
|
||||||
if not post_id_match:
|
if not post_id_match:
|
||||||
@@ -117,7 +137,7 @@ async def post_reddit():
|
|||||||
post_id = post_id_match.group(1)
|
post_id = post_id_match.group(1)
|
||||||
is_posted = cur.execute("SELECT post_id FROM posted WHERE guild_id = ? AND post_id = ?", (guild_id, post_id)).fetchone()
|
is_posted = cur.execute("SELECT post_id FROM posted WHERE guild_id = ? AND post_id = ?", (guild_id, post_id)).fetchone()
|
||||||
if is_posted:
|
if is_posted:
|
||||||
print(f"Post {post_id} already posted in guild {guild_id}, skipping.")
|
logging.info(f"Post {post_id} already posted in guild {guild_id}, skipping.")
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
channel = await bot.fetch_channel(int(channel_id))
|
channel = await bot.fetch_channel(int(channel_id))
|
||||||
@@ -125,21 +145,21 @@ async def post_reddit():
|
|||||||
await channel.send(f"{title}\nOriginal post: {image_url}")
|
await channel.send(f"{title}\nOriginal post: {image_url}")
|
||||||
cur.execute("INSERT OR REPLACE INTO posted (guild_id, post_id) VALUES (?, ?)", (guild_id, post_id))
|
cur.execute("INSERT OR REPLACE INTO posted (guild_id, post_id) VALUES (?, ?)", (guild_id, post_id))
|
||||||
con.commit()
|
con.commit()
|
||||||
print(f"Posted to channel {channel_id} in guild {guild_id}")
|
logging.info(f"Posted to channel {channel_id} in guild {guild_id}")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print(f"Channel {channel_id} not found for guild {guild_id}")
|
logging.warning(f"Channel {channel_id} not found for guild {guild_id}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error sending to channel {channel_id}: {e}")
|
logging.error(f"Error sending to channel {channel_id}: {e}")
|
||||||
print("All posts processed.")
|
logging.info("All posts processed.")
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
print(f"Database error: {e}")
|
logging.error(f"Database error: {e}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("Starting bot...")
|
logging.info("Starting bot...")
|
||||||
asyncio.run(init_db())
|
asyncio.run(init_db())
|
||||||
TOKEN = os.getenv("FLUXER_TOKEN")
|
TOKEN = os.getenv("FLUXER_TOKEN")
|
||||||
if not TOKEN:
|
if not TOKEN:
|
||||||
print("Error: FLUXER_TOKEN not found in environment variables")
|
logging.error("Error: FLUXER_TOKEN not found in environment variables")
|
||||||
else:
|
else:
|
||||||
bot.run(TOKEN)
|
bot.run(TOKEN)
|
||||||
Reference in New Issue
Block a user