fix posting with multiple guilds in DB
This commit is contained in:
30
main.py
30
main.py
@@ -54,16 +54,21 @@ async def get_latest_post(subreddit):
|
|||||||
async def init_db():
|
async def init_db():
|
||||||
try:
|
try:
|
||||||
con = sqlite3.connect('data/ich_iel-bot.db')
|
con = sqlite3.connect('data/ich_iel-bot.db')
|
||||||
con = con.cursor()
|
cur = con.cursor()
|
||||||
con.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)")
|
||||||
con.execute("CREATE TABLE IF NOT EXISTS posted (guild_id INTEGER, post_id VARCHAR(255) PRIMARY KEY)")
|
cur.execute("CREATE TABLE IF NOT EXISTS posted (guild_id INTEGER, post_id VARCHAR(255), PRIMARY KEY (guild_id, post_id))")
|
||||||
con.connection.commit()
|
con.commit()
|
||||||
print("Database initialized successfully")
|
print("Database initialized successfully")
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
print(f"Database initialization error: {e}")
|
print(f"Database initialization error: {e}")
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def setChannel(message):
|
async def setChannel(message):
|
||||||
|
""" Is not implement yet according to fluxer-py on Fluxer
|
||||||
|
if not message.author.guild_permissions.administrator or not message.author.guild_permissions.manage_channels:
|
||||||
|
await message.channel.send("You need administrator permissions to use this command.")
|
||||||
|
return
|
||||||
|
"""
|
||||||
args = message.content.split()
|
args = message.content.split()
|
||||||
if len(args) == 2:
|
if len(args) == 2:
|
||||||
try:
|
try:
|
||||||
@@ -102,12 +107,13 @@ async def post_reddit():
|
|||||||
if not rows:
|
if not rows:
|
||||||
print("No channels set")
|
print("No channels set")
|
||||||
return
|
return
|
||||||
for title, image_url in posts:
|
for guild_id, channel_id in rows:
|
||||||
post_id_match = re.search(r"\/([^\/]+)\.(jpg|png|jpeg|gif)$", image_url)
|
print(f"Processing guild {guild_id}")
|
||||||
if not post_id_match:
|
for title, image_url in posts:
|
||||||
continue
|
post_id_match = re.search(r"\/([^\/]+)\.(jpg|png|jpeg|gif)$", image_url)
|
||||||
post_id = post_id_match.group(1)
|
if not post_id_match:
|
||||||
for guild_id, channel_id in rows:
|
continue
|
||||||
|
post_id = post_id_match.group(1)
|
||||||
is_posted = con.execute("SELECT post_id FROM posted WHERE guild_id = ? AND post_id = ?", (guild_id, post_id)).fetchone()
|
is_posted = con.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.")
|
print(f"Post {post_id} already posted in guild {guild_id}, skipping.")
|
||||||
@@ -119,12 +125,12 @@ async def post_reddit():
|
|||||||
con.execute("INSERT OR REPLACE INTO posted (guild_id, post_id) VALUES (?, ?)", (guild_id, post_id))
|
con.execute("INSERT OR REPLACE INTO posted (guild_id, post_id) VALUES (?, ?)", (guild_id, post_id))
|
||||||
con.connection.commit()
|
con.connection.commit()
|
||||||
print(f"Posted to channel {channel_id} in guild {guild_id}")
|
print(f"Posted to channel {channel_id} in guild {guild_id}")
|
||||||
return
|
break
|
||||||
else:
|
else:
|
||||||
print(f"Channel {channel_id} not found for guild {guild_id}")
|
print(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}")
|
print(f"Error sending to channel {channel_id}: {e}")
|
||||||
print("Alle gefundenen Posts wurden bereits gepostet.")
|
print("All posts processed.")
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
print(f"Database error: {e}")
|
print(f"Database error: {e}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user