channel-statistics.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. registerPlugin({
  2. name: 'Client Statistics Script',
  3. version: '1.0',
  4. description: 'log client events to db',
  5. author: 'mcj201',
  6. vars: [
  7. {
  8. name: 'host',
  9. title: 'MySQL Host',
  10. type: 'string'
  11. },
  12. {
  13. name: 'username',
  14. title: 'MySQL User',
  15. type: 'string'
  16. },
  17. {
  18. name: 'password',
  19. title: 'MySQL Password',
  20. type: 'password'
  21. },
  22. {
  23. name: 'database',
  24. title: 'MySQL Database',
  25. type: 'string'
  26. },
  27. ],
  28. autorun: false,
  29. requiredModules: [
  30. 'db'
  31. ]
  32. }, function(sinusbot, config, meta) {
  33. const db = require('db');
  34. const engine = require('engine');
  35. const backend = require('backend');
  36. const event = require('event');
  37. const helpers = require('helpers');
  38. let dbc = null;
  39. function reconnect() {
  40. if(!dbc) {
  41. dbc = db.connect({ driver: 'mysql', host: config.host, username: config.username, password: config.password, database: config.database }, function(err) {
  42. if (err) {
  43. engine.log(err);
  44. } else {
  45. engine.log('connection successful');
  46. }
  47. });
  48. }
  49. }
  50. reconnect();
  51. setInterval(reconnect, 1000 * 3600);
  52. let channelIdMap = {};
  53. let serverId = null;
  54. event.on('connect', () => {
  55. const serverinfo = backend.extended().getServerInfo();
  56. updateServer(serverinfo, function(id) {
  57. serverId = id;
  58. engine.log('serverId:', serverId);
  59. backend.getChannels().forEach(channel => updateChannel(serverId, channel, function(id) {
  60. channelIdMap[channel.id()] = id;
  61. updateChannelEvent(id, channel);
  62. }));
  63. });
  64. });
  65. event.on('clientMove', ({ client, fromChannel, toChannel }) => {
  66. if(fromChannel) updateChannelEvent(channelIdMap[fromChannel.id()], fromChannel);
  67. if(toChannel) updateChannelEvent(channelIdMap[toChannel.id()], toChannel);
  68. });
  69. event.on('channelCreate', (channel, client) => {
  70. updateChannel(serverId, channel, function(id) {
  71. channelIdMap[channel.id()] = id;
  72. updateChannelEvent(id, channel);
  73. });
  74. });
  75. event.on('channelUpdate', (channel, client) => {
  76. updateChannel(serverId, channel, function(id) {
  77. channelIdMap[channel.id()] = id;
  78. updateChannelEvent(id, channel);
  79. });
  80. });
  81. function updateServer(serverinfo, cb) {
  82. if(!dbc || !serverinfo) {
  83. engine.log('error on server update');
  84. return;
  85. }
  86. dbc.query("SELECT id, name FROM server WHERE uid = ?", serverinfo.UID(), function(err, res) {
  87. if (!err) {
  88. if(res.length > 0) {
  89. const serverId = res[0].id;
  90. dbc.exec("UPDATE server SET name = ? WHERE id = ?", serverinfo.name(), serverId);
  91. cb(serverId);
  92. } else {
  93. dbc.exec("INSERT INTO server (uid, name) VALUES (?, ?)", serverinfo.UID(), serverinfo.name(), function() {
  94. dbc.query("SELECT id FROM server WHERE uid = ?", serverinfo.UID(), function(err, res) {
  95. if(!err && res.length > 0) {
  96. cb(res[0].id);
  97. } else {
  98. console.log(err, res);
  99. }
  100. });
  101. });
  102. }
  103. } else {
  104. console.log(err, res);
  105. }
  106. });
  107. }
  108. function updateChannel(serverId, channel, cb) {
  109. if(!dbc || !serverId) {
  110. return;
  111. }
  112. const parentId = channel.parent() ? channel.parent().id() : null;
  113. dbc.query("SELECT * FROM channel WHERE channelId = ? AND serverId = ?", channel.id(), serverId, function(err, res) {
  114. if (!err) {
  115. if(res.length > 0) {
  116. const id = res[0].id;
  117. dbc.exec("UPDATE channel SET name = ?, parentId = ?, position = ?, description = ? WHERE id = ?",
  118. channel.name(), parentId, channel.position(), channel.description(), id);
  119. cb(id);
  120. } else {
  121. dbc.exec("INSERT INTO channel (channelId, name, serverId, parentId, position, description) VALUES (?, ?, ?, ?, ?, ?)",
  122. channel.id(), channel.name(), serverId, parentId, channel.position(), channel.description(), function() {
  123. dbc.query("SELECT id FROM channel WHERE channelId = ? AND serverId = ?", channel.id(), serverId, function(err, res) {
  124. if(!err && res.length > 0) {
  125. cb(res[0].id);
  126. } else {
  127. console.log(err, res);
  128. }
  129. });
  130. });
  131. }
  132. } else {
  133. console.log(err, res);
  134. }
  135. });
  136. }
  137. function updateChannelEvent(id, channel) {
  138. if(!dbc || !id) {
  139. return;
  140. }
  141. const clients = channel.getClientCount();
  142. dbc.query("SELECT * FROM channelEvent WHERE channelId = ? AND date > DATE_SUB(NOW(), INTERVAL 1 MINUTE) ORDER BY date DESC LIMIT 1", id, function(err, res) {
  143. if(!err) {
  144. if(res.length > 0) {
  145. engine.log('channel ' + channel.name() + ' updated to ' + clients + ' clients');
  146. dbc.exec("UPDATE channelEvent SET clientCount = ? WHERE id = ?", clients, res[0].id);
  147. } else {
  148. dbc.query("SELECT * FROM channelEvent WHERE channelId = ? ORDER BY date DESC LIMIT 1", id, function(err, res) {
  149. if(!err && res.length > 0 && res[0].clientCount === clients)
  150. return;
  151. if(clients > 0)
  152. engine.log('channel ' + channel.name() + ' has now ' + clients + ' clients');
  153. dbc.exec("INSERT INTO channelEvent (channelId, clientCount) VALUES (?, ?)", id, clients);
  154. });
  155. }
  156. } else {
  157. console.log(err, res);
  158. }
  159. });
  160. }
  161. });