Skip to content

Commit

Permalink
add other features
Browse files Browse the repository at this point in the history
  • Loading branch information
arenasys committed Jun 6, 2023
1 parent 0270189 commit 88715ee
Show file tree
Hide file tree
Showing 5 changed files with 371 additions and 7 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

2. Start with `python bot.py`

3. Set favourites channel and emoji threshold
3. Set favourites channel and emoji threshold, and enable favourites for some channels.
`^setup #favourites 3`

4. Enable favourites for a specific channel
`^add #general`
`^add #memes`
`^add #memes`

4. Enable archive channel and add archived channels.
`$setup #archive`
`$add #memes`
178 changes: 178 additions & 0 deletions archive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import discord
import db as database

db = database.load('archive.db')
if not db:
db = {"guilds": {}}
database.dump(db, 'archive.db')

async def initialize(client):
return

async def setup_archive(client, guild_id, archive_id):
if str(guild_id) in db["guilds"]:
return

guild = client.get_guild(guild_id)
archive = guild.get_channel(archive_id)

nominate = await archive.send("React with 📦 to enable archiving!\nReact any image in your archive with ❌ to delete.")
await nominate.add_reaction('📦')
await nominate.pin()

data = {
"nominate": nominate.id,
"archive": archive_id,
"users": {},
"channels": []
}

db["guilds"][str(guild_id)] = data
database.dump(db, 'archive.db')

async def add_channel(client, guild_id, channel_id):
if not str(guild_id) in db["guilds"]:
return

db["guilds"][str(guild_id)]["channels"] += [channel_id]
database.dump(db, 'archive.db')

async def create_archive(client, guild_id, user_id):
guild = client.get_guild(guild_id)
user = guild.get_member(user_id)
name = user.display_name

await delete_archive(client, guild_id, user_id)

guild_data = db["guilds"][str(guild_id)]

archive_id = guild_data["archive"]
archive = guild.get_channel(archive_id)

message = await archive.send(name + "'s archive")
thread = await archive.create_thread(name=name, message=message, type=discord.ChannelType.public_thread)
guild_data["users"][str(user_id)] = thread.id
database.dump(db, 'archive.db')

channels = guild_data["channels"]

for channel_id in channels:
channel = client.get_guild(guild_id).get_channel(channel_id)

print("start", channel.name)
history = [m async for m in channel.history(limit=None)][::-1]
for m in history:
if m.author.id == user_id:
for a in m.attachments:
if "image" in a.content_type or "video" in a.content_type:
await thread.send(a.url)
for e in m.embeds:
if e.type == "image" or e.type == "video":
await thread.send(e.url)
print("finish", channel.name)

async def delete_archive(client, guild_id, user_id):
guild_data = db["guilds"][str(guild_id)]

if not str(user_id) in guild_data["users"]:
return

thread_id = guild_data["users"][str(user_id)]

guild = client.get_guild(guild_id)

if thread_id:
archive_id = guild_data["archive"]
archive = guild.get_channel(archive_id)

await archive.get_partial_message(thread_id).delete()
thread = archive.get_thread(thread_id)

if thread:
await thread.delete()

del guild_data["users"][str(user_id)]
database.dump(db, 'archive.db')

async def on_message(client, message):
await on_command(client, message)

if not message.attachments and not message.embeds:
return

guild = message.guild

if not str(guild.id) in db["guilds"]:
return

guild_data = db["guilds"][str(guild.id)]

if not message.channel.id in guild_data["channels"]:
return

if not str(message.author.id) in guild_data["users"]:
return

archive_id = guild_data["archive"]
archive = guild.get_channel(archive_id)

thread_id = guild_data["users"][str(message.author.id)]
thread = archive.get_thread(thread_id)

for a in message.attachments:
if "image" in a.content_type or "video" in a.content_type:
await thread.send(a.url)
for e in message.embeds:
if e.type == "image" or e.type == "video":
await thread.send(e.url)

async def on_reaction_add(client, payload):
guild_id = payload.guild_id

if not str(guild_id) in db["guilds"]:
return

guild_data = db["guilds"][str(guild_id)]

if payload.message_id == guild_data["nominate"] and str(payload.emoji) == "📦":
await create_archive(client, guild_id, payload.user_id)
return

if str(payload.emoji) == "❌" and str(payload.user_id) in guild_data["users"]:
if payload.channel_id == guild_data["users"][str(payload.user_id)]:
thread_id = payload.channel_id
thread = client.get_guild(guild_id).get_thread(thread_id)
message = thread.get_partial_message(payload.message_id)

await message.delete()

async def on_reaction_remove(client, payload):
guild_id = payload.guild_id

if not str(guild_id) in db["guilds"]:
return

guild_data = db["guilds"][str(guild_id)]

if payload.message_id != guild_data["nominate"]:
return

if str(payload.emoji) != "📦":
return

await delete_archive(client, guild_id, payload.user_id)

async def on_command(client, message):
if not message.author.guild_permissions.administrator:
return

command = message.content.lower()

if command.startswith("$setup"):
archive_id = int(command.split()[1][2:-1])
await setup_archive(client, message.guild.id, archive_id)

if command.startswith("$add"):
channel_id = int(command.split()[1][2:-1])
await add_channel(client, message.guild.id, channel_id)

26 changes: 25 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import discord
import archive
import hearts
import params

client_intents = discord.Intents()
client_intents.guilds = True
Expand All @@ -10,9 +12,9 @@
client_intents.guild_reactions = True

client = discord.Client(intents = client_intents, max_messages=100000)
tree = discord.app_commands.CommandTree(client)

token = ""

with open('token.txt') as f:
token = f.read()

Expand All @@ -23,6 +25,10 @@ async def on_ready():
a = discord.Activity(type=discord.ActivityType.watching, name="for ❤️")
await client.change_presence(status=discord.Status.online, activity=a)

await archive.initialize(client)

await tree.sync()

@client.event
async def on_message(request):
if type(request.channel) == discord.DMChannel: #ignore dms
Expand All @@ -33,13 +39,31 @@ async def on_message(request):

if type(request.author) != discord.member.Member: #ignore non guild users
return

reply = await archive.on_message(client, request)
if reply:
await request.reply(reply)

reply = await hearts.on_message(client, request)
if reply:
await request.reply(reply)

@client.event
async def on_raw_reaction_add(payload):
await archive.on_reaction_add(client, payload)
await hearts.on_reaction_add(client, payload)


@client.event
async def on_raw_reaction_remove(payload):
await archive.on_reaction_remove(client, payload)

@tree.context_menu(name="Parameters", auto_locale_strings=False)
async def parameters(interaction: discord.Interaction, message: discord.Message):
await params.parameters(interaction, message, False)

@tree.context_menu(name="Raw Parameters", auto_locale_strings=False)
async def raw_parameters(interaction: discord.Interaction, message: discord.Message):
await params.parameters(interaction, message, True)

client.run(token)
33 changes: 31 additions & 2 deletions hearts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import requests
from bs4 import BeautifulSoup

EMOJIS = "❤️ 💖 ⭐ 👅 🥵 😳 😍 💦 👍 👌 😭 💢 ♥️ ❤️‍🔥 💕 💗 💓 💞 💟 😻 🫶 🖤 💙 🤎 💝 💚 😘 ❤️‍🩹 🧡 💜 🤍 💛 💘 🔥 💯 "
EMOJIS = "❤️ 💖 ⭐ 👅 🥵 😳 😍 💦 👍 👌 😭 💢 ♥️ ❤️‍🔥 💕 💗 💓 💞 💟 😻 🫶 🖤 💙 🤎 💝 💚 😘 ❤️‍🩹 🧡 💜 🤍 💛 💘 🔥 💯 🌊 👍🏻 🍑 🙌 🎉 🐱 🍦 😻 "
NITRO = []

db = database.load('hearts.db')
if not db:
Expand Down Expand Up @@ -64,6 +65,9 @@ async def on_reaction_add(client, payload):
return

guild_data = db["guilds"][str(guild_id)]
hearts_id = guild_data["hearts"]

await check_for_delete(client, payload)

if not payload.channel_id in guild_data["channels"]:
return
Expand All @@ -77,6 +81,30 @@ async def on_reaction_add(client, payload):

await check_for_reacts(client, message)

async def check_for_delete(client, payload):
if str(payload.emoji) != "❌":
return

guild_id = payload.guild_id
guild_data = db["guilds"][str(guild_id)]
hearts_id = guild_data["hearts"]
hearts = client.get_guild(guild_id).get_channel(hearts_id)

if payload.channel_id != hearts_id:
return

message = await hearts.fetch_message(payload.message_id)
embed = message.embeds
if not embed:
return
embed = embed[0]
author_id = embed.author.icon_url.split("/")[4]

if author_id != str(payload.user_id):
return

await message.delete()

async def check_for_reacts(client, message):
guild_data = db["guilds"][str(message.guild.id)]

Expand All @@ -88,7 +116,8 @@ async def check_for_reacts(client, message):

total = []
for r in message.reactions:
if not str(r.emoji) + " " in EMOJIS:
valid = str(r.emoji) + " " in EMOJIS or getattr(r.emoji, "name", "") in NITRO
if not valid:
continue
total += [user.id async for user in r.users()]
total = len(set(total))
Expand Down
Loading

0 comments on commit 88715ee

Please sign in to comment.