Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eduu code reorganized for package format #264

Merged
merged 15 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/run-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Set up configuration
run: |
curl https://nekobin.com/raw/ribemororo -o config.py
curl https://nekobin.com/raw/kumagetaya -o eduu/config.py
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Start the bot
run: |
python bot.py test
python -m eduu test
38 changes: 29 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
# Cache
__pycache__/
.spyproject/
.vscode/

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# PyCharm project
.idea/
.venv/
*.session-journal
*.session
*.db
config.ini
config.py

# Visual Studio Code workspace settings
.vscode

# Session files
eduu/*.session*

# Database
eduu/database/*.db

# Bot personal config file
eduu/config.ini
eduu/config.py

# Misc
unknown_errors.txt
downloads/
eduu/downloads/
.spyproject/
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ Setup

2. Install the required modules from the requirements.txt with ``pip3 install -Ur requirements.txt``.
3. Go to https://my.telegram.org/apps and create a new app.
4. Create a new ``config.py`` file from the ``config.py.example`` file (``cp config.py.example config.py``).
4. Create a new ``config.py`` file from the ``config.py.example`` file (``cp eduu/config.py.example eduu/config.py``).
5. Place your token, IDs and api keys to your config.py file.


Running
-------
- To run the bot you just need to run ``python3 bot.py``. In case you installed from a virtualenv, run ``. .venv/bin/activate`` before this.
- To run the bot you just need to run ``python3 -m eduu``. In case you installed from a virtualenv, run ``. .venv/bin/activate`` before this.
- Running it on `screen <https://en.wikipedia.org/wiki/GNU_Screen>`__ or `tmux <https://en.wikipedia.org/wiki/Tmux>`__ is highly recommended if you want to keep the bot running on a server.


Expand Down
51 changes: 0 additions & 51 deletions bot.py

This file was deleted.

31 changes: 0 additions & 31 deletions config.py.example

This file was deleted.

17 changes: 0 additions & 17 deletions consts.py

This file was deleted.

5 changes: 5 additions & 0 deletions eduu/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""EduuRobot Core!"""
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2021 Amano Team

__version__ = "2.0.0-beta"
62 changes: 62 additions & 0 deletions eduu/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2021 Amano Team

import asyncio
import logging
import sys

import pyrogram
from pyrogram import Client, idle
from pyrogram.errors import BadRequest

import eduu
from eduu.config import API_HASH, API_ID, TOKEN, disabled_plugins, log_chat
from eduu.utils import del_restarted, get_restarted, shell_exec
from eduu.utils.consts import http

client = Client(
session_name="bot",
app_version=f"EduuRobot v{eduu.__version__}",
api_id=API_ID,
api_hash=API_HASH,
bot_token=TOKEN,
workers=24,
parse_mode="html",
plugins=dict(root="eduu.plugins", exclude=disabled_plugins),
)


async def main() -> None:
await client.start()

# Saving commit number
client.version_code = int((await shell_exec("git rev-list --count HEAD"))[0])

client.me = await client.get_me()

if "test" not in sys.argv:
wr = get_restarted()
del_restarted()

start_message = (
"<b>EduuRobot started!</b>\n\n"
f"<b>Version:</b> <code>v{eduu.__version__} ({client.version_code})</code>\n"
f"<b>Pyrogram:</b> <code>v{pyrogram.__version__}</code>"
)

try:
await client.send_message(chat_id=log_chat, text=start_message)
if wr:
await client.edit_message_text(wr[0], wr[1], "Restarted successfully!")
except BadRequest:
logging.warning("Unable to send message to log_chat.")

await idle()

await http.aclose()
await client.stop()


loop = asyncio.get_event_loop()

loop.run_until_complete(main())
28 changes: 28 additions & 0 deletions eduu/config.py.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import List, Optional

# Bot token from Bot Father
TOKEN: str = "YOUR_TOKEN_HERE"

# Your API ID and API HASH
# Get it from https://my.telegram.org/apps
API_ID: int = 123123
API_HASH: str = ""


# Chat used for logs
log_chat: int = 123456789

# Sudoers and super sudoers
super_sudoers: List[int] = [123456789]
sudoers: List[int] = [987654321]
sudoers.append(super_sudoers)

# Prefixes for commands
# e.g: /command and !command
prefix: List[str] = ["/", "!"]

# List of disabled plugins
disabled_plugins: List[str] = []

# API Keys (Optional)
TENOR_API_KEY: Optional[str] = ""
9 changes: 9 additions & 0 deletions eduu/database/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""EduuRobot Database!"""
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2021 Amano Team

from typing import List

from .database_handler import db, dbc

__all__: List[str] = ["db", "dbc"]
5 changes: 4 additions & 1 deletion dbh.py → eduu/database/database_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2021 Amano Team

import sqlite3

db = sqlite3.connect("eduu.db")
db = sqlite3.connect("eduu/database/eduu.db")
dbc = db.cursor()


Expand Down
3 changes: 3 additions & 0 deletions eduu/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""EduuRobot Plugins!"""
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2021 Amano Team
15 changes: 8 additions & 7 deletions plugins/admin.py → eduu/plugins/admin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2021 Amano Team

import asyncio
from typing import Optional

from config import prefix
from pyrogram import Client, filters
from pyrogram.types import ChatPermissions, Message, User

from dbh import db, dbc
from localization import use_chat_lang
from utils import commands, require_admin, time_extract
from eduu.config import prefix
from eduu.database import db, dbc
from eduu.utils import commands, require_admin, time_extract
from eduu.utils.consts import admin_status
from eduu.utils.localization import use_chat_lang


async def get_reason_text(c: Client, m: Message) -> Message:
Expand All @@ -22,9 +26,6 @@ async def get_reason_text(c: Client, m: Message) -> Message:
return reason


admin_status = ["creator", "administrator"]


def check_if_antichannelpin(chat_id):
dbc.execute("SELECT antichannelpin FROM groups WHERE chat_id = ?", (chat_id,))
res = dbc.fetchone()[0]
Expand Down
11 changes: 7 additions & 4 deletions plugins/cats.py → eduu/plugins/cats.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from config import prefix
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2021 Amano Team

from pyrogram import Client, filters
from pyrogram.types import Message

from consts import http
from localization import use_chat_lang
from utils import commands
from eduu.config import prefix
from eduu.utils import commands
from eduu.utils.consts import http
from eduu.utils.localization import use_chat_lang


@Client.on_message(filters.command("cat", prefix))
Expand Down
8 changes: 6 additions & 2 deletions plugins/chats.py → eduu/plugins/chats.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2021 Amano Team

from pyrogram import Client
from pyrogram.types import Message

from utils import add_chat, chat_exists
from eduu.utils import add_chat, chat_exists

# This is the first plugin run to guarantee that the actual chat is initialized in the DB.
# This is the first plugin run to guarantee
# that the actual chat is initialized in the DB.


@Client.on_message(group=-1)
Expand Down
9 changes: 6 additions & 3 deletions plugins/cmds_list.py → eduu/plugins/cmds_list.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2021 Amano Team

from pyrogram import Client, filters
from pyrogram.types import (
CallbackQuery,
Expand All @@ -6,8 +9,8 @@
Message,
)

from localization import use_chat_lang
from utils import commands
from eduu.utils import commands
from eduu.utils.localization import use_chat_lang


def gen_categories_kb(strings_manager):
Expand Down Expand Up @@ -67,7 +70,7 @@ async def show_help(c: Client, m: Message, strings):
[
InlineKeyboardButton(
strings("start_chat", context="start"),
url=f"https://t.me/{c._bot_username}?start=help",
url=f"https://t.me/{c.me.username}?start=help",
)
]
]
Expand Down