guild_id added to database
This commit is contained in:
34
main.py
34
main.py
@@ -45,11 +45,14 @@ async def setChannel(message):
|
|||||||
if len(args) == 2:
|
if len(args) == 2:
|
||||||
try:
|
try:
|
||||||
channel_id = int(args[1])
|
channel_id = int(args[1])
|
||||||
|
channel = await bot.fetch_channel(channel_id)
|
||||||
|
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}")
|
||||||
try:
|
try:
|
||||||
con = sqlite3.connect('ich_iel-bot.db')
|
con = sqlite3.connect('ich_iel-bot.db')
|
||||||
con = con.cursor()
|
con = con.cursor()
|
||||||
con.execute("CREATE TABLE IF NOT EXISTS channels (key TEXT PRIMARY KEY, value TEXT)")
|
con.execute("CREATE TABLE IF NOT EXISTS channels (guild_id INTEGER PRIMARY KEY, channel_id INTEGER)")
|
||||||
con.execute("INSERT OR REPLACE INTO channels (key, value) VALUES (?, ?)", ("channel_id", str(channel_id)))
|
con.execute("INSERT OR REPLACE INTO channels (guild_id, channel_id) VALUES (?, ?)", (guild_id, channel_id))
|
||||||
con.connection.commit()
|
con.connection.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:
|
||||||
@@ -61,7 +64,7 @@ async def setChannel(message):
|
|||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def version(message):
|
async def version(message):
|
||||||
await message.channel.send("Version 0.1 is running")
|
await message.channel.send("Version 0.2 is running")
|
||||||
|
|
||||||
async def post_reddit():
|
async def post_reddit():
|
||||||
subreddit = "ich_iel" # I guess I could make this editable through the env file, so it's not just a bot for ich_iel lol
|
subreddit = "ich_iel" # I guess I could make this editable through the env file, so it's not just a bot for ich_iel lol
|
||||||
@@ -70,17 +73,20 @@ async def post_reddit():
|
|||||||
try:
|
try:
|
||||||
con = sqlite3.connect('ich_iel-bot.db') # i like sqlite
|
con = sqlite3.connect('ich_iel-bot.db') # i like sqlite
|
||||||
con = con.cursor()
|
con = con.cursor()
|
||||||
con.execute("SELECT value FROM channels WHERE key = ?", ("channel_id",))
|
con.execute("SELECT guild_id, channel_id FROM channels")
|
||||||
result = con.fetchone()
|
rows = con.fetchall()
|
||||||
if result:
|
if not rows:
|
||||||
channel_id = int(result[0])
|
print("No channels set")
|
||||||
channel = await bot.fetch_channel(channel_id)
|
return
|
||||||
if channel:
|
for guild_id, channel_id in rows:
|
||||||
await channel.send(f"{title}\n{image_url}")
|
try:
|
||||||
else:
|
channel = await bot.fetch_channel(int(channel_id))
|
||||||
print("Channel not found")
|
if channel:
|
||||||
else:
|
await channel.send(f"{title}\n{image_url}")
|
||||||
print("No channel set")
|
else:
|
||||||
|
print(f"Channel {channel_id} not found for guild {guild_id}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error sending to channel {channel_id}: {e}")
|
||||||
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