Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. const Command = require('../../structures/Command');
  2. const { MessageEmbed } = require('discord.js');
  3. const request = require('node-superfetch');
  4. const { stripIndents } = require('common-tags');
  5. const { verify } = require('../../util/Util');
  6.  
  7. module.exports = class AkinatorCommand extends Command {
  8. constructor(client) {
  9. super(client, {
  10. name: 'akinator',
  11. aliases: ['the-web-genie', 'web-genie'],
  12. group: 'games',
  13. memberName: 'akinator',
  14. description: 'Think about a real or fictional character, I will try to guess who it is.',
  15. clientPermissions: ['EMBED_LINKS']
  16. });
  17.  
  18. this.sessions = new Map();
  19. }
  20.  
  21. async run(msg) {
  22. if (this.sessions.has(msg.channel.id)) return msg.reply('Only one game may be occuring per channel.');
  23. try {
  24. let ans = null;
  25. this.sessions.set(msg.channel.id, { progression: 0 });
  26. while (this.sessions.get(msg.channel.id).progression < 95) {
  27. const data = ans === null ? await this.createSession(msg.channel) : await this.progress(msg.channel, ans);
  28. if (!data || !data.answers || this.sessions.get(msg.channel.id).step >= 80) break;
  29. const answers = data.answers.map(answer => answer.answer.toLowerCase());
  30. answers.push('end');
  31. await msg.say(stripIndents`
  32. **${++data.step}.** ${data.question} (${Math.round(Number.parseInt(data.progression, 10))}%)
  33. ${data.answers.map(answer => answer.answer).join(' | ')}
  34. `);
  35. const filter = res => res.author.id === msg.author.id && answers.includes(res.content.toLowerCase());
  36. const msgs = await msg.channel.awaitMessages(filter, {
  37. max: 1,
  38. time: 30000
  39. });
  40. if (!msgs.size) {
  41. await msg.say('Sorry, time is up!');
  42. break;
  43. }
  44. if (msgs.first().content.toLowerCase() === 'end') break;
  45. ans = answers.indexOf(msgs.first().content.toLowerCase());
  46. }
  47. const guess = await this.guess(msg.channel);
  48. if (!guess) return msg.reply('Hmm... I seem to be having a bit of trouble. Check back soon!');
  49. const embed = new MessageEmbed()
  50. .setColor(0xF78B26)
  51. .setTitle(`I'm ${Math.round(guess.proba * 100)}% sure it's...`)
  52. .setDescription(`${guess.name}${guess.description ? `\n_${guess.description}_` : ''}`)
  53. .setThumbnail(guess.absolute_picture_path);
  54. await msg.embed(embed);
  55. const verification = await verify(msg.channel, msg.author);
  56. this.sessions.delete(msg.channel.id);
  57. if (verification === 0) return msg.say('I guess your silence means I have won.');
  58. if (!verification) return msg.say('Bravo, you have defeated me.');
  59. return msg.say('Guessed right one more time! I love playing with you!');
  60. } catch (err) {
  61. this.sessions.delete(msg.channel.id);
  62. return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
  63. }
  64. }
  65.  
  66. async createSession(channel) {
  67. const { body } = await request
  68. .get('https://srv2.akinator.com:9157/ws/new_session')
  69. .query({
  70. partner: 1,
  71. player: 'website-desktop',
  72. constraint: 'ETAT<>\'AV\'',
  73. soft_constraint: channel.nsfw ? '' : 'ETAT=\'EN\'',
  74. question_filter: channel.nsfw ? '' : 'cat=1',
  75. _: Date.now()
  76. });
  77. const data = body.parameters;
  78. if (!data) return null;
  79. this.sessions.set(channel.id, {
  80. id: data.identification.session,
  81. signature: data.identification.signature,
  82. step: 0,
  83. progression: Number.parseInt(data.step_information.progression, 10)
  84. });
  85. return data.step_information;
  86. }
  87.  
  88. async progress(channel, answer) {
  89. const session = this.sessions.get(channel.id);
  90. const { body } = await request
  91. .get('https://srv2.akinator.com:9157/ws/answer')
  92. .query({
  93. session: session.id,
  94. signature: session.signature,
  95. step: session.step,
  96. answer,
  97. question_filter: channel.nsfw ? '' : 'cat=1',
  98. _: Date.now()
  99. });
  100. const data = body.parameters;
  101. if (!data) return null;
  102. this.sessions.set(channel.id, {
  103. id: session.id,
  104. signature: session.signature,
  105. step: Number.parseInt(data.step, 10),
  106. progression: Number.parseInt(data.progression, 10)
  107. });
  108. return data;
  109. }
  110.  
  111. async guess(channel) {
  112. const session = this.sessions.get(channel.id);
  113. const { body } = await request
  114. .get('https://srv2.akinator.com:9157/ws/list')
  115. .query({
  116. session: session.id,
  117. signature: session.signature,
  118. step: session.step,
  119. size: 2,
  120. max_pic_width: 246,
  121. max_pic_height: 294,
  122. pref_photos: 'VO-OK',
  123. duel_allowed: 1,
  124. mode_question: 0,
  125. _: Date.now()
  126. });
  127. if (!body.parameters) return null;
  128. return body.parameters.elements[0].element;
  129. }
  130. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement