main.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. const fs = require('fs');
  2. const util = require('util');
  3. util.inspect.defaultOptions = {compact:false,breakLength:Infinity};
  4. const Discord = require('discord.js');
  5. const DBL = require("dblapi.js");
  6. var request = require('request');
  7. var client = new Discord.Client( {disableEveryone:true} );
  8. const dbl = new DBL(process.env.dbltoken, {statsInterval:10800000}, client);
  9. var i18n = JSON.parse(fs.readFileSync('i18n.json', 'utf8').trim());
  10. var minecraft = JSON.parse(fs.readFileSync('minecraft.json', 'utf8').trim());
  11. var pause = {};
  12. var defaultPermissions = new Discord.Permissions(268954688).toArray();
  13. var defaultSettings = {
  14. "default": {
  15. "lang": "en",
  16. "wiki": "help"
  17. }
  18. }
  19. var settings = defaultSettings;
  20. function getSettings(callback) {
  21. request( {
  22. uri: process.env.read + process.env.file + process.env.access,
  23. json: true
  24. }, function( error, response, body ) {
  25. if ( error || !response || !body || body.error ) {
  26. console.log( '- Fehler beim Erhalten der Einstellungen' + ( error ? ': ' + error : ( body ? ( body.error ? ': ' + body.error : '.' ) : '.' ) ) );
  27. }
  28. else {
  29. console.log( '- Einstellungen erfolgreich ausgelesen.' );
  30. settings = Object.assign({}, body);
  31. }
  32. callback();
  33. } );
  34. }
  35. function setStatus() {
  36. if ( settings == defaultSettings ) client.user.setStatus('invisible');
  37. else {
  38. client.user.setStatus('online');
  39. client.user.setActivity( process.env.prefix + ' help' );
  40. }
  41. }
  42. var defaultSites = [];
  43. var allSites = defaultSites;
  44. function getAllSites() {
  45. request( {
  46. uri: 'https://help.gamepedia.com/api.php?action=allsites&format=json&formatversion=2&do=getSiteStats&filter=wikis|wiki_domain,wiki_display_name,official_wiki,wiki_managers',
  47. json: true
  48. }, function( error, response, body ) {
  49. if ( error || !response || !body || body.status != 'okay' || !body.data || !body.data.wikis ) {
  50. console.log( '- Fehler beim Erhalten der Wikis' + ( error ? ': ' + error : ( body ? ( body.error ? ': ' + body.error.info : '.' ) : '.' ) ) );
  51. }
  52. else {
  53. console.log( '- Wikis erfolgreich ausgelesen.' );
  54. allSites = Object.assign([], body.data.wikis.filter( site => /^[a-z\d-]{1,30}\.gamepedia\.com$/.test(site.wiki_domain) ));
  55. }
  56. } );
  57. }
  58. client.on('ready', () => {
  59. getSettings(setStatus);
  60. getAllSites();
  61. console.log( '- Erfolgreich als ' + client.user.username + ' angemeldet!' );
  62. } );
  63. dbl.on('posted', () => {
  64. request.post( {
  65. uri: 'https://bots.discord.pw/api/bots/' + client.user.id + '/stats',
  66. headers: {
  67. authorization: process.env.dbpwtoken
  68. },
  69. body: {
  70. server_count: client.guilds.size
  71. },
  72. json: true
  73. }, function( error, response, body ) {
  74. console.log( '- Anzahl der Server: ' + client.guilds.size );
  75. } );
  76. } );
  77. dbl.on('error', error => {
  78. console.log( '--- DBL-ERROR: ' + new Date(Date.now()).toLocaleTimeString('de-DE', { timeZone: 'Europe/Berlin' }) + ' ---\n- ' + error );
  79. } );
  80. var cmdmap = {
  81. help: cmd_help,
  82. test: cmd_test,
  83. invite: cmd_invite,
  84. stop: cmd_stop,
  85. pause: cmd_pause,
  86. say: cmd_multiline,
  87. delete: cmd_multiline,
  88. poll: cmd_multiline,
  89. voice: cmd_voice,
  90. settings: cmd_settings,
  91. info: cmd_info,
  92. eval: cmd_eval,
  93. get: cmd_get
  94. }
  95. var multilinecmdmap = {
  96. say: cmd_say,
  97. delete: cmd_delete,
  98. poll: cmd_umfrage,
  99. eval: cmd_eval,
  100. get: cmd_get
  101. }
  102. var pausecmdmap = {
  103. test: cmd_test,
  104. stop: cmd_stop,
  105. pause: cmd_pause,
  106. say: cmd_multiline,
  107. delete: cmd_multiline,
  108. eval: cmd_eval,
  109. get: cmd_get
  110. }
  111. var minecraftcmdmap = {
  112. command: cmd_befehl2,
  113. bug: cmd_bug
  114. }
  115. function cmd_settings(lang, msg, args, line) {
  116. if ( admin(msg) ) {
  117. if ( msg.guild.id in settings ) {
  118. var text = lang.settings.current.replace( '%1$s', '- `' + process.env.prefix + ' settings lang`' ).replace( '%2$s', 'https://' + settings[msg.guild.id].wiki + '.gamepedia.com/ - `' + process.env.prefix + ' settings wiki`' ) + ' - `' + process.env.prefix + ' settings channel`\n';
  119. if ( settings[msg.guild.id].channels ) {
  120. Object.keys(settings[msg.guild.id].channels).forEach( function(channel) {
  121. text += '<#' + channel + '>: <https://' + settings[msg.guild.id].channels[channel] + '.gamepedia.com/>\n';
  122. } );
  123. } else text += lang.settings.nochannels;
  124. } else {
  125. var text = lang.settings.missing.replace( '%1$s', '`' + process.env.prefix + ' settings lang`' ).replace( '%2$s', '`' + process.env.prefix + ' settings wiki`' );
  126. }
  127. if ( args.length ) {
  128. if ( args[0] ) args[0] = args[0].toLowerCase();
  129. if ( args[1] ) args[1] = args.slice(1).join(' ').toLowerCase();
  130. var langs = '\n' + lang.settings.langhelp.replace( '%s', process.env.prefix + ' settings lang' ) + ' `' + i18n.allLangs[1].join(', ') + '`';
  131. var wikis = '\n' + lang.settings.wikihelp.replace( '%s', process.env.prefix + ' settings wiki' );
  132. var channels = '\n' + lang.settings.wikihelp.replace( '%s', process.env.prefix + ' settings channel' );
  133. var nolangs = lang.settings.langinvalid + langs;
  134. var nowikis = lang.settings.wikiinvalid + wikis;
  135. var nochannels = lang.settings.wikiinvalid + channels;
  136. var regex = /^(?:(?:https?:)?\/\/)?([a-z\d-]{1,30})\.gamepedia\.com/
  137. if ( msg.guild.id in settings ) {
  138. var current = args[0] + ( line == 'changed' ? line : '' );
  139. if ( args[0] == 'lang' ) {
  140. if ( args[1] ) {
  141. if ( args[1] in i18n.allLangs[0] ) edit_settings(lang, msg, 'lang', i18n.allLangs[0][args[1]]);
  142. else msg.reply( nolangs );
  143. } else msg.reply( lang.settings[current] + langs );
  144. } else if ( args[0] == 'wiki' ) {
  145. if ( args[1] ) {
  146. if ( regex.test(args[1]) ) edit_settings(lang, msg, 'wiki', regex.exec(args[1])[1]);
  147. else find_wikis(lang, msg, 'wiki', args[1].split(' '), nowikis);
  148. } else msg.reply( lang.settings[current] + ' https://' + settings[msg.guild.id].wiki + '.gamepedia.com/' + wikis );
  149. } else if ( args[0] == 'channel' ) {
  150. if ( args[1] ) {
  151. if ( regex.test(args[1]) ) edit_settings(lang, msg, 'channel', regex.exec(args[1])[1]);
  152. else find_wikis(lang, msg, 'channel', args[1].split(' '), nochannels);
  153. } else if ( settings[msg.guild.id].channels && msg.channel.id in settings[msg.guild.id].channels ) {
  154. msg.reply( lang.settings[current] + ' https://' + settings[msg.guild.id].channels[msg.channel.id] + '.gamepedia.com/' + channels );
  155. } else msg.reply( lang.settings[current] + ' https://' + settings[msg.guild.id].wiki + '.gamepedia.com/' + channels );
  156. } else msg.reply( text );
  157. } else {
  158. if ( args[0] == 'lang' ) {
  159. if ( args[1] ) {
  160. if ( args[1] in i18n.allLangs[0] ) edit_settings(lang, msg, 'lang', i18n.allLangs[0][args[1]]);
  161. else msg.reply( nolangs );
  162. } else msg.reply( lang.settings.lang + langs );
  163. } else if ( args[0] == 'wiki' || args[0] == 'channel' ) {
  164. if ( args[1] ) {
  165. if ( regex.test(args[1]) ) edit_settings(lang, msg, 'wiki', regex.exec(args[1])[1]);
  166. else find_wikis(lang, msg, 'wiki', args[1].split(' '), nowikis);
  167. } else msg.reply( lang.settings.wikimissing + wikis );
  168. } else msg.reply( text );
  169. }
  170. } else msg.reply( text );
  171. } else {
  172. msg.reactEmoji('❌');
  173. }
  174. }
  175. function find_wikis(lang, msg, key, value, text) {
  176. if ( allSites.find( site => site.wiki_domain == value.join('') + '.gamepedia.com' ) ) edit_settings(lang, msg, key, value.join(''));
  177. else {
  178. var sites = allSites.filter( site => site.wiki_display_name.toLowerCase().includes( value.join(' ') ) );
  179. if ( 0 < sites.length && sites.length < 21 ) {
  180. text += '\n\n' + lang.settings.foundwikis;
  181. sites.forEach( function(site) {
  182. text += '\n' + site.wiki_display_name + ': `' + site.wiki_domain + '`';
  183. } );
  184. }
  185. msg.reply( text, {split:true} );
  186. }
  187. }
  188. function edit_settings(lang, msg, key, value) {
  189. msg.reactEmoji('⏳').then( function( reaction ) {
  190. if ( settings == defaultSettings ) {
  191. console.log( '- Fehler beim Erhalten bestehender Einstellungen.' );
  192. msg.reply( lang.settings.save_failed );
  193. reaction.removeEmoji();
  194. }
  195. else {
  196. var temp_settings = Object.assign({}, settings);
  197. if ( !( msg.guild.id in temp_settings ) ) temp_settings[msg.guild.id] = Object.assign({}, defaultSettings['default']);
  198. if ( key == 'channel' ) {
  199. if ( !temp_settings[msg.guild.id].channels ) temp_settings[msg.guild.id].channels = {};
  200. temp_settings[msg.guild.id].channels[msg.channel.id] = value;
  201. } else temp_settings[msg.guild.id][key] = value;
  202. Object.keys(temp_settings).forEach( function(guild) {
  203. if ( !client.guilds.has(guild) && guild != 'default' ) {
  204. delete temp_settings[guild];
  205. } else {
  206. var channels = temp_settings[guild].channels;
  207. if ( channels ) {
  208. Object.keys(channels).forEach( function(channel) {
  209. if ( channels[channel] == temp_settings[guild].wiki || !client.guilds.get(guild).channels.has(channel) ) delete channels[channel];
  210. } );
  211. if ( !Object.keys(channels).length ) delete temp_settings[guild].channels;
  212. }
  213. }
  214. } );
  215. request.post( {
  216. uri: process.env.save + process.env.access,
  217. body: {
  218. branch: 'master',
  219. commit_message: client.user.username + ': Einstellungen aktualisiert.',
  220. actions: [
  221. {
  222. action: 'update',
  223. file_path: process.env.file,
  224. content: JSON.stringify( temp_settings, null, '\t' )
  225. }
  226. ]
  227. },
  228. json: true
  229. }, function( error, response, body ) {
  230. if ( error || !response || response.statusCode != 201 || !body || body.error ) {
  231. console.log( '- Fehler beim Bearbeiten' + ( error ? ': ' + error : ( body ? ( body.message ? ': ' + body.message : ( body.error ? ': ' + body.error : '.' ) ) : '.' ) ) );
  232. msg.reply( lang.settings.save_failed );
  233. }
  234. else {
  235. settings = Object.assign({}, temp_settings);
  236. if ( key == 'lang' ) lang = i18n[value];
  237. cmd_settings(lang, msg, [key], 'changed');
  238. console.log( '- Einstellungen erfolgreich aktualisiert.' );
  239. }
  240. reaction.removeEmoji();
  241. } );
  242. }
  243. } );
  244. }
  245. function cmd_info(lang, msg, args, line) {
  246. if ( args.length ) cmd_link(lang, msg, line.split(' ').slice(1).join(' '));
  247. else {
  248. var owner = '*MarkusRost*';
  249. if ( msg.channel.type == 'text' && msg.guild.members.has(process.env.owner) ) owner = '<@' + process.env.owner + '>';
  250. msg.channel.send( lang.disclaimer.replace( '%s', owner ) );
  251. cmd_helpserver(lang, msg);
  252. cmd_invite(lang, msg, args, line);
  253. }
  254. }
  255. function cmd_helpserver(lang, msg) {
  256. msg.channel.send( lang.helpserver + '\nhttps://discord.gg/v77RTk5' );
  257. }
  258. function cmd_help(lang, msg, args, line) {
  259. if ( admin(msg) && !( msg.guild.id in settings ) ) {
  260. cmd_settings(lang, msg, [], line);
  261. cmd_helpserver(lang, msg);
  262. }
  263. var cmds = lang.help.list;
  264. var isMinecraft = ( lang.link == minecraft[lang.lang].link );
  265. if ( args.length ) {
  266. if ( mention(msg, args.join(' ')) ) cmd_helpserver(lang, msg);
  267. else if ( args[0].toLowerCase() == 'admin' ) {
  268. if ( msg.channel.type != 'text' || admin(msg) ) {
  269. var cmdlist = lang.help.admin + '\n';
  270. for ( var i = 0; i < cmds.length; i++ ) {
  271. if ( cmds[i].admin && !cmds[i].hide ) {
  272. cmdlist += '🔹 `' + process.env.prefix + ' ' + cmds[i].cmd + '`\n\t' + cmds[i].desc + '\n';
  273. }
  274. }
  275. msg.channel.send( cmdlist, {split:true} );
  276. }
  277. else {
  278. msg.reply( lang.help.noadmin );
  279. }
  280. }
  281. else {
  282. var cmdlist = ''
  283. for ( var i = 0; i < cmds.length; i++ ) {
  284. if ( cmds[i].cmd.split(' ')[0] === args[0].toLowerCase() && !cmds[i].unsearchable && ( msg.channel.type != 'text' || !cmds[i].admin || admin(msg) ) && ( !cmds[i].minecraft || isMinecraft ) ) {
  285. cmdlist += '🔹 `' + process.env.prefix + ' ' + cmds[i].cmd + '`\n\t' + cmds[i].desc + '\n';
  286. }
  287. }
  288. if ( cmdlist == '' ) msg.reactEmoji('❓');
  289. else msg.channel.send( cmdlist, {split:true} );
  290. }
  291. }
  292. else {
  293. var cmdlist = lang.help.all + '\n';
  294. for ( var i = 0; i < cmds.length; i++ ) {
  295. if ( !cmds[i].hide && !cmds[i].admin && ( !cmds[i].minecraft || isMinecraft ) ) {
  296. cmdlist += '🔹 `' + process.env.prefix + ' ' + cmds[i].cmd + '`\n\t' + cmds[i].desc + '\n';
  297. }
  298. }
  299. msg.channel.send( cmdlist, {split:true} );
  300. }
  301. }
  302. function cmd_say(lang, msg, args, line) {
  303. if ( admin(msg) ) {
  304. args = emoji(args);
  305. var text = args.join(' ');
  306. if ( args[0] == 'alarm' ) text = '🚨 **' + args.slice(1).join(' ') + '** 🚨';
  307. var imgs = [];
  308. var i = 0;
  309. msg.attachments.forEach( function(img) {
  310. imgs[i] = {attachment:img.proxyURL,name:img.filename};
  311. i++;
  312. } );
  313. if ( msg.author.id == process.env.owner ) {
  314. try {
  315. text = eval( '`' + text + '`' );
  316. } catch ( error ) {
  317. console.log( '- ' + error );
  318. }
  319. }
  320. if ( text || imgs[0] ) {
  321. msg.channel.send( text, {disableEveryone:!msg.member.hasPermission(['MENTION_EVERYONE']),files:imgs} ).then( message => msg.delete().catch( error => console.log( '- ' + error ) ), error => msg.reactEmoji('error') );
  322. }
  323. } else {
  324. msg.reactEmoji('❌');
  325. }
  326. }
  327. function cmd_test(lang, msg, args, line) {
  328. if ( args.length ) {
  329. cmd_link(lang, msg, line.split(' ').slice(1).join(' '));
  330. } else if ( msg.channel.type != 'text' || !pause[msg.guild.id] ) {
  331. var text = lang.test.default;
  332. var x = Math.floor(Math.random() * lang.test.random);
  333. if ( x < lang.test.text.length ) text = lang.test.text[x];
  334. msg.reply( text );
  335. console.log( '- Dies ist ein Test: Voll funktionsfähig!' );
  336. } else {
  337. msg.reply( lang.test.pause );
  338. console.log( '- Dies ist ein Test: Pausiert!' );
  339. }
  340. }
  341. function cmd_invite(lang, msg, args, line) {
  342. if ( args.length ) {
  343. cmd_link(lang, msg, line.split(' ').slice(1).join(' '));
  344. } else {
  345. client.generateInvite(defaultPermissions).then( invite => msg.channel.send( lang.invite.bot + '\n<' + invite + '>' ) );
  346. }
  347. }
  348. function cmd_eval(lang, msg, args, line) {
  349. if ( msg.author.id == process.env.owner ) {
  350. try {
  351. var text = util.inspect( eval( args.join(' ') ) );
  352. } catch ( error ) {
  353. var text = error.toString();
  354. }
  355. console.log( '--- EVAL START ---\n\u200b' + text.replace( /\n/g, '\n\u200b' ) + '\n--- EVAL END ---' );
  356. if ( text == 'Promise {\n <pending>\n}' ) msg.reactEmoji('✅');
  357. else msg.channel.send( '```js\n' + text + '\n```', {split:{prepend:'```js\n',append:'\n```'}} ).catch( err => console.log( '- ' + err ) );
  358. } else if ( msg.channel.type != 'text' || !pause[msg.guild.id] ) {
  359. msg.reactEmoji('❌');
  360. }
  361. }
  362. function cmd_stop(lang, msg, args, line) {
  363. if ( msg.author.id == process.env.owner && mention(msg, args.join(' ')) ) {
  364. msg.reply( 'I\'ll turn me off now!' );
  365. console.log( '- Ich schalte mich nun aus!' );
  366. client.destroy();
  367. } else if ( msg.channel.type != 'text' || !pause[msg.guild.id] ) {
  368. cmd_link(lang, msg, line.split(' ').slice(1).join(' '));
  369. }
  370. }
  371. function cmd_pause(lang, msg, args, line) {
  372. if ( msg.channel.type == 'text' && msg.author.id == process.env.owner && mention(msg, args.join(' ')) ) {
  373. if ( pause[msg.guild.id] ) {
  374. msg.reply( 'I\'m up again!' );
  375. console.log( '- Ich bin wieder wach!' );
  376. delete pause[msg.guild.id];
  377. } else {
  378. msg.reply( 'I\'m going to sleep now!' );
  379. console.log( '- Ich lege mich nun schlafen!' );
  380. pause[msg.guild.id] = true;
  381. }
  382. } else if ( msg.channel.type != 'text' || !pause[msg.guild.id] ) {
  383. cmd_link(lang, msg, line.split(' ').slice(1).join(' '));
  384. }
  385. }
  386. function cmd_delete(lang, msg, args, line) {
  387. if ( admin(msg) ) {
  388. if ( /^\d+$/.test(args[0]) && parseInt(args[0], 10) + 1 > 0 ) {
  389. if ( parseInt(args[0], 10) > 99 ) {
  390. msg.reply( lang.delete.big.replace( '%s', '`99`' ) );
  391. }
  392. else {
  393. msg.channel.bulkDelete(parseInt(args[0], 10) + 1, true).then( messages => {
  394. msg.reply( lang.delete.success.replace( '%s', messages.size - 1 ) ).then( antwort => antwort.delete(3000) );
  395. console.log( '- Die letzten ' + ( messages.size - 1 ) + ' Nachrichten in #' + msg.channel.name + ' wurden gelöscht!' );
  396. } );
  397. }
  398. }
  399. else {
  400. msg.reply( lang.delete.invalid );
  401. }
  402. } else {
  403. msg.reactEmoji('❌');
  404. }
  405. }
  406. function cmd_link(lang, msg, title, wiki = lang.link, cmd = ' ', querystring = '', fragment = '') {
  407. if ( cmd == ' ' && admin(msg) && !( msg.guild.id in settings ) ) {
  408. cmd_settings(lang, msg, [], '');
  409. cmd_helpserver(lang, msg);
  410. }
  411. if ( title.includes( '#' ) ) {
  412. fragment = title.split('#').slice(1).join('#');
  413. title = title.split('#')[0];
  414. }
  415. if ( /\?[a-z]+=/.test(title) ) {
  416. var querystart = title.search(/\?[a-z]+=/);
  417. querystring = title.substr(querystart + 1);
  418. title = title.substr(0, querystart);
  419. }
  420. var linksuffix = ( querystring ? '?' + querystring.toTitle() : '' ) + ( fragment ? '#' + fragment.toSection() : '' );
  421. if ( title.length > 300 ) {
  422. title = title.substr(0, 300);
  423. msg.reactEmoji('⚠');
  424. }
  425. var invoke = title.split(' ')[0].toLowerCase();
  426. var args = title.split(' ').slice(1);
  427. var mclang = minecraft[lang.lang];
  428. var aliasInvoke = ( invoke in mclang.aliase ) ? mclang.aliase[invoke] : invoke;
  429. if ( !msg.notminecraft && wiki == mclang.link && ( aliasInvoke in minecraftcmdmap || invoke.startsWith('/') ) ) {
  430. if ( aliasInvoke in minecraftcmdmap ) minecraftcmdmap[aliasInvoke](lang, mclang, msg, args, title, cmd, querystring, fragment);
  431. else cmd_befehl(lang, mclang, msg, invoke.substr(1), args, title, cmd, querystring, fragment);
  432. }
  433. else if ( ( invoke == 'random' || invoke == '🎲' ) && !args.join('') && !linksuffix ) cmd_random(lang, msg, wiki);
  434. else if ( invoke == 'page' || invoke == lang.search.page ) msg.channel.send( '<https://' + wiki + '.gamepedia.com/' + args.join('_').toTitle() + linksuffix + '>' );
  435. else if ( invoke == 'search' || invoke == lang.search.search ) msg.channel.send( '<https://' + wiki + '.gamepedia.com/Special:Search/' + args.join('_').toTitle() + linksuffix + '>' );
  436. else if ( invoke == 'diff' ) cmd_diff(lang, msg, args, wiki);
  437. else if ( !msg.notuser && ( invoke == 'user' || invoke == lang.search.user.unknown || invoke == lang.search.user.male || invoke == lang.search.user.female ) ) cmd_user(lang, msg, args.join('_').toTitle(), wiki, title.toTitle(), cmd, querystring, fragment);
  438. else if ( !msg.notuser && invoke.startsWith('user:') ) cmd_user(lang, msg, title.substr(5).toTitle(), wiki, title.toTitle(), cmd, querystring, fragment);
  439. else if ( !msg.notuser && invoke.startsWith('userprofile:') ) cmd_user(lang, msg, title.substr(12).toTitle(), wiki, title.toTitle(), cmd, querystring, fragment);
  440. else if ( !msg.notuser && invoke.startsWith(lang.search.user.unknown + ':') ) cmd_user(lang, msg, title.substr(lang.search.user.unknown.length + 1).toTitle(), wiki, title.toTitle(), cmd, querystring, fragment);
  441. else if ( !msg.notuser && invoke.startsWith(lang.search.user.male + ':') ) cmd_user(lang, msg, title.substr(lang.search.user.male.length + 1).toTitle(), wiki, title.toTitle(), cmd, querystring, fragment);
  442. else if ( !msg.notuser && invoke.startsWith(lang.search.user.female + ':') ) cmd_user(lang, msg, title.substr(lang.search.user.female.length + 1).toTitle(), wiki, title.toTitle(), cmd, querystring, fragment);
  443. else {
  444. msg.reactEmoji('⏳').then( function( reaction ) {
  445. request( {
  446. uri: 'https://' + wiki + '.gamepedia.com/api.php?action=query&format=json&meta=siteinfo&siprop=general&iwurl=true&redirects=true&titles=' + encodeURIComponent( title ),
  447. json: true
  448. }, function( error, response, body ) {
  449. if ( error || !response || !body || !body.query ) {
  450. if ( response && response.request && response.request.uri && response.request.uri.href == 'https://www.gamepedia.com/' ) {
  451. console.log( '- Dieses Wiki existiert nicht! ' + ( error ? error.message : ( body ? ( body.error ? body.error.info : '' ) : '' ) ) );
  452. msg.reactEmoji('nowiki');
  453. }
  454. else {
  455. console.log( '- Fehler beim Erhalten der Suchergebnisse' + ( error ? ': ' + error : ( body ? ( body.error ? ': ' + body.error.info : '.' ) : '.' ) ) );
  456. msg.channel.send( '<https://' + wiki + '.gamepedia.com/Special:Search/' + title.toTitle() + linksuffix + '>' ).then( message => message.reactEmoji('error') );
  457. }
  458. }
  459. else {
  460. if ( body.query.pages ) {
  461. if ( body.query.pages['-1'] && ( ( body.query.pages['-1'].missing != undefined && body.query.pages['-1'].known == undefined ) || body.query.pages['-1'].invalid != undefined ) ) {
  462. request( {
  463. uri: 'https://' + wiki + '.gamepedia.com/api.php?action=query&format=json&list=search&srnamespace=0|4|12|14|10000|10002|10004|10006|10008|10010&srsearch=' + encodeURIComponent( title ) + '&srlimit=1',
  464. json: true
  465. }, function( srerror, srresponse, srbody ) {
  466. if ( srerror || !srresponse || !srbody || !srbody.query || ( !srbody.query.search[0] && srbody.query.searchinfo.totalhits != 0 ) ) {
  467. console.log( '- Fehler beim Erhalten der Suchergebnisse' + ( srerror ? ': ' + srerror : ( srbody ? ( srbody.error ? ': ' + srbody.error.info : '.' ) : '.' ) ) );
  468. msg.channel.send( '<https://' + wiki + '.gamepedia.com/Special:Search/' + title.toTitle() + linksuffix + '>' ).then( message => message.reactEmoji('error') );
  469. }
  470. else {
  471. if ( srbody.query.searchinfo.totalhits == 0 ) {
  472. msg.reactEmoji('🤷');
  473. }
  474. else {
  475. var pagelink = 'https://' + wiki + '.gamepedia.com/' + srbody.query.search[0].title.toTitle() + linksuffix;
  476. if ( title.toTitle().toLowerCase() == srbody.query.search[0].title.toTitle().toLowerCase() ) {
  477. msg.channel.send( pagelink );
  478. }
  479. else if ( srbody.query.searchinfo.totalhits == 1 ) {
  480. msg.channel.send( pagelink + '\n' + lang.search.infopage.replace( '%s', '`' + process.env.prefix + cmd + lang.search.page + ' ' + title + '`' ) );
  481. }
  482. else {
  483. msg.channel.send( pagelink + '\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 + '`' ) );
  484. }
  485. }
  486. }
  487. } );
  488. }
  489. else {
  490. msg.channel.send( 'https://' + wiki + '.gamepedia.com/' + Object.values(body.query.pages)[0].title.toTitle() + ( querystring ? '?' + querystring.toTitle() : '' ) + ( body.query.redirects && body.query.redirects[0].tofragment ? '#' + body.query.redirects[0].tofragment.toSection() : ( fragment ? '#' + fragment.toSection() : '' ) ) );
  491. }
  492. }
  493. else if ( body.query.interwiki ) {
  494. var inter = body.query.interwiki[0];
  495. var intertitle = inter.title.substr(inter.iw.length + 1);
  496. var regex = /^(?:https?:)?\/\/(.*)\.gamepedia\.com\//.exec(inter.url);
  497. if ( regex != null ) {
  498. var iwtitle = decodeURIComponent( inter.url.replace( regex[0], '' ) ).replace( /\_/g, ' ' ).replace( intertitle.replace( /\_/g, ' ' ), intertitle );
  499. cmd_link(lang, msg, iwtitle, regex[1], ' !' + regex[1] + ' ', querystring, fragment);
  500. } else msg.channel.send( inter.url + linksuffix );
  501. }
  502. else {
  503. msg.channel.send( 'https://' + wiki + '.gamepedia.com/' + body.query.general.mainpage.toTitle() );
  504. }
  505. }
  506. reaction.removeEmoji();
  507. } );
  508. } );
  509. }
  510. }
  511. function cmd_umfrage(lang, msg, args, line) {
  512. if ( admin(msg) ) {
  513. var imgs = [];
  514. var a = 0;
  515. msg.attachments.forEach( function(img) {
  516. imgs[a] = {attachment:img.proxyURL,name:img.filename};
  517. a++;
  518. } );
  519. if ( args.length || imgs[0] ) {
  520. var reactions = [];
  521. args = emoji(args);
  522. for ( var i = 0; ( i < args.length || imgs[0] ); i++ ) {
  523. var reaction = args[i];
  524. var custom = /^<a?:/;
  525. var pattern = /^[\w\säÄöÖüÜßẞ!"#$%&'()*+,./:;<=>?@^`{|}~–[\]\-\\]{2,}/;
  526. if ( !custom.test(reaction) && pattern.test(reaction) ) {
  527. cmd_sendumfrage(lang, msg, args, reactions, imgs, i);
  528. break;
  529. } else if ( reaction == '' ) {
  530. } else {
  531. if ( custom.test(reaction) ) {
  532. reaction = reaction.substring(reaction.lastIndexOf(':') + 1, reaction.length - 1);
  533. }
  534. reactions[i] = reaction;
  535. if ( i == args.length - 1 ) {
  536. cmd_sendumfrage(lang, msg, args, reactions, imgs, i + 1);
  537. break;
  538. }
  539. }
  540. }
  541. } else {
  542. args[0] = line.split(' ')[1];
  543. cmd_help(lang, msg, args, line);
  544. }
  545. } else {
  546. msg.reactEmoji('❌');
  547. }
  548. }
  549. function cmd_sendumfrage(lang, msg, args, reactions, imgs, i) {
  550. msg.channel.send( lang.poll.title + args.slice(i).join(' '), {disableEveryone:!msg.member.hasPermission(['MENTION_EVERYONE']),files:imgs} ).then( poll => {
  551. msg.delete().catch( error => console.log( '- ' + error ) );
  552. if ( reactions.length ) {
  553. reactions.forEach( function(entry) {
  554. poll.react(entry).catch( error => poll.reactEmoji('error') );
  555. } );
  556. } else {
  557. poll.reactEmoji('support');
  558. poll.reactEmoji('oppose');
  559. }
  560. }, error => msg.reactEmoji('error') );
  561. }
  562. function cmd_user(lang, msg, username, wiki, title, cmd, querystring, fragment) {
  563. if ( !username || username.includes( '/' ) || username.toLowerCase().startsWith('talk:') || username.toLowerCase().startsWith(lang.user.talk) ) {
  564. msg.notuser = true;
  565. cmd_link(lang, msg, title, wiki, cmd, querystring, fragment);
  566. } else {
  567. msg.reactEmoji('⏳').then( function( reaction ) {
  568. request( {
  569. uri: 'https://' + wiki + '.gamepedia.com/api.php?action=query&format=json&list=users&usprop=blockinfo|groups|editcount|registration|gender&ususers=' + encodeURIComponent( username ),
  570. json: true
  571. }, function( error, response, body ) {
  572. if ( error || !response || !body || !body.query || !body.query.users[0] ) {
  573. if ( response && response.request && response.request.uri && response.request.uri.href == 'https://www.gamepedia.com/' ) {
  574. console.log( '- Dieses Wiki existiert nicht! ' + ( error ? error.message : ( body ? ( body.error ? body.error.info : '' ) : '' ) ) );
  575. msg.reactEmoji('nowiki');
  576. }
  577. else {
  578. console.log( '- Fehler beim Erhalten der Suchergebnisse' + ( error ? ': ' + error : ( body ? ( body.error ? ': ' + body.error.info : '.' ) : '.' ) ) );
  579. msg.channel.send( '<https://' + wiki + '.gamepedia.com/User:' + username + '>' ).then( message => message.reactEmoji('error') );
  580. }
  581. }
  582. else {
  583. if ( body.query.users[0].missing == "" || body.query.users[0].invalid == "" ) {
  584. msg.reactEmoji('🤷');
  585. }
  586. else {
  587. username = body.query.users[0].name.replace( / /g, '_' );
  588. var timeoptions = {
  589. year: 'numeric',
  590. month: 'short',
  591. day: 'numeric',
  592. hour: '2-digit',
  593. minute: '2-digit',
  594. timeZone: 'UTC',
  595. timeZoneName: 'short'
  596. }
  597. var gender = body.query.users[0].gender;
  598. switch (gender) {
  599. case 'male':
  600. gender = lang.user.gender.male;
  601. break;
  602. case 'female':
  603. gender = lang.user.gender.female;
  604. break;
  605. default:
  606. gender = lang.user.gender.unknown;
  607. }
  608. var registration = (new Date(body.query.users[0].registration)).toLocaleString(lang.user.dateformat, timeoptions);
  609. var editcount = body.query.users[0].editcount;
  610. var groups = body.query.users[0].groups;
  611. var group = '';
  612. for ( var i = 0; i < lang.user.group.length; i++ ) {
  613. if ( groups.includes( lang.user.group[i][0] ) ) {
  614. var thisSite = allSites.find( site => site.wiki_domain == wiki + '.gamepedia.com' );
  615. if ( lang.user.group[i][0] == 'hydra_staff' && thisSite && thisSite.wiki_managers.includes( username.replace( /\_/g, ' ' ) ) ) group = lang.user.manager;
  616. else group = lang.user.group[i][1];
  617. break;
  618. }
  619. }
  620. var isBlocked = false;
  621. var blockedtimestamp = (new Date(body.query.users[0].blockedtimestamp)).toLocaleString(lang.user.dateformat, timeoptions);
  622. var blockexpiry = body.query.users[0].blockexpiry;
  623. if ( blockexpiry == 'infinity' ) {
  624. blockexpiry = lang.user.until_infinity;
  625. isBlocked = true;
  626. } else if ( blockexpiry ) {
  627. var blockexpirydate = blockexpiry.replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2,3})/, '$1-$2-$3T$4:$5:$6Z');
  628. blockexpiry = (new Date(blockexpirydate)).toLocaleString(lang.user.dateformat, timeoptions);
  629. if ( Date.parse(blockexpirydate) > Date.now() ) isBlocked = true;
  630. }
  631. var blockedby = body.query.users[0].blockedby;
  632. var blockreason = body.query.users[0].blockreason;
  633. 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 ) + ( isBlocked ? '\n\n' + lang.user.blocked.replace( '%1$s', blockedtimestamp ).replace( '%2$s', blockexpiry ).replace( '%3$s', blockedby ).replace( '%4$s', blockreason.wikicode() ) : '' ) );
  634. }
  635. }
  636. reaction.removeEmoji();
  637. } );
  638. } );
  639. }
  640. }
  641. function cmd_diff(lang, msg, args, wiki) {
  642. if ( args[0] ) {
  643. var error = false;
  644. var title = '';
  645. var revision = 0;
  646. var diff = 0;
  647. var relative = 'prev';
  648. if ( /^\d+$/.test(args[0]) ) {
  649. revision = args[0];
  650. if ( args[1] ) {
  651. if ( /^\d+$/.test(args[1]) ) {
  652. diff = args[1];
  653. }
  654. else if ( args[1] == 'prev' || args[1] == 'next' || args[1] == 'cur' ) {
  655. relative = args[1];
  656. }
  657. else error = true;
  658. }
  659. }
  660. else if ( args[0] == 'prev' || args[0] == 'next' || args[0] == 'cur' ) {
  661. relative = args[0];
  662. if ( args[1] ) {
  663. if ( /^\d+$/.test(args[1]) ) {
  664. revision = args[1];
  665. }
  666. else error = true;
  667. }
  668. else error = true;
  669. }
  670. else title = args.join('_').replace( /\?/g, '%3F' );
  671. if ( error ) msg.reactEmoji('error');
  672. else if ( diff ) {
  673. var argids = [];
  674. if ( parseInt(revision, 10) > parseInt(diff, 10) ) argids = [revision, diff];
  675. else if ( parseInt(revision, 10) == parseInt(diff, 10) ) argids = [revision];
  676. else argids = [diff, revision];
  677. cmd_diffsend(lang, msg, argids, wiki);
  678. }
  679. else {
  680. msg.reactEmoji('⏳').then( function( reaction ) {
  681. request( {
  682. uri: 'https://' + wiki + '.gamepedia.com/api.php?action=compare&format=json&prop=ids' + ( title ? '&fromtitle=' + title : '&fromrev=' + revision ) + '&torelative=' + relative,
  683. json: true
  684. }, function( error, response, body ) {
  685. var noerror = false;
  686. if ( body && body.error ) {
  687. switch ( body.error.code ) {
  688. case 'nosuchrevid':
  689. noerror = true;
  690. break;
  691. case 'missingtitle':
  692. noerror = true;
  693. break;
  694. case 'invalidtitle':
  695. noerror = true;
  696. break;
  697. default:
  698. noerror = false;
  699. }
  700. }
  701. if ( error || !response || !body || ( !body.compare && !noerror ) ) {
  702. if ( response && response.request && response.request.uri && response.request.uri.href == 'https://www.gamepedia.com/' ) {
  703. console.log( '- Dieses Wiki existiert nicht! ' + ( error ? error.message : ( body ? ( body.error ? body.error.info : '' ) : '' ) ) );
  704. msg.reactEmoji('nowiki');
  705. }
  706. else {
  707. console.log( '- Fehler beim Erhalten der Suchergebnisse' + ( error ? ': ' + error : ( body ? ( body.error ? ': ' + body.error.info : '.' ) : '.' ) ) );
  708. msg.channel.send( '<https://' + wiki + '.gamepedia.com/' + title + '?diff=' + relative + ( title ? '' : '&oldid=' + revision ) + '>' ).then( message => message.reactEmoji('error') );
  709. }
  710. }
  711. else {
  712. if ( noerror ) msg.reply( lang.diff.badrev );
  713. else if ( body.compare.fromarchive != undefined || body.compare.toarchive != undefined ) msg.reactEmoji('error');
  714. else {
  715. var argids = [];
  716. var ids = body.compare;
  717. if ( ids.fromrevid && !ids.torevid ) argids = [ids.fromrevid];
  718. else if ( !ids.fromrevid && ids.torevid ) argids = [ids.torevid];
  719. else if ( ids.fromrevid > ids.torevid ) argids = [ids.fromrevid, ids.torevid];
  720. else if ( ids.fromrevid == ids.torevid ) argids = [ids.fromrevid];
  721. else argids = [ids.torevid, ids.fromrevid];
  722. cmd_diffsend(lang, msg, argids, wiki);
  723. }
  724. }
  725. reaction.removeEmoji();
  726. } );
  727. } );
  728. }
  729. }
  730. else msg.reactEmoji('error');
  731. }
  732. function cmd_diffsend(lang, msg, args, wiki) {
  733. request( {
  734. uri: 'https://' + wiki + '.gamepedia.com/api.php?action=query&format=json&list=tags&tglimit=500&tgprop=displayname&prop=revisions&rvprop=ids|timestamp|flags|user|size|comment|tags&revids=' + args.join('|'),
  735. json: true
  736. }, function( error, response, body ) {
  737. if ( error || !response || !body || !body.query ) {
  738. if ( response && response.request && response.request.uri && response.request.uri.href == 'https://www.gamepedia.com/' ) {
  739. console.log( '- Dieses Wiki existiert nicht! ' + ( error ? error.message : ( body ? ( body.error ? body.error.info : '' ) : '' ) ) );
  740. msg.reactEmoji('nowiki');
  741. }
  742. else {
  743. console.log( '- Fehler beim Erhalten der Suchergebnisse' + ( error ? ': ' + error : ( body ? ( body.error ? ': ' + body.error.info : '.' ) : '.' ) ) );
  744. msg.channel.send( '<https://' + wiki + '.gamepedia.com/?diff=' + args[0] + ( args[1] ? '&oldid=' + args[1] : '' ) + '>' ).then( message => message.reactEmoji('error') );
  745. }
  746. }
  747. else {
  748. if ( body.query.badrevids ) msg.reply( lang.diff.badrev );
  749. else if ( body.query.pages && !body.query.pages['-1'] ) {
  750. var pages = Object.values(body.query.pages);
  751. if ( pages.length != 1 ) msg.channel.send( '<https://' + wiki + '.gamepedia.com/?diff=' + args[0] + ( args[1] ? '&oldid=' + args[1] : '' ) + '>' );
  752. else {
  753. var title = pages[0].title.toTitle();
  754. var revisions = [];
  755. if ( pages[0].revisions[1] ) revisions = [pages[0].revisions[1], pages[0].revisions[0]];
  756. else revisions = [pages[0].revisions[0]];
  757. var diff = revisions[0].revid;
  758. var oldid = ( revisions[1] ? revisions[1].revid : 0 );
  759. var editor = ( revisions[0].userhidden != undefined ? lang.diff.hidden : revisions[0].user );
  760. var timeoptions = {
  761. year: 'numeric',
  762. month: 'short',
  763. day: 'numeric',
  764. hour: '2-digit',
  765. minute: '2-digit',
  766. timeZone: 'UTC',
  767. timeZoneName: 'short'
  768. }
  769. var timestamp = (new Date(revisions[0].timestamp)).toLocaleString(lang.user.dateformat, timeoptions);
  770. var size = revisions[0].size - ( revisions[1] ? revisions[1].size : 0 );
  771. var comment = ( revisions[0].commenthidden != undefined ? lang.diff.hidden : revisions[0].comment );
  772. if ( !comment ) comment = lang.diff.nocomment;
  773. var tags = [lang.diff.notags];
  774. var entry = body.query.tags;
  775. revisions[0].tags.forEach( function(tag, t) {
  776. for ( var i = 0; i < entry.length; i++ ) {
  777. if ( entry[i].name == tag ) {
  778. tags[t] = entry[i].displayname;
  779. break;
  780. }
  781. }
  782. } );
  783. msg.channel.send( '<https://' + wiki + '.gamepedia.com/' + title + '?diff=' + diff + '&oldid=' + oldid + '>\n\n' + lang.diff.info.replace( '%1$s', editor ).replace( '%2$s', timestamp ).replace( '%3$s', size ).replace( '%4$s', comment.wikicode() ).replace( '%5$s', tags.join(', ').replace( /<[^>]+>(.+)<\/[^>]+>/g, '$1' ) ) );
  784. }
  785. }
  786. else msg.reactEmoji('error');
  787. }
  788. } );
  789. }
  790. function cmd_random(lang, msg, wiki) {
  791. msg.reactEmoji('⏳').then( function( reaction ) {
  792. request( {
  793. uri: 'https://' + wiki + '.gamepedia.com/api.php?action=query&format=json&list=random&rnnamespace=0',
  794. json: true
  795. }, function( error, response, body ) {
  796. if ( error || !response || !body || !body.query || !body.query.random[0] ) {
  797. if ( response && response.request && response.request.uri && response.request.uri.href == 'https://www.gamepedia.com/' ) {
  798. console.log( '- Dieses Wiki existiert nicht! ' + ( error ? error.message : ( body ? ( body.error ? body.error.info : '' ) : '' ) ) );
  799. msg.reactEmoji('nowiki');
  800. }
  801. else {
  802. console.log( '- Fehler beim Erhalten der Suchergebnisse' + ( error ? ': ' + error : ( body ? ( body.error ? ': ' + body.error.info : '.' ) : '.' ) ) );
  803. msg.channel.send( '<https://' + wiki + '.gamepedia.com/Special:Random>' ).then( message => message.reactEmoji('error') );
  804. }
  805. }
  806. else {
  807. msg.channel.send( '🎲 https://' + wiki + '.gamepedia.com/' + body.query.random[0].title.toTitle() );
  808. }
  809. reaction.removeEmoji();
  810. } );
  811. } );
  812. }
  813. function cmd_bug(lang, mclang, msg, args, title, cmd, querystring, fragment) {
  814. if ( args.length && /\d+$/.test(args[0]) && !args[1] ) {
  815. msg.reactEmoji('⏳').then( function( reaction ) {
  816. var project = '';
  817. if ( /^\d+$/.test(args[0]) ) project = 'MC-';
  818. request( {
  819. uri: 'https://bugs.mojang.com/rest/api/2/issue/' + project + args[0] + '?fields=summary',
  820. json: true
  821. }, function( error, response, body ) {
  822. if ( error || !response || !body || body['status-code'] == 404 ) {
  823. console.log( '- Fehler beim Erhalten der Zusammenfassung' + ( error ? ': ' + error : ( body ? ': ' + body.message : '.' ) ) );
  824. if ( body && body['status-code'] == 404 ) msg.reactEmoji('error');
  825. else msg.channel.send( 'https://bugs.mojang.com/browse/' + project + args[0] ).then( message => message.reactEmoji('error') );
  826. }
  827. else {
  828. if ( body.errorMessages || body.errors ) {
  829. if ( body.errorMessages && body.errorMessages[0] == 'Issue Does Not Exist' ) {
  830. msg.reactEmoji('❓');
  831. }
  832. else {
  833. msg.channel.send( mclang.bug.private + '\nhttps://bugs.mojang.com/browse/' + project + args[0] );
  834. }
  835. }
  836. else if ( !body.fields ) {
  837. msg.reactEmoji('error');
  838. }
  839. else {
  840. msg.channel.send( body.fields.summary + '\nhttps://bugs.mojang.com/browse/' + body.key );
  841. }
  842. }
  843. reaction.removeEmoji();
  844. } );
  845. } );
  846. }
  847. else {
  848. msg.notminecraft = true;
  849. cmd_link(lang, msg, title, mclang.link, cmd, querystring, fragment);
  850. }
  851. }
  852. function cmd_befehl(lang, mclang, msg, befehl, args, title, cmd, querystring, fragment) {
  853. var aliasCmd = ( ( befehl in mclang.cmd.aliase ) ? mclang.cmd.aliase[befehl] : befehl ).toLowerCase();
  854. if ( aliasCmd in mclang.cmd.list ) {
  855. var regex = new RegExp('/' + aliasCmd, 'g');
  856. var cmdSyntax = mclang.cmd.list[aliasCmd].join( '\n' ).replace( regex, '/' + befehl );
  857. msg.channel.send( '```md\n' + cmdSyntax + '```<https://' + mclang.link + '.gamepedia.com/' + mclang.cmd.page + aliasCmd + '>', {split:{maxLength:2000,prepend:'```md\n',append:'```'}} );
  858. }
  859. else {
  860. msg.reactEmoji('❓');
  861. msg.notminecraft = true;
  862. cmd_link(lang, msg, title, mclang.link, cmd, querystring, fragment);
  863. }
  864. }
  865. function cmd_befehl2(lang, mclang, msg, args, title, cmd, querystring, fragment) {
  866. if ( args.length ) {
  867. if ( args[0].startsWith('/') ) cmd_befehl(lang, mclang, msg, args[0].substr(1), args.slice(1), title, cmd);
  868. else cmd_befehl(lang, mclang, msg, args[0], args.slice(1), title, cmd);
  869. }
  870. else {
  871. msg.notminecraft = true;
  872. cmd_link(lang, msg, title, mclang.link, cmd, querystring, fragment);
  873. }
  874. }
  875. function cmd_multiline(lang, msg, args, line) {
  876. msg.reactEmoji('error');
  877. }
  878. function cmd_voice(lang, msg, args, line) {
  879. if ( admin(msg) && !args.length ) msg.reply( lang.voice.text + '\n`' + lang.voice.channel + ' – <' + lang.voice.name + '>`' );
  880. else cmd_link(lang, msg, line.split(' ').slice(1).join(' '));
  881. }
  882. function cmd_get(lang, msg, args, line) {
  883. var id = args.join().replace( /^\\?<(?:@!?|#)(\d+)>$/, '$1' );
  884. if ( msg.author.id == process.env.owner && /^\d+$/.test(id) ) {
  885. if ( client.guilds.has(id) ) {
  886. var guild = client.guilds.get(id);
  887. var permissions = ( guild.me.permissions.has(defaultPermissions) ? '*none*' : '`' + guild.me.permissions.missing(defaultPermissions).join('`, `') + '`' );
  888. var guildsettings = ( guild.id in settings ? '```json\n' + JSON.stringify( settings[guild.id], null, '\t' ) + '\n```' : '*default*' );
  889. msg.channel.send( 'Guild: ' + guild.name + ' `' + guild.id + '`\nOwner: ' + guild.owner.user.tag + ' `' + guild.ownerID + '` ' + guild.owner.toString() + '\nMissing permissions: ' + permissions + '\nSettings: ' + guildsettings, {split:true} );
  890. } else if ( client.guilds.some( guild => guild.members.has(id) ) ) {
  891. var text = '';
  892. client.guilds.filter( guild => guild.members.has(id) ).forEach( function(guild) {
  893. var member = guild.members.get(id);
  894. if ( !text ) text = 'User: ' + member.user.tag + ' `' + member.id + '` ' + member.toString() + '\nGuilds:';
  895. text += '\n' + guild.name + ' `' + guild.id + '`' + ( member.permissions.has('MANAGE_GUILD') ? '\*' : '' );
  896. } );
  897. msg.channel.send( text, {split:true} );
  898. } else if ( client.guilds.some( guild => guild.channels.filter( chat => chat.type == 'text' ).has(id) ) ) {
  899. var channel = client.guilds.find( guild => guild.channels.filter( chat => chat.type == 'text' ).has(id) ).channels.get(id);
  900. var permissions = ( channel.memberPermissions(channel.guild.me).has(defaultPermissions) ? '*none*' : '`' + channel.memberPermissions(channel.guild.me).missing(defaultPermissions).join('`, `') + '`' );
  901. var wiki = ( channel.guild.id in settings ? ( settings[channel.guild.id].channels && channel.id in settings[channel.guild.id].channels ? settings[channel.guild.id].channels[channel.id] : settings[channel.guild.id].wiki ) : settings['default'].wiki );
  902. msg.channel.send( 'Guild: ' + channel.guild.name + ' `' + channel.guild.id + '`\nChannel: #' + channel.name + ' `' + channel.id + '` ' + channel.toString() + '\nMissing permissions: ' + permissions + '\nDefault Wiki: `' + wiki + '`', {split:true} );
  903. } else msg.reply( 'I couldn\'t find a result for `' + id + '`' );
  904. } else if ( msg.channel.type != 'text' || !pause[msg.guild.id] ) cmd_link(lang, msg, line.split(' ').slice(1).join(' '));
  905. }
  906. function mention(msg, arg) {
  907. arg = arg.trim();
  908. if ( arg == '@' + client.user.username || ( msg.channel.type == 'text' && arg == '@' + msg.guild.me.displayName ) ) return true;
  909. else return false;
  910. }
  911. function admin(msg) {
  912. if ( msg.channel.type == 'text' && ( ( msg.member && msg.member.permissions.has('MANAGE_GUILD') ) || msg.author.id == process.env.owner ) ) return true;
  913. else return false;
  914. }
  915. function emoji(args) {
  916. var text = args.join(' ');
  917. var regex = /(<a?:)(\d+)(>)/g;
  918. if ( regex.test(text) ) {
  919. regex.lastIndex = 0;
  920. var emojis = client.emojis;
  921. var entry;
  922. while ( ( entry = regex.exec(text) ) !== null ) {
  923. if ( emojis.has(entry[2]) ) {
  924. text = text.replace(entry[0], emojis.get(entry[2]).toString());
  925. } else {
  926. text = text.replace(entry[0], entry[1] + 'unknown_emoji:' + entry[2] + entry[3]);
  927. }
  928. }
  929. args = text.split(' ');
  930. }
  931. return args;
  932. }
  933. String.prototype.toTitle = function() {
  934. return this.replace( / /g, '_' ).replace( /\%/g, '%25' ).replace( /\?/g, '%3F' );
  935. };
  936. String.prototype.toSection = function() {
  937. return encodeURIComponent( this.replace( / /g, '_' ) ).replace( /\'/g, '%27' ).replace( /\(/g, '%28' ).replace( /\)/g, '%29' ).replace( /\%/g, '.' );
  938. };
  939. String.prototype.wikicode = function() {
  940. return this.replace( /\[\[(?:[^\|\]]+\|)?([^\]]+)\]\]/g, '$1' ).replace( /\/\*\s*([^\*]+?)\s*\*\//g, '→$1:' );
  941. };
  942. Object.prototype.reactEmoji = function(name) {
  943. var emoji = '440871715938238494';
  944. switch ( name ) {
  945. case 'nowiki':
  946. emoji = '505884572001763348';
  947. break;
  948. case 'error':
  949. emoji = '440871715938238494';
  950. break;
  951. case 'support':
  952. emoji = '448222377009086465';
  953. break;
  954. case 'oppose':
  955. emoji = '448222455425794059';
  956. break;
  957. default:
  958. emoji = name;
  959. }
  960. return this.react(emoji).catch( error => console.log( '- ' + error ) );
  961. };
  962. Object.prototype.removeEmoji = function() {
  963. return this.remove().catch( error => console.log( '- ' + error ) );
  964. };
  965. function prefix(text) {
  966. if ( text.toLowerCase().startsWith( process.env.prefix + ' ' ) || text.toLowerCase() == process.env.prefix ) return true;
  967. else return false;
  968. }
  969. client.on('message', msg => {
  970. var cont = msg.content;
  971. var author = msg.author;
  972. var channel = msg.channel;
  973. if ( channel.type == 'text' ) var permissions = channel.permissionsFor(client.user);
  974. if ( cont.toLowerCase().includes( process.env.prefix ) && !msg.webhookID && author.id != client.user.id ) {
  975. if ( settings == defaultSettings ) getSettings(setStatus);
  976. if ( allSites == defaultSites ) getAllSites();
  977. var setting = Object.assign({}, settings['default']);
  978. if ( channel.type == 'text' && msg.guild.id in settings ) setting = Object.assign({}, settings[msg.guild.id]);
  979. var lang = i18n[setting.lang];
  980. lang.link = setting.wiki;
  981. if ( setting.channels && channel.id in setting.channels ) lang.link = setting.channels[channel.id];
  982. if ( ( channel.type != 'text' || permissions.has(['SEND_MESSAGES','ADD_REACTIONS','USE_EXTERNAL_EMOJIS']) ) ) {
  983. var invoke = cont.split(' ')[1] ? cont.split(' ')[1].toLowerCase() : '';
  984. var aliasInvoke = ( invoke in lang.aliase ) ? lang.aliase[invoke] : invoke;
  985. if ( prefix( cont ) && aliasInvoke in multilinecmdmap ) {
  986. if ( channel.type != 'text' || permissions.has('MANAGE_MESSAGES') ) {
  987. var args = cont.split(' ').slice(2);
  988. console.log( ( msg.guild ? msg.guild.name : '@' + author.username ) + ': ' + cont );
  989. if ( channel.type != 'text' || !pause[msg.guild.id] || ( author.id == process.env.owner && aliasInvoke in pausecmdmap ) ) multilinecmdmap[aliasInvoke](lang, msg, args, cont);
  990. } else if ( admin(msg) ) {
  991. console.log( msg.guild.name + ': Fehlende Berechtigungen - MANAGE_MESSAGES' );
  992. msg.reply( lang.missingperm + ' `MANAGE_MESSAGES`' );
  993. }
  994. } else {
  995. var count = 0;
  996. msg.cleanContent.replace(/\u200b/g, '').split('\n').forEach( function(line) {
  997. if ( prefix( line ) && count < 10 ) {
  998. count++;
  999. invoke = line.split(' ')[1] ? line.split(' ')[1].toLowerCase() : '';
  1000. var args = line.split(' ').slice(2);
  1001. aliasInvoke = ( invoke in lang.aliase ) ? lang.aliase[invoke] : invoke;
  1002. console.log( ( msg.guild ? msg.guild.name : '@' + author.username ) + ': ' + line );
  1003. if ( channel.type != 'text' || !pause[msg.guild.id] ) {
  1004. if ( aliasInvoke in cmdmap ) cmdmap[aliasInvoke](lang, msg, args, line);
  1005. else if ( /^![a-z\d-]{1,30}$/.test(invoke) ) cmd_link(lang, msg, args.join(' '), invoke.substr(1), ' ' + invoke + ' ');
  1006. else cmd_link(lang, msg, line.split(' ').slice(1).join(' '));
  1007. } else if ( channel.type == 'text' && pause[msg.guild.id] && author.id == process.env.owner && aliasInvoke in pausecmdmap ) {
  1008. pausecmdmap[aliasInvoke](lang, msg, args, line);
  1009. }
  1010. } else if ( prefix( line ) && count == 10 ) {
  1011. count++;
  1012. console.log( '- Nachricht enthält zu viele Befehle!' );
  1013. msg.reactEmoji('⚠');
  1014. channel.send( lang.limit.replace( '%s', author.toString() ) ).then( message => message.reactEmoji('⚠') );
  1015. }
  1016. } );
  1017. }
  1018. } else if ( admin(msg) ) {
  1019. console.log( msg.guild.name + ': Fehlende Berechtigungen - ' + permissions.missing(['SEND_MESSAGES','ADD_REACTIONS','USE_EXTERNAL_EMOJIS']) );
  1020. if ( permissions.has(['SEND_MESSAGES']) ) msg.reply( lang.missingperm + ' `' + permissions.missing(['ADD_REACTIONS','USE_EXTERNAL_EMOJIS']).join('`, `') + '`' );
  1021. }
  1022. }
  1023. });
  1024. client.on('voiceStateUpdate', (oldm, newm) => {
  1025. if ( settings == defaultSettings ) getSettings(setStatus);
  1026. if ( allSites == defaultSites ) getAllSites();
  1027. if ( oldm.guild.me.permissions.has('MANAGE_ROLES') && oldm.voiceChannelID != newm.voiceChannelID ) {
  1028. var setting = Object.assign({}, settings['default']);
  1029. if ( oldm.guild.id in settings ) setting = Object.assign({}, settings[oldm.guild.id]);
  1030. var lang = i18n[setting.lang];
  1031. if ( oldm.voiceChannel ) {
  1032. var oldrole = oldm.roles.find( role => role.name == lang.voice.channel + ' – ' + oldm.voiceChannel.name );
  1033. if ( oldrole && oldrole.comparePositionTo(oldm.guild.me.highestRole) < 0 ) {
  1034. oldm.removeRole( oldrole, lang.voice.left.replace( '%1$s', oldm.displayName ).replace( '%2$s', oldm.voiceChannel.name ) );
  1035. console.log( oldm.guild.name + ': ' + oldm.displayName + ' hat den Sprachkanal "' + oldm.voiceChannel.name + '" verlassen.' );
  1036. }
  1037. }
  1038. if ( newm.voiceChannel ) {
  1039. var newrole = newm.guild.roles.find( role => role.name == lang.voice.channel + ' – ' + newm.voiceChannel.name );
  1040. if ( newrole && newrole.comparePositionTo(newm.guild.me.highestRole) < 0 ) {
  1041. newm.addRole( newrole, lang.voice.join.replace( '%1$s', newm.displayName ).replace( '%2$s', newm.voiceChannel.name ) );
  1042. console.log( newm.guild.name + ': ' + newm.displayName + ' hat den Sprachkanal "' + newm.voiceChannel.name + '" betreten.' );
  1043. }
  1044. }
  1045. }
  1046. });
  1047. client.on('guildCreate', guild => {
  1048. 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.id + ')' ) );
  1049. console.log( '- Ich wurde zu einem Server hinzugefügt.' );
  1050. });
  1051. client.on('guildDelete', guild => {
  1052. 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.id + ')' ) );
  1053. console.log( '- Ich wurde von einem Server entfernt.' );
  1054. if ( !guild.available ) {
  1055. console.log( '- Dieser Server ist nicht erreichbar.' );
  1056. }
  1057. else if ( settings == defaultSettings ) {
  1058. console.log( '- Fehler beim Erhalten bestehender Einstellungen.' );
  1059. }
  1060. else {
  1061. var temp_settings = Object.assign({}, settings);
  1062. Object.keys(temp_settings).forEach( function(guild) {
  1063. if ( !client.guilds.has(guild) && guild != 'default' ) delete temp_settings[guild];
  1064. } );
  1065. request.post( {
  1066. uri: process.env.save + process.env.access,
  1067. body: {
  1068. branch: 'master',
  1069. commit_message: 'Wiki-Bot: Einstellungen entfernt.',
  1070. actions: [
  1071. {
  1072. action: 'update',
  1073. file_path: process.env.file,
  1074. content: JSON.stringify( temp_settings, null, '\t' )
  1075. }
  1076. ]
  1077. },
  1078. json: true
  1079. }, function( error, response, body ) {
  1080. if ( error || !response || response.statusCode != 201 || !body || body.error ) {
  1081. console.log( '- Fehler beim Bearbeiten' + ( error ? ': ' + error : ( body ? ( body.message ? ': ' + body.message : ( body.error ? ': ' + body.error : '.' ) ) : '.' ) ) );
  1082. }
  1083. else {
  1084. settings = Object.assign({}, temp_settings);
  1085. console.log( '- Einstellungen erfolgreich aktualisiert.' );
  1086. }
  1087. } );
  1088. }
  1089. });
  1090. client.login(process.env.token);
  1091. client.on('error', error => {
  1092. console.log( '--- ERROR: ' + new Date(Date.now()).toLocaleTimeString('de-DE', { timeZone: 'Europe/Berlin' }) + ' ---\n- ' + error.name + ': ' + error.message );
  1093. });