main.js 46 KB

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