-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnythingMayHappenBot.py
136 lines (108 loc) · 7.19 KB
/
AnythingMayHappenBot.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
import discord
from discord.ext import commands
import re
from functions import mysql_push, env_pull, DEBUG_MESSAGE, add_new_link, connector_function, check_mysql_connection
from sqlalchemy.ext.declarative import declarative_base
# Discord Bot Setup
intents = discord.Intents.all() # Defines the intents for the bot
client = discord.Client(intents=intents) # Defines the client for the bot
bot = commands.Bot(command_prefix='! ', intents=intents) # Defines the bot itself
# SQLAlchemy Setup
Base = declarative_base()
def env_use():
SQL_HOST, SQL_USER, SQL_PASS, SQL_DATABASE, DISCORD_CHANNEL_ID, GUILD_ID, ALL_CATEGORIES_CHANNEL_ID, TOKEN, SUBREDDIT_CHANNEL, EXTENSION_CHANNEL, VIDEOS_CHANNEL, PHONE_APPS_CHANNEL, GITHUB_CHANNEL, BLENDER_ADDONS_CHANNEL, EBOOKS_PDF_CHANNEL, VIDEO_GAMES_CHANNEL, AI_WEBSITE_CHANNEL, SOFTWARE_CHANNEL, HIDDEN_USERS, DEBUG_MODE, SECRET_TOKEN = env_pull()
return SQL_HOST, SQL_USER, SQL_PASS, SQL_DATABASE, DISCORD_CHANNEL_ID, GUILD_ID, ALL_CATEGORIES_CHANNEL_ID, TOKEN, SUBREDDIT_CHANNEL, EXTENSION_CHANNEL, VIDEOS_CHANNEL, PHONE_APPS_CHANNEL, GITHUB_CHANNEL, BLENDER_ADDONS_CHANNEL, EBOOKS_PDF_CHANNEL, VIDEO_GAMES_CHANNEL, AI_WEBSITE_CHANNEL, SOFTWARE_CHANNEL, HIDDEN_USERS, DEBUG_MODE, SECRET_TOKEN
SQL_HOST, SQL_USER, SQL_PASS, SQL_DATABASE, DISCORD_CHANNEL_ID, GUILD_ID, ALL_CATEGORIES_CHANNEL_ID, TOKEN, SUBREDDIT_CHANNEL, EXTENSION_CHANNEL, VIDEOS_CHANNEL, PHONE_APPS_CHANNEL, GITHUB_CHANNEL, BLENDER_ADDONS_CHANNEL, EBOOKS_PDF_CHANNEL, VIDEO_GAMES_CHANNEL, AI_WEBSITE_CHANNEL, SOFTWARE_CHANNEL, HIDDEN_USERS, DEBUG_MODE, SECRET_TOKEN = env_use()
# List of channels where the bot is allowed to send messages
allowed_channels = [int(DISCORD_CHANNEL_ID), int(ALL_CATEGORIES_CHANNEL_ID), int(SUBREDDIT_CHANNEL), int(EXTENSION_CHANNEL), int(VIDEOS_CHANNEL), int(PHONE_APPS_CHANNEL), int(GITHUB_CHANNEL), int(BLENDER_ADDONS_CHANNEL), int(EBOOKS_PDF_CHANNEL), int(AI_WEBSITE_CHANNEL), int(SOFTWARE_CHANNEL), int(VIDEO_GAMES_CHANNEL)]
@bot.event
async def on_ready():
SQL_HOST, SQL_USER, SQL_PASS, SQL_DATABASE, DISCORD_CHANNEL_ID, GUILD_ID, ALL_CATEGORIES_CHANNEL_ID_int, TOKEN, SUBREDDIT_CHANNEL, EXTENSION_CHANNEL, VIDEOS_CHANNEL, PHONE_APPS_CHANNEL, GITHUB_CHANNEL, BLENDER_CHANNEL, EBOOKS_PDF_CHANNEL, VIDEO_GAMES_CHANNEL, AI_WEBSITE_CHANNEL, SOFTWARE_CHANNEL, HIDDEN_USERS, DEBUG_MODE, SECRET_TOKEN = env_use()
print("Bot is ready.")
if DEBUG_MODE:
print(f"Bots name: {bot.user.name}")
print(f"Bots ID: {bot.user.id}")
DEBUG_MESSAGE(SQL_HOST, SQL_USER, SQL_PASS, SQL_DATABASE, DISCORD_CHANNEL_ID, GUILD_ID, ALL_CATEGORIES_CHANNEL_ID_int, TOKEN, SUBREDDIT_CHANNEL, EXTENSION_CHANNEL, VIDEOS_CHANNEL, PHONE_APPS_CHANNEL, GITHUB_CHANNEL, BLENDER_ADDONS_CHANNEL, EBOOKS_PDF_CHANNEL, VIDEO_GAMES_CHANNEL, AI_WEBSITE_CHANNEL, SOFTWARE_CHANNEL, HIDDEN_USERS, DEBUG_MODE, SECRET_TOKEN)
# Check MySQL connection during startup
await check_mysql_connection(SQL_HOST, SQL_USER, SQL_PASS, SQL_DATABASE, bot)
classified_user = HIDDEN_USERS
print(classified_user)
@bot.event
async def on_message(message):
if message.author == bot.user:
if DEBUG_MODE:
print("Bot message detected. Ignoring...")
return
# Split the message into lines to handle multiple links
lines = message.content.split('\n')
# if message.content.startswith("htttp://") or message.content.startswith("https://"):
# await message.delete()
for line in lines:
line = line.strip()
if not line:
continue
command_tag = ""
if line.startswith("!setchannel"):
channel_id = int(line.split()[1])
allowed_channels.append(channel_id)
await message.channel.send(f"Channel with ID {channel_id} has been added to the list of allowed channels.")
continue
elif message.channel.id not in allowed_channels:
continue
if line.startswith("!delete"):
command_tag = "!delete"
url = line.split()[1]
else:
url = line
# Check if the link is a valid URL
match = re.match(r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)', url)
match_2 = re.match(r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]+\.[a-zA-Z0-9()]{2,6}[/a-zA-Z0-9._?=\-&]+', url)
link = url.replace("http://", "").replace("https://", "")
if match or match_2:
try:
await message.delete() # Delete the user's message if it contains a valid link
print(f"Deleted message from {message.author} in {message.channel}")
except discord.Forbidden:
break
except discord.NotFound:
break
except discord.HTTPException as e:
break
engine = await connector_function(SQL_HOST, SQL_USER, SQL_PASS, SQL_DATABASE)
if not engine:
return
Base = declarative_base()
class Link(Base):
__tablename__ = 'link'
id = Column(Integer, primary_key=True)
link = Column(String)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
if command_tag == "!delete":
result = session.query(Link).filter_by(link=link).first()
if not result:
await message.channel.send(f"{url} is **not present** in the database {message.author.mention}.", delete_after=5)
print(f"{url} is not present in the database {message.author.name}.")
else:
session.delete(result)
session.commit()
await message.channel.send(f"{url} has been **removed** from the database {message.author.mention}.", delete_after=5)
print(f"{url} has been **removed** from the database {message.author.name}.")
continue # Move to the next link
result = session.query(Link).filter_by(link=link).first()
if not result:
new_link = Link(link=link)
session.add(new_link)
session.commit()
await add_new_link(link, session, Link, message, bot, DEBUG_MODE, command_tag, url, message.author, int(DISCORD_CHANNEL_ID), int(GITHUB_CHANNEL), int(BLENDER_ADDONS_CHANNEL), int(ALL_CATEGORIES_CHANNEL_ID), classified_user, int(PHONE_APPS_CHANNEL), int(VIDEOS_CHANNEL), int(EXTENSION_CHANNEL), int(SUBREDDIT_CHANNEL), int(EBOOKS_PDF_CHANNEL), int(VIDEO_GAMES_CHANNEL), int(AI_WEBSITE_CHANNEL), int(SOFTWARE_CHANNEL), message.author.mention, message.author.name)
else:
target_channel = bot.get_channel(int(DISCORD_CHANNEL_ID))
if message.author.id in classified_user:
await target_channel.send(f"{url} is already in the database **user classified**.", delete_after=5)
else:
await target_channel.send(f"{url} is already in the database {message.author.mention}.", delete_after=5)
print(f"{url} is already in the database.")
bot.run(TOKEN)