forked from 4ndv/lolbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlolbot.py
125 lines (92 loc) · 3.2 KB
/
lolbot.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
# -*- coding: utf-8 -*-
import os
import sys
import time
from vkplus import VkPlus
import settings
def main():
try:
BLACKLIST = settings.blacklist
except:
BLACKLIST = (0, 0)
try:
path = settings.path
except:
path = 'plugins/'
global cmds
cmds = {}
plugins = {}
global lastmessid
lastmessid = 0
print('Авторизация в вк...')
vk = VkPlus(settings.vk_login, settings.vk_password, settings.vk_app_id)
print('Успешная авторизация с аккаунтом: ' + settings.vk_login)
print('Подгружаем плагины...')
print('---------------------------')
# Подгружаем плагины
sys.path.insert(0, path)
for f in os.listdir(path):
fname, ext = os.path.splitext(f)
if ext == '.py':
mod = __import__(fname)
plugins[fname] = mod.Plugin(vk)
sys.path.pop(0)
print('---------------------------')
# Регистрируем плагины
for plugin in plugins.values():
for key, value in plugin.getkeys().items():
cmds[key] = value
print('Приступаю к приему сообщений')
while True:
# обозначаем что аккаунт онлайн
vk.method('account.setOnline')
values = {
'out': 0,
'offset': 0,
'count': 20,
'time_offset': 50,
'filters': 0,
'preview_length': 0,
'last_message_id': lastmessid
}
response = vk.method('messages.get', values)
if response is not None and response['items']:
lastmessid = response['items'][0]['id']
for item in response['items']:
if item['read_state'] == 0 and item['user_id'] not in BLACKLIST:
command(item, cmds)
vk.markasread(item['id']) # Помечаем прочитанным
time.sleep(0.5)
def command(message, cmds):
if message['body'] == u'':
return
words = message['body'].split()
try:
prefixes = settings.prefixes
except:
prefixes = ['lolbot', u'лолбот', u'лб', u'lb', u'фб', u'файнбот', u'fb', u'finebot']
if words[0].lower() in prefixes:
print('> ' + message['body']).encode('utf-8')
if len(words) > 1 and words[1] in cmds:
command_execute(message, words[1].lower(), words[2:])
def command_execute(message, plugin, params):
if plugin and plugin in cmds:
# Помечаем прочитанным перед выполнением команды.
vk.markasread(message['id'])
# Приоритеты аргументов:
# 0. message - всегда есть.
# 1. params, with_args, args
# 0
args = [message]
# 1
if any(s in cmds[plugin].plugin_type for s in ['params', 'with_args', 'args']):
args.append(params)
# 2
if 'settings' in cmds[plugin].plugin_type:
args.append(settings)
cmds[plugin].call(*args)
return True
else:
return False
if __name__ == '__main__':
main()