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.

87 lines
2.1 KiB

  1. const fs = require('fs')
  2. const Discord = require('discord.js');
  3. const Client = require('./client');
  4. const {
  5. prefix,
  6. token,
  7. } = require('./config.json');
  8. const client = new Client();
  9. client.commands = new Discord.Collection();
  10. const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
  11. for (const file of commandFiles) {
  12. const command = require(`./commands/${file}`);
  13. client.commands.set(command.name, command);
  14. }
  15. console.log(client.commands);
  16. client.once('ready', () => {
  17. console.log('Ready!');
  18. });
  19. client.once('reconnecting', () => {
  20. console.log('Reconnecting!');
  21. });
  22. client.once('disconnect', () => {
  23. console.log('Disconnect!');
  24. });
  25. client.on('message', async message => {
  26. if (message.author.bot) return;
  27. if (!message.content.startsWith(prefix)) return;
  28. const args = message.content.slice(prefix.length).split(/ +/);
  29. const commandName = args.shift().toLowerCase();
  30. if (!client.commands.has(commandName)) return;
  31. const command = client.commands.get(commandName);
  32. const permitted_roles = client.commands.get(commandName)["roles"];
  33. const permitted_channels = client.commands.get(commandName)["channels"]
  34. has_roles = false
  35. if (permitted_roles){
  36. for(i = 0; i < permitted_roles.length; i++){
  37. if(message.member.roles.cache.has(permitted_roles[i])){
  38. has_roles = true
  39. }
  40. }
  41. if (!has_roles && permitted_roles.length != 0){
  42. message.reply('You are not allowed to run this command!');
  43. return;
  44. }
  45. }
  46. if(permitted_channels){
  47. msg_channel = message.channel;
  48. channel_allowed = false
  49. for(i = 0; i < permitted_channels.length; i++){
  50. if(permitted_channels[i] == "dm" && msg_channel instanceof Discord.DMChannel){
  51. channel_allowed = true;
  52. break;
  53. }else if(permitted_channels[i] == msg_channel.id){
  54. channel_allowed = true;
  55. break;
  56. }
  57. }
  58. if(!channel_allowed){
  59. return;
  60. }
  61. }
  62. try {
  63. command.execute(message);
  64. } catch (error) {
  65. console.error(error);
  66. message.reply('There was an error trying to execute that command!');
  67. }
  68. });
  69. client.login(token);