bitbot-3.11-fork/modules/stats.py

92 lines
2.8 KiB
Python
Raw Normal View History

2016-05-09 15:14:12 +00:00
import time
from src import ModuleManager, utils
2016-05-09 15:14:12 +00:00
class Module(ModuleManager.BaseModule):
def _uptime(self):
return utils.to_pretty_time(int(time.time()-self.bot.start_time))
@utils.hook("received.command.uptime")
2016-05-09 15:14:12 +00:00
def uptime(self, event):
"""
:help: Show my uptime
"""
event["stdout"].write("Uptime: %s" % self._uptime())
@utils.hook("api.uptime")
def uptime_api(self, event):
return self._uptime()
2016-05-09 15:14:12 +00:00
def _stats(self):
2016-05-09 15:14:12 +00:00
networks = len(self.bot.servers)
channels = 0
users = 0
for server in self.bot.servers.values():
channels += len(server.channels)
users += len(server.users)
return [networks, channels, users]
2016-05-09 15:14:12 +00:00
@utils.hook("received.command.stats")
def stats(self, event):
"""
:help: Show my network/channel/user stats
"""
networks, channels, users = self._stats()
2016-05-09 15:14:12 +00:00
response = "I currently have %d network" % networks
if networks > 1:
response += "s"
response += ", %d channel" % channels
if channels > 1:
response += "s"
response += " and %d visible user" % users
if users > 1:
response += "s"
event["stdout"].write(response)
@utils.hook("api.stats")
def stats_api(self, event):
networks, channels, users = self._stats()
return {"networks": networks, "channels": channels, "users": users}
2018-10-04 16:10:15 +00:00
2018-10-04 16:18:40 +00:00
@utils.hook("api.servers")
def servers_api(self, event):
servers = {}
for server in self.bot.servers.values():
servers[server.id] = {
"hostname": server.target_hostname,
"port": server.port,
"tls": server.tls,
"alias": server.alias
}
return servers
def _channel_stats(self, channel):
return {
"users": len(channel.users),
"topic": channel.topic
}
2018-10-04 16:10:15 +00:00
@utils.hook("api.channels")
def channels_api(self, event):
print(event["path"])
2018-10-04 16:21:40 +00:00
if event["path"]:
server_id = event["path"][0]
2018-10-04 16:10:15 +00:00
if not server_id.isdigit():
return None
server_id = int(server_id)
server = self.bot.get_server(server_id)
if not server:
return None
channels = {}
for channel in server.channels.values():
channels[channel.name] = self._channel_stats(channel)
2018-10-04 16:10:15 +00:00
return channels
else:
channels = {}
for server in self.bot.servers.values():
channels[server.id] = {}
for channel in server.channels.values():
channels[server.id][str(channel)] = self._channel_stats(
channel)
2018-10-04 16:10:15 +00:00
return channels