Guest User

Untitled

a guest
Jun 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. // EarrapePotter.js 2018/06/24 @author sanyo[jp]
  2. // This program works with node.js
  3.  
  4. // **Please add the library below**
  5. // npm install discord.js
  6. // npm install node-opus
  7.  
  8. // **Run**
  9. // node EarrapePotter.js
  10.  
  11. // Bot user token
  12. const TOKEN = "Token";
  13. // 音割れポッター sound file path
  14. const EARRAPE_POTTER = "音割れポッター.mp4"
  15. // play volume
  16. const VOLUME = 1
  17.  
  18. const Discord = require("discord.js");
  19.  
  20. const client = new Discord.Client();
  21.  
  22. client.on('message', msg => {
  23. var command = msg.content.trim().toLowerCase();
  24.  
  25. // play
  26. if (/^\?{6,}$/.test(command)) {
  27. var guild = client.guilds.get(msg.guild.id);
  28. var voiceChannel = getVoiceChannel(guild, msg.author);
  29. if (voiceChannel != null) {
  30. if (voiceChannel.connection == null) {
  31. // play
  32. voiceChannel.join().then(voiceConnection => {
  33. playEarrapePotter(voiceConnection);
  34. }).catch(console.log);
  35. } else {
  36. // replay
  37. var voiceConnection = voiceChannel.connection;
  38. var dispatcher = voiceConnection.dispatcher;
  39. dispatcher.removeAllListeners('end');
  40. dispatcher.end();
  41. playEarrapePotter(voiceConnection);
  42. }
  43. }
  44. }
  45.  
  46. // stop
  47. if (/^!\?*$/.test(command)) {
  48. var guild = client.guilds.get(msg.guild.id);
  49. var voiceChannel = getVoiceChannel(guild, msg.author);
  50. if (voiceChannel != null && voiceChannel.connection != null && voiceChannel.connection.dispatcher != null) {
  51. voiceChannel.connection.dispatcher.end();
  52. }
  53. }
  54. });
  55.  
  56. function playEarrapePotter(voiceConnection) {
  57. var dispatcher = voiceConnection.playFile(EARRAPE_POTTER);
  58. dispatcher.on('end', () => {
  59. voiceConnection.channel.leave();
  60. log("leave");
  61. });
  62. dispatcher.setVolume(VOLUME);
  63. log("play");
  64. }
  65.  
  66. function getVoiceChannel(guild, user) {
  67. for (var voiceChannel of guild.channels.values()) {
  68. if (voiceChannel.type !== 'voice')
  69. continue;
  70. for (var member of voiceChannel.members.values())
  71. if (member.user.id === user.id)
  72. return voiceChannel;
  73. }
  74. }
  75.  
  76. function log(str) {
  77. console.log(new Date().toString() + ": " + str);
  78. }
  79.  
  80. client.login(TOKEN);
  81.  
  82. client.on('ready', () => {
  83. log('ready!');
  84. });
Add Comment
Please, Sign In to add comment