-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
106 lines (93 loc) · 3.68 KB
/
Copy pathbot.py
File metadata and controls
106 lines (93 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import asyncio
import sqlite3
from discord.ext import commands
from discord import Intents
# Load environment variables
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
FEED_CHANNELS_ENV = os.getenv('FEED_CHANNELS')
if not DISCORD_TOKEN or not FEED_CHANNELS_ENV:
print('Please set the DISCORD_TOKEN and FEED_CHANNELS environment variables.')
exit(1)
# Parse FEED_CHANNELS environment variable into a dictionary {feed_name: channel_id}
FEED_CHANNELS = {}
try:
for pair in FEED_CHANNELS_ENV.split(','):
name, cid = pair.split('=')
FEED_CHANNELS[name.strip()] = int(cid.strip())
except ValueError:
print('Error parsing FEED_CHANNELS environment variable. Please use the format feedName=channelID,anotherFeedName=anotherChannelID')
exit(1)
# Initialize the bot
intents = Intents.default()
intents.message_content = True
intents.messages = True
bot = commands.Bot(command_prefix='!', intents=intents)
# Initialize SQLite database
conn = sqlite3.connect('messages.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS messages
(message_id INTEGER PRIMARY KEY,
feed_name TEXT,
title TEXT,
content TEXT,
link TEXT,
pubDate TEXT,
author TEXT)''')
conn.commit()
def add_message_to_db(feed_name, message):
# Ignore messages without content
if not message.content.strip():
return
# The first line is the title, rest is content
lines = message.content.strip().split('\n', 1)
if not lines:
return # Ignore empty messages
title = lines[0]
content = lines[1] if len(lines) > 1 else ''
link = f'https://discordapp.com/channels/{message.guild.id}/{message.channel.id}/{message.id}'
pubDate = message.created_at.isoformat()
author = message.author.name.capitalize()
message_id = message.id
c.execute('''INSERT OR REPLACE INTO messages
(message_id, feed_name, title, content, link, pubDate, author)
VALUES (?, ?, ?, ?, ?, ?, ?)''',
(message_id, feed_name, title, content, link, pubDate, author))
conn.commit()
def delete_message_from_db(message_id):
c.execute("DELETE FROM messages WHERE message_id = ?", (message_id,))
conn.commit()
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
for feed_name, channel_id in FEED_CHANNELS.items():
channel = bot.get_channel(channel_id)
if channel is None:
print(f'Channel with ID {channel_id} for feed "{feed_name}" not found.')
continue
# Fetch previous messages
async for message in channel.history(limit=None, oldest_first=True):
if message.author.bot:
continue # Skip messages from bots
add_message_to_db(feed_name, message)
print('Loaded previous messages.')
@bot.event
async def on_message(message):
for feed_name, channel_id in FEED_CHANNELS.items():
if message.channel.id == channel_id and not message.author.bot:
add_message_to_db(feed_name, message)
break # No need to check other feeds
@bot.event
async def on_message_edit(before, after):
for feed_name, channel_id in FEED_CHANNELS.items():
if after.channel.id == channel_id and not after.author.bot:
add_message_to_db(feed_name, after)
break # No need to check other feeds
@bot.event
async def on_message_delete(message):
for feed_name, channel_id in FEED_CHANNELS.items():
if message.channel.id == channel_id and not message.author.bot:
delete_message_from_db(message.id)
break # No need to check other feeds
if __name__ == "__main__":
bot.run(DISCORD_TOKEN)