-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
158 lines (119 loc) · 4.49 KB
/
main.py
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
from functools import wraps
from requests import JSONDecodeError, get
from telethon import Button, TelegramClient, events
from telethon.errors.rpcerrorlist import UserNotParticipantError
from config import *
from database import *
########################################### CLIENT FUNCS ################
kuki = TelegramClient("KUKIBOT", APP_ID, APP_HASH).start(bot_token=BOT_TOKEN)
########################################### HANDLER AND RYTS CHK #########
def cmd(**args):
args["pattern"] = "^(?i)[/!]" + args["pattern"] + f"(?: |$|{BOT_USERNAME})(.*)"
def decorator(func):
kuki.add_event_handler(func, events.NewMessage(**args))
return func
return decorator
def cbk(**args):
def decorator(func):
kuki.add_event_handler(func, events.CallbackQuery(**args))
return func
return decorator
def ryts(func):
@wraps(func)
async def admin_check(e):
try:
perms = await e.client.get_permissions(e.chat_id, e.sender_id)
if not perms.is_admin:
return await e.reply("You are not an admin!")
elif not perms.change_info:
return await e.reply("You don't have permission to change info!")
elif e.is_private:
return await e.reply("You can't use this command in a private chat!")
else:
return await func(e)
except (UserNotParticipantError, ValueError):
return await e.reply("You are not in this chat!")
return admin_check
def aichat(func):
@wraps(func)
async def ai_check(e):
if e.is_private:
await func(e)
elif Chat.is_ai_chat(e.chat_id):
await func(e)
else:
return
return ai_check
############################################# API CHAT ###################
class CONV:
def __init__(self):
self.bot = BOT_NAME
self.owner = OWNER
self.token = KUKI_TOKEN
self.url = "https://kukiapi.xyz/api"
def message(self, text):
try:
txt = get(
self.url
+ f"/apikey={self.token}/{self.bot}/{self.owner}/message={text}",
timeout=10,
)
return txt.json()["reply"]
except (JSONDecodeError, TimeoutError):
return "KUKI is not responding. Try again later."
except Exception as e:
return e
########################################## MSG HANDLERS ##################
@cmd(pattern="start")
async def start(e):
await e.reply("hey, I'm {}, a chatbot for Telegram.\n".format(BOT_NAME))
@cmd(pattern="setchat")
@ryts
async def setchat(e):
if e.is_private:
await e.reply("This command can't be used in private chats.")
return
buttons = Button.inline(
"Enable", data="enable_{}".format(e.sender_id)
), Button.inline("Disable", data="disable_{}".format(e.sender_id))
await e.reply("AI chat setup", buttons=buttons)
@cbk(pattern="enable_(.*)")
async def enable_ai(e):
user = int(e.pattern_match.group(1))
await kuki.get_permissions(e.chat_id, e.sender_id)
if not user == e.sender_id:
return await e.answer("You ain't the one who used this command.", alert=True)
elif Chat.is_ai_chat(e.chat_id):
return await e.edit("AI is already enabled in this chat.")
await e.edit(
"Successfully enabled Kuki Ai in **{}** by [{}](tg://user?id={})".format(
e.chat.title, e.sender.first_name, e.sender_id
)
)
Chat.add_chat(e.chat_id)
@cbk(pattern="disable_(.*)")
async def disable_ai(e):
user = int(e.pattern_match.group(1))
await kuki.get_permissions(e.chat_id, e.sender_id)
if not user == e.sender_id:
return await e.answer("You aint the one who used this command.", alert=True)
elif not Chat.is_ai_chat(e.chat_id):
return await e.edit("AI is already disabled in this chat.")
await e.edit(
"Successfully disabled Kuki Ai in **{}** by [{}](tg://user?id={})".format(
e.chat.title, e.sender.first_name, e.sender_id
)
)
Chat.rm_chat(e.chat_id)
@kuki.on(
events.NewMessage(incoming=True, func=lambda x: bool(x.mentioned) or x.is_private)
)
@aichat
async def kuki_handler(e):
c = CONV()
if e.text.startswith("/start") or not e.text:
return
await e.reply(c.message(e.raw_text))
################################## INITIALIZATION ########################
print("KUKI AI IS NOW ONLINE\n\nCONTACT @METAVOIDSUPPORT FOR QUERIES")
kuki.run_until_disconnected()