forked from TheJunky/PyCore
/
psBot.py
118 lines (106 loc) · 4.75 KB
/
psBot.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
#!/usr/bin/env python
import subprocess
import time
from subspace_bot.helpers import bot_main
from subspace_bot.interface import BotInterface
from subspace_bot.constants.commands import *
from subspace_bot.constants.events import *
from subspace_bot.constants.messages import *
class Bot(BotInterface):
def __init__(self, ssbot, md):
BotInterface.__init__(self, ssbot, md)
# register Your Module
ssbot.register_module_info(
__name__,
"processbot",
"The Junky",
"displays server stats",
".01"
)
# register your commands
self.cmd_id_kb = ssbot.register_command(
'!killbot', # command
"!kb", # alias can be None if no alias
1, # min access level to use this command
COMMAND_LIST_PP, # what types of messages this command will accept
"smods", # category this command belongs to
"", # what args if any this command accepts use "" if none
"*******") # short description of the command displayed in help
self.cmd_id_lkb = ssbot.register_command(
'!listbots', # command
"!lk", # alias can be None if no alias
1, # min access level to use this command
COMMAND_LIST_PP, # what types of messages this command will accept
"smods", # category this command belongs to
"", # what args if any this command accepts use "" if none
"*******") # short description of the command displayed in help
self.killable_bots = {
# display name:(accesslevel to kill, bot name in ps,
# current status)
"Pubvents": (1, "EGSoft.BotConsoleApp.exe", None)
}
self.log_player = None
self.log_time = time.time()
def killProcess(self, procname):
p = subprocess.Popen(
'taskkill /f /im ' + procname, stdout=subprocess.PIPE)
r = p.communicate()
return r
# os.system('taskkill /f /im '+ procname)
# for proc in psutil.process_iter():
# print proc.name
# if proc.name == procname:
# proc.kill()
# return True
# return False
#
def handle_events(self, ssbot, event):
if event.type == EVENT_COMMAND:
if event.command.id == self.cmd_id_kb:
if len(event.arguments) > 0:
bot = self.killable_bots.get(event.arguments[0], None)
if bot:
if event.plvl >= bot[0]:
ssbot.send_private_message(event.player, "ok")
f = self.killProcess(bot[1])
# for l in f :
ssbot.send_private_message(event.player, f[0])
ssbot.send_public_message("?alert %s killed %s" % (
event.pname, event.arguments[0]))
else:
ssbot.send_private_message(
event.player,
("Access Denied min level to kill that "
"bot is %s") % str(bot[0])
)
else:
ssbot.send_private_message(
event.player,
"bot not found in killable process list %s" %
event.arguments[0]
)
if event.command.id == self.cmd_id_lkb:
ssbot.send_private_message(event.player, "--Killable bots--")
for key, value in self.killable_bots.iteritems():
ssbot.send_private_message(
event.player, key + " -- " + str(value))
if (event.type == EVENT_MESSAGE and
event.message_type == MESSAGE_TYPE_ARENA and
self.log_player is not None):
m = event.message
if m.startswith(("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")):
ssbot.send_private_message(self.log_player, m)
if (event.type == EVENT_TICK and
self.log_player is not None and
self.log_time >= time.time()):
self.log_player = None
if (event.type == EVENT_LEAVE and event.player == self.log_player):
self.log_player = None
def cleanup(self):
# put any cleanup code in here this is called when bot is about to die
pass
if __name__ == '__main__':
# bot runs in this if not run by master
# generic main function for when you run bot in standalone mode
# we pass in the Bot class to the function, so it can run it for us
bot_main(Bot, False, True, "#master")