birthday.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. registerPlugin({
  2. name: 'Birthday Script',
  3. version: '1.0',
  4. description: 'create birthday notifications',
  5. author: 'mcj201',
  6. vars: [
  7. {
  8. name: 'message',
  9. title: 'The message that should be displayed. (%n = nickname, %b = list of birthdays)',
  10. type: 'multiline'
  11. },
  12. {
  13. name: 'type',
  14. title: 'Message-Type',
  15. type: 'select',
  16. options: [
  17. 'Private chat',
  18. 'Poke'
  19. ]
  20. },
  21. {
  22. name: 'nDays',
  23. title: 'send the notification upto this amount of days after birthday',
  24. type: 'number'
  25. },
  26. {
  27. name: 'serverGroup',
  28. title: 'Server group name/id for birthdays',
  29. type: 'string'
  30. }
  31. ],
  32. autorun: true
  33. }, function(sinusbot, config, meta) {
  34. const event = require('event')
  35. const engine = require('engine')
  36. const backend = require('backend')
  37. const format = require('format')
  38. var store = require('store');
  39. engine.log(`Loaded ${meta.name} v${meta.version} by ${meta.author}.`)
  40. event.on('load', () => {
  41. const command = require('command');
  42. if (!command) {
  43. engine.log('command.js library not found! Please download command.js and enable it to be able use this script!');
  44. return;
  45. }
  46. let bDays = store.get('birthdays') || {};
  47. let notifs = store.get('birthday_notifications') || {};
  48. setInterval(updateServerGroups, 1000 * 60);
  49. event.on('clientMove', ({ client, fromChannel }) => {
  50. const avail = getNotifications(client, 30);
  51. if (avail.length < 1)
  52. return;
  53. let msgs = []
  54. for(const uid of avail) {
  55. msgs.push(`${getName(uid)}: ${formatDate(getBday(uid))}`);
  56. }
  57. const msg = config.message.replace('%n', client.name()).replace('%b', msgs.join('\r\n'))
  58. if (!fromChannel) {
  59. if (config.type == '0') {
  60. client.chat(msg)
  61. } else {
  62. client.poke(msg)
  63. }
  64. updateServerGroups();
  65. }
  66. })
  67. command.createCommand('birthdays')
  68. .help('Show user birthdays')
  69. .manual('Show user birthdays from DB.')
  70. .exec((client, args, reply, ev) => {
  71. let msgs = ["List of saved birthdays:"];
  72. for(const uid in bDays) {
  73. msgs.push(`${getName(uid)}: ${formatDate(getBday(uid))}`);
  74. }
  75. reply(msgs.join('\r\n'));
  76. });
  77. command.createCommand('birthday')
  78. .addArgument(command.createArgument('string').setName('date'))
  79. .help('Set user birthdays')
  80. .manual('Save user birthdays to DB.')
  81. .exec((client, args, reply, ev) => {
  82. var date = args.date.split('.');
  83. if(date.length >= 2) {
  84. let m = date[0];
  85. date[0] = date[1];
  86. date[1] = m;
  87. }
  88. date = new Date(date);
  89. if(args.date === "") {
  90. let date = getBday(ev.client.uid());
  91. if(date)
  92. reply(`Your birthday is ${formatDate(date)}.`);
  93. else
  94. reply(`Set your birthday first! e.g. !birthday 24.12.`);
  95. } else if(!isNaN(date)) {
  96. setBday(ev.client, date);
  97. reply(`Your birthday was set to ${formatDate(date)}.`);
  98. } else {
  99. setBday(ev.client, date);
  100. reply(`Your birthday has been cleared.`);
  101. }
  102. });
  103. function setBday(client, date) {
  104. if(isNaN(date) && bDays[client.uid()]) {
  105. delete bDays[client.uid()];
  106. } else if(!isNaN(date)) {
  107. bDays[client.uid()] = [client.name(), date];
  108. }
  109. store.set('birthdays', bDays);
  110. }
  111. function getBday(uid) {
  112. if(!bDays[uid])
  113. return undefined;
  114. let [name, date] = bDays[uid];
  115. if(date)
  116. return new Date(date);
  117. else
  118. return undefined;
  119. }
  120. function getName(uid) {
  121. let [name, date] = bDays[uid];
  122. return name;
  123. }
  124. function getNotifications(client, nDays = 30) {
  125. const start = new Date();
  126. start.setDate(start.getDate()-nDays);
  127. const now = new Date();
  128. let sentNotifs = notifs[client.uid()] || {};
  129. let avail = [];
  130. for(const uid in bDays) {
  131. let bDay = new Date(bDays[uid][1]);
  132. bDay.setFullYear((new Date()).getFullYear());
  133. let lastNotif = new Date(sentNotifs[uid]);
  134. if(bDay >= start && bDay <= now && (isNaN(lastNotif) || lastNotif < start)) {
  135. avail.push(uid);
  136. sentNotifs[uid] = now;
  137. }
  138. }
  139. notifs[client.uid()] = sentNotifs;
  140. store.set('birthday_notifications', notifs);
  141. return avail;
  142. }
  143. function formatDate(dt) {
  144. if(dt)
  145. return `${dt.getDate()}.${dt.getMonth()+1}.`;
  146. else
  147. return 'invalid date';
  148. }
  149. function updateServerGroups() {
  150. if(config.serverGroup === "")
  151. return;
  152. const now = new Date();
  153. for(const client of backend.getClients()) {
  154. if(bDays[client.uid()]) {
  155. const bDay = new Date(bDays[client.uid()][1]);
  156. let hasGroup = false;
  157. for(const group of client.getServerGroups()) {
  158. hasGroup |= group.name() === config.serverGroup || group.id() == config.serverGroup;
  159. }
  160. if(bDay.getDate() === now.getDate() && bDay.getMonth() === now.getMonth()) {
  161. if(!hasGroup) client.addToServerGroup(config.serverGroup);
  162. } else {
  163. if(hasGroup) client.removeFromServerGroup(config.serverGroup);
  164. }
  165. }
  166. }
  167. }
  168. });
  169. });