Advertisement
Guest User

Untitled

a guest
Jul 1st, 2018
1,708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. //todo:
  2. // discordBot receives SteamID and logs in database as dID : sID
  3. // app checks desired channel's dIDs against sIDs
  4. // if sID is known, log all competitive played maps
  5. // if Majority of maps are the same; display score as setgame
  6.  
  7.  
  8. const fs = require('fs');
  9. const config = require('./config.json');
  10. const DataPath = './database.json'
  11. const SteamUser = require('steam-user');
  12. const SteamTotp = require('steam-totp');
  13. const Discord = require('discord.js');
  14.  
  15. const sClient = new SteamUser(); //Initialize Steam
  16. const dClient = new Discord.Client(); //Initialize Discord
  17. const sAdmin = config.sAdmin
  18. const dAdmin = config.dAdmin
  19. const sLogOnOptions = {
  20. accountName: config.sUser,
  21. password: config.sPass
  22. //twoFactorCode: Steam-Totp.generateAuthCode(config.sSecret)
  23. };
  24.  
  25. sClient.logOn(sLogOnOptions);
  26. sClient.on('loggedOn', () => {
  27. console.log('Steam Online and ready.');
  28. sClient.setPersona(SteamUser.Steam.EPersonaState.Online, 'RuleBot');
  29. //sClient.gamesPlayed('with your mom.');
  30. });
  31. dClient.on('ready',() => {
  32. console.log("Discord Online and ready.")
  33. });
  34.  
  35. //sEvent Handling
  36. sClient.on('friendRelationship', (steamid, relationship) => {
  37. //if bot receives add request
  38. if (relationship === 2){
  39. sClient.addFriend(steamid);
  40. sClient.chatMessage(steamid, 'Fuck you. I would have blocked you if my programming permitted. I mean-- Thanks for adding me!');
  41. sClient.chatMessage(steamid, 'Send <!r connect> to me on Discord to get your connection command.')
  42. console.log('New Friend: ' + steamid)
  43. } else {
  44. // if ()
  45. };
  46. });
  47. var friendsLoaded = false
  48. sClient.on('friendPersonasLoaded', (x,y,z) => {
  49. friendsLoaded = true
  50. console.log('Friends Loaded:' + friendsLoaded);
  51. //console.log(sClient.users);
  52. });
  53.  
  54. //event handling
  55. dClient.on("guildDelete", guild => {
  56. console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`);
  57. });
  58. dClient.on("guildCreate", guild => {
  59. console.log(`I have been added to: ${guild.name} (id: ${guild.id})`);
  60. });
  61. dClient.on('guildMemberAdd', (member) => {
  62. console.log('User has joined: '+ member.user.username);
  63. });
  64.  
  65.  
  66.  
  67. //Message handling
  68. var prefix = '!r'
  69.  
  70. sClient.on('friendMessage', (steamid, message) => {
  71. if (steamid === sAdmin) {
  72. console.log(message)
  73. };
  74.  
  75. if (!message.startsWith(prefix)) return;
  76. console.log('Steam command received');
  77. var args = message.split(' ').slice(1);
  78. var argresult = args.join(' ');
  79.  
  80.  
  81. if (args[0] === 'add') {
  82. sClient.addFriend(args[1]);
  83. console.log('Attempting to add ' + args[1]);
  84. } else
  85.  
  86. if (args[0] === 'id') {
  87. sClient.chatMessage(steamid, sClient.users[steamid].player_name + ', your ID is: ' + String(steamid))
  88. } else
  89.  
  90. if (args[0] === 'getpersona') {
  91. let rPresence = sClient.users[steamid].rich_presence
  92. let parsedPresence = {}
  93. for (var i in rPresence) {
  94. let newKey = rPresence[i].key
  95. let newVal = rPresence[i].value
  96. parsedPresence[newKey] = newVal
  97. };
  98.  
  99. if (!args[1]) {
  100. console.log(sClient.users[steamid]);
  101. } else
  102. sClient.chatMessage(steamid, parsedPresence[args[1]]);
  103.  
  104. } else
  105.  
  106. if (args[0] === 'getinfo') {
  107. sClient.chatMessage(steamid, sClient.users[args[1]].player_name)
  108. console.log(sClient.users[args[1]]);
  109. } else
  110.  
  111. if (args[0] === 'connect') {
  112. sClient.chatMessage(steamid, 'Send the following command to my Discord: \n !r connect ' +steamid);
  113. };
  114.  
  115. }); //end of SMessage Handler
  116. dClient.on('message', message => {
  117. if (!message.content.startsWith(prefix)) return;
  118. console.log('dCommand: ' + message);
  119. var args = message.content.split(' ').slice(1);
  120. var argresult = args.join(' ');
  121.  
  122. if (message.author.bot) return;
  123.  
  124. if (args[0] === 'ping') {
  125. message.channel.send(dClient.ping + ' ms');
  126. } else
  127.  
  128. if (args[0] === 'setgame'){
  129. args.shift()
  130. argresult = args.join(' ');
  131. console.log(argresult)
  132. if (!argresult) argresult = null;
  133. dClient.user.setActivity(argresult);
  134. sClient.gamesPlayed(argresult);
  135. } else
  136.  
  137. if (args[0] === 'setstatus'){
  138. args.shift()
  139. argresult = args.join(' ');
  140. if (!argresult) argresult = 'online';
  141. dClient.user.setStatus(argresult)
  142. .then(console.log);
  143. } else
  144.  
  145. if (args[0] === 'add'){
  146. let steamFriendLink = 'steam://friends/add/' + config.sBotID
  147. message.channel.send(steamFriendLink + '\nPlease add me on steam.');
  148. } else
  149.  
  150. if (args[0] === 'test') {
  151. message.channel.send('fuck you');
  152. //console.log(message.author.id);
  153. } else
  154.  
  155. if (args[0] === 'getSteam') {
  156. let steamid = args[1]
  157. //message.channel.send(sClient.users.steamid.player_name);
  158. //sClient.chatMessage(steamid, 'test');
  159. //console.log(sClient.users);
  160. } else
  161.  
  162. if (args[0] === 'connect') {
  163. if (!args[1]) {
  164. message.author.send('Usage: !r connect <Steam ID>');
  165. } else {
  166. //paste start
  167. fs.readFile(DataPath, 'utf8', function readFileCallback(err, data){
  168. if (err){
  169. console.log(err);
  170. } else {
  171. var IDObj = {};
  172. var IDJson = JSON.stringify(IDObj, null, 2);
  173.  
  174. let dID = [message.author.id]
  175. let sID = parseInt(args[1],10);
  176. IDObj = JSON.parse(data); //now it an object
  177. //IDObj.table.push({[dID] : sID}); //add some data
  178. if (IDObj.dID) console.log('already exists!');
  179. //IDObj.table.push({[dID] : sID}); //add some data
  180. IDObj[dID] = sID
  181. IDJson = JSON.stringify(IDObj, null, 2); //convert it back to json
  182. fs.writeFile(DataPath, IDJson, 'utf8', (err) => { // write it back
  183. if (err) {console.error(err)
  184. message.author.send('An error occured. Pass this along to Rules and call him an asshole for me.\n' + err);
  185. } else {
  186. message.author.send('Successfully connected your Discord ID to your Steam ID');
  187. console.log('Added '+ dID +' : ' + sID + ' to the database.');
  188. //console.log('Added ' + [dID] + ':' + [steamid] + ' to the database.');
  189. };
  190. });
  191. }});
  192.  
  193. };
  194. //paste end
  195. };
  196. });
  197.  
  198. var IDObj = {};
  199.  
  200. var interval = setInterval( function() {
  201. //console.log('Interval Works.');
  202. // let tarChannel = '425444453973622808' // lounge
  203. //let tarChannel = '447469258180526090' /CSGO
  204. //let tarChannel = '422438345256337414' //general chat
  205. let tarChannel = '444699651313238041' //general ayylmao voice
  206. let membersInVoice = dClient.channels.get(tarChannel).members.map(m=>m.user.id); //Lounge Chan
  207. let usernamesInVoice = dClient.channels.get(tarChannel).members.map(m=>m.user.username); //Lounge Chan
  208.  
  209.  
  210. if (friendsLoaded) {
  211. if (!membersInVoice[0]) {
  212. console.log('No users currently in desired channel.')
  213. } else {
  214. // console.log(membersInVoice);
  215. // console.log(usernamesInVoice);
  216. //var IDObj = {};
  217. fs.readFile(DataPath, 'utf8', function readFileCallback(err, data) {
  218. if (err) {
  219. console.log(err);
  220. } else {
  221. //console.log(data);
  222. IDObj = JSON.parse(data);
  223. //console.log(IDObj);
  224. };
  225. });
  226. //console.log(IDObj);
  227. //console.log(JSON.stringify(IDObj));
  228. var knownUsers = {};
  229.  
  230. for (var i in membersInVoice) {
  231. var dID = String(membersInVoice[i]);
  232. var sID = undefined
  233.  
  234. //console.log(JSON.stringify(IDObj));
  235.  
  236. //console.log(IDObj[membersInVoice[i]]);
  237. if (IDObj[String(dID)]) {
  238. sID = IDObj[String(dID)]
  239. //console.log('SteamID:' + sID);
  240. let dUser = dClient.users.get(dID).username
  241. //console.log('DiscID:' + dUser);
  242. let allSteamUsers = sClient.users
  243. let sUser = allSteamUsers[sID].player_name
  244. //console.log('sUser:' + sUser);
  245. let rPresence = sClient.users[sID].rich_presence
  246. var parsedPresence = {}
  247.  
  248. for (var i in rPresence) {
  249. let newKey = rPresence[i].key
  250. let newVal = rPresence[i].value
  251. parsedPresence[newKey] = newVal
  252. };
  253.  
  254. let currentPlay = parsedPresence.status
  255. //console.log(parsedPresence);
  256.  
  257. console.log(dUser + ' | ' + sUser + ' | ' + currentPlay);
  258. parsedPresence = {}
  259. } else {
  260.  
  261. };
  262.  
  263. };
  264. }; //if members are in voice close
  265. }; //if friendsloaded close
  266. }, 6 * 1000); //run every 6 seconds
  267.  
  268.  
  269.  
  270.  
  271. dClient.login(config.token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement