Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d802aba94 | |||
| 668eb52673 | |||
| 90684ba27f | |||
| 08778bf0a5 | |||
| 63972faa63 | |||
| f0729f43c9 | |||
| 91b31ed114 | |||
| 6df7d68f5a | |||
| 8615cc4d0d | |||
| b9e1740e10 | |||
| 4c874cbaa4 | |||
| 36a61e978b | |||
| 6e145fbf56 | |||
| 2adec3b76c |
@@ -4,12 +4,15 @@ A bot for Fluxer which posts a post from ich_iel every hour.
|
|||||||
|
|
||||||
## Installation:
|
## Installation:
|
||||||
|
|
||||||
|
Clone repo:
|
||||||
```
|
```
|
||||||
git clone https://git.scrunkly.cat/Michelle/ich_iel-Bot.git
|
git clone https://git.scrunkly.cat/Michelle/ich_iel-Bot.git
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Check .env file and add your FLUXER_TOKEN.
|
||||||
|
|
||||||
|
Then start the container:
|
||||||
```
|
```
|
||||||
docker compose build
|
|
||||||
docker compose up -d
|
docker compose up -d
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -1,6 +1,7 @@
|
|||||||
services:
|
services:
|
||||||
ich_iel-bot:
|
ich_iel-bot:
|
||||||
build: .
|
image: git.scrunkly.cat/michelle/ich_iel-bot:latest
|
||||||
|
restart: unless-stopped
|
||||||
volumes:
|
volumes:
|
||||||
- ./data:/app/data
|
- ./data:/app/data
|
||||||
env_file: ".env"
|
env_file: ".env"
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
|
import aiohttp
|
||||||
import fluxer
|
import fluxer
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import base64
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import os
|
import os
|
||||||
@@ -62,7 +64,7 @@ async def get_latest_post(subreddit):
|
|||||||
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")):
|
||||||
logging.info(f"Found image post: {post['title']} - {post['url']}")
|
logging.debug(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):
|
||||||
@@ -118,7 +120,7 @@ async def setChannel(message):
|
|||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def version(message):
|
async def version(message):
|
||||||
await message.channel.send("Version 0.4 is running")
|
await message.channel.send("Version 0.5.1 is running\nSource code: https://github.com/michelleDeko/ich_iel-bot")
|
||||||
|
|
||||||
# the cat bot died, so i wanted to add this command to this bot
|
# the cat bot died, so i wanted to add this command to this bot
|
||||||
@bot.command()
|
@bot.command()
|
||||||
@@ -133,6 +135,46 @@ async def cat(message):
|
|||||||
else:
|
else:
|
||||||
await message.channel.send("Failed to fetch cat image")
|
await message.channel.send("Failed to fetch cat image")
|
||||||
|
|
||||||
|
# i thought dogs and foxes would be nice to have too
|
||||||
|
@bot.command()
|
||||||
|
async def dog(message):
|
||||||
|
response = requests.get("https://dog.ceo/api/breeds/image/random")
|
||||||
|
if response.status_code == 200:
|
||||||
|
data = response.json()
|
||||||
|
if data and isinstance(data, dict) and "message" in data:
|
||||||
|
await message.channel.send(data["message"])
|
||||||
|
else:
|
||||||
|
await message.channel.send("Could not fetch dog image")
|
||||||
|
else:
|
||||||
|
await message.channel.send("Failed to fetch dog image")
|
||||||
|
|
||||||
|
@bot.command()
|
||||||
|
async def fox(message):
|
||||||
|
response = requests.get("https://randomfox.ca/floof/")
|
||||||
|
if response.status_code == 200:
|
||||||
|
data = response.json()
|
||||||
|
if data and isinstance(data, dict) and "image" in data:
|
||||||
|
await message.channel.send(data["image"])
|
||||||
|
else:
|
||||||
|
await message.channel.send("Could not fetch fox image")
|
||||||
|
else:
|
||||||
|
await message.channel.send("Failed to fetch fox image")
|
||||||
|
|
||||||
|
# time for racoons
|
||||||
|
@bot.command()
|
||||||
|
async def racoon(message):
|
||||||
|
urls = random.choice([
|
||||||
|
"https://api.mapach.es/v1/meme",
|
||||||
|
"https://api.mapach.es/v1/coon"
|
||||||
|
])
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(urls) as response:
|
||||||
|
image_bytes = await response.read()
|
||||||
|
if response.status != 200:
|
||||||
|
await message.channel.send("Failed to fetch racoon image")
|
||||||
|
return
|
||||||
|
await message.channel.send(file=fluxer.File(image_bytes, filename="racoon.jpg"))
|
||||||
|
|
||||||
async def post_reddit():
|
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)
|
||||||
@@ -156,7 +198,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:
|
||||||
logging.info(f"Post {post_id} already posted in guild {guild_id}, skipping.")
|
logging.debug(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))
|
||||||
@@ -170,13 +212,14 @@ async def post_reddit():
|
|||||||
else:
|
else:
|
||||||
logging.warning(f"Bot is not in guild {guild_id}, removing guild from database")
|
logging.warning(f"Bot is not in guild {guild_id}, removing guild from database")
|
||||||
cur.execute("DELETE FROM channels WHERE guild_id = ?", (guild_id,))
|
cur.execute("DELETE FROM channels WHERE guild_id = ?", (guild_id,))
|
||||||
|
cur.execute("DELETE FROM posted WHERE guild_id = ?", (guild_id,))
|
||||||
con.commit()
|
con.commit()
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
logging.warning(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:
|
||||||
logging.error(f"Error sending to channel {channel_id}: {e}")
|
logging.error(f"Error sending to channel {channel_id}: {e}")
|
||||||
logging.info("All posts processed.")
|
logging.debug("All posts processed.")
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
logging.error(f"Database error: {e}")
|
logging.error(f"Database error: {e}")
|
||||||
|
|
||||||
@@ -201,27 +244,6 @@ async def check_guild(guild_id):
|
|||||||
logging.warning(f"Bot doesn't have access to guild {guild_id}")
|
logging.warning(f"Bot doesn't have access to guild {guild_id}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@bot.event
|
|
||||||
async def on_guild_remove(guild):
|
|
||||||
if isinstance(guild, fluxer.Guild):
|
|
||||||
guild_id = guild.id
|
|
||||||
logging.info(f"Removed from guild: {guild.name} (ID: {guild_id}), removing from database")
|
|
||||||
else:
|
|
||||||
if guild.get("unavailable"):
|
|
||||||
logging.info(f"Guild {guild.get('id')} is temporarily unavailable, ignoring")
|
|
||||||
return
|
|
||||||
guild_id = int(guild["id"])
|
|
||||||
logging.info(f"Removed from guild {guild_id}, removing from database")
|
|
||||||
try:
|
|
||||||
con = sqlite3.connect('data/ich_iel-bot.db')
|
|
||||||
cur = con.cursor()
|
|
||||||
cur.execute("DELETE FROM channels WHERE guild_id = ?", (guild_id,))
|
|
||||||
cur.execute("DELETE FROM posted WHERE guild_id = ?", (guild_id,))
|
|
||||||
con.commit()
|
|
||||||
logging.info(f"Guild {guild_id} removed from database successfully")
|
|
||||||
except sqlite3.Error as e:
|
|
||||||
logging.error(f"Database error while removing guild {guild_id}: {e}")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
logging.info("Starting bot...")
|
logging.info("Starting bot...")
|
||||||
asyncio.run(init_db())
|
asyncio.run(init_db())
|
||||||
|
|||||||
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
fluxer.py
|
fluxer.py
|
||||||
requests
|
requests
|
||||||
asyncio
|
asyncio
|
||||||
dotenv
|
dotenv
|
||||||
|
aiohttp
|
||||||
Reference in New Issue
Block a user