Skip to content

Instantly share code, notes, and snippets.

@eajtciv
Created June 24, 2018 15:04
Show Gist options
  • Save eajtciv/fb72e34b3905395fb51d91414822c028 to your computer and use it in GitHub Desktop.
Save eajtciv/fb72e34b3905395fb51d91414822c028 to your computer and use it in GitHub Desktop.
音割れポッター discord bot
// EarrapePotter.js 2018/06/24 @author sanyo[jp]
// This program works with node.js
// **Please add the library below**
// npm install discord.js
// npm install node-opus
// **Run**
// node EarrapePotter.js
// Bot user token
const TOKEN = "Token";
// 音割れポッター sound file path
const EARRAPE_POTTER = "音割れポッター.mp4"
// play volume
const VOLUME = 1
const Discord = require("discord.js");
const client = new Discord.Client();
client.on('message', msg => {
var command = msg.content.trim().toLowerCase();
// play
if (/^\?{6,}$/.test(command)) {
var guild = client.guilds.get(msg.guild.id);
var voiceChannel = getVoiceChannel(guild, msg.author);
if (voiceChannel != null) {
if (voiceChannel.connection == null) {
// play
voiceChannel.join().then(voiceConnection => {
playEarrapePotter(voiceConnection);
}).catch(console.log);
} else {
// replay
var voiceConnection = voiceChannel.connection;
var dispatcher = voiceConnection.dispatcher;
dispatcher.removeAllListeners('end');
dispatcher.end();
playEarrapePotter(voiceConnection);
}
}
}
// stop
if (/^!\?*$/.test(command)) {
var guild = client.guilds.get(msg.guild.id);
var voiceChannel = getVoiceChannel(guild, msg.author);
if (voiceChannel != null && voiceChannel.connection != null && voiceChannel.connection.dispatcher != null) {
voiceChannel.connection.dispatcher.end();
}
}
});
function playEarrapePotter(voiceConnection) {
var dispatcher = voiceConnection.playFile(EARRAPE_POTTER);
dispatcher.on('end', () => {
voiceConnection.channel.leave();
log("leave");
});
dispatcher.setVolume(VOLUME);
log("play");
}
function getVoiceChannel(guild, user) {
for (var voiceChannel of guild.channels.values()) {
if (voiceChannel.type !== 'voice')
continue;
for (var member of voiceChannel.members.values())
if (member.user.id === user.id)
return voiceChannel;
}
}
function log(str) {
console.log(new Date().toString() + ": " + str);
}
client.login(TOKEN);
client.on('ready', () => {
log('ready!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment