Dbot.on('message', function(user, userID, channelID, message, rawEvent) { if(userID != Dbot.id && channelID == DChannelId){ Tbot.say(config.twitch.channel, '[Discord:' + user + ']' + Dbot.fixMessage(message)); socket.call('msg', ['[Discord:' + user + '] ' + Dbot.fixMessage(message) ]); } console.log("Discord message! " + message); });
ircbot.on("message", (sender, channel, message) => { if (channel != config.bridgeSettings.channel.irc) { return; } for (var i of config.bridgeSettings.other.ignore.triggers) { if (message.startsWith(i)) { return; } } for (var i of config.bridgeSettings.other.ignore.ircUsers) { if (sender.nick == i) { return; } } if ((message.indexOf("@everyone")) > -1 || message.indexOf("@here") > -1) { ircbot.notice(sender, "Your message as been rejected due to it containing a mention to everyone."); bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `ALERT: Rejected message from ${sender.nick} on IRC due to mention to everyone.` }); console.log(`(IRC) ${sender.nick} => Discord: ${bot.fixMessage(message)} (Message was rejected.)`); return; } bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `<${sender.nick}> ${message}` }); console.log(`(IRC) ${sender.nick} => Discord: ${bot.fixMessage(message)}`); });
bot.on("message", function(user, userID, channelID, message, server) { if (message === settings.prefix + "hi") { bot.sendMessage({ to: channelID, message: "hi" }); } else if (message.split(" ")[0] === prefix + "poll") { var formData = { title: "This is a test poll.", options: [ "Option #1", "Option #2" ], multi: true } var options = { url: "http://strawpoll.me/api/v2/polls", headers: { "Content-Type": "application/json" }, form: formData } request.post(options, function(err, httpResponse, body){ console.log(httpResponse); }); bot.sendMessage({ to: channelID, message: "polling..." }); } });
ircbot.on("topic", (changer, channel, topic) => { bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `${changer.nick} has changed the topic of ${channel} to: ${topic}` }); bot.editChannelInfo({ channel: config.bridgeSettings.channel.discord, topic: "Put !, &, or $ before your messages to keep them from sending. Topic: " + topic }); });
bot.on('ready', function() { console.log(bot.username + " - (" + bot.id + ")"); bot.joinVoiceChannel(voiceChannelID, function() { bot.getAudioContext({channel: voiceChannelID, stereo: true}, handleStream); }); });
bot.on('message', function(user, userID, channelID, message, rawEvent) { var search , cardnamePattern = /\[([A-Za-z0-9 \'\.]+)\]/ ; if (message === "ping") { bot.sendMessage({ to: channelID, message: "HELLO! HELLO! HELLO!" }); }else if(search = message.match(cardnamePattern)){ // These code snippets use an open-source library. http://unirest.io/nodejs var searchUri = encodeURIComponent(search[1]); unirest.get("https://omgvamp-hearthstone-v1.p.mashape.com/cards/"+searchUri) .header("X-Mashape-Key", "zMbSuxAxzmmsh7sUk74MHMDOEXMzp1G5eYNjsnCWLWcwTHmcQE") .header("Accept", "application/json") .end(function (result) { console.log(user+" requested info on \""+searchUri+"\"", result.body); if(result.body.error == 404 && result.body.message == "Card not found."){ bot.sendMessage({ to: channelID, message: "No card found!" }); }else{ for(var i=0; i<result.body.length; i++){ if(result.body[i].type == "Minion" || result.body[i].type == "Spell" || result.body[i].type == "Weapon"){ bot.sendMessage({ to: channelID, message: templates[result.body[i].type](result.body[i]) }); } } } }); } });
Tbot.addListener("message", function (from, to, text, message) { console.log("Twitch message! " + text); Dbot.sendMessage({ to: DChannelId, message: '[Twitch:' + from + '] ' + text }); socket.call('msg', ['[Twitch:' + from + '] ' + text ]); });
.end(function (result) { console.log(user+" requested info on \""+searchUri+"\"", result.body); if(result.body.error == 404 && result.body.message == "Card not found."){ bot.sendMessage({ to: channelID, message: "No card found!" }); }else{ for(var i=0; i<result.body.length; i++){ if(result.body[i].type == "Minion" || result.body[i].type == "Spell" || result.body[i].type == "Weapon"){ bot.sendMessage({ to: channelID, message: templates[result.body[i].type](result.body[i]) }); } } } });
ircbot.on("kick", (kicker, user, channel, message) => { if (user.nick == ircbot.myNick) { return; } bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `${user.nick} was kicked from ${channel} by ${kicker.nick} (${message})` }); });
ircbot.on("join", (user, channel) => { if (user.nick == ircbot.myNick) { return; } bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `${user.nick} has joined ${channel}` }); });
ircbot.on("quit", (user, channels, message) => { if (user.nick == ircbot.myNick) { return; } bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `${user.nick} has quit (${message})` }); });
bot.on("message", (user, userID, channelID, message, rawEvent) => { if (channelID != config.bridgeSettings.channel.discord) { return; } for (var i of config.bridgeSettings.other.ignore.triggers) { if (message.startsWith(i)) { return; } } for (var i of config.bridgeSettings.other.ignore.discordUsers) { if (userID == i) { return; } } if (userID == bot.id) { return; } ircbot.message(config.bridgeSettings.channel.irc, `${toNickname(userID) || user}: ${bot.fixMessage(message)}`); console.log(`(Discord) ${toNickname(userID) || user} => IRC: ${bot.fixMessage(message)}`); });
function other() { ircbot.on("join", (user, channel) => { if (user.nick == ircbot.myNick) { return; } bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `${user.nick} has joined ${channel}` }); }); ircbot.on("part", (user, channel, message) => { if (user.nick == ircbot.myNick) { return; } bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `${user.nick} has left ${channel} (${message})` }); }); ircbot.on("quit", (user, channels, message) => { if (user.nick == ircbot.myNick) { return; } bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `${user.nick} has quit (${message})` }); }); ircbot.on("kick", (kicker, user, channel, message) => { if (user.nick == ircbot.myNick) { return; } bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `${user.nick} was kicked from ${channel} by ${kicker.nick} (${message})` }); }); ircbot.on("topic", (changer, channel, topic) => { bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `${changer.nick} has changed the topic of ${channel} to: ${topic}` }); bot.editChannelInfo({ channel: config.bridgeSettings.channel.discord, topic: "Put !, &, or $ before your messages to keep them from sending. Topic: " + topic }); }); bot.editChannelInfo({ channel: config.bridgeSettings.channel.discord, topic: "Put !, &, or $ before your messages to keep them from sending. Topic: " + ircbot.channels[config.bridgeSettings.channel.irc].topic }); }
events.on('update', data => { // Send private message to owner if (configModule.get().ownerID) { bot.sendMessage({ to: configModule.get().ownerID, message: 'There is a new version available for the bot.\n\n' + 'Visit <https://github.com/simonknittel/discord-bot-api> to download the latest version.\n' + 'Check out the CHANGELOG.md file for important changes.\n\n' + 'Your version: ' + data.currentVersion + '\n' + 'Latest version: ' + data.latestVersion + '\n', }); } });
bot.on('message', function(user, userID, channelID, message, rawEvent, server, serverID) { if (!(channelID in bot.directMessages)) { if (message.split(" ")[0] === settings.prefix + "deport"){ if (bot.servers[bot.serverFromChannel(channelID)].members[userID].roles.indexOf("196567540271546369") > -1){ connection.query("UPDATE citizens SET status = REPLACE(status, \"alive\", \"deported\") WHERE id = " + connection.escape(message.split(" ")[1]) + ";") bot.sendMessage({ to: channelID, message: "banning user..." }); bot.ban({ channel: bot.serverFromChannel(channelID), target: message.split(" ")[1] }); } else { bot.sendMessage({ to: channelID, message: "you don't have permission to do this" }); } } } });
setTimeout(function() { if (fileArr[0]) { bot.uploadFile({ to: channelID, file: fileArr.shift() }, function(err, res) { if (err) { resArr.push(err); } else { resArr.push(res); } if (resArr.length === len) if (typeof(callback) === 'function') callback(resArr); }); _sendFiles(); } }, interval);
setTimeout(function() { if (messageArr[0]) { bot.sendMessage({ to: ID, message: messageArr.shift() }, function(err, res) { if (err) { resArr.push(err); } else { resArr.push(res); } if (resArr.length === len) if (typeof(callback) === 'function') callback(resArr); }); _sendMessages(); } }, interval);
/*Variable area*/ var Discordbot = require('discord.io'); var bot = new Discordbot({ token: "", autorun: true }); /*Event area*/ bot.on("ready", function(rawEvent) { console.log("Connected!"); console.log("Logged in as: "); console.log(bot.username + " - (" + bot.id + ")"); }); bot.on("message", function(user, userID, channelID, message, rawEvent) { console.log(user + " - " + userID); console.log("in " + channelID); console.log(message); console.log("----------"); if (message === "ping") { sendMessages(channelID, ["Pong"]); //Sending a message with our helper function } else if (message === "picture") { sendFiles(channelID, ["fillsquare.png"]); //Sending a file with our helper function } }); bot.on("presence", function(user, userID, status, gameName, rawEvent) { /*console.log(user + " is now: " + status);*/ });
Dbot.on('disconnected', function() { Dbot.connect(); });
var DiscordClient = require('discord.io'); var bot = new DiscordClient(getNewDcClient()); var whiteCh = "154514530284601344"; var fs = require('fs'); var http = require('http'); var you_I = getText(); var greeting = require('./greeting.js'); var ending = require('./ending.js'); var playing = require('./playing.js'); var findGNN = require('./findGNN.js'); var steamSales = require('./steam.js'); var review = require('./review.js'); var stuck = require('./stuck.js'); var msgLog = require('./MessageLog.js'); var pattern = require('./pattern.js'); var guideRecord = require('./GuideRecord.js'); var num2cht = require('./NumToCht.js'); var dbUserGame = require('./dbUserGame.js'); bot.on('ready', discordReady); bot.on('message', discordMsg); function getNewDcClient() { var client = {}; client.email = ""; client.password = ""; client.autorun = true; return client; } function discordReady() {
function say(ch, ms) { bot.sendMessage({ to: ch, message: ms }); }
//main.js //dependencies const Discord = require("discord.io"), config = require("./config.json"), bot = new Discord({ token: config.discord.token, autorun: true }), IRC = require("internet-relay-chat"), ircbot = new IRC({ "server": config.IRC.server.hostname, "port": config.IRC.server.port, "secure": config.IRC.server.ssl, "password": config.IRC.server.password, "nick": config.IRC.nick, "floodDelay": config.IRC.floodDelay, "autoReconnect": config.IRC.autoReconnect, "username": config.IRC.username, "realname": config.IRC.realname }); //IRC lib lacks an autoconnect ircbot.connect(); //global vars and consts var ircCS = false, discordCS = false;
reconnectInterval = setInterval(() => { bot.connect(); }, 15000);
bot.joinVoiceChannel(voiceChannelID, function() { bot.getAudioContext({channel: voiceChannelID, stereo: true}, handleStream); });
socket.on('closed', function() { socket.boot(); }); }).catch(function (err) { //If this is a failed request, don't log the entire request. Just log the body if(err.message !== undefined && err.message.body !== undefined) { err = err.message.body; } console.log('error joining chat:', err); }); //Connect to discord server var Dbot = new DiscordClient({ autorun: true, email: config.discord.email, password: config.discord.password }); //Reconnect to discord server in case of websocket closed Dbot.on('disconnected', function() { Dbot.connect(); }); //Connect to twitch server var Tbot = new irc.Client('irc.twitch.tv', config.twitch.username, { port: 6667, channels: [config.twitch.channel], debug: false, password: config.twitch.oauth, username: config.twitch.username,
function start() { if (!(discordCS && ircCS)) { return; } if (config.bridgeSettings.channel.discordServer in bot.servers) { if (config.bridgeSettings.channel.discord in bot.servers[config.bridgeSettings.channel.discordServer].channels) { console.log("Listening in Discord"); bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: "ALERT: The bot is now listening." }); ircbot.message(config.bridgeSettings.channel.irc, "ALERT: The bot is now listening.") bot.on("message", (user, userID, channelID, message, rawEvent) => { if (channelID != config.bridgeSettings.channel.discord) { return; } for (var i of config.bridgeSettings.other.ignore.triggers) { if (message.startsWith(i)) { return; } } for (var i of config.bridgeSettings.other.ignore.discordUsers) { if (userID == i) { return; } } if (userID == bot.id) { return; } ircbot.message(config.bridgeSettings.channel.irc, `${toNickname(userID) || user}: ${bot.fixMessage(message)}`); console.log(`(Discord) ${toNickname(userID) || user} => IRC: ${bot.fixMessage(message)}`); }); console.log("Listening in IRC."); ircbot.on("message", (sender, channel, message) => { if (channel != config.bridgeSettings.channel.irc) { return; } for (var i of config.bridgeSettings.other.ignore.triggers) { if (message.startsWith(i)) { return; } } for (var i of config.bridgeSettings.other.ignore.ircUsers) { if (sender.nick == i) { return; } } if ((message.indexOf("@everyone")) > -1 || message.indexOf("@here") > -1) { ircbot.notice(sender, "Your message as been rejected due to it containing a mention to everyone."); bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `ALERT: Rejected message from ${sender.nick} on IRC due to mention to everyone.` }); console.log(`(IRC) ${sender.nick} => Discord: ${bot.fixMessage(message)} (Message was rejected.)`); return; } bot.sendMessage({ to: config.bridgeSettings.channel.discord, message: `<${sender.nick}> ${message}` }); console.log(`(IRC) ${sender.nick} => Discord: ${bot.fixMessage(message)}`); }); other(); } else { throw "You are not in that Discord channel."; } } else { throw "You are not in that Discord server."; } }
bot.on('ready', () => { console.log(chalk.green('Plugins')); for (const name in configModule.get().plugins) { if (configModule.get().plugins.hasOwnProperty(name)) { if (!plugins.hasOwnProperty(name)) { console.log(chalk.red(name + ' failed to load')); continue; } console.log(name + ' loaded'); } } console.log(''); // Empty line console.log(chalk.green('Discord Bot API started.')); console.log('v' + packageJSON.version); console.log(''); // Empty line reconnectInterval = null; // Set the name of the bot to the one defined in the configModule.json if (configModule.get().credentials.name) { bot.editUserInfo({ password: configModule.get().credentials.password, username: configModule.get().credentials.name, }); } // Set the avatar of the bot to the one defined in the configModule.json if (configModule.get().credentials.avatar && configModule.get().credentials.avatar !== null) { const reg = new RegExp(/^(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)$/, 'gi'); if (reg.test(configModule.get().credentials.avatar)) { request({ url: configModule.get().credentials.avatar, encoding: null, }, (error, response, body) => { if (!error && response.statusCode == 200) { setAvatar(new Buffer(body).toString('base64')); } else { console.log(chalk.red('The avatar could not be set. Make sure the path is correct.')); } }); } else { setAvatar(fs.readFileSync(configModule.get().credentials.avatar, 'base64')); } } else if (configModule.get().credentials.avatar === null) { bot.editUserInfo({ avatar: null, password: configModule.get().credentials.password, }); } // Accept the invites defined in the config.json if (configModule.get().invites) { for (const invite of configModule.get().invites) { const inviteID = invite.replace('https://discord.gg/', ''); if (inviteID.length <= 0) { continue; } bot.acceptInvite(inviteID); } } // Listen for update events events.on('update', data => { // Send private message to owner if (configModule.get().ownerID) { bot.sendMessage({ to: configModule.get().ownerID, message: 'There is a new version available for the bot.\n\n' + 'Visit <https://github.com/simonknittel/discord-bot-api> to download the latest version.\n' + 'Check out the CHANGELOG.md file for important changes.\n\n' + 'Your version: ' + data.currentVersion + '\n' + 'Latest version: ' + data.latestVersion + '\n', }); } }); });