Skip to content

Commit

Permalink
Mode opt to switch uploader details in caption via /mode (Ns-Bots#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrvishal2k2 authored Jun 20, 2021
1 parent 972d88e commit 8536ce1
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
}
},
"addons": [
{
"plan": "heroku-postgresql",
"options": {"version": "12"}
}
],
"buildpacks": [{
"url": "https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest"
Expand Down
58 changes: 58 additions & 0 deletions database/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session

import os
import threading
import asyncio

from sqlalchemy import Column, Integer, Boolean, String, ForeignKey, UniqueConstraint, func

DATABASE_URL = os.environ.get("DATABASE_URL", "")

def start() -> scoped_session:
engine = create_engine(DATABASE_URL, client_encoding="utf8")
BASE.metadata.bind = engine
BASE.metadata.create_all(engine)
return scoped_session(sessionmaker(bind=engine, autoflush=False))

BASE = declarative_base()
SESSION = start()

INSERTION_LOCK = threading.RLock()

class Database(BASE):
__tablename__ = "database"
id = Column(String, primary_key=True)
up_name = Column(Boolean)

def __init__(self, id, up_name):
self.id = str(id)
self.up_name = up_name

Database.__table__.create(checkfirst=True)

async def update_as_name(id, mode):
with INSERTION_LOCK:
msg = SESSION.query(Database).get(str(id))
if not msg:
msg = Database(str(id), False)
else:
msg.up_name = mode
SESSION.delete(msg)
SESSION.add(msg)
SESSION.commit()

async def get_data(id):
try:
user_data = SESSION.query(Database).get(str(id))
if not user_data:
new_user = Database(str(id), False)
SESSION.add(new_user)
SESSION.commit()
user_data = SESSION.query(Database).get(str(id))
return user_data
finally:
SESSION.close()


38 changes: 37 additions & 1 deletion plugins/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from pyrogram.errors import ListenerCanceled
from database.database import *

DB_CHANNEL_ID = os.environ.get("DB_CHANNEL_ID")
OWNER_ID = os.environ.get("OWNER_ID")
BATCH = []
Expand Down Expand Up @@ -85,7 +87,28 @@ async def start(c, m, cb=False):
if msg.empty:
return await send_msg.edit(f"πŸ₯΄ Sorry bro your file was deleted by file owner or bot owner\n\nFor more help contact my owner πŸ‘‰ {owner.mention(style='md')}")

caption = f"{msg.caption.markdown}" if msg.caption else ""
caption = f"{msg.caption.markdown}\n\n\n" if msg.caption else ""
as_uploadername = (await get_data(str(chat_id))).up_name

if as_uploadername:
if chat_id.startswith('-100'):
channel = await c.get_chat(int(chat_id))
caption += "**--Uploader Details:--**\n\n"
caption += f"__πŸ“’ Channel Name:__ `{channel.title}`\n\n"
caption += f"__πŸ—£ User Name:__ @{channel.username}\n\n" if channel.username else ""
caption += f"__πŸ‘€ Channel Id:__ `{channel.id}`\n\n"
caption += f"__πŸ’¬ DC ID:__ {channel.dc_id}\n\n" if channel.dc_id else ""
caption += f"__πŸ‘ Members Count:__ {channel.members_count}\n\n" if channel.members_count else ""
else:
user = await c.get_users(int(chat_id))
caption += "**--Uploader Details:--**\n\n"
caption += f"__🦚 First Name:__ `{user.first_name}`\n\n"
caption += f"__🐧 Last Name:__ `{user.last_name}`\n\n" if user.last_name else ""
caption += f"__πŸ‘ User Name:__ @{user.username}\n\n" if user.username else ""
caption += f"__πŸ‘€ User Id:__ `{user.id}`\n\n"
caption += f"__πŸ’¬ DC ID:__ {user.dc_id}\n\n" if user.dc_id else ""


await send_msg.delete()
await msg.copy(m.from_user.id, caption=caption)

Expand Down Expand Up @@ -162,6 +185,19 @@ async def batch(c, m):

await message.edit(text=url)

@Client.on_message(filters.command('mode') & filters.incoming & filters.private)
async def set_mode(c,m):
usr = m.from_user.id
if len(m.command) > 1:
usr = m.command[1]
caption_mode = (await get_data(usr)).up_name
if caption_mode:
await update_as_name(str(usr), False)
text = "Uploader Details in Caption: **Disabled ❌**"
else:
await update_as_name(str(usr), True)
text = "Uploader Details in Caption: **Enabled βœ”οΈ**"
await m.reply_text(text, quote=True)

async def decode(base64_string):
base64_bytes = base64_string.encode("ascii")
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pyrogram==1.2.9
Pyromod
TgCrypto
psycopg2-binary
sqlalchemy==1.3.23

0 comments on commit 8536ce1

Please sign in to comment.