Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. const Discord = require('discord.js');
  2. const fs = require("fs");
  3. const client = new Discord.Client();
  4.  
  5. let channels;
  6. let data = null;
  7. let token = "";
  8. let voiceLogChannel = "645340704624869376";
  9. let prefix = "$";
  10.  
  11. client.on('ready', () => {
  12. console.log(`Logged in as ${client.user.username}!`);
  13. updateChannels();
  14. setLogChannel(data.voiceLogChannel);
  15. });
  16.  
  17. client.on('voiceStateUpdate', (oldMember, newMember) => {
  18. // Check if voiceLogChannel has been set
  19. if (data.voiceLogChannel === "")
  20. return;
  21.  
  22. let username = oldMember.displayName;
  23. let oldVCID = oldMember.voiceChannelID;
  24. let newVCID = newMember.voiceChannelID;
  25.  
  26. let oldChannelName = (oldVCID != null && typeof oldVCID != undefined) ? channels.get(oldVCID).name : null;
  27. let newChannelName = (newVCID != null && typeof newVCID != undefined) ? channels.get(newVCID).name : null;
  28.  
  29. if (oldChannelName === null)
  30. voiceLogChannel.sendMessage(`${username} connected to voice and joined ${newChannelName}`);
  31. else if (newChannelName === null)
  32. voiceLogChannel.sendMessage(`${username} disconnected`);
  33. else
  34. voiceLogChannel.sendMessage(`${username} moved to ${newChannelName}`);
  35. });
  36.  
  37. client.on('channelCreate', (channel) => {
  38. updateChannels();
  39. });
  40.  
  41. client.on('channelDelete', (channel) => {
  42. updateChannels();
  43. });
  44.  
  45. client.on('message', (message) => {
  46. let msg = message.content;
  47.  
  48. if (!msg.startsWith(prefix))
  49. return;
  50.  
  51. // extract command and parameters
  52. let cmd = msg.replace(prefix, '').slice(0, msg.indexOf(' ') - 1);
  53. let params = msg.slice(msg.indexOf(' ') + 1, msg.length + 1);
  54.  
  55. // check if the user has permission to make changes to the bot
  56. if (!message.member.hasPermission("ADMINISTRATOR", true))
  57. return;
  58. else {
  59. switch(cmd) {
  60. case "setLogChannel":
  61. // Primitive implementation, pls change
  62. setLogChannel(params, message.channel);
  63. writedata();
  64. break;
  65. default:
  66. message.channel.sendMessage("Sorry, I don't recognize that command T_T.");
  67. }
  68. }
  69. });
  70.  
  71. let readdata = function () {
  72. data = JSON.parse(fs.readFileSync("data.json"));
  73. token = data.token;
  74. prefix = data.prefix;
  75. }
  76.  
  77. let writedata = function () {
  78. fs.writeFile('data.json', JSON.stringify(data), 'utf8', () => {
  79. console.log('Data written successfully!');
  80. });
  81. }
  82.  
  83. let updateChannels = function () {
  84. channels = client.channels;
  85. }
  86.  
  87. let setLogChannel = function (channel, msgChannel) {
  88. voiceLogChannel = channels.get(channels.findKey('name', channel));
  89.  
  90. // Return if no channel has been set
  91. if (typeof voiceLogChannel === 'undefined')
  92. return;
  93. else if (typeof voiceLogChannel === 'undefined' && msgChannel) {
  94. msgChannel.sendMessage(`Couldn't find channel '${channel}'`);
  95. return;
  96. }
  97. else if (voiceLogChannel.type === 'voice' && msgChannel) {
  98. msgChannel.sendMessage(`Can only log to text channels`);
  99. return;
  100. }
  101.  
  102. data.voiceLogChannel = channel;
  103.  
  104. if (msgChannel)
  105. msgChannel.sendMessage(`Channel for logging set to '${channel}'`);
  106. }
  107.  
  108. readdata();
  109. client.login(token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement