Browse Source

Create Database and add Videonotes Module

Nicolas "Colin" Neht 4 months ago
parent
commit
d80650e3cd
4 changed files with 99 additions and 4 deletions
  1. 1
    0
      .gitignore
  2. 2
    2
      HELP.md
  3. 15
    2
      pyrobot/__init__.py
  4. 81
    0
      pyrobot/modules/videonotes.py

+ 1
- 0
.gitignore View File

@@ -1,5 +1,6 @@
1 1
 # Custom ignores
2 2
 *.session
3
+*.db
3 4
 
4 5
 # Byte-compiled / optimized / DLL files
5 6
 __pycache__/

+ 2
- 2
HELP.md View File

@@ -8,10 +8,10 @@ This program is a Userbot for the [Telegram Messenger](https://telegram.org) bas
8 8
 
9 9
 * Check if your bot is running with `.alive` or `.up`
10 10
 * Mute, kick or ban people
11
-* Convert video files from any format to Telegram Gif (.mp4)*
11
+* Convert video files from any format to Telegram Gif (.mp4) <sup>(Telegram's GIFs are .mp4 files)</sup>
12 12
 * Evaluate and Execute code with `.eval` and `.exec`
13 13
 * Get information about your admins and members, as well as files.
14
-* Annoy people with memes :>
14
+* Annoy people with memes (:
15 15
 * Get a blank steam account with `?acc`
16 16
 * Check Information about a user with `.whois`. Works with Username, Userid and by reply.
17 17
 * Test your servers (or PCs) speed and ping your connection to Telegram servers.

+ 15
- 2
pyrobot/__init__.py View File

@@ -1,9 +1,11 @@
1 1
 import sys
2 2
 import os
3
+from pathlib import Path
3 4
 import logging
4 5
 from datetime import datetime
5 6
 from pyrogram import Client
6 7
 import dotenv
8
+import sqlite3
7 9
 
8 10
 # We need logging this early for our Version Check
9 11
 logging.basicConfig(
@@ -41,13 +43,24 @@ else:
41 43
     LOGS.error("The .env doesn't seem to be edited, please do so.")
42 44
     quit(1)
43 45
 
46
+# Create Database if there is none yet.
47
+PYRO_DB = str(Path(__file__).parent.parent / 'pyrobot.db')
48
+if not os._exists(PYRO_DB):
49
+    LOGS.info("Database doesn't exists, creating...")
50
+    db = sqlite3.connect(PYRO_DB)
51
+    c = db.cursor()
52
+    c.execute(
53
+        """CREATE TABLE IF NOT EXISTS video_notes (name TEXT UNIQUE ON CONFLICT FAIL, file_id TEXT UNIQUE ON CONFLICT FAIL);""")
54
+    db.commit()
55
+    db.close()
56
+    LOGS.info("Database created, continuing...")
57
+
44 58
 # Prepare the bot
45 59
 BOT = Client(
46 60
     session_name="PyroBot",
47 61
     api_id=API_ID,
48 62
     api_hash=API_HASH,
49
-    app_version=f"PyroBot \U0001f525\U0001F916 v{__version__}"
50
-)
63
+    app_version=f"PyroBot \U0001f525\U0001F916 v{__version__}")
51 64
 
52 65
 # Global Variables
53 66
 ISAFK = False

+ 81
- 0
pyrobot/modules/videonotes.py View File

@@ -0,0 +1,81 @@
1
+import os
2
+import sqlite3
3
+from time import sleep
4
+
5
+from pyrogram import Filters, Message
6
+from pyrobot import BOT, PYRO_DB
7
+
8
+# DB_PATH = str(Path(__file__).parent.parent.parent / 'pyrobot.db')
9
+
10
+# -- Constants -- #
11
+
12
+SAVE_NOTE = "INSERT OR FAIL INTO video_notes VALUES ('{}', '{}')"
13
+GET_NOTE = "SELECT file_id FROM video_notes WHERE name='{}'"
14
+REMOVE_NOTE = "DELETE FROM video_notes WHERE EXISTS (VALUES (name='{}'))"
15
+LIST_NOTES = "SELECT name FROM video_notes"
16
+
17
+# -- Constants End -- #
18
+
19
+# -- Helpers -- #
20
+
21
+def SendNote(bot: BOT, message: Message, file_id: str):
22
+    "Send a Videonote as a reply if the command was a reply"
23
+    reply_id = None
24
+
25
+    if message.reply_to_message:
26
+        reply_id = message.reply_to_message.message_id
27
+    
28
+    bot.send_video_note(
29
+        message.chat.id,
30
+        file_id,
31
+        reply_to_message_id = reply_id
32
+    )
33
+
34
+    message.delete()
35
+
36
+# -- Helpers End -- #
37
+
38
+
39
+@BOT.on_message(Filters.command("notes", ".") & Filters.me)
40
+def list_vids(bot: BOT, message: Message):
41
+    db = sqlite3.connect(PYRO_DB)
42
+    c = db.cursor()
43
+    c.execute(LIST_NOTES)
44
+    result = c.fetchall()
45
+    res = [i[0] for i in result]
46
+    notes = "**List of available Videnotes:**\n`.note [name]`\n"
47
+    for note in res:
48
+        notes += f"→ `{note}`\n"
49
+    BOT.send_message("me", notes)
50
+    message.edit("List of available Videonotes sent in Saved Mesages")
51
+    sleep(2)
52
+    message.delete()
53
+
54
+
55
+@BOT.on_message(Filters.command("savenote", ".") & Filters.me)
56
+def save_note(bot: BOT, message: Message):
57
+    cmd = message.command
58
+    try: cmd[1]
59
+    except IndexError:
60
+        message.edit("I need a name to save to...")
61
+        sleep(2)
62
+        message.delete()
63
+        return
64
+
65
+    note = BOT.send_video_note(
66
+        chat_id='me',
67
+        video_note=message.reply_to_message.download('./note.mp4'))
68
+    note.delete()
69
+    os.remove('./note.mp4')
70
+    db = sqlite3.connect(PYRO_DB)
71
+    c = db.cursor()
72
+    try:
73
+        c.execute(SAVE_NOTE.format(cmd[1], note.video_note.file_id))
74
+    except sqlite3.IntegrityError:
75
+        message.edit(f'Videonote "`{cmd[1]}`" already saved.')
76
+        return
77
+    db.commit()
78
+    db.close()
79
+    message.edit(f'Videnote saved as "`{cmd[1]}`".')
80
+    sleep(2)
81
+    message.delete()

Loading…
Cancel
Save