Compare commits

...

10 Commits

Author SHA1 Message Date
GandC Snow
695d122339 add new command 2021-12-08 00:31:39 +03:00
GandC Snow
41fe31f477 improvements 2021-12-08 00:28:29 +03:00
GandC Snow
994a5425da strange fix 2021-11-05 23:49:05 +03:00
GandC Snow
3f8cc40ac0 message to nick 2021-11-05 18:11:32 +03:00
GandC Snow
f7f44d5556 remove broken error message 2021-10-29 20:54:42 +03:00
GandC Snow
6abf4de124 fix 2021-10-29 20:42:29 +03:00
GandC Snow
cb6eb51c1e change notice to privmsg 2021-10-29 20:40:35 +03:00
GandC Snow
7c5ba8d332 fix using section; add new command 2021-10-21 22:10:46 +03:00
GandC Snow
c14fa3e2d7 fix using section; add new command 2021-10-21 22:09:28 +03:00
GandC Snow
312aa354f6 fix using section 2021-10-21 22:08:12 +03:00
2 changed files with 28 additions and 26 deletions

View File

@ -1,6 +1,6 @@
Info bot
========
Statbot -- это irc бот, который может рассказать вам о загруженности вашего пк.
Statbot -- это irc бот, который может рассказать вам о загруженности вашего пк или сервера.
installing
@ -13,9 +13,9 @@ pip3 install irc psutil
Using
-----
```bash
python3 bot.py
python3 bot.py '#main' GandC_cenral_bot s1.tomas.gl
```
Commands
--------
Всего доступно 7 команд: help, stats, uptime, ram, cpu, uname, network. По желанию вы можете добавить свои.
Всего доступно 8 команд: help, uptime, ram, cpu, uname, network, temp, stop. По желанию вы можете добавить свои.

48
bot.py
View File

@ -3,6 +3,11 @@ import irc.strings
import psutil, platform
from sys import argv
channel = argv[1]
nickname = argv[2]
server = argv[3]
if len(argv) !=4:
print('Usage: {} channel nickname server'.format(argv[0]))
def get_size(bytes, suffix="B"):
factor = 1024
@ -16,6 +21,10 @@ class TestBot(irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel
self.connection.add_global_handler('disconnect', self.on_disconnect)
def on_disconnect(self, connection, event):
exit(1)
def on_welcome(self, c, e):
c.mode(self._nickname, '+B')
@ -33,19 +42,17 @@ class TestBot(irc.bot.SingleServerIRCBot):
return
def do_command(self, e, cmd):
nick = e.source.nick
#nick = e.source.nick ## Uncomment when need to send to user not channel
c = self.connection
if cmd.lower() == "uptime":
result = subprocess.run(["uptime"], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
output = result.stdout.decode().strip()
c.notice(nick, output)
c.privmsg(channel, output)
elif cmd.lower() == "uname":
# >>> print(f"Version: {uname.version}")
# Version: #1 SMP Debian 4.19.208-1 (2021-09-29)
uname = platform.uname()
end = 'System name:' + uname.version
c.notice(nick, end)
c.privmsg(channel, end)
elif cmd.lower() == "ram":
svmem = psutil.virtual_memory()
@ -53,52 +60,47 @@ class TestBot(irc.bot.SingleServerIRCBot):
used = 'Used Ram: ' + get_size(svmem.used)
percentage = 'Percent:' + str(svmem.percent) + '%'
avail = 'Available:' + get_size(svmem.available)
c.notice(nick, total)
c.notice(nick, avail)
c.notice(nick, used)
c.notice(nick, percentage)
c.privmsg(channel, total)
c.privmsg(channel, avail)
c.privmsg(channel, used)
c.privmsg(channel, percentage)
elif cmd.lower() == "cpu":
percent = psutil.cpu_percent()
end = 'CPU load: {}%'.format(percent)
c.notice(nick, end)
c.privmsg(channel, end)
elif cmd.lower() == "network":
if_addrs = psutil.net_if_addrs()
for interface_name, interface_addresses in if_addrs.items():
header = 'Interface: ' + interface_name
c.notice(nick, header)
c.privmsg(channel, header)
for address in interface_addresses:
if str(address.family) == 'AddressFamily.AF_INET':
ip = 'IP Address: ' + address.address
mask = 'Netmask: ' + address.netmask
c.notice(nick, ' {} / {}'.format(ip, mask))
c.privmsg(channel, ' {} / {}'.format(ip, mask))
elif cmd.lower() == 'temp':
temperatures = psutil.sensors_temperatures()
for name, device_temp in temperatures.items():
header = 'Device: ' + name
c.notice(nick, header)
c.privmsg(channel, header)
for sensor in device_temp:
sensor_name = sensor.label
if not sensor_name: sensor_name = '<no name>'
c.notice(nick, ' Sensor name: {}, current: {}'.format(sensor_name, sensor.current))
c.privmsg(channel, ' Sensor name: {}, current: {}'.format(sensor_name, sensor.current))
elif cmd.lower() == "help":
end = 'Available commands are: help, uptime, ram, cpu, uname, network, temp'
c.notice(nick, end)
c.privmsg(channel, end)
elif cmd.lower() == "stop":
exit(1)
else:
end = 'Unknow command. Sorry'
c.notice(nick, end)
c.privmsg(channel, end)
def main():
# print(argv)
if len(argv) != 4:
print('Usage: {} channel nickname server'.format(argv[0]))
exit(1)
channel = argv[1]
nickname = argv[2]
server = argv[3]
bot = TestBot(channel, nickname, server)
bot.start()