You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. const fs = require('fs')
  2. let rssParser = require('rss-parser');
  3. const Discord = require('discord.js');
  4. const Client = require('./client');
  5. const fetch = require("node-fetch");
  6. let feeds = {
  7. "REDDIT": ['https://www.reddit.com/.rss', 'https://i.redd.it/rq36kl1xjxr01.png', "https://reddit.com"],
  8. "WIRED": ['https://www.wired.com/feed/rss', "https://www.wired.com/wp-content/themes/Phoenix/assets/images/article-icon.jpg", "https://www.wired.com"]
  9. };
  10. const threshold = 3;
  11. var CronJob = require('cron').CronJob;
  12. const {
  13. prefix,
  14. token,
  15. } = require('./config.json');
  16. var channels = '734387503464710165';
  17. async function execute() {
  18. let channel = "734687050707632239";
  19. let parser = new rssParser();
  20. let news = []
  21. for (item in feeds) {
  22. let feed = await parser.parseURL(feeds[item][0]);
  23. for (var i = feed.items.length - 1; i > feed.items.length - 1 - threshold; i--) {
  24. let data = feed.items[i];
  25. news.push({
  26. title: data.title,
  27. url: data.link,
  28. author: item,
  29. author_img: feeds[item][1],
  30. author_url: feeds[item][2],
  31. content: data.contentSnippet
  32. });
  33. }
  34. };
  35. news.forEach((item) => {
  36. let newsEmbed = new Discord.MessageEmbed()
  37. .setColor('#0099ff')
  38. .setTitle(item.title)
  39. .setURL(item.url)
  40. .setAuthor(item.author, item.author_img, item.author_url)
  41. .setDescription(item.content)
  42. .setTimestamp();
  43. client.channels.cache.get(`734687050707632239`).send(newsEmbed);
  44. });
  45. };
  46. const client = new Client();
  47. client.commands = new Discord.Collection();
  48. const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
  49. for (const file of commandFiles) {
  50. const command = require(`./commands/${file}`);
  51. client.commands.set(command.name, command);
  52. }
  53. console.log(client.commands);
  54. client.once('ready', () => {
  55. console.log('Ready!');
  56. console.log('Before job instantiation');
  57. const job = new CronJob('00 21 13 * * *', function () {
  58. client.channels.cache.get(`734387503464710165`).send(`rss time`);
  59. execute();
  60. const d = new Date();
  61. console.log('onTick:', d);
  62. });
  63. console.log('After job instantiation');
  64. job.start();
  65. });
  66. client.once('reconnecting', () => {
  67. console.log('Reconnecting!');
  68. });
  69. client.once('disconnect', () => {
  70. console.log('Disconnect!');
  71. });
  72. client.on('message', async message => {
  73. if (message.author.bot) return;
  74. if (!message.content.startsWith(prefix)) return;
  75. const args = message.content.slice(prefix.length).split(/ +/);
  76. const commandName = args.shift().toLowerCase();
  77. if (!client.commands.has(commandName)) return;
  78. const command = client.commands.get(commandName);
  79. const permitted_roles = client.commands.get(commandName)["roles"];
  80. const permitted_channels = client.commands.get(commandName)["channels"]
  81. has_roles = false
  82. if (permitted_roles) {
  83. for (i = 0; i < permitted_roles.length; i++) {
  84. if (message.member.roles.cache.has(permitted_roles[i])) {
  85. has_roles = true
  86. }
  87. }
  88. if (!has_roles && permitted_roles.length != 0) {
  89. message.reply('You are not allowed to run this command!');
  90. return;
  91. }
  92. }
  93. if (permitted_channels) {
  94. msg_channel = message.channel;
  95. channel_allowed = false
  96. for (i = 0; i < permitted_channels.length; i++) {
  97. if (permitted_channels[i] == "dm" && msg_channel instanceof Discord.DMChannel) {
  98. channel_allowed = true;
  99. break;
  100. } else if (permitted_channels[i] == msg_channel.id) {
  101. channel_allowed = true;
  102. break;
  103. }
  104. }
  105. if (!channel_allowed) {
  106. return;
  107. }
  108. }
  109. try {
  110. command.execute(message);
  111. } catch (error) {
  112. console.error(error);
  113. message.reply('There was an error trying to execute that command!');
  114. }
  115. });
  116. client.login(token);