main.rb 7.7 KB

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