main.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. const fs = require('fs');
  2. const Discord = require('discord.js');
  3. var request = require('request');
  4. request = request.defaults({jar: true});
  5. var client = new Discord.Client( {disableEveryone:true} );
  6. var i18n = JSON.parse(fs.readFileSync('i18n.json', 'utf8'));
  7. var pause = {};
  8. var defaultSettings = {
  9. "default": {
  10. "lang": "en",
  11. "wiki": "help"
  12. }
  13. }
  14. var settings = defaultSettings;
  15. function getSettings(callback) {
  16. request( {
  17. uri: process.env.site + process.env.page + '?action=raw&ctype=javascript',
  18. json: true
  19. }, function( error, response, body ) {
  20. if ( error || !response || !body ) {
  21. console.log( 'Fehler beim Erhalten der Einstellungen' + ( error ? ': ' + error.message : '.' ) );
  22. }
  23. else {
  24. console.log( 'Einstellungen erfolgreich ausgelesen.' );
  25. settings = body;
  26. }
  27. callback();
  28. } );
  29. }
  30. function setStatus() {
  31. if ( settings == defaultSettings ) client.user.setStatus('invisible');
  32. else client.user.setStatus('online');
  33. }
  34. client.on('ready', () => {
  35. getSettings(setStatus);
  36. console.log( 'Erfolgreich als ' + client.user.username + ' angemeldet!' );
  37. client.user.setActivity( process.env.prefix + 'help' );
  38. } );
  39. var cmdmap = {
  40. help: cmd_help,
  41. test: cmd_test,
  42. invite: cmd_invite,
  43. stop: cmd_stop,
  44. pause: cmd_pause,
  45. server: cmd_serverlist,
  46. say: cmd_multiline,
  47. delete: cmd_multiline,
  48. purge: cmd_multiline,
  49. poll: cmd_multiline,
  50. voice: cmd_voice,
  51. settings: cmd_settings,
  52. info: cmd_info,
  53. eval: cmd_multiline
  54. }
  55. var multilinecmdmap = {
  56. say: cmd_say,
  57. delete: cmd_delete,
  58. poll: cmd_umfrage,
  59. eval: cmd_eval
  60. }
  61. var pausecmdmap = {
  62. test: cmd_test,
  63. stop: cmd_stop,
  64. pause: cmd_pause,
  65. server: cmd_serverlist,
  66. say: cmd_multiline,
  67. delete: cmd_multiline,
  68. eval: cmd_multiline
  69. }
  70. function cmd_settings(lang, msg, args, line) {
  71. if ( admin(msg) ) {
  72. if ( msg.guild.id in settings ) var text = lang.settings.current + '\n`' + process.env.prefix + 'settings lang` – ' + settings[msg.guild.id].lang + '\n`' + process.env.prefix + 'settings wiki` – ' + settings[msg.guild.id].wiki;
  73. else var text = lang.settings.missing.replace( '%1$s', '`' + process.env.prefix + 'settings lang`' ).replace( '%2$s', '`' + process.env.prefix + 'settings wiki`' );
  74. if ( args.length ) {
  75. if ( args[0] ) args[0] = args[0].toLowerCase();
  76. if ( args[1] ) args[1] = args[1].toLowerCase();
  77. var langs = '\n' + lang.settings.langs + ' `' + Object.keys(i18n) + '`';
  78. var wikis = '\n' + lang.settings.wikis;
  79. var nolangs = lang.settings.nolangs + langs;
  80. var regex = /^(?:(?:https?:)?\/\/)?([a-z\d-]{1,30})/
  81. if ( msg.guild.id in settings ) {
  82. if ( args[0] == 'lang' ) {
  83. if ( args[1] ) {
  84. if ( args[1] in i18n ) edit_settings(lang, msg, 'lang', args[1]);
  85. else msg.reply( nolangs );
  86. } else msg.reply( lang.settings.lang + ' `' + settings[msg.guild.id].lang + '`' + langs );
  87. } else if ( args[0] == 'wiki' ) {
  88. if ( args[1] ) {
  89. if ( regex.test(args[1]) ) edit_settings(lang, msg, 'wiki', regex.exec(args[1])[1]);
  90. else cmd_settings(lang, msg, ['wiki'], line);
  91. } else msg.reply( lang.settings.wiki + '\nhttps://' + settings[msg.guild.id].wiki + '.gamepedia.com/' + wikis );
  92. } else msg.reply( text );
  93. } else {
  94. if ( args[0] == 'lang' ) {
  95. if ( args[1] ) {
  96. if ( args[1] in i18n ) edit_settings(lang, msg, 'lang', args[1]);
  97. else msg.reply( nolangs );
  98. } else msg.reply( lang.settings.nolang + langs );
  99. } else if ( args[0] == 'wiki' ) {
  100. if ( args[1] ) {
  101. if ( regex.test(args[1]) ) edit_settings(lang, msg, 'wiki', regex.exec(args[1])[1]);
  102. else cmd_settings(lang, msg, ['wiki'], line);
  103. } else msg.reply( lang.settings.nowiki + wikis );
  104. } else msg.reply( text );
  105. }
  106. } else msg.reply( text );
  107. } else {
  108. msg.react('❌');
  109. }
  110. }
  111. function edit_settings(lang, msg, key, value) {
  112. var hourglass;
  113. msg.react('⏳').then( function( reaction ) {
  114. hourglass = reaction;
  115. var url = process.env.site + 'api.php';
  116. request.get( {
  117. uri: url + '?action=query&format=json&meta=tokens&type=login',
  118. json: true
  119. }, function( error, response, body ) {
  120. if ( error || !response || !body || body.error || !body.query ) {
  121. console.log( 'Fehler beim Erhalten des Anmeldetoken' + ( error ? ': ' + error.message : ( body ? ( body.error ? ': ' + body.error.info : '.' ) : '.' ) ) );
  122. msg.reply( lang.settings.save_failed );
  123. }
  124. else {
  125. request.post( {
  126. uri: url,
  127. form: {
  128. action: 'login',
  129. format: 'json',
  130. lgname: process.env.username,
  131. lgpassword: process.env.password,
  132. lgtoken: body.query.tokens.logintoken
  133. },
  134. json: true
  135. }, function( lerror, lresponse, lbody ) {
  136. if ( lerror || !lresponse || !lbody || lbody.error || !body.query ) {
  137. console.log( 'Fehler beim Anmelden' + ( lerror ? ': ' + lerror.message : ( lbody ? ( lbody.error ? ': ' + lbody.error.info : '.' ) : '.' ) ) );
  138. msg.reply( lang.settings.save_failed );
  139. }
  140. else {
  141. request.post( {
  142. uri: url,
  143. form: {
  144. action: 'query',
  145. format: 'json',
  146. meta: 'tokens',
  147. type: 'csrf'
  148. },
  149. json: true
  150. }, function( perror, presponse, pbody ) {
  151. if ( perror || !presponse || !pbody || pbody.error || !body.query ) {
  152. console.log( 'Fehler beim Erhalten des Bearbeitungstoken' + ( perror ? ': ' + perror.message : ( pbody ? ( pbody.error ? ': ' + pbody.error.info : '.' ) : '.' ) ) );
  153. msg.reply( lang.settings.save_failed );
  154. }
  155. else if ( settings == defaultSettings ) {
  156. console.log( 'Fehler beim Erhalten bestehender Einstellungen.' );
  157. msg.reply( lang.settings.save_failed );
  158. }
  159. else {
  160. var temp_settings = settings;
  161. if ( !( msg.guild.id in temp_settings ) ) temp_settings[msg.guild.id] = defaultSettings['default'];
  162. temp_settings[msg.guild.id][key] = value;
  163. Object.keys(temp_settings).forEach( function(guild) {
  164. if ( !client.guilds.has(guild) && guild != 'default' ) delete temp_settings[guild];
  165. } );
  166. request.post( {
  167. uri: url,
  168. form: {
  169. action: 'edit',
  170. format: 'json',
  171. title: process.env.page,
  172. text: JSON.stringify( temp_settings, null, '\t' ),
  173. summary: 'Einstellungen aktualisiert.',
  174. bot: true,
  175. token: pbody.query.tokens.csrftoken
  176. },
  177. json: true
  178. }, function( eerror, eresponse, ebody ) {
  179. if ( eerror || !eresponse || !ebody || ebody.error || !body.query ) {
  180. console.log( 'Fehler beim Bearbeiten' + ( eerror ? ': ' + eerror.message : ( ebody ? ( ebody.error ? ': ' + ebody.error.info : '.' ) : '.' ) ) );
  181. msg.reply( lang.settings.save_failed );
  182. }
  183. else {
  184. settings = temp_settings;
  185. if ( key == 'lang' ) lang = i18n[value];
  186. cmd_settings(lang, msg, [key], '')
  187. console.log( 'Einstellungen erfolgreich aktualisiert.' );
  188. }
  189. } );
  190. }
  191. } );
  192. }
  193. } );
  194. }
  195. if ( hourglass != undefined ) hourglass.remove();
  196. } );
  197. } );
  198. }
  199. function cmd_info(lang, msg, args, line) {
  200. if ( args.length ) cmd_link(lang, msg, line.split(' ').slice(1).join(' '), lang.link, '');
  201. else {
  202. msg.channel.send( lang.disclaimer + '\nhttps://discord.gg/v77RTk5' );
  203. cmd_invite(lang, msg, ['<@' + client.user.id + '>'], line);
  204. }
  205. }
  206. function cmd_help(lang, msg, args, line) {
  207. if ( admin(msg) && !( msg.guild.id in settings ) ) cmd_settings(lang, msg, [], line);
  208. var cmds = lang.help.list;
  209. if ( args.length ) {
  210. if ( args[0].toLowerCase() == 'admin' && ( msg.channel.type != 'text' || admin(msg) ) ) {
  211. if ( args[1] && args[1].toLowerCase() == 'emoji' ) {
  212. var cmdlist = lang.help.emoji + '\n';
  213. var i = 0;
  214. client.emojis.forEach( function(emoji) {
  215. var br = '\t\t';
  216. if ( i % 3 == 2 ) br = '\n';
  217. cmdlist += emoji.toString() + '`' + emoji.toString().replace(emoji.name + ':', '') + '`' + br;
  218. i++;
  219. } );
  220. msg.channel.send( cmdlist, {split:true} );
  221. }
  222. else {
  223. var cmdlist = lang.help.admin + '\n';
  224. for ( var i = 0; i < cmds.length; i++ ) {
  225. if ( cmds[i].admin && !cmds[i].hide ) {
  226. cmdlist += '🔹 `' + process.env.prefix + cmds[i].cmd + '`\n\t' + cmds[i].desc + '\n';
  227. }
  228. }
  229. msg.channel.send( cmdlist );
  230. }
  231. }
  232. else {
  233. var cmdlist = ''
  234. for ( var i = 0; i < cmds.length; i++ ) {
  235. if ( cmds[i].cmd.split(' ')[0] === args[0].toLowerCase() && !cmds[i].unsearchable && ( msg.channel.type != 'text' || !cmds[i].admin || admin(msg) ) ) {
  236. cmdlist += '🔹 `' + process.env.prefix + cmds[i].cmd + '`\n\t' + cmds[i].desc + '\n';
  237. }
  238. }
  239. if ( cmdlist == '' ) msg.react('❓');
  240. else msg.channel.send( cmdlist );
  241. }
  242. }
  243. else {
  244. var cmdlist = lang.help.all + '\n';
  245. for ( var i = 0; i < cmds.length; i++ ) {
  246. if ( !cmds[i].hide && !cmds[i].admin ) {
  247. cmdlist += '🔹 `' + process.env.prefix + cmds[i].cmd + '`\n\t' + cmds[i].desc + '\n';
  248. }
  249. }
  250. msg.channel.send( cmdlist );
  251. }
  252. }
  253. function cmd_say(lang, msg, args, line) {
  254. if ( admin(msg) ) {
  255. args = emoji(args);
  256. var text = args.join(' ');
  257. if ( args[0] == 'alarm' ) text = '🚨 **' + args.slice(1).join(' ') + '** 🚨';
  258. var imgs = [];
  259. var i = 0;
  260. msg.attachments.forEach( function(img) {
  261. imgs[i] = {attachment:img.proxyURL,name:img.filename};
  262. i++;
  263. } );
  264. if ( text || imgs[0] ) {
  265. msg.channel.send( text, {disableEveryone:false,files:imgs} ).then( message => msg.delete(), error => msg.react('440871715938238494') );
  266. }
  267. } else {
  268. msg.react('❌');
  269. }
  270. }
  271. function cmd_test(lang, msg, args, line) {
  272. if ( msg.channel.type != 'text' || !pause[msg.guild.id] ) {
  273. var text = '';
  274. var x = Math.floor(Math.random() * lang.test.random);
  275. if ( x < lang.test.text.length ) text = lang.test.text[x];
  276. else text = lang.test.default;
  277. msg.reply( text );
  278. console.log( 'Dies ist ein Test: Voll funktionsfähig!' );
  279. } else {
  280. msg.reply( lang.test.pause );
  281. console.log( 'Dies ist ein Test: Pausiert!' );
  282. }
  283. }
  284. function cmd_invite(lang, msg, args, line) {
  285. if ( args.length && args[0].replace( '!', '' ) == '<@' + client.user.id + '>' ) {
  286. client.generateInvite(268954689).then( invite => msg.channel.send( lang.invite.bot + '\n<' + invite + '>' ) );
  287. } else {
  288. cmd_link(lang, msg, line.split(' ').slice(1).join(' '), lang.link, '');
  289. }
  290. }
  291. function cmd_eval(lang, msg, args, line) {
  292. if ( msg.author.id == process.env.owner && args.length ) {
  293. try {
  294. var text = eval( args.join(' ') );
  295. } catch ( error ) {
  296. var text = error.name + ': ' + error.message;
  297. }
  298. console.log( text );
  299. msg.channel.send( '```js\n' + text + '```', {split:{prepend:'```js\n',append:'```'}} ).catch( err => msg.channel.send( '```js\n' + err.name + ': ' + err.message + '```', {split:{prepend:'```js\n',append:'```'}} ) );
  300. } else if ( msg.channel.type != 'text' || !pause[msg.guild.id] ) {
  301. msg.react('❌');
  302. }
  303. }
  304. function cmd_stop(lang, msg, args, line) {
  305. if ( msg.author.id == process.env.owner && args.length && args[0].replace( '!', '' ) == '<@' + client.user.id + '>' ) {
  306. msg.reply( 'ich schalte mich nun aus!' );
  307. console.log( 'Ich schalte mich nun aus!' );
  308. client.destroy();
  309. } else if ( msg.channel.type != 'text' || !pause[msg.guild.id] ) {
  310. cmd_link(lang, msg, line.split(' ').slice(1).join(' '), lang.link, '');
  311. }
  312. }
  313. function cmd_pause(lang, msg, args, line) {
  314. if ( msg.channel.type == 'text' && msg.author.id == process.env.owner && args.length && args[0].replace( '!', '' ) == '<@' + client.user.id + '>' ) {
  315. if ( pause[msg.guild.id] ) {
  316. msg.reply( 'ich bin wieder wach!' );
  317. console.log( 'Ich bin wieder wach!' );
  318. pause[msg.guild.id] = false;
  319. } else {
  320. msg.reply( 'ich lege mich nun schlafen!' );
  321. console.log( 'Ich lege mich nun schlafen!' );
  322. pause[msg.guild.id] = true;
  323. }
  324. } else if ( msg.channel.type != 'text' || !pause[msg.guild.id] ) {
  325. cmd_link(lang, msg, line.split(' ').slice(1).join(' '), lang.link, '');
  326. }
  327. }
  328. function cmd_delete(lang, msg, args, line) {
  329. if ( admin(msg) ) {
  330. if ( /^\d+$/.test(args[0]) && parseInt(args[0], 10) + 1 > 0 ) {
  331. if ( parseInt(args[0], 10) > 99 ) {
  332. msg.reply( lang.delete.big.replace( '%s', '`99`' ) );
  333. }
  334. else {
  335. msg.channel.bulkDelete(parseInt(args[0], 10) + 1, true).then( messages => {
  336. msg.reply( lang.delete.success.replace( '%s', messages.size - 1 ) ).then( antwort => antwort.delete(3000) );
  337. console.log( 'Die letzten ' + ( messages.size - 1 ) + ' Nachrichten in #' + msg.channel.name + ' wurden gelöscht!' );
  338. } );
  339. }
  340. }
  341. else {
  342. msg.reply( lang.delete.invalid );
  343. }
  344. } else {
  345. msg.react('❌');
  346. }
  347. }
  348. function cmd_link(lang, msg, title, wiki, cmd) {
  349. if ( cmd == '' && admin(msg) && !( msg.guild.id in settings ) ) cmd_settings(lang, msg, [], '');
  350. var invoke = title.split(' ')[0].toLowerCase();
  351. var args = title.split(' ').slice(1);
  352. if ( invoke == 'page' || invoke == lang.search.page ) msg.channel.send( 'https://' + wiki + '.gamepedia.com/' + args.join('_') );
  353. else if ( invoke == 'search' || invoke == lang.search.search ) msg.channel.send( 'https://' + wiki + '.gamepedia.com/Special:Search/' + args.join('_') );
  354. else if ( invoke == 'diff' ) cmd_diff(lang, msg, args, wiki);
  355. else if ( title == '' || title.indexOf( '#' ) != -1 || title.indexOf( '?' ) != -1 ) msg.channel.send( 'https://' + wiki + '.gamepedia.com/' + title.replace( / /g, '_' ) );
  356. else if ( invoke == 'user' || invoke == lang.search.user.unknown || invoke == lang.search.user.male || invoke == lang.search.user.female ) cmd_user(lang, msg, args.join('_'), wiki, title.replace( / /g, '_' ));
  357. else if ( invoke.startsWith('user:') ) cmd_user(lang, msg, title.substr(5), wiki, title.replace( / /g, '_' ));
  358. else if ( invoke.startsWith('userprofile:') ) cmd_user(lang, msg, title.substr(12), wiki, title.replace( / /g, '_' ));
  359. else if ( invoke.startsWith(lang.search.user.unknown + ':') ) cmd_user(lang, msg, title.substr(lang.search.user.unknown.length + 1), wiki, title.replace( / /g, '_' ));
  360. else if ( invoke.startsWith(lang.search.user.male + ':') ) cmd_user(lang, msg, title.substr(lang.search.user.male.length + 1), wiki, title.replace( / /g, '_' ));
  361. else if ( invoke.startsWith(lang.search.user.female + ':') ) cmd_user(lang, msg, title.substr(lang.search.user.female.length + 1), wiki, title.replace( / /g, '_' ));
  362. else {
  363. var hourglass;
  364. msg.react('⏳').then( function( reaction ) {
  365. hourglass = reaction;
  366. request( {
  367. uri: 'https://' + wiki + '.gamepedia.com/api.php?action=query&format=json&meta=siteinfo&siprop=interwikimap&redirects=true&titles=' + title,
  368. json: true
  369. }, function( error, response, body ) {
  370. if ( error || !response || !body || !body.query ) {
  371. console.log( 'Fehler beim Erhalten der Suchergebnisse' + ( error ? ': ' + error.message : ( body ? ( body.error ? ': ' + body.error.info : '.' ) : '.' ) ) );
  372. msg.channel.send( 'https://' + wiki + '.gamepedia.com/' + title.replace( / /g, '_' ) ).then( message => message.react('440871715938238494') );
  373. }
  374. else {
  375. if ( body.query.pages ) {
  376. if ( body.query.pages['-1'] && body.query.pages['-1'].missing != undefined ) {
  377. request( {
  378. uri: 'https://' + wiki + '.gamepedia.com/api.php?action=query&format=json&list=search&srnamespace=0|4|6|10|12|14&srsearch=' + title + '&srlimit=1',
  379. json: true
  380. }, function( srerror, srresponse, srbody ) {
  381. if ( srerror || !srresponse || !srbody || !srbody.query || ( !srbody.query.search[0] && srbody.query.searchinfo.totalhits != 0 ) ) {
  382. console.log( 'Fehler beim Erhalten der Suchergebnisse' + ( srerror ? ': ' + srerror.message : ( srbody ? ( srbody.error ? ': ' + srbody.error.info : '.' ) : '.' ) ) );
  383. msg.channel.send( 'https://' + wiki + '.gamepedia.com/' + title.replace( / /g, '_' ) ).then( message => message.react('440871715938238494') );
  384. }
  385. else {
  386. if ( srbody.query.searchinfo.totalhits == 0 ) {
  387. msg.react('🤷');
  388. }
  389. else if ( srbody.query.searchinfo.totalhits == 1 ) {
  390. msg.channel.send( 'https://' + wiki + '.gamepedia.com/' + encodeURI( srbody.query.search[0].title.replace( / /g, '_' ) ) + '\n' + lang.search.infopage.replace( '%s', '`' + process.env.prefix + cmd + lang.search.page + ' ' + title + '`' ) );
  391. }
  392. else {
  393. msg.channel.send( 'https://' + wiki + '.gamepedia.com/' + encodeURI( srbody.query.search[0].title.replace( / /g, '_' ) ) + '\n' + lang.search.infosearch.replace( '%1$s', '`' + process.env.prefix + cmd + lang.search.page + ' ' + title + '`' ).replace( '%2$s', '`' + process.env.prefix + cmd + lang.search.search + ' ' + title + '`' ) );
  394. }
  395. }
  396. } );
  397. }
  398. else {
  399. msg.channel.send( 'https://' + wiki + '.gamepedia.com/' + encodeURI( ( Object.values(body.query.pages)[0].title + ( body.query.redirects && body.query.redirects[0].tofragment ? '#' + body.query.redirects[0].tofragment : '' ) ).replace( / /g, '_' ) ) );
  400. }
  401. }
  402. else if ( body.query.interwiki ) {
  403. var inter = body.query.interwiki[0];
  404. var intertitle = inter.title.substr(inter.iw.length+1);
  405. var regex = /^(?:https?:)?\/\/(.*)\.gamepedia\.com\//
  406. var entry = body.query.interwikimap;
  407. for ( var i = 0; i < entry.length; i++ ) {
  408. if ( entry[i].prefix == inter.iw ) {
  409. if ( regex.test(entry[i].url) ) {
  410. var iwtitle = entry[i].url.replace( '$1', intertitle ).replace( regex.exec(entry[i].url)[0], '' );
  411. var link = regex.exec(entry[i].url)[1];
  412. cmd_link(lang, msg, iwtitle, link, '!' + link + ' ');
  413. }
  414. else msg.channel.send( entry[i].url.replace( '$1', encodeURI( intertitle.replace( / /g, '_' ) ) ) );
  415. break;
  416. }
  417. }
  418. }
  419. else {
  420. msg.react('🤷');
  421. }
  422. }
  423. if ( hourglass != undefined ) hourglass.remove();
  424. } );
  425. } );
  426. }
  427. }
  428. function cmd_serverlist(lang, msg, args, line) {
  429. if ( msg.author.id == process.env.owner && args.join(' ') == 'list all <@' + client.user.id + '>' ) {
  430. var guilds = client.guilds;
  431. var serverlist = 'Ich befinde mich aktuell auf ' + guilds.size + ' Servern:\n\n';
  432. guilds.forEach( function(guild) {
  433. serverlist += '"' + guild.toString() + '" von ' + guild.owner.toString() + ' mit ' + guild.memberCount + ' Mitgliedern\n' + guild.channels.find('type', 'text').toString() + ' (' + guild.id + ')\n\n';
  434. } );
  435. msg.author.send( serverlist, {split:{char:'\n\n'}} );
  436. } else if ( msg.author.id == process.env.owner && args.join(' ') == 'list all <@' + client.user.id + '> permissions' ) {
  437. var guilds = client.guilds;
  438. var serverlist = 'Ich befinde mich aktuell auf ' + guilds.size + ' Servern:\n\n';
  439. guilds.forEach( function(guild) {
  440. var perms = ' ';
  441. var allperms = Object.entries(guild.me.permissions.serialize());
  442. allperms.forEach( function(perm) {
  443. if ( perm[1] ) perms += perm[0] + ', ';
  444. } );
  445. perms = perms.substr(0, perms.length -2);
  446. serverlist += '"' + guild.toString() + '" von ' + guild.owner.toString() + ' mit ' + guild.memberCount + ' Mitgliedern\n' + guild.channels.find('type', 'text').toString() + perms + '\n\n';
  447. } );
  448. msg.author.send( serverlist, {split:{char:'\n\n'}} );
  449. } else if ( msg.author.id == process.env.owner && args.join(' ') == 'list all <@' + client.user.id + '> members' ) {
  450. var guilds = client.guilds;
  451. var serverlist = 'Ich befinde mich aktuell auf ' + guilds.size + ' Servern:\n\n';
  452. guilds.forEach( function(guild) {
  453. var members = ' ';
  454. var allmembers = guild.members;
  455. if ( !allmembers.has(process.env.owner) && guild.memberCount < 50 ) {
  456. allmembers.forEach( function(member) {
  457. members += member.toString() + ', ';
  458. } );
  459. }
  460. members = members.substr(0, members.length -2);
  461. serverlist += '"' + guild.toString() + '" von ' + guild.owner.toString() + ' mit ' + guild.memberCount + ' Mitgliedern\n' + guild.channels.find('type', 'text').toString() + members + '\n\n';
  462. } );
  463. msg.author.send( serverlist, {split:{char:'\n\n'}} );
  464. } else if ( msg.channel.type != 'text' || !pause[msg.guild.id] ) {
  465. cmd_link(lang, msg, line.split(' ').slice(1).join(' '), lang.link, '');
  466. }
  467. }
  468. function cmd_umfrage(lang, msg, args, line) {
  469. if ( admin(msg) ) {
  470. var imgs = [];
  471. var a = 0;
  472. msg.attachments.forEach( function(img) {
  473. imgs[a] = {attachment:img.proxyURL,name:img.filename};
  474. a++;
  475. } );
  476. if ( args.length || imgs[0] ) {
  477. var reactions = [];
  478. args = emoji(args);
  479. for ( var i = 0; ( i < args.length || imgs[0] ); i++ ) {
  480. var reaction = args[i];
  481. var custom = /^<a?:/;
  482. var pattern = /^[\w\säÄöÖüÜßẞ!"#$%&'()*+,./:;<=>?@^`{|}~–[\]\-\\]{2,}/;
  483. if ( !custom.test(reaction) && pattern.test(reaction) ) {
  484. cmd_sendumfrage(lang, msg, args, reactions, imgs, i);
  485. break;
  486. } else if ( reaction == '' ) {
  487. } else {
  488. if ( custom.test(reaction) ) {
  489. reaction = reaction.substring(reaction.lastIndexOf(':')+1, reaction.length-1);
  490. }
  491. reactions[i] = reaction;
  492. if ( i == args.length-1 ) {
  493. cmd_sendumfrage(lang, msg, args, reactions, imgs, i+1);
  494. break;
  495. }
  496. }
  497. }
  498. } else {
  499. args[0] = line.split(' ')[1];
  500. cmd_help(lang, msg, args, line);
  501. }
  502. } else {
  503. msg.react('❌');
  504. }
  505. }
  506. function cmd_sendumfrage(lang, msg, args, reactions, imgs, i) {
  507. msg.channel.send( lang.poll.title + args.slice(i).join(' '), {disableEveryone:false,files:imgs} ).then( poll => {
  508. msg.delete();
  509. if ( reactions.length ) {
  510. reactions.forEach( function(entry) {
  511. poll.react(entry).catch( error => poll.react('440871715938238494') );
  512. } );
  513. } else {
  514. poll.react('448222377009086465');
  515. poll.react('448222455425794059');
  516. }
  517. }, error => msg.react('440871715938238494') );
  518. }
  519. function cmd_user(lang, msg, username, wiki, title) {
  520. if ( !username || username.indexOf( '/' ) != -1 || username.toLowerCase().startsWith('talk:') || username.toLowerCase().startsWith(lang.user.talk) ) {
  521. msg.channel.send( 'https://' + wiki + '.gamepedia.com/' + title );
  522. } else {
  523. var hourglass;
  524. msg.react('⏳').then( function( reaction ) {
  525. hourglass = reaction;
  526. request( {
  527. uri: 'https://' + wiki + '.gamepedia.com/api.php?action=query&format=json&list=users&usprop=blockinfo|groups|editcount|registration|gender&ususers=' + username,
  528. json: true
  529. }, function( error, response, body ) {
  530. if ( error || !response || !body || !body.query || !body.query.users[0] ) {
  531. console.log( 'Fehler beim Erhalten der Suchergebnisse' + ( error ? ': ' + error.message : ( body ? ( body.error ? ': ' + body.error.info : '.' ) : '.' ) ) );
  532. msg.channel.send( '<https://' + wiki + '.gamepedia.com/User:' + username + '>' ).then( message => message.react('440871715938238494') );
  533. }
  534. else {
  535. if ( body.query.users[0].missing == "" || body.query.users[0].invalid == "" ) {
  536. msg.react('🤷');
  537. }
  538. else {
  539. username = body.query.users[0].name.replace( / /g, '_' );
  540. var options = {
  541. year: "numeric",
  542. month: "short",
  543. day: "numeric",
  544. hour: "2-digit",
  545. minute: "2-digit"
  546. }
  547. var gender = body.query.users[0].gender;
  548. switch (gender) {
  549. case 'male':
  550. gender = lang.user.gender.male;
  551. break;
  552. case 'female':
  553. gender = lang.user.gender.female;
  554. break;
  555. default:
  556. gender = lang.user.gender.unknown;
  557. }
  558. var registration = (new Date(body.query.users[0].registration)).toLocaleString(lang.user.dateformat, options);
  559. var editcount = body.query.users[0].editcount;
  560. var groups = body.query.users[0].groups;
  561. var group = '';
  562. for ( var i = 0; i < lang.user.group.length; i++ ) {
  563. if ( groups.includes(lang.user.group[i][0]) ) {
  564. group = lang.user.group[i][1];
  565. break;
  566. }
  567. }
  568. var blockid = body.query.users[0].blockid;
  569. var blockedtimestamp = (new Date(body.query.users[0].blockedtimestamp)).toLocaleString(lang.user.dateformat, options);
  570. var blockexpiry = body.query.users[0].blockexpiry;
  571. if ( blockexpiry == 'infinity' ) {
  572. blockexpiry = lang.user.until_infinity;
  573. } else if ( blockexpiry ) {
  574. blockexpiry = (new Date(blockexpiry.replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2,3})/, '$1-$2-$3T$4:$5:$6Z'))).toLocaleString(lang.user.dateformat, options);
  575. }
  576. var blockedby = body.query.users[0].blockedby;
  577. var blockreason = body.query.users[0].blockreason;
  578. msg.channel.send( '<https://' + wiki + '.gamepedia.com/UserProfile:' + username + '>\n\n' + lang.user.info.replace( '%1$s', gender ).replace( '%2$s', registration ).replace( '%3$s', editcount ).replace( '%4$s', group ) + ( blockid ? '\n\n' + lang.user.blocked.replace( '%1$s', blockedtimestamp ).replace( '%2$s', blockexpiry ).replace( '%3$s', blockedby ).replace( '%4$s', blockreason ) : '' ) );
  579. }
  580. }
  581. if ( hourglass != undefined ) hourglass.remove();
  582. } );
  583. } );
  584. }
  585. }
  586. function cmd_diff(lang, msg, args, wiki) {
  587. if ( args[0] ){
  588. var title = '';
  589. var x;
  590. for ( var i = 0; i < args.length; i++ ) {
  591. if ( parseInt(args[i], 10) || args[i] == 'next' || args[i] == 'prev' ) {
  592. x = i;
  593. i = args.length;
  594. } else {
  595. if ( title ) title += '_';
  596. title += args[i];
  597. }
  598. }
  599. msg.channel.send( '<https://' + wiki + '.gamepedia.com/' + title + '?diff=' + ( args[x] ? args[x] + ( args[x+1] ? '&oldid=' + args[x+1] : '' ) : '' ) + '>' );
  600. }
  601. else msg.react('440871715938238494');
  602. }
  603. function cmd_multiline(lang, msg, args, line) {
  604. msg.react('440871715938238494');
  605. }
  606. function cmd_voice(lang, msg, args, line) {
  607. if ( admin(msg) ) {
  608. msg.reply( lang.voice.text + '\n`' + lang.voice.channel + ' – <' + lang.voice.name + '>`' );
  609. } else if ( msg.channel.type != 'text' || !pause[msg.guild.id] ) {
  610. cmd_link(lang, msg, line.split(' ').slice(1).join(' '), lang.link, '');
  611. }
  612. }
  613. function admin(msg) {
  614. if ( msg.channel.type == 'text' && ( msg.member.permissions.has('MANAGE_GUILD') || msg.author.id == process.env.owner ) ) return true;
  615. else return false;
  616. }
  617. function emoji(args) {
  618. var text = args.join(' ');
  619. var regex = /(<a?:)(\d+)(>)/g;
  620. if ( regex.test(text) ) {
  621. regex.lastIndex = 0;
  622. var emojis = client.emojis;
  623. var entry;
  624. while ( ( entry = regex.exec(text) ) !== null ) {
  625. if ( emojis.has(entry[2]) ) {
  626. text = text.replace(entry[0], emojis.get(entry[2]).toString());
  627. } else {
  628. text = text.replace(entry[0], entry[1] + 'unknown_emoji:' + entry[2] + entry[3]);
  629. }
  630. }
  631. args = text.split(' ');
  632. }
  633. return args;
  634. }
  635. client.on('message', msg => {
  636. var cont = msg.content;
  637. var author = msg.author;
  638. var channel = msg.channel;
  639. if ( cont.indexOf( process.env.prefix ) != -1 && !msg.webhookID && author.id != client.user.id && ( channel.type != 'text' || channel.permissionsFor(client.user).has('SEND_MESSAGES') ) ) {
  640. if ( settings == defaultSettings ) getSettings(setStatus);
  641. var setting = settings['default'];
  642. if ( channel.type == 'text' && msg.guild.id in settings ) setting = settings[msg.guild.id];
  643. var lang = i18n[setting.lang];
  644. lang.link = setting.wiki;
  645. var invoke = cont.split(' ')[1].toLowerCase();
  646. var aliasInvoke = ( invoke in lang.aliase ) ? lang.aliase[invoke] : invoke;
  647. if ( cont.toLowerCase().startsWith(process.env.prefix) && aliasInvoke in multilinecmdmap ) {
  648. if ( channel.type != 'text' || channel.permissionsFor(client.user).has('MANAGE_MESSAGES') ) {
  649. var args = cont.split(' ').slice(2);
  650. console.log((msg.guild ? msg.guild.name : '@' + author.username) + ': ' + invoke + ' - ' + args);
  651. if ( channel.type != 'text' || !pause[msg.guild.id] || ( author.id == process.env.owner && aliasInvoke in pausecmdmap ) ) multilinecmdmap[aliasInvoke](lang, msg, args, cont);
  652. } else {
  653. msg.reply( lang.missingperm );
  654. }
  655. } else {
  656. cont.split('\n').forEach( function(line) {
  657. if ( line.toLowerCase().startsWith(process.env.prefix) ) {
  658. invoke = line.split(' ')[1].toLowerCase();
  659. var args = line.split(' ').slice(2);
  660. aliasInvoke = ( invoke in lang.aliase ) ? lang.aliase[invoke] : invoke;
  661. console.log((msg.guild ? msg.guild.name : '@' + author.username) + ': ' + invoke + ' - ' + args);
  662. if ( channel.type != 'text' || !pause[msg.guild.id] ) {
  663. if ( aliasInvoke in cmdmap ) cmdmap[aliasInvoke](lang, msg, args, line);
  664. else if ( invoke.startsWith('!') ) cmd_link(lang, msg, args.join(' '), invoke.substr(1), invoke + ' ');
  665. else cmd_link(lang, msg, line.split(' ').slice(1).join(' '), lang.link, '');
  666. } else if ( channel.type == 'text' && pause[msg.guild.id] && author.id == process.env.owner && aliasInvoke in pausecmdmap ) {
  667. pausecmdmap[aliasInvoke](lang, msg, args, line);
  668. }
  669. }
  670. } );
  671. }
  672. }
  673. });
  674. client.on('voiceStateUpdate', (oldm, newm) => {
  675. if ( settings == defaultSettings ) getSettings(setStatus);
  676. if ( oldm.guild.me.permissions.has('MANAGE_ROLES') && oldm.voiceChannelID != newm.voiceChannelID ) {
  677. var setting = settings['default'];
  678. if ( oldm.guild.id in settings ) setting = settings[oldm.guild.id];
  679. var lang = i18n[setting.lang];
  680. lang.link = setting.wiki;
  681. if ( oldm.voiceChannel ) {
  682. var oldrole = oldm.guild.roles.find('name', lang.voice.channel + ' – ' + oldm.voiceChannel.name);
  683. if ( oldrole && oldrole.comparePositionTo(oldm.guild.me.highestRole) < 0 ) {
  684. oldm.removeRole( oldrole, lang.voice.left.replace( '%1$s', oldm.displayName ).replace( '%2$s', oldm.voiceChannel.name ) );
  685. console.log( oldm.guild.name + ': ' + oldm.displayName + ' hat den Sprachkanal "' + oldm.voiceChannel.name + '" verlassen.' );
  686. }
  687. }
  688. if ( newm.voiceChannel ) {
  689. var newrole = newm.guild.roles.find('name', lang.voice.channel + ' – ' + newm.voiceChannel.name);
  690. if ( newrole && newrole.comparePositionTo(newm.guild.me.highestRole) < 0 ) {
  691. newm.addRole( newrole, lang.voice.join.replace( '%1$s', newm.displayName ).replace( '%2$s', newm.voiceChannel.name ) );
  692. console.log( newm.guild.name + ': ' + newm.displayName + ' hat den Sprachkanal "' + newm.voiceChannel.name + '" betreten.' );
  693. }
  694. }
  695. }
  696. });
  697. client.on('guildCreate', guild => {
  698. client.fetchUser(process.env.owner).then( owner => owner.send( 'Ich wurde zu einem Server hinzugefügt:\n\n' + '"' + guild.toString() + '" von ' + guild.owner.toString() + ' mit ' + guild.memberCount + ' Mitgliedern\n' + guild.channels.find('type', 'text').toString() + ' (' + guild.id + ')' ) );
  699. console.log( 'Ich wurde zu einem Server hinzugefügt.' );
  700. });
  701. client.on('guildDelete', guild => {
  702. client.fetchUser(process.env.owner).then( owner => owner.send( 'Ich wurde von einem Server entfernt:\n\n' + '"' + guild.toString() + '" von ' + guild.owner.toString() + ' mit ' + guild.memberCount + ' Mitgliedern\n' + guild.channels.find('type', 'text').toString() + ' (' + guild.id + ')' ) );
  703. console.log( 'Ich wurde von einem Server entfernt.' );
  704. });
  705. client.login(process.env.token);