birthday.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. autorun: true
  28. }, function(sinusbot, config, meta) {
  29. const event = require('event')
  30. const engine = require('engine')
  31. const backend = require('backend')
  32. const format = require('format')
  33. var store = require('store');
  34. engine.log(`Loaded ${meta.name} v${meta.version} by ${meta.author}.`)
  35. event.on('load', () => {
  36. const command = require('command');
  37. if (!command) {
  38. engine.log('command.js library not found! Please download command.js and enable it to be able use this script!');
  39. return;
  40. }
  41. let bDays = store.get('birthdays') || {};
  42. let notifs = store.get('birthday_notifications') || {};
  43. event.on('clientMove', ({ client, fromChannel }) => {
  44. const avail = getNotifications(client, 30);
  45. if (avail.length < 1)
  46. return;
  47. let msgs = []
  48. for(const uid of avail) {
  49. msgs.push(`${getName(uid)}: ${formatDate(getBday(uid))}`);
  50. }
  51. const msg = config.message.replace('%n', client.name()).replace('%b', msgs.join('\r\n'))
  52. if (!fromChannel) {
  53. if (config.type == '0') {
  54. client.chat(msg)
  55. } else {
  56. client.poke(msg)
  57. }
  58. }
  59. })
  60. command.createCommand('birthdays')
  61. .help('Show user birthdays')
  62. .manual('Show user birthdays from DB.')
  63. .exec((client, args, reply, ev) => {
  64. let msgs = ["List of saved birthdays:"];
  65. for(const uid in bDays) {
  66. msgs.push(`${getName(uid)}: ${formatDate(getBday(uid))}`);
  67. }
  68. reply(msgs.join('\r\n'));
  69. });
  70. command.createCommand('birthday')
  71. .addArgument(command.createArgument('string').setName('date'))
  72. .help('Set user birthdays')
  73. .manual('Save user birthdays to DB.')
  74. .exec((client, args, reply, ev) => {
  75. var date = args.date.split('.');
  76. if(date.length >= 2) {
  77. let m = date[0];
  78. date[0] = date[1];
  79. date[1] = m;
  80. }
  81. date = new Date(date);
  82. if(args.date === "") {
  83. let date = getBday(ev.client.uid());
  84. if(date)
  85. reply(`Your birthday is ${formatDate(date)}.`);
  86. else
  87. reply(`Set your birthday first! e.g. !birthday 24.12.`);
  88. } else if(!isNaN(date)) {
  89. setBday(ev.client, date);
  90. reply(`Your birthday was set to ${formatDate(date)}.`);
  91. } else {
  92. setBday(ev.client, date);
  93. reply(`Your birthday has been cleared.`);
  94. }
  95. });
  96. function setBday(client, date) {
  97. if(isNaN(date) && bDays[client.uid()]) {
  98. delete bDays[client.uid()];
  99. } else if(!isNaN(date)) {
  100. bDays[client.uid()] = [client.name(), date];
  101. }
  102. store.set('birthdays', bDays);
  103. }
  104. function getBday(uid) {
  105. if(!bDays[uid])
  106. return undefined;
  107. let [name, date] = bDays[uid];
  108. if(date)
  109. return new Date(date);
  110. else
  111. return undefined;
  112. }
  113. function getName(uid) {
  114. let [name, date] = bDays[uid];
  115. return name;
  116. }
  117. function getNotifications(client, nDays = 30) {
  118. const start = new Date();
  119. start.setDate(start.getDate()-nDays);
  120. const now = new Date();
  121. let sentNotifs = notifs[client.uid()] || {};
  122. let avail = [];
  123. for(const uid in bDays) {
  124. let bDay = new Date(bDays[uid][1]);
  125. bDay.setFullYear((new Date()).getFullYear());
  126. let lastNotif = new Date(sentNotifs[uid]);
  127. if(bDay >= start && bDay <= now && (isNaN(lastNotif) || lastNotif < start)) {
  128. avail.push(uid);
  129. sentNotifs[uid] = now;
  130. }
  131. }
  132. notifs[client.uid()] = sentNotifs;
  133. store.set('birthday_notifications', notifs);
  134. return avail;
  135. }
  136. function formatDate(dt) {
  137. if(dt)
  138. return `${dt.getDate()}.${dt.getMonth()+1}.`;
  139. else
  140. return 'invalid date';
  141. }
  142. });
  143. });