File size: 92,139 Bytes
056f521 |
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 |
# <============================================== IMPORTS =========================================================>
import ast
import csv
import json
import os
import re
import time
import uuid
from io import BytesIO
from telegram import (
ChatMember,
InlineKeyboardButton,
InlineKeyboardMarkup,
MessageEntity,
Update,
)
from telegram.constants import ParseMode
from telegram.error import BadRequest, Forbidden, TelegramError
from telegram.ext import CallbackQueryHandler, CommandHandler, ContextTypes
from telegram.helpers import mention_html, mention_markdown
import Database.sql.feds_sql as sql
from Mikobot import (
DRAGONS,
EVENT_LOGS,
LOGGER,
OWNER_ID,
SUPPORT_CHAT,
dispatcher,
function,
)
from Mikobot.plugins.disable import DisableAbleCommandHandler
from Mikobot.plugins.helper_funcs.alternate import send_message
from Mikobot.plugins.helper_funcs.chat_status import is_user_admin
from Mikobot.plugins.helper_funcs.extraction import (
extract_unt_fedban,
extract_user,
extract_user_fban,
)
from Mikobot.plugins.helper_funcs.string_handling import markdown_parser
# <=======================================================================================================>
FBAN_ERRORS = {
"User is an administrator of the chat",
"Chat not found",
"Not enough rights to restrict/unrestrict chat member",
"User_not_participant",
"Peer_id_invalid",
"Group chat was deactivated",
"Need to be inviter of a user to kick it from a basic group",
"Chat_admin_required",
"Only the creator of a basic group can kick group administrators",
"Channel_private",
"Not in the chat",
"Have no rights to send a message",
}
UNFBAN_ERRORS = {
"User is an administrator of the chat",
"Chat not found",
"Not enough rights to restrict/unrestrict chat member",
"User_not_participant",
"Method is available for supergroup and channel chats only",
"Not in the chat",
"Channel_private",
"Chat_admin_required",
"Have no rights to send a message",
}
# <================================================ FUNCTION =======================================================>
async def new_fed(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat = update.effective_chat
user = update.effective_user
message = update.effective_message
bot = context.bot
if chat.type != "private":
await update.effective_message.reply_text(
"Federations can only be created by privately messaging me.",
)
return
if len(message.text) == 1:
await send_message(
update.effective_message,
"Please write the name of the federation!",
)
return
fednam = message.text.split(None, 1)[1]
if not fednam == "":
fed_id = str(uuid.uuid4())
fed_name = fednam
LOGGER.info(fed_id)
x = sql.new_fed(user.id, fed_name, fed_id)
if not x:
await update.effective_message.reply_text(
f"Can't federate! Please contact @{SUPPORT_CHAT} if the problem persist.",
)
return
await update.effective_message.reply_text(
"*You have succeeded in creating a new federation!*"
"\nName: `{}`"
"\nID: `{}`"
"\n\nUse the command below to join the federation:"
"\n`/joinfed {}`".format(fed_name, fed_id, fed_id),
parse_mode=ParseMode.MARKDOWN,
)
try:
await bot.send_message(
EVENT_LOGS,
"New Federation: <b>{}</b>\nID: <pre>{}</pre>".format(fed_name, fed_id),
parse_mode=ParseMode.HTML,
)
except:
LOGGER.warning("Cannot send a message to EVENT_LOGS")
else:
await update.effective_message.reply_text(
"Please write down the name of the federation",
)
async def del_fed(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
if chat.type != "private":
await update.effective_message.reply_text(
"Federations can only be deleted by privately messaging me.",
)
return
if args:
is_fed_id = args[0]
getinfo = sql.get_fed_info(is_fed_id)
if getinfo is False:
await update.effective_message.reply_text("This federation does not exist.")
return
if int(getinfo["owner"]) == int(user.id) or int(user.id) == OWNER_ID:
fed_id = is_fed_id
else:
await update.effective_message.reply_text(
"Only federation owners can do this!"
)
return
else:
await update.effective_message.reply_text("What should I delete?")
return
if is_user_fed_owner(fed_id, user.id) is False:
await update.effective_message.reply_text("Only federation owners can do this!")
return
await update.effective_message.reply_text(
"You sure you want to delete your federation? This cannot be reverted, you will lose your entire ban list, and '{}' will be permanently lost.".format(
getinfo["fname"],
),
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
text="⚠️ Delete Federation ⚠️",
callback_data="rmfed_{}".format(fed_id),
),
],
[InlineKeyboardButton(text="Cancel", callback_data="rmfed_cancel")],
],
),
)
async def rename_fed(update: Update, context: ContextTypes.DEFAULT_TYPE):
user = update.effective_user
msg = update.effective_message
args = msg.text.split(None, 2)
if len(args) < 3:
return await msg.reply_text("usage: /renamefed <fed_id> <newname>")
fed_id, newname = args[1], args[2]
verify_fed = sql.get_fed_info(fed_id)
if not verify_fed:
return await msg.reply_text("This fed not exist in my database!")
if is_user_fed_owner(fed_id, user.id):
sql.rename_fed(fed_id, user.id, newname)
await msg.reply_text(f"Successfully renamed your fed name to {newname}!")
else:
await msg.reply_text("Only federation owner can do this!")
async def fed_chat(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
fed_id = sql.get_fed_id(chat.id)
user_id = update.effective_message.from_user.id
if not await is_user_admin(update.effective_chat, user_id):
await update.effective_message.reply_text(
"You must be an admin to execute this command",
)
return
if not fed_id:
await update.effective_message.reply_text(
"This group is not in any federation!"
)
return
user = update.effective_user
chat = update.effective_chat
info = sql.get_fed_info(fed_id)
text = "This group is part of the following federation:"
text += "\n{} (ID: <code>{}</code>)".format(info["fname"], fed_id)
await update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)
async def join_fed(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
message = update.effective_message
administrators = await chat.get_administrators()
fed_id = sql.get_fed_id(chat.id)
if user.id in DRAGONS:
pass
else:
for admin in administrators:
status = admin.status
if status == "creator":
if str(admin.user.id) == str(user.id):
pass
else:
await update.effective_message.reply_text(
"Only group creators can use this command!",
)
return
if fed_id:
await message.reply_text("You cannot join two federations from one chat")
return
if len(args) >= 1:
getfed = sql.search_fed_by_id(args[0])
if getfed is False:
await message.reply_text("Please enter a valid federation ID")
return
x = sql.chat_join_fed(args[0], chat.title, chat.id)
if not x:
await message.reply_text(
f"Failed to join federation! Please contact @{SUPPORT_CHAT} should this problem persist!",
)
return
get_fedlog = await sql.get_fed_log(args[0])
if get_fedlog:
if ast.literal_eval(get_fedlog):
await bot.send_message(
get_fedlog,
"Chat *{}* has joined the federation *{}*".format(
chat.title,
getfed["fname"],
),
parse_mode="markdown",
message_thread_id=(
message.message_thread_id if chat.is_forum else None
),
)
await message.reply_text(
"This group has joined the federation: {}!".format(getfed["fname"]),
)
async def leave_fed(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our PM!",
)
return
fed_id = sql.get_fed_id(chat.id)
fed_info = sql.get_fed_info(fed_id)
# administrators = await chat.get_administrators().status
getuser = await bot.get_chat_member(chat.id, user.id).status
if getuser in "creator" or user.id in DRAGONS:
if sql.chat_leave_fed(chat.id) is True:
get_fedlog = await sql.get_fed_log(fed_id)
if get_fedlog:
if ast.literal_eval(get_fedlog):
await bot.send_message(
get_fedlog,
"Chat *{}* has left the federation *{}*".format(
chat.title,
fed_info["fname"],
),
parse_mode="markdown",
message_thread_id=(
update.effective_message.message_thread_id
if chat.is_forum
else None
),
)
await send_message(
update.effective_message,
"This group has left the federation {}!".format(fed_info["fname"]),
)
else:
await update.effective_message.reply_text(
"How can you leave a federation that you never joined?!",
)
else:
await update.effective_message.reply_text(
"Only group creators can use this command!"
)
async def user_join_fed(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
msg = update.effective_message
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
if is_user_fed_owner(fed_id, user.id) or user.id in DRAGONS:
user_id = await extract_user(msg, context, args)
if user_id:
user = await bot.get_chat(user_id)
elif not msg.reply_to_message and not args:
user = msg.from_user
elif not msg.reply_to_message and (
not args
or (
len(args) >= 1
and not args[0].startswith("@")
and not args[0].isdigit()
and not msg.parse_entities([MessageEntity.TEXT_MENTION])
)
):
await msg.reply_text("I cannot extract user from this message")
return
else:
LOGGER.warning("error")
getuser = sql.search_user_in_fed(fed_id, user_id)
fed_id = sql.get_fed_id(chat.id)
info = sql.get_fed_info(fed_id)
get_owner = ast.literal_eval(info["fusers"])["owner"]
get_owner = await bot.get_chat(get_owner)
if isinstance(get_owner, ChatMember):
if user_id == get_owner.id:
await update.effective_message.reply_text(
"You do know that the user is the federation owner, right? RIGHT?",
)
return
if getuser:
await update.effective_message.reply_text(
"I cannot promote users who are already federation admins! Can remove them if you want!",
)
return
if user_id == bot.id:
await update.effective_message.reply_text(
"I already am a federation admin in all federations!",
)
return
res = sql.user_join_fed(fed_id, user_id)
if res:
await update.effective_message.reply_text("Successfully Promoted!")
else:
await update.effective_message.reply_text("Failed to promote!")
else:
await update.effective_message.reply_text("Only federation owners can do this!")
async def user_demote_fed(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
if is_user_fed_owner(fed_id, user.id):
msg = update.effective_message
user_id = await extract_user(msg, context, args)
if user_id:
user = await bot.get_chat(user_id)
elif not msg.reply_to_message and not args:
user = msg.from_user
elif not msg.reply_to_message and (
not args
or (
len(args) >= 1
and not args[0].startswith("@")
and not args[0].isdigit()
and not msg.parse_entities([MessageEntity.TEXT_MENTION])
)
):
await msg.reply_text("I cannot extract user from this message")
return
else:
LOGGER.warning("error")
if user_id == bot.id:
await update.effective_message.reply_text(
"The thing you are trying to demote me from will fail to work without me! Just saying.",
)
return
if sql.search_user_in_fed(fed_id, user_id) is False:
await update.effective_message.reply_text(
"I cannot demote people who are not federation admins!",
)
return
res = sql.user_demote_fed(fed_id, user_id)
if res is True:
await update.effective_message.reply_text("Demoted from a Fed Admin!")
else:
await update.effective_message.reply_text("Demotion failed!")
else:
await update.effective_message.reply_text("Only federation owners can do this!")
return
async def fed_info(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
if args:
fed_id = args[0]
info = sql.get_fed_info(fed_id)
else:
if chat.type == "private":
await send_message(
update.effective_message,
"You need to provide me a fedid to check fedinfo in my pm.",
)
return
fed_id = sql.get_fed_id(chat.id)
if not fed_id:
await send_message(
update.effective_message,
"This group is not in any federation!",
)
return
info = sql.get_fed_info(fed_id)
if is_user_fed_admin(fed_id, user.id) is False:
await update.effective_message.reply_text(
"Only a federation admin can do this!"
)
return
owner = await bot.get_chat(info["owner"])
try:
owner_name = owner.first_name + " " + owner.last_name
except:
owner_name = owner.first_name
FEDADMIN = sql.all_fed_users(fed_id)
TotalAdminFed = len(FEDADMIN)
user = update.effective_user
chat = update.effective_chat
info = sql.get_fed_info(fed_id)
text = "<b>ℹ️ Federation Information:</b>"
text += "\nFedID: <code>{}</code>".format(fed_id)
text += "\nName: {}".format(info["fname"])
text += "\nCreator: {}".format(mention_html(owner.id, owner_name))
text += "\nAll Admins: <code>{}</code>".format(TotalAdminFed)
getfban = sql.get_all_fban_users(fed_id)
text += "\nTotal banned users: <code>{}</code>".format(len(getfban))
getfchat = sql.all_fed_chats(fed_id)
text += "\nNumber of groups in this federation: <code>{}</code>".format(
len(getfchat),
)
await update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)
async def fed_admin(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
if not fed_id:
await update.effective_message.reply_text(
"This group is not in any federation!"
)
return
if is_user_fed_admin(fed_id, user.id) is False:
await update.effective_message.reply_text("Only federation admins can do this!")
return
user = update.effective_user
chat = update.effective_chat
info = sql.get_fed_info(fed_id)
text = "<b>Federation Admin {}:</b>\n\n".format(info["fname"])
text += "👑 Owner:\n"
owner = await bot.get_chat(info["owner"])
try:
owner_name = owner.first_name + " " + owner.last_name
except:
owner_name = owner.first_name
text += " • {}\n".format(mention_html(owner.id, owner_name))
members = sql.all_fed_members(fed_id)
if len(members) == 0:
text += "\n🔱 There are no admins in this federation"
else:
text += "\n🔱 Admin:\n"
for x in members:
user = await bot.get_chat(x)
text += " • {}\n".format(mention_html(user.id, user.first_name))
await update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)
async def fed_ban(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
if not fed_id:
await update.effective_message.reply_text(
"This group is not a part of any federation!",
)
return
info = sql.get_fed_info(fed_id)
getfednotif = sql.user_feds_report(info["owner"])
if is_user_fed_admin(fed_id, user.id) is False:
await update.effective_message.reply_text("Only federation admins can do this!")
return
message = update.effective_message
user_id, reason = extract_unt_fedban(message, context, args)
fban, fbanreason, fbantime = sql.get_fban_user(fed_id, user_id)
if not user_id:
await message.reply_text("You don't seem to be referring to a user")
return
if user_id == bot.id:
await message.reply_text(
"What is funnier than kicking the group creator? Self sacrifice.",
)
return
if is_user_fed_owner(fed_id, user_id) is True:
await message.reply_text("Why did you try the federation fban?")
return
if is_user_fed_admin(fed_id, user_id) is True:
await message.reply_text("He is a federation admin, I can't fban him.")
return
if user_id == OWNER_ID:
await message.reply_text("Disaster level God cannot be fed banned!")
return
if int(user_id) in DRAGONS:
await message.reply_text("Dragons cannot be fed banned!")
return
if user_id in [777000, 1087968824]:
await message.reply_text("Fool! You can't attack Telegram's native tech!")
return
try:
user_chat = await bot.get_chat(user_id)
isvalid = True
fban_user_id = user_chat.id
fban_user_name = user_chat.first_name
fban_user_lname = user_chat.last_name
fban_user_uname = user_chat.username
except BadRequest as excp:
if not str(user_id).isdigit():
await send_message(update.effective_message, excp.message)
return
elif len(str(user_id)) != 9:
await send_message(update.effective_message, "That's so not a user!")
return
isvalid = False
fban_user_id = int(user_id)
fban_user_name = "user({})".format(user_id)
fban_user_lname = None
fban_user_uname = None
if isvalid and user_chat.type != "private":
await send_message(update.effective_message, "That's so not a user!")
return
if isvalid:
user_target = mention_html(fban_user_id, fban_user_name)
else:
user_target = fban_user_name
if fban:
fed_name = info["fname"]
# https://t.me/OnePunchSupport/41606 // https://t.me/OnePunchSupport/41619
# starting = "The reason fban is replaced for {} in the Federation <b>{}</b>.".format(user_target, fed_name)
# await send_message(update.effective_message, starting, parse_mode=ParseMode.HTML)
# if reason == "":
# reason = "No reason given."
temp = sql.un_fban_user(fed_id, fban_user_id)
if not temp:
await message.reply_text("Failed to update the reason for fedban!")
return
x = sql.fban_user(
fed_id,
fban_user_id,
fban_user_name,
fban_user_lname,
fban_user_uname,
reason,
int(time.time()),
)
if not x:
await message.reply_text(
f"Failed to ban from the federation! If this problem continues, contact @{SUPPORT_CHAT}.",
)
return
fed_chats = sql.all_fed_chats(fed_id)
# Will send to current chat
await bot.send_message(
chat.id,
"<b>FedBan reason updated</b>"
"\n<b>Federation:</b> {}"
"\n<b>Federation Admin:</b> {}"
"\n<b>User:</b> {}"
"\n<b>User ID:</b> <code>{}</code>"
"\n<b>Reason:</b> {}".format(
fed_name,
mention_html(user.id, user.first_name),
user_target,
fban_user_id,
reason,
),
parse_mode="HTML",
)
# Send message to owner if fednotif is enabled
if getfednotif:
await bot.send_message(
info["owner"],
"<b>FedBan reason updated</b>"
"\n<b>Federation:</b> {}"
"\n<b>Federation Admin:</b> {}"
"\n<b>User:</b> {}"
"\n<b>User ID:</b> <code>{}</code>"
"\n<b>Reason:</b> {}".format(
fed_name,
mention_html(user.id, user.first_name),
user_target,
fban_user_id,
reason,
),
parse_mode="HTML",
)
# If fedlog is set, then send message, except fedlog is current chat
get_fedlog = await sql.get_fed_log(fed_id)
if get_fedlog:
if int(get_fedlog) != int(chat.id):
await bot.send_message(
get_fedlog,
"<b>FedBan reason updated</b>"
"\n<b>Federation:</b> {}"
"\n<b>Federation Admin:</b> {}"
"\n<b>User:</b> {}"
"\n<b>User ID:</b> <code>{}</code>"
"\n<b>Reason:</b> {}".format(
fed_name,
mention_html(user.id, user.first_name),
user_target,
fban_user_id,
reason,
),
parse_mode="HTML",
)
for fedschat in fed_chats:
try:
# Do not spam all fed chats
"""
bot.send_message(chat, "<b>FedBan reason updated</b>" \
"\n<b>Federation:</b> {}" \
"\n<b>Federation Admin:</b> {}" \
"\n<b>User:</b> {}" \
"\n<b>User ID:</b> <code>{}</code>" \
"\n<b>Reason:</b> {}".format(fed_name, mention_html(user.id, user.first_name), user_target, fban_user_id, reason), parse_mode="HTML")
"""
await bot.ban_chat_member(fedschat, fban_user_id)
except BadRequest as excp:
if excp.message in FBAN_ERRORS:
try:
await dispatcher.bot.getChat(fedschat)
except Forbidden:
sql.chat_leave_fed(fedschat)
LOGGER.info(
"Chat {} has leave fed {} because I was kicked".format(
fedschat,
info["fname"],
),
)
continue
elif excp.message == "User_id_invalid":
break
else:
LOGGER.warning(
"Could not fban on {} because: {}".format(chat, excp.message),
)
except TelegramError:
pass
# Also do not spam all fed admins
"""
send_to_list(bot, FEDADMIN,
"<b>FedBan reason updated</b>" \
"\n<b>Federation:</b> {}" \
"\n<b>Federation Admin:</b> {}" \
"\n<b>User:</b> {}" \
"\n<b>User ID:</b> <code>{}</code>" \
"\n<b>Reason:</b> {}".format(fed_name, mention_html(user.id, user.first_name), user_target, fban_user_id, reason),
html=True)
"""
# Fban for fed subscriber
subscriber = list(sql.get_subscriber(fed_id))
if len(subscriber) != 0:
for fedsid in subscriber:
all_fedschat = sql.all_fed_chats(fedsid)
for fedschat in all_fedschat:
try:
await bot.ban_chat_member(fedschat, fban_user_id)
except BadRequest as excp:
if excp.message in FBAN_ERRORS:
try:
await dispatcher.bot.getChat(fedschat)
except Forbidden:
targetfed_id = sql.get_fed_id(fedschat)
sql.unsubs_fed(fed_id, targetfed_id)
LOGGER.info(
"Chat {} has unsub fed {} because I was kicked".format(
fedschat,
info["fname"],
),
)
continue
elif excp.message == "User_id_invalid":
break
else:
LOGGER.warning(
"Unable to fban on {} because: {}".format(
fedschat,
excp.message,
),
)
except TelegramError:
pass
# await send_message(update.effective_message, "Fedban Reason has been updated.")
return
fed_name = info["fname"]
# starting = "Starting a federation ban for {} in the Federation <b>{}</b>.".format(
# user_target, fed_name)
# await update.effective_message.reply_text(starting, parse_mode=ParseMode.HTML)
# if reason == "":
# reason = "No reason given."
x = sql.fban_user(
fed_id,
fban_user_id,
fban_user_name,
fban_user_lname,
fban_user_uname,
reason,
int(time.time()),
)
if not x:
await message.reply_text(
f"Failed to ban from the federation! If this problem continues, contact @{SUPPORT_CHAT}.",
)
return
fed_chats = sql.all_fed_chats(fed_id)
# Will send to current chat
await bot.send_message(
chat.id,
"<b>New FedBan</b>"
"\n<b>Federation:</b> {}"
"\n<b>Federation Admin:</b> {}"
"\n<b>User:</b> {}"
"\n<b>User ID:</b> <code>{}</code>"
"\n<b>Reason:</b> {}".format(
fed_name,
mention_html(user.id, user.first_name),
user_target,
fban_user_id,
reason,
),
parse_mode="HTML",
)
# Send message to owner if fednotif is enabled
if getfednotif:
await bot.send_message(
info["owner"],
"<b>New FedBan</b>"
"\n<b>Federation:</b> {}"
"\n<b>Federation Admin:</b> {}"
"\n<b>User:</b> {}"
"\n<b>User ID:</b> <code>{}</code>"
"\n<b>Reason:</b> {}".format(
fed_name,
mention_html(user.id, user.first_name),
user_target,
fban_user_id,
reason,
),
parse_mode="HTML",
)
# If fedlog is set, then send message, except fedlog is current chat
get_fedlog = await sql.get_fed_log(fed_id)
if get_fedlog:
if int(get_fedlog) != int(chat.id):
await bot.send_message(
get_fedlog,
"<b>New FedBan</b>"
"\n<b>Federation:</b> {}"
"\n<b>Federation Admin:</b> {}"
"\n<b>User:</b> {}"
"\n<b>User ID:</b> <code>{}</code>"
"\n<b>Reason:</b> {}".format(
fed_name,
mention_html(user.id, user.first_name),
user_target,
fban_user_id,
reason,
),
parse_mode="HTML",
)
chats_in_fed = 0
for fedschat in fed_chats:
chats_in_fed += 1
try:
# Do not spamming all fed chats
"""
bot.send_message(chat, "<b>FedBan reason updated</b>" \
"\n<b>Federation:</b> {}" \
"\n<b>Federation Admin:</b> {}" \
"\n<b>User:</b> {}" \
"\n<b>User ID:</b> <code>{}</code>" \
"\n<b>Reason:</b> {}".format(fed_name, mention_html(user.id, user.first_name), user_target, fban_user_id, reason), parse_mode="HTML")
"""
await bot.ban_chat_member(fedschat, fban_user_id)
except BadRequest as excp:
if excp.message in FBAN_ERRORS:
pass
elif excp.message == "User_id_invalid":
break
else:
LOGGER.warning(
"Could not fban on {} because: {}".format(chat, excp.message),
)
except TelegramError:
pass
# Also do not spamming all fed admins
"""
send_to_list(bot, FEDADMIN,
"<b>FedBan reason updated</b>" \
"\n<b>Federation:</b> {}" \
"\n<b>Federation Admin:</b> {}" \
"\n<b>User:</b> {}" \
"\n<b>User ID:</b> <code>{}</code>" \
"\n<b>Reason:</b> {}".format(fed_name, mention_html(user.id, user.first_name), user_target, fban_user_id, reason),
html=True)
"""
# Fban for fed subscriber
subscriber = list(sql.get_subscriber(fed_id))
if len(subscriber) != 0:
for fedsid in subscriber:
all_fedschat = sql.all_fed_chats(fedsid)
for fedschat in all_fedschat:
try:
await bot.ban_chat_member(fedschat, fban_user_id)
except BadRequest as excp:
if excp.message in FBAN_ERRORS:
try:
await dispatcher.bot.getChat(fedschat)
except Forbidden:
targetfed_id = sql.get_fed_id(fedschat)
sql.unsubs_fed(fed_id, targetfed_id)
LOGGER.info(
"Chat {} has unsub fed {} because I was kicked".format(
fedschat,
info["fname"],
),
)
continue
elif excp.message == "User_id_invalid":
break
else:
LOGGER.warning(
"Unable to fban on {} because: {}".format(
fedschat,
excp.message,
),
)
except TelegramError:
pass
async def unfban(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
message = update.effective_message
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
if not fed_id:
await update.effective_message.reply_text(
"This group is not a part of any federation!",
)
return
info = sql.get_fed_info(fed_id)
getfednotif = sql.user_feds_report(info["owner"])
if is_user_fed_admin(fed_id, user.id) is False:
await update.effective_message.reply_text("Only federation admins can do this!")
return
user_id = extract_user_fban(message, context, args)
if not user_id:
await message.reply_text("You do not seem to be referring to a user.")
return
try:
user_chat = await bot.get_chat(user_id)
isvalid = True
fban_user_id = user_chat.id
fban_user_name = user_chat.first_name
fban_user_lname = user_chat.last_name
fban_user_uname = user_chat.username
except BadRequest as excp:
if not str(user_id).isdigit():
await send_message(update.effective_message, excp.message)
return
elif len(str(user_id)) != 9:
await send_message(update.effective_message, "That's so not a user!")
return
isvalid = False
fban_user_id = int(user_id)
fban_user_name = "user({})".format(user_id)
fban_user_lname = None
fban_user_uname = None
if isvalid and user_chat.type != "private":
await message.reply_text("That's so not a user!")
return
if isvalid:
user_target = mention_html(fban_user_id, fban_user_name)
else:
user_target = fban_user_name
fban, fbanreason, fbantime = sql.get_fban_user(fed_id, fban_user_id)
if fban is False:
await message.reply_text("This user is not fbanned!")
return
banner = update.effective_user
chat_list = sql.all_fed_chats(fed_id)
# Will send to current chat
await bot.send_message(
chat.id,
"<b>Un-FedBan</b>"
"\n<b>Federation:</b> {}"
"\n<b>Federation Admin:</b> {}"
"\n<b>User:</b> {}"
"\n<b>User ID:</b> <code>{}</code>".format(
info["fname"],
mention_html(user.id, user.first_name),
user_target,
fban_user_id,
),
parse_mode="HTML",
)
# Send message to owner if fednotif is enabled
if getfednotif:
await bot.send_message(
info["owner"],
"<b>Un-FedBan</b>"
"\n<b>Federation:</b> {}"
"\n<b>Federation Admin:</b> {}"
"\n<b>User:</b> {}"
"\n<b>User ID:</b> <code>{}</code>".format(
info["fname"],
mention_html(user.id, user.first_name),
user_target,
fban_user_id,
),
parse_mode="HTML",
)
# If fedlog is set, then send message, except fedlog is current chat
get_fedlog = await sql.get_fed_log(fed_id)
if get_fedlog:
if int(get_fedlog) != int(chat.id):
await bot.send_message(
get_fedlog,
"<b>Un-FedBan</b>"
"\n<b>Federation:</b> {}"
"\n<b>Federation Admin:</b> {}"
"\n<b>User:</b> {}"
"\n<b>User ID:</b> <code>{}</code>".format(
info["fname"],
mention_html(user.id, user.first_name),
user_target,
fban_user_id,
),
parse_mode="HTML",
)
unfbanned_in_chats = 0
for fedchats in chat_list:
unfbanned_in_chats += 1
try:
member = await bot.get_chat_member(fedchats, user_id)
if member.status == "kicked":
await bot.unban_chat_member(fedchats, user_id)
# Do not spamming all fed chats
"""
bot.send_message(chat, "<b>Un-FedBan</b>" \
"\n<b>Federation:</b> {}" \
"\n<b>Federation Admin:</b> {}" \
"\n<b>User:</b> {}" \
"\n<b>User ID:</b> <code>{}</code>".format(info['fname'], mention_html(user.id, user.first_name), user_target, fban_user_id), parse_mode="HTML")
"""
except BadRequest as excp:
if excp.message in UNFBAN_ERRORS:
pass
elif excp.message == "User_id_invalid":
break
else:
LOGGER.warning(
"Could not fban on {} because: {}".format(chat, excp.message),
)
except TelegramError:
pass
try:
x = sql.un_fban_user(fed_id, user_id)
if not x:
await send_message(
update.effective_message,
"Un-fban failed, this user may already be un-fedbanned!",
)
return
except:
pass
# UnFban for fed subscriber
subscriber = list(sql.get_subscriber(fed_id))
if len(subscriber) != 0:
for fedsid in subscriber:
all_fedschat = sql.all_fed_chats(fedsid)
for fedschat in all_fedschat:
try:
await bot.unban_chat_member(fedchats, user_id)
except BadRequest as excp:
if excp.message in FBAN_ERRORS:
try:
await dispatcher.bot.getChat(fedschat)
except Forbidden:
targetfed_id = sql.get_fed_id(fedschat)
sql.unsubs_fed(fed_id, targetfed_id)
LOGGER.info(
"Chat {} has unsub fed {} because I was kicked".format(
fedschat,
info["fname"],
),
)
continue
elif excp.message == "User_id_invalid":
break
else:
LOGGER.warning(
"Unable to fban on {} because: {}".format(
fedschat,
excp.message,
),
)
except TelegramError:
pass
if unfbanned_in_chats == 0:
await send_message(
update.effective_message,
"This person has been un-fbanned in 0 chats.",
)
if unfbanned_in_chats > 0:
await send_message(
update.effective_message,
"This person has been un-fbanned in {} chats.".format(unfbanned_in_chats),
)
# Also do not spamming all fed admins
"""
FEDADMIN = sql.all_fed_users(fed_id)
for x in FEDADMIN:
getreport = sql.user_feds_report(x)
if getreport is False:
FEDADMIN.remove(x)
send_to_list(bot, FEDADMIN,
"<b>Un-FedBan</b>" \
"\n<b>Federation:</b> {}" \
"\n<b>Federation Admin:</b> {}" \
"\n<b>User:</b> {}" \
"\n<b>User ID:</b> <code>{}</code>".format(info['fname'], mention_html(user.id, user.first_name),
mention_html(user_chat.id, user_chat.first_name),
user_chat.id),
html=True)
"""
async def set_frules(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
if not fed_id:
await update.effective_message.reply_text(
"This group is not in any federation!"
)
return
if is_user_fed_admin(fed_id, user.id) is False:
await update.effective_message.reply_text("Only fed admins can do this!")
return
if len(args) >= 1:
msg = update.effective_message
raw_text = msg.text
args = raw_text.split(None, 1) # use python's maxsplit to separate cmd and args
if len(args) == 2:
txt = args[1]
offset = len(txt) - len(raw_text) # set correct offset relative to command
markdown_rules = markdown_parser(
txt,
entities=msg.parse_entities(),
offset=offset,
)
x = sql.set_frules(fed_id, markdown_rules)
if not x:
await update.effective_message.reply_text(
f"Whoa! There was an error while setting federation rules! If you wondered why please ask it in @{SUPPORT_CHAT}!",
)
return
rules = sql.get_fed_info(fed_id)["frules"]
getfed = sql.get_fed_info(fed_id)
get_fedlog = await sql.get_fed_log(fed_id)
if get_fedlog:
if ast.literal_eval(get_fedlog):
await bot.send_message(
get_fedlog,
"*{}* has updated federation rules for fed *{}*".format(
user.first_name,
getfed["fname"],
),
parse_mode="markdown",
message_thread_id=(
update.effective_message.message_thread_id
if chat.is_forum
else None
),
)
await update.effective_message.reply_text(
f"Rules have been changed to :\n{rules}!"
)
else:
await update.effective_message.reply_text("Please write rules to set this up!")
async def get_frules(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
if not fed_id:
await update.effective_message.reply_text(
"This group is not in any federation!"
)
return
rules = sql.get_frules(fed_id)
text = "*Rules in this fed:*\n"
text += rules
await update.effective_message.reply_text(text, parse_mode=ParseMode.MARKDOWN)
async def fed_broadcast(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
msg = update.effective_message
user = update.effective_user
chat = update.effective_chat
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
if args:
chat = update.effective_chat
fed_id = sql.get_fed_id(chat.id)
fedinfo = sql.get_fed_info(fed_id)
if is_user_fed_owner(fed_id, user.id) is False:
await update.effective_message.reply_text(
"Only federation owners can do this!"
)
return
# Parsing md
raw_text = msg.text
args = raw_text.split(None, 1) # use python's maxsplit to separate cmd and args
txt = args[1]
offset = len(txt) - len(raw_text) # set correct offset relative to command
text_parser = markdown_parser(txt, entities=msg.parse_entities(), offset=offset)
text = text_parser
try:
broadcaster = user.first_name
except:
broadcaster = user.first_name + " " + user.last_name
text += "\n\n- {}".format(mention_markdown(user.id, broadcaster))
chat_list = sql.all_fed_chats(fed_id)
failed = 0
for chat in chat_list:
title = "*New broadcast from Fed {}*\n".format(fedinfo["fname"])
try:
await bot.sendMessage(
chat,
title + text,
parse_mode="markdown",
message_thread_id=msg.message_thread_id if chat.is_forum else None,
)
except TelegramError:
try:
await dispatcher.bot.getChat(chat)
except Forbidden:
failed += 1
sql.chat_leave_fed(chat)
LOGGER.info(
"Chat {} has left fed {} because I was kicked".format(
chat,
fedinfo["fname"],
),
)
continue
failed += 1
LOGGER.warning("Couldn't send broadcast to {}".format(str(chat)))
send_text = "The federation broadcast is complete"
if failed >= 1:
send_text += "{} the group failed to receive the message, probably because it left the Federation.".format(
failed,
)
await update.effective_message.reply_text(send_text)
async def fed_ban_list(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args, chat_data = context.bot, context.args, context.chat_data
chat = update.effective_chat
user = update.effective_user
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
info = sql.get_fed_info(fed_id)
if not fed_id:
await update.effective_message.reply_text(
"This group is not a part of any federation!",
)
return
if is_user_fed_owner(fed_id, user.id) is False:
await update.effective_message.reply_text("Only Federation owners can do this!")
return
user = update.effective_user
chat = update.effective_chat
getfban = sql.get_all_fban_users(fed_id)
if len(getfban) == 0:
await update.effective_message.reply_text(
"The federation ban list of {} is empty".format(info["fname"]),
parse_mode=ParseMode.HTML,
)
return
if args:
if args[0] == "json":
jam = time.time()
new_jam = jam + 1800
cek = get_chat(chat.id, chat_data)
if cek.get("status"):
if jam <= int(cek.get("value")):
waktu = time.strftime(
"%H:%M:%S %d/%m/%Y",
time.localtime(cek.get("value")),
)
await update.effective_message.reply_text(
"You can backup your data once every 30 minutes!\nYou can back up data again at `{}`".format(
waktu,
),
parse_mode=ParseMode.MARKDOWN,
)
return
else:
if user.id not in DRAGONS:
put_chat(chat.id, new_jam, chat_data)
else:
if user.id not in DRAGONS:
put_chat(chat.id, new_jam, chat_data)
backups = ""
for users in getfban:
getuserinfo = sql.get_all_fban_users_target(fed_id, users)
json_parser = {
"user_id": users,
"first_name": getuserinfo["first_name"],
"last_name": getuserinfo["last_name"],
"user_name": getuserinfo["user_name"],
"reason": getuserinfo["reason"],
}
backups += json.dumps(json_parser)
backups += "\n"
with BytesIO(str.encode(backups)) as output:
output.name = "mikobot_fbanned_users.json"
await update.effective_message.reply_document(
document=output,
filename="mikobot_fbanned_users.json",
caption="Total {} User are blocked by the Federation {}.".format(
len(getfban),
info["fname"],
),
)
return
elif args[0] == "csv":
jam = time.time()
new_jam = jam + 1800
cek = get_chat(chat.id, chat_data)
if cek.get("status"):
if jam <= int(cek.get("value")):
waktu = time.strftime(
"%H:%M:%S %d/%m/%Y",
time.localtime(cek.get("value")),
)
await update.effective_message.reply_text(
"You can back up data once every 30 minutes!\nYou can back up data again at `{}`".format(
waktu,
),
parse_mode=ParseMode.MARKDOWN,
)
return
else:
if user.id not in DRAGONS:
put_chat(chat.id, new_jam, chat_data)
else:
if user.id not in DRAGONS:
put_chat(chat.id, new_jam, chat_data)
backups = "id,firstname,lastname,username,reason\n"
for users in getfban:
getuserinfo = sql.get_all_fban_users_target(fed_id, users)
backups += (
"{user_id},{first_name},{last_name},{user_name},{reason}".format(
user_id=users,
first_name=getuserinfo["first_name"],
last_name=getuserinfo["last_name"],
user_name=getuserinfo["user_name"],
reason=getuserinfo["reason"],
)
)
backups += "\n"
with BytesIO(str.encode(backups)) as output:
output.name = "mikobot_fbanned_users.csv"
await update.effective_message.reply_document(
document=output,
filename="mikobot_fbanned_users.csv",
caption="Total {} User are blocked by Federation {}.".format(
len(getfban),
info["fname"],
),
)
return
text = "<b>{} users have been banned from the federation {}:</b>\n".format(
len(getfban),
info["fname"],
)
for users in getfban:
getuserinfo = sql.get_all_fban_users_target(fed_id, users)
if getuserinfo is False:
text = "There are no users banned from the federation {}".format(
info["fname"],
)
break
user_name = getuserinfo["first_name"]
if getuserinfo["last_name"]:
user_name += " " + getuserinfo["last_name"]
text += " • {} (<code>{}</code>)\n".format(
mention_html(users, user_name),
users,
)
try:
await update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)
except:
jam = time.time()
new_jam = jam + 1800
cek = get_chat(chat.id, chat_data)
if cek.get("status"):
if jam <= int(cek.get("value")):
waktu = time.strftime(
"%H:%M:%S %d/%m/%Y",
time.localtime(cek.get("value")),
)
await update.effective_message.reply_text(
"You can back up data once every 30 minutes!\nYou can back up data again at `{}`".format(
waktu,
),
parse_mode=ParseMode.MARKDOWN,
)
return
else:
if user.id not in DRAGONS:
put_chat(chat.id, new_jam, chat_data)
else:
if user.id not in DRAGONS:
put_chat(chat.id, new_jam, chat_data)
cleanr = re.compile("<.*?>")
cleantext = re.sub(cleanr, "", text)
with BytesIO(str.encode(cleantext)) as output:
output.name = "fbanlist.txt"
await update.effective_message.reply_document(
document=output,
filename="fbanlist.txt",
caption="The following is a list of users who are currently fbanned in the Federation {}.".format(
info["fname"],
),
)
async def fed_notif(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
msg = update.effective_message
fed_id = sql.get_fed_id(chat.id)
if not fed_id:
await update.effective_message.reply_text(
"This group is not a part of any federation!",
)
return
if args:
if args[0] in ("yes", "on"):
sql.set_feds_setting(user.id, True)
await msg.reply_text(
"Reporting Federation back up! Every user who is fban / unfban you will be notified via PM.",
)
elif args[0] in ("no", "off"):
sql.set_feds_setting(user.id, False)
await msg.reply_text(
"Reporting Federation has stopped! Every user who is fban / unfban you will not be notified via PM.",
)
else:
await msg.reply_text("Please enter `on`/`off`", parse_mode="markdown")
else:
getreport = sql.user_feds_report(user.id)
await msg.reply_text(
"Your current Federation report preferences: `{}`".format(getreport),
parse_mode="markdown",
)
async def fed_chats(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
info = sql.get_fed_info(fed_id)
if not fed_id:
await update.effective_message.reply_text(
"This group is not a part of any federation!",
)
return
if is_user_fed_admin(fed_id, user.id) is False:
await update.effective_message.reply_text("Only federation admins can do this!")
return
getlist = sql.all_fed_chats(fed_id)
if len(getlist) == 0:
await update.effective_message.reply_text(
"No users are fbanned from the federation {}".format(info["fname"]),
parse_mode=ParseMode.HTML,
)
return
text = "<b>New chat joined the federation {}:</b>\n".format(info["fname"])
for chats in getlist:
try:
chat_obj = await dispatcher.bot.getChat(chats)
chat_name = chat_obj.title
except Forbidden:
sql.chat_leave_fed(chats)
LOGGER.info(
"Chat {} has leave fed {} because I was kicked".format(
chats,
info["fname"],
),
)
continue
text += " • {} (<code>{}</code>)\n".format(chat_name, chats)
try:
await update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)
except:
cleanr = re.compile("<.*?>")
cleantext = re.sub(cleanr, "", text)
with BytesIO(str.encode(cleantext)) as output:
output.name = "fedchats.txt"
await update.effective_message.reply_document(
document=output,
filename="fedchats.txt",
caption="Here is a list of all the chats that joined the federation {}.".format(
info["fname"],
),
)
async def fed_import_bans(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, chat_data = context.bot, context.chat_data
chat = update.effective_chat
user = update.effective_user
msg = update.effective_message
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
info = sql.get_fed_info(fed_id)
getfed = sql.get_fed_info(fed_id)
if not fed_id:
await update.effective_message.reply_text(
"This group is not a part of any federation!",
)
return
if is_user_fed_owner(fed_id, user.id) is False:
await update.effective_message.reply_text("Only Federation owners can do this!")
return
if msg.reply_to_message and msg.reply_to_message.document:
jam = time.time()
new_jam = jam + 1800
cek = get_chat(chat.id, chat_data)
if cek.get("status"):
if jam <= int(cek.get("value")):
waktu = time.strftime(
"%H:%M:%S %d/%m/%Y",
time.localtime(cek.get("value")),
)
await update.effective_message.reply_text(
"You can get your data once every 30 minutes!\nYou can get data again at `{}`".format(
waktu,
),
parse_mode=ParseMode.MARKDOWN,
)
return
else:
if user.id not in DRAGONS:
put_chat(chat.id, new_jam, chat_data)
else:
if user.id not in DRAGONS:
put_chat(chat.id, new_jam, chat_data)
# if int(int(msg.reply_to_message.document.file_size)/1024) >= 200:
# msg.reply_text("This file is too big!")
# return
success = 0
failed = 0
try:
file_info = await bot.get_file(msg.reply_to_message.document.file_id)
except BadRequest:
await msg.reply_text(
"Try downloading and re-uploading the file, this one seems broken!",
)
return
fileformat = msg.reply_to_message.document.file_name.split(".")[-1]
if fileformat == "json":
multi_fed_id = []
multi_import_userid = []
multi_import_firstname = []
multi_import_lastname = []
multi_import_username = []
multi_import_reason = []
with BytesIO() as file:
file_info.download_to_object(out=file)
file.seek(0)
reading = file.read().decode("UTF-8")
splitting = reading.split("\n")
for x in splitting:
if x == "":
continue
try:
data = json.loads(x)
except json.decoder.JSONDecodeError as err:
failed += 1
continue
try:
import_userid = int(data["user_id"]) # Make sure it int
import_firstname = str(data["first_name"])
import_lastname = str(data["last_name"])
import_username = str(data["user_name"])
import_reason = str(data["reason"])
except ValueError:
failed += 1
continue
# Checking user
if int(import_userid) == bot.id:
failed += 1
continue
if is_user_fed_owner(fed_id, import_userid) is True:
failed += 1
continue
if is_user_fed_admin(fed_id, import_userid) is True:
failed += 1
continue
if str(import_userid) == str(OWNER_ID):
failed += 1
continue
if int(import_userid) in DRAGONS:
failed += 1
continue
multi_fed_id.append(fed_id)
multi_import_userid.append(str(import_userid))
multi_import_firstname.append(import_firstname)
multi_import_lastname.append(import_lastname)
multi_import_username.append(import_username)
multi_import_reason.append(import_reason)
success += 1
sql.multi_fban_user(
multi_fed_id,
multi_import_userid,
multi_import_firstname,
multi_import_lastname,
multi_import_username,
multi_import_reason,
)
text = "Blocks were successfully imported. {} people are blocked.".format(
success,
)
if failed >= 1:
text += " {} Failed to import.".format(failed)
get_fedlog = await sql.get_fed_log(fed_id)
if get_fedlog:
if ast.literal_eval(get_fedlog):
teks = "Fed *{}* has successfully imported data. {} banned.".format(
getfed["fname"],
success,
)
if failed >= 1:
teks += " {} Failed to import.".format(failed)
await bot.send_message(
get_fedlog,
teks,
parse_mode="markdown",
message_thread_id=(
update.effective_message.message_thread_id
if chat.is_forum
else None
),
)
elif fileformat == "csv":
multi_fed_id = []
multi_import_userid = []
multi_import_firstname = []
multi_import_lastname = []
multi_import_username = []
multi_import_reason = []
file_info.download_to_drive(
"fban_{}.csv".format(msg.reply_to_message.document.file_id),
)
with open(
"fban_{}.csv".format(msg.reply_to_message.document.file_id),
"r",
encoding="utf8",
) as csvFile:
reader = csv.reader(csvFile)
for data in reader:
try:
import_userid = int(data[0]) # Make sure it int
import_firstname = str(data[1])
import_lastname = str(data[2])
import_username = str(data[3])
import_reason = str(data[4])
except ValueError:
failed += 1
continue
# Checking user
if int(import_userid) == bot.id:
failed += 1
continue
if is_user_fed_owner(fed_id, import_userid) is True:
failed += 1
continue
if is_user_fed_admin(fed_id, import_userid) is True:
failed += 1
continue
if str(import_userid) == str(OWNER_ID):
failed += 1
continue
if int(import_userid) in DRAGONS:
failed += 1
continue
multi_fed_id.append(fed_id)
multi_import_userid.append(str(import_userid))
multi_import_firstname.append(import_firstname)
multi_import_lastname.append(import_lastname)
multi_import_username.append(import_username)
multi_import_reason.append(import_reason)
success += 1
# t = ThreadWithReturnValue(target=sql.fban_user, args=(fed_id, str(import_userid), import_firstname, import_lastname, import_username, import_reason,))
# t.start()
sql.multi_fban_user(
multi_fed_id,
multi_import_userid,
multi_import_firstname,
multi_import_lastname,
multi_import_username,
multi_import_reason,
)
csvFile.close()
os.remove("fban_{}.csv".format(msg.reply_to_message.document.file_id))
text = "Files were imported successfully. {} people banned.".format(success)
if failed >= 1:
text += " {} Failed to import.".format(failed)
get_fedlog = await sql.get_fed_log(fed_id)
if get_fedlog:
if ast.literal_eval(get_fedlog):
teks = "Fed *{}* has successfully imported data. {} banned.".format(
getfed["fname"],
success,
)
if failed >= 1:
teks += " {} Failed to import.".format(failed)
await bot.send_message(
get_fedlog,
teks,
parse_mode="markdown",
message_thread_id=(
update.effective_message.message_thread_id
if chat.is_forum
else None
),
)
else:
await send_message(update.effective_message, "This file is not supported.")
return
await send_message(update.effective_message, text)
async def del_fed_button(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
userid = query.message.chat.id
fed_id = query.data.split("_")[1]
if fed_id == "cancel":
await query.message.edit_text("Federation deletion cancelled")
return
getfed = sql.get_fed_info(fed_id)
if getfed:
delete = sql.del_fed(fed_id)
if delete:
await query.message.edit_text(
"You have removed your Federation! Now all the Groups that are connected with `{}` do not have a Federation.".format(
getfed["fname"],
),
parse_mode="markdown",
)
async def fed_stat_user(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
msg = update.effective_message
if args:
if args[0].isdigit():
user_id = args[0]
else:
user_id = await extract_user(msg, context, args)
else:
user_id = await extract_user(msg, context, args)
if user_id:
if len(args) == 2 and args[0].isdigit():
fed_id = args[1]
user_name, reason, fbantime = sql.get_user_fban(fed_id, str(user_id))
if fbantime:
fbantime = time.strftime("%d/%m/%Y", time.localtime(fbantime))
else:
fbantime = "Unavaiable"
if user_name is False:
await send_message(
update.effective_message,
"Fed {} not found!".format(fed_id),
parse_mode="markdown",
)
return
if user_name == "" or user_name is None:
user_name = "He/she"
if not reason:
await send_message(
update.effective_message,
"{} is not banned in this federation!".format(user_name),
)
else:
teks = "{} banned in this federation because:\n`{}`\n*Banned at:* `{}`".format(
user_name,
reason,
fbantime,
)
await send_message(
update.effective_message, teks, parse_mode="markdown"
)
return
user_name, fbanlist = sql.get_user_fbanlist(str(user_id))
if user_name == "":
try:
user_first = await bot.get_chat(user_id)
if isinstance(user_first, ChatMember):
user_name = user_id.first_name
except BadRequest:
user_name = "He/she"
if user_name == "" or user_name is None:
user_name = "He/she"
if len(fbanlist) == 0:
await send_message(
update.effective_message,
"{} is not banned in any federation!".format(user_name),
)
return
else:
teks = "{} has been banned in this federation:\n".format(user_name)
for x in fbanlist:
teks += "- `{}`: {}\n".format(x[0], x[1][:20])
teks += "\nIf you want to find out more about the reasons for Fedban specifically, use /fbanstat <FedID>"
await send_message(update.effective_message, teks, parse_mode="markdown")
elif not msg.reply_to_message and not args:
user_id = msg.from_user.id
user_name, fbanlist = sql.get_user_fbanlist(user_id)
if user_name == "":
user_name = msg.from_user.first_name
if len(fbanlist) == 0:
await send_message(
update.effective_message,
"{} is not banned in any federation!".format(user_name),
)
else:
teks = "{} has been banned in this federation:\n".format(user_name)
for x in fbanlist:
teks += "- `{}`: {}\n".format(x[0], x[1][:20])
teks += "\nIf you want to find out more about the reasons for Fedban specifically, use /fbanstat <FedID>"
await send_message(update.effective_message, teks, parse_mode="markdown")
else:
fed_id = args[0]
fedinfo = sql.get_fed_info(fed_id)
if not fedinfo:
await send_message(
update.effective_message, "Fed {} not found!".format(fed_id)
)
return
name, reason, fbantime = sql.get_user_fban(fed_id, msg.from_user.id)
if fbantime:
fbantime = time.strftime("%d/%m/%Y", time.localtime(fbantime))
else:
fbantime = "Unavaiable"
if not name:
name = msg.from_user.first_name
if not reason:
await send_message(
update.effective_message,
"{} is not banned in this federation".format(name),
)
return
await send_message(
update.effective_message,
"{} banned in this federation because:\n`{}`\n*Banned at:* `{}`".format(
name,
reason,
fbantime,
),
parse_mode="markdown",
)
async def set_fed_log(update: Update, context: ContextTypes.DEFAULT_TYPE):
args = context.args
chat = update.effective_chat
user = update.effective_user
msg = update.effective_message
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
if args:
fedinfo = sql.get_fed_info(args[0])
if not fedinfo:
await send_message(
update.effective_message, "This Federation does not exist!"
)
return
isowner = is_user_fed_owner(args[0], user.id)
if not isowner:
await send_message(
update.effective_message,
"Only federation creator can set federation logs.",
)
return
setlog = sql.set_fed_log(args[0], chat.id)
if setlog:
await send_message(
update.effective_message,
"Federation log `{}` has been set to {}".format(
fedinfo["fname"],
chat.title,
),
parse_mode="markdown",
)
else:
await send_message(
update.effective_message,
"You have not provided your federated ID!",
)
async def unset_fed_log(update: Update, context: ContextTypes.DEFAULT_TYPE):
args = context.args
chat = update.effective_chat
user = update.effective_user
msg = update.effective_message
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
if args:
fedinfo = sql.get_fed_info(args[0])
if not fedinfo:
await send_message(
update.effective_message, "This Federation does not exist!"
)
return
isowner = is_user_fed_owner(args[0], user.id)
if not isowner:
await send_message(
update.effective_message,
"Only federation creator can set federation logs.",
)
return
setlog = sql.set_fed_log(args[0], None)
if setlog:
await send_message(
update.effective_message,
"Federation log `{}` has been revoked on {}".format(
fedinfo["fname"],
chat.title,
),
parse_mode="markdown",
)
else:
await send_message(
update.effective_message,
"You have not provided your federated ID!",
)
async def subs_feds(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
msg = update.effective_message
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
fedinfo = sql.get_fed_info(fed_id)
if not fed_id:
await send_message(
update.effective_message, "This group is not in any federation!"
)
return
if is_user_fed_owner(fed_id, user.id) is False:
await send_message(update.effective_message, "Only fed owner can do this!")
return
if args:
getfed = sql.search_fed_by_id(args[0])
if getfed is False:
await send_message(
update.effective_message,
"Please enter a valid federation id.",
)
return
subfed = sql.subs_fed(args[0], fed_id)
if subfed:
await send_message(
update.effective_message,
"Federation `{}` has subscribe the federation `{}`. Every time there is a Fedban from that federation, this federation will also banned that user.".format(
fedinfo["fname"],
getfed["fname"],
),
parse_mode="markdown",
)
get_fedlog = await sql.get_fed_log(args[0])
if get_fedlog:
if int(get_fedlog) != int(chat.id):
await bot.send_message(
get_fedlog,
"Federation `{}` has subscribe the federation `{}`".format(
fedinfo["fname"],
getfed["fname"],
),
parse_mode="markdown",
message_thread_id=(
update.effective_message.message_thread_id
if chat.is_forum
else None
),
)
else:
await send_message(
update.effective_message,
"Federation `{}` already subscribe the federation `{}`.".format(
fedinfo["fname"],
getfed["fname"],
),
parse_mode="markdown",
)
else:
await send_message(
update.effective_message,
"You have not provided your federated ID!",
)
async def unsubs_feds(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
msg = update.effective_message
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
fedinfo = sql.get_fed_info(fed_id)
if not fed_id:
await send_message(
update.effective_message, "This group is not in any federation!"
)
return
if is_user_fed_owner(fed_id, user.id) is False:
await send_message(update.effective_message, "Only fed owner can do this!")
return
if args:
getfed = sql.search_fed_by_id(args[0])
if getfed is False:
await send_message(
update.effective_message,
"Please enter a valid federation id.",
)
return
subfed = sql.unsubs_fed(args[0], fed_id)
if subfed:
await send_message(
update.effective_message,
"Federation `{}` now unsubscribe fed `{}`.".format(
fedinfo["fname"],
getfed["fname"],
),
parse_mode="markdown",
)
get_fedlog = await sql.get_fed_log(args[0])
if get_fedlog:
if int(get_fedlog) != int(chat.id):
await bot.send_message(
get_fedlog,
"Federation `{}` has unsubscribe fed `{}`.".format(
fedinfo["fname"],
getfed["fname"],
),
parse_mode="markdown",
message_thread_id=(
update.effective_message.message_thread_id
if chat.is_forum
else None
),
)
else:
await send_message(
update.effective_message,
"Federation `{}` is not subscribing `{}`.".format(
fedinfo["fname"],
getfed["fname"],
),
parse_mode="markdown",
)
else:
await send_message(
update.effective_message,
"You have not provided your federated ID!",
)
async def get_myfedsubs(update: Update, context: ContextTypes.DEFAULT_TYPE):
args = context.args
chat = update.effective_chat
user = update.effective_user
msg = update.effective_message
if chat.type == "private":
await send_message(
update.effective_message,
"This command is specific to the group, not to our pm!",
)
return
fed_id = sql.get_fed_id(chat.id)
fedinfo = sql.get_fed_info(fed_id)
if not fed_id:
await send_message(
update.effective_message, "This group is not in any federation!"
)
return
if is_user_fed_owner(fed_id, user.id) is False:
await send_message(update.effective_message, "Only fed owner can do this!")
return
try:
getmy = sql.get_mysubs(fed_id)
except:
getmy = []
if len(getmy) == 0:
await send_message(
update.effective_message,
"Federation `{}` is not subscribing any federation.".format(
fedinfo["fname"],
),
parse_mode="markdown",
)
return
else:
listfed = "Federation `{}` is subscribing federation:\n".format(
fedinfo["fname"],
)
for x in getmy:
listfed += "- `{}`\n".format(x)
listfed += (
"\nTo get fed info `/fedinfo <fedid>`. To unsubscribe `/unsubfed <fedid>`."
)
await send_message(update.effective_message, listfed, parse_mode="markdown")
async def get_myfeds_list(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat = update.effective_chat
user = update.effective_user
msg = update.effective_message
fedowner = sql.get_user_owner_fed_full(user.id)
if fedowner:
text = "*You are owner of feds:\n*"
for f in fedowner:
text += "- `{}`: *{}*\n".format(f["fed_id"], f["fed"]["fname"])
else:
text = "*You are not have any feds!*"
await send_message(update.effective_message, text, parse_mode="markdown")
def is_user_fed_admin(fed_id, user_id):
fed_admins = sql.all_fed_users(fed_id)
if fed_admins is False:
return False
if int(user_id) in fed_admins or int(user_id) == OWNER_ID:
return True
else:
return False
def is_user_fed_owner(fed_id, user_id):
getsql = sql.get_fed_info(fed_id)
if getsql is False:
return False
getfedowner = ast.literal_eval(getsql["fusers"])
if getfedowner is None or getfedowner is False:
return False
getfedowner = getfedowner["owner"]
if str(user_id) == getfedowner or int(user_id) == OWNER_ID:
return True
else:
return False
# There's no handler for this yet, but updating for v12 in case its used
async def welcome_fed(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
chat = update.effective_chat
user = update.effective_user
fed_id = sql.get_fed_id(chat.id)
fban, fbanreason, fbantime = sql.get_fban_user(fed_id, user.id)
if fban:
await update.effective_message.reply_text(
"This user is banned in current federation! I will remove him.",
)
await bot.ban_chat_member(chat.id, user.id)
return True
else:
return False
def __stats__():
all_fbanned = sql.get_all_fban_users_global()
all_feds = sql.get_all_feds_users_global()
return "• {} banned users across {} Federations".format(
len(all_fbanned),
len(all_feds),
)
def __user_info__(user_id, chat_id):
fed_id = sql.get_fed_id(chat_id)
if fed_id:
fban, fbanreason, fbantime = sql.get_fban_user(fed_id, user_id)
info = sql.get_fed_info(fed_id)
infoname = info["fname"]
if int(info["owner"]) == user_id:
text = "Federation owner of: <b>{}</b>.".format(infoname)
elif is_user_fed_admin(fed_id, user_id):
text = "Federation admin of: <b>{}</b>.".format(infoname)
elif fban:
text = "Federation banned: <b>Yes</b>"
text += "\n<b>Reason:</b> {}".format(fbanreason)
else:
text = "Federation banned: <b>No</b>"
else:
text = ""
return text
# Temporary data
def put_chat(chat_id, value, chat_data):
# print(chat_data)
if value is False:
status = False
else:
status = True
chat_data[chat_id] = {"federation": {"status": status, "value": value}}
def get_chat(chat_id, chat_data):
# print(chat_data)
try:
value = chat_data[chat_id]["federation"]
return value
except KeyError:
return {"status": False, "value": False}
async def fed_owner_help(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.effective_message.reply_text(
"""*👑 Fed Owner Only:*
» `/newfed <fed_name>`*:* Creates a Federation, One allowed per user
» `/renamefed <fed_id> <new_fed_name>`*:* Renames the fed id to a new name
» `/delfed <fed_id>`*:* Delete a Federation, and any information related to it. Will not cancel blocked users
» `/fpromote <user>`*:* Assigns the user as a federation admin. Enables all commands for the user under `Fed Admins`
» `/fdemote <user>`*:* Drops the User from the admin Federation to a normal User
» `/subfed <fed_id>`*:* Subscribes to a given fed ID, bans from that subscribed fed will also happen in your fed
» `/unsubfed <fed_id>`*:* Unsubscribes to a given fed ID
» `/setfedlog <fed_id>`*:* Sets the group as a fed log report base for the federation
» `/unsetfedlog <fed_id>`*:* Removed the group as a fed log report base for the federation
» `/fbroadcast <message>`*:* Broadcasts a messages to all groups that have joined your fed
» `/fedsubs`*:* Shows the feds your group is subscribed to `(broken rn)`""",
parse_mode=ParseMode.MARKDOWN,
)
async def fed_admin_help(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.effective_message.reply_text(
"""*🔱 Fed Admins:*
» `/fban <user> <reason>`*:* Fed bans a user
» `/unfban <user> <reason>`*:* Removes a user from a fed ban
» `/fedinfo <fed_id>`*:* Information about the specified Federation
» `/joinfed <fed_id>`*:* Join the current chat to the Federation. Only chat owners can do this. Every chat can only be in one Federation
» `/leavefed <fed_id>`*:* Leave the Federation given. Only chat owners can do this
» `/setfrules <rules>`*:* Arrange Federation rules
» `/fedadmins`*:* Show Federation admin
» `/fbanlist`*:* Displays all users who are victimized at the Federation at this time
» `/fedchats`*:* Get all the chats that are connected in the Federation
» `/chatfed `*:* See the Federation in the current chat\n""",
parse_mode=ParseMode.MARKDOWN,
)
async def fed_user_help(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.effective_message.reply_text(
"""*🎩 Any user:*
» `/fbanstat`*:* Shows if you/or the user you are replying to or their username is fbanned somewhere or not
» `/fednotif <on/off>`*:* Federation settings not in PM when there are users who are fbaned/unfbanned
» `/frules`*:* See Federation regulations\n""",
parse_mode=ParseMode.MARKDOWN,
)
# <=================================================== HELP ====================================================>
__mod_name__ = "FEDS"
__help__ = """
➠ *Everything is fun, until a spammer starts entering your group, and you have to block it. Then you need to start banning more, and more, and it hurts*.
*But then you have many groups, and you don't want this spammer to be in one of your groups - how can you deal? Do you have to manually block it, in all your groups*?\n
*No longer!* *With Federation, you can make a ban in one chat overlap with all other chats*.\n
*You can even designate federation admins, so your trusted admin can ban all the spammers from chats you want to protect*.\n
➠ *Commands:*
➠ Feds are now divided into 3 sections for your ease.
» /fedownerhelp: Provides help for fed creation and owner only commands.
» /fedadminhelp: Provides help for fed administration commands.
» /feduserhelp: Provides help for commands anyone can use.
"""
# <================================================ HANDLER =======================================================>
function(CommandHandler("newfed", new_fed, block=False))
function(CommandHandler("delfed", del_fed, block=False))
function(CommandHandler("renamefed", rename_fed, block=False))
function(CommandHandler("joinfed", join_fed, block=False))
function(CommandHandler("leavefed", leave_fed, block=False))
function(CommandHandler("fpromote", user_join_fed, block=False))
function(CommandHandler("fdemote", user_demote_fed, block=False))
function(CommandHandler("fedinfo", fed_info, block=False))
function(DisableAbleCommandHandler("fban", fed_ban, block=False))
function(CommandHandler("unfban", unfban, block=False))
function(CommandHandler("fbroadcast", fed_broadcast, block=False))
function(CommandHandler("setfrules", set_frules, block=False))
function(CommandHandler("frules", get_frules, block=False))
function(CommandHandler("chatfed", fed_chat, block=False))
function(CommandHandler("fedadmins", fed_admin, block=False))
function(CommandHandler("fbanlist", fed_ban_list, block=False))
function(CommandHandler("fednotif", fed_notif, block=False))
function(CommandHandler("fedchats", fed_chats, block=False))
function(CommandHandler("importfbans", fed_import_bans, block=False))
function(DisableAbleCommandHandler(["fedstat", "fbanstat"], fed_stat_user, block=False))
function(CommandHandler("setfedlog", set_fed_log, block=False))
function(CommandHandler("unsetfedlog", unset_fed_log, block=False))
function(CommandHandler("subfed", subs_feds, block=False))
function(CommandHandler("unsubfed", unsubs_feds, block=False))
function(CommandHandler("fedsubs", get_myfedsubs, block=False))
function(CommandHandler("myfeds", get_myfeds_list, block=False))
function(CallbackQueryHandler(del_fed_button, pattern=r"rmfed_", block=False))
function(CommandHandler("fedownerhelp", fed_owner_help, block=False))
function(CommandHandler("fedadminhelp", fed_admin_help, block=False))
function(CommandHandler("feduserhelp", fed_user_help, block=False))
# <================================================ END =======================================================>
|