main.rb 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. require 'yaml'
  2. require 'discordrb'
  3. require 'icunicode'
  4. require 'open-uri'
  5. require 'fileutils'
  6. require 'securerandom'
  7. require 'net/http'
  8. require 'json'
  9. require 'youtube-dl.rb'
  10. settings = YAML.load(File.read "config.yaml")['settings']
  11. bot = Discordrb::Commands::CommandBot.new token: settings['token'], prefix: settings['prefix']
  12. bot.command(:translit) do |_event, script, *text|
  13. _event.message.mentions.each do |user|
  14. member = user.on(_event.channel.server)
  15. original_name = member.nick
  16. original_name ||= member.username
  17. transliterated = original_name.transliterate(script).force_encoding("UTF-8")
  18. member.nick = transliterated
  19. _event.send_message("Congratulations on your new nickname, #{member.mention}")
  20. end
  21. _event.send_message("Translitterated: #{text.join(' ').transliterate(script).force_encoding("UTF-8")}")
  22. "Finished translitting"
  23. end
  24. bot.command(:reset) do |_event|
  25. _event.message.mentions.each do |user|
  26. user.on(_event.channel.server).nickname = nil
  27. end
  28. "Finished resetting"
  29. end
  30. bot.command(:loli, description: "Calls for the lolice.") do |_event|
  31. "https://tankernn.eu/~frans/files/loli_police_#{rand(1..3)}.png"
  32. end
  33. bot.command(:reap, description: "Reaps images posted by the caller in the current channel.") do |_event, stop_id|
  34. next "No stop_id supplied" if stop_id == nil
  35. FileUtils::mkdir_p("/tmp/tankbot_images/")
  36. stop = false
  37. earliest_message_id = nil
  38. control_message = _event.send_message("Reaping images sent by #{_event.author.mention}. React to this message to stop.")
  39. until stop
  40. messages = _event.channel.history(100, earliest_message_id)
  41. messages.select{ |message| message.author === _event.author }.each do |message|
  42. puts "#{message.id}, #{stop_id}"
  43. if control_message.reactions? or message.id == stop_id
  44. stop = true
  45. break
  46. end
  47. message.attachments.select{ |attachment| attachment.image? }.each do |attachment|
  48. control_message.edit(control_message.content + "\nDownloading **#{attachment.filename}**... (#{message.timestamp.strftime("%F")})")
  49. open("/tmp/tankbot_images/#{attachment.id}-#{attachment.filename}", 'wb') do |file|
  50. file << open(attachment.url).read
  51. end
  52. end
  53. end
  54. stop = messages.length < 100
  55. earliest_message_id = messages.last.id
  56. end
  57. filename = "#{SecureRandom.uuid}.tar.gz"
  58. `tar -czf /var/www/scr/#{filename} /tmp/tankbot_images`
  59. FileUtils::rm_rf("/tmp/tankbot_images")
  60. control_message.edit("https://scr.tankernn.eu/#{filename}")
  61. end
  62. bot.command(:neko, description: "Requests (sometimes lewd) nekos.") do |_event, keyword|
  63. url = "https://nekos.life/api/v2/img/"
  64. options = ["cum", "les", "meow", "tickle", "lewd", "feed", "bj",
  65. "nsfw_neko_gif", "poke", "anal", "slap", "avatar", "pussy",
  66. "lizard", "classic", "kuni", "pat", "kiss", "neko", "cuddle",
  67. "fox_girl", "boobs", "random_hentai_gif", "hug"]
  68. if options.include? keyword then
  69. response = JSON.parse(Net::HTTP.get(URI("#{url}#{keyword}")))
  70. "Here's your lewds! °˖✧◝(⁰▿⁰)◜✧˖°\n#{response['url']}"
  71. else
  72. "No such tag. Please specify one of `#{options.join(", ")}`"
  73. end
  74. end
  75. bot.command(:lmgtfy, description: "Helps tech-illiterate people to enlightenment.") do |_event, *args|
  76. "http://lmgtfy.com/?s=d&q=#{args.join('+')}"
  77. end
  78. bot.command(:copypasta, description: "Cites the holy texts.") do |_event, keyword|
  79. pastafile = "copypastas.json"
  80. file = File.read pastafile
  81. pastas = JSON.parse file
  82. if pastas.include? keyword then
  83. pastas[keyword]
  84. else
  85. "No such pasta. Available pastas include `#{pastas.keys.join(", ")}`"
  86. end
  87. end
  88. class UserQueue
  89. def initialize(user)
  90. @user = user
  91. @songs = []
  92. end
  93. attr_accessor :user
  94. attr_accessor :songs
  95. end
  96. class FairQueue
  97. def initialize(voice_bot)
  98. @voice_bot = voice_bot
  99. @queues = []
  100. @now_playing = nil
  101. end
  102. def append(user, video)
  103. @queues.append UserQueue.new user unless @queues.any? { |queue| queue.user == user }
  104. @queues.select{ |queue| queue.user == user }.first.songs.append(video)
  105. end
  106. def queue
  107. queues = @queues.map{ |queue| queue.songs }
  108. target_length = queues.map{ |queue| queue.length }.max
  109. queues.map{ |queue| queue + (target_length - queue.length).times.collect{nil} }.transpose.flatten.compact
  110. end
  111. def play
  112. until @queues.empty?
  113. queue = @queues.shift
  114. @now_playing = queue.songs.shift
  115. # Rotate user to last place
  116. @queues.append queue unless queue.songs.empty?
  117. # Play song
  118. # song_log("Playing *#{video.title}*...")
  119. @voice_bot.play_file(@now_playing.filename)
  120. end
  121. @voice_bot.destroy
  122. end
  123. attr_accessor :now_playing
  124. end
  125. def format_title(video)
  126. total_seconds = video.information[:duration]
  127. seconds = total_seconds % 60
  128. minutes = (total_seconds / 60) % 60
  129. hours = total_seconds / (60 * 60)
  130. timestamp = format("%02d:%02d", minutes, seconds)
  131. timestamp = format("%02d:%s", hours, timestamp) if hours > 0
  132. "**#{video.information[:fulltitle]}** `[#{timestamp}]`"
  133. end
  134. fairqueues = Hash.new
  135. youtube_dl_options = {
  136. default_search: 'ytsearch',
  137. format: 'bestaudio',
  138. output: 'cache/%(title)s-%(id)s.%(ext)s'
  139. }
  140. bot.command(:play, description: "Plays 'music' of your choosing in your voice channel.") do |_event, *query|
  141. voice_bot = _event.voice
  142. unless voice_bot then
  143. channel = _event.user.voice_channel
  144. next "You're not in any voice channel!" unless channel
  145. voice_bot = bot.voice_connect(channel)
  146. fairqueues[_event.server] = FairQueue.new(voice_bot)
  147. end
  148. video = YoutubeDL.download query.join(' '), youtube_dl_options
  149. fairqueue = fairqueues[_event.server]
  150. fairqueue.append(_event.user, video)
  151. if voice_bot.playing? then
  152. "Added #{format_title(video)} to the queue."
  153. else
  154. Thread.new{fairqueue.play}
  155. "Started playing #{format_title(video)}"
  156. end
  157. end
  158. bot.command(:skip, description: "Expresses your dislike of the currently playing 'music'.") do |_event|
  159. _event.voice.stop_playing
  160. end
  161. bot.command(:np, description: "Shows what 'music' is currently playing.") do |_event|
  162. queue = fairqueues[_event.server]
  163. next "Nothing is playing." unless _event.voice
  164. format_title(queue.now_playing)
  165. end
  166. bot.command(:stop, description: "Puts an end to your misery.") do |_event|
  167. if _event.voice
  168. _event.voice.destroy
  169. "Stopped playing."
  170. else
  171. "Nothing is playing."
  172. end
  173. end
  174. bot.command(:queue, description: "Lists the impending torture.") do |_event|
  175. queue = fairqueues[_event.server]
  176. next "Nothing is playing." unless queue
  177. next "The queue is empty." if queue.queue.empty?
  178. queue.queue.each_with_index.map{|video, i| "#{i + 1}. #{format_title(video)}"}.join("\n")
  179. end
  180. bot.run