main.rb 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. require 'sqlite3'
  11. db = SQLite3::Database.open 'tankbot.db'
  12. $settings = YAML.load(File.read "config.yaml")['settings']
  13. bot = Discordrb::Commands::CommandBot.new token: $settings['token'], prefix: $settings['prefix']
  14. def save_settings
  15. File.open("config.yaml", 'w') do |file|
  16. file.write(YAML.dump({'settings' => $settings}))
  17. end
  18. end
  19. bot.command(:translit) do |_event, script, *text|
  20. _event.message.mentions.each do |user|
  21. member = user.on(_event.channel.server)
  22. original_name = member.nick
  23. original_name ||= member.username
  24. transliterated = original_name.transliterate(script).force_encoding("UTF-8")
  25. member.nick = transliterated
  26. _event.send_message("Congratulations on your new nickname, #{member.mention}")
  27. end
  28. _event.send_message("Translitterated: #{text.join(' ').transliterate(script).force_encoding("UTF-8")}")
  29. "Finished translitting"
  30. end
  31. bot.command(:reset) do |_event|
  32. _event.message.mentions.each do |user|
  33. user.on(_event.channel.server).nickname = nil
  34. end
  35. "Finished resetting"
  36. end
  37. bot.command(:reap, description: "Reaps images posted by the caller in the current channel.") do |_event, stop_id|
  38. next "No stop_id supplied" if stop_id == nil
  39. FileUtils::mkdir_p("/tmp/tankbot_images/")
  40. stop = false
  41. earliest_message_id = nil
  42. control_message = _event.send_message("Reaping images sent by #{_event.author.mention}. React to this message to stop.")
  43. until stop
  44. messages = _event.channel.history(100, earliest_message_id)
  45. messages.select{ |message| message.author === _event.author }.each do |message|
  46. puts "#{message.id}, #{stop_id}"
  47. if control_message.reactions? or message.id == stop_id
  48. stop = true
  49. break
  50. end
  51. message.attachments.select{ |attachment| attachment.image? }.each do |attachment|
  52. control_message.edit(control_message.content + "\nDownloading **#{attachment.filename}**... (#{message.timestamp.strftime("%F")})")
  53. open("/tmp/tankbot_images/#{attachment.id}-#{attachment.filename}", 'wb') do |file|
  54. file << open(attachment.url).read
  55. end
  56. end
  57. end
  58. stop = messages.length < 100
  59. earliest_message_id = messages.last.id
  60. end
  61. filename = "#{SecureRandom.uuid}.tar.gz"
  62. `tar -czf /var/www/scr/#{filename} /tmp/tankbot_images`
  63. FileUtils::rm_rf("/tmp/tankbot_images")
  64. control_message.edit("https://scr.tankernn.eu/#{filename}")
  65. end
  66. bot.command(:neko, description: "Requests (sometimes lewd) nekos.") do |_event, keyword|
  67. url = "https://nekos.life/api/v2/img/"
  68. options = ["meow", "woof", "tickle", "feed", "poke", "slap", "avatar", "waifu",
  69. "lizard", "pat", "kiss", "neko", "cuddle", "fox_girl", "hug"]
  70. if options.include? keyword then
  71. response = JSON.parse(Net::HTTP.get(URI("#{url}#{keyword}")))
  72. "Here's your lewds! °˖✧◝(⁰▿⁰)◜✧˖°\n#{response['url']}"
  73. else
  74. "No such tag. Please specify one of `#{options.join(", ")}`"
  75. end
  76. end
  77. bot.command(:lmgtfy, description: "Helps tech-illiterate people to enlightenment.") do |_event, *args|
  78. "http://lmgtfy.com/?s=d&q=#{args.join('+')}"
  79. end
  80. bot.command(:copypasta, description: "Cites the holy texts.") do |_event, keyword|
  81. pastafile = "copypastas.json"
  82. file = File.read pastafile
  83. pastas = JSON.parse file
  84. if pastas.include? keyword then
  85. pastas[keyword]
  86. else
  87. "No such pasta. Available pastas include `#{pastas.keys.join(", ")}`"
  88. end
  89. end
  90. class UserQueue
  91. def initialize(user)
  92. @user = user
  93. @songs = []
  94. end
  95. attr_accessor :user
  96. attr_accessor :songs
  97. end
  98. class FairQueue
  99. def initialize(voice_bot)
  100. @voice_bot = voice_bot
  101. @queues = []
  102. @now_playing = nil
  103. end
  104. def append(user, video)
  105. @queues.append UserQueue.new user unless @queues.any? { |queue| queue.user == user }
  106. @queues.select{ |queue| queue.user == user }.first.songs.append(video)
  107. end
  108. def queue
  109. queues = @queues.map{ |queue| queue.songs }
  110. target_length = queues.map{ |queue| queue.length }.max
  111. queues.map{ |queue| queue + (target_length - queue.length).times.collect{nil} }.transpose.flatten.compact
  112. end
  113. def play
  114. until @queues.empty?
  115. queue = @queues.shift
  116. @now_playing = queue.songs.shift
  117. # Rotate user to last place
  118. @queues.append queue unless queue.songs.empty?
  119. # Play song
  120. # song_log("Playing *#{video.title}*...")
  121. @voice_bot.play_file(@now_playing.filename)
  122. end
  123. @voice_bot.destroy
  124. end
  125. attr_accessor :now_playing
  126. end
  127. def format_title(video)
  128. total_seconds = video.information[:duration]
  129. seconds = total_seconds % 60
  130. minutes = (total_seconds / 60) % 60
  131. hours = total_seconds / (60 * 60)
  132. timestamp = format("%02d:%02d", minutes, seconds)
  133. timestamp = format("%02d:%s", hours, timestamp) if hours > 0
  134. "**#{video.information[:fulltitle]}** `[#{timestamp}]`"
  135. end
  136. fairqueues = Hash.new
  137. youtube_dl_options = {
  138. default_search: 'ytsearch',
  139. format: 'bestaudio',
  140. output: 'cache/%(title)s-%(id)s.%(ext)s'
  141. }
  142. bot.command(:play, description: "Plays 'music' of your choosing in your voice channel.") do |_event, *query|
  143. voice_bot = _event.voice
  144. unless voice_bot then
  145. channel = _event.user.voice_channel
  146. next "You're not in any voice channel!" unless channel
  147. voice_bot = bot.voice_connect(channel)
  148. voice_bot.volume = $settings['volume']
  149. fairqueues[_event.server] = FairQueue.new(voice_bot)
  150. end
  151. video = YoutubeDL.download query.join(' '), youtube_dl_options
  152. fairqueue = fairqueues[_event.server]
  153. fairqueue.append(_event.user, video)
  154. if voice_bot.playing? then
  155. "Added #{format_title(video)} to the queue."
  156. else
  157. Thread.new{fairqueue.play}
  158. "Started playing #{format_title(video)}"
  159. end
  160. end
  161. bot.command(:skip, description: "Expresses your dislike of the currently playing 'music'.") do |_event|
  162. _event.voice.stop_playing
  163. end
  164. bot.command(:np, description: "Shows what 'music' is currently playing.") do |_event|
  165. queue = fairqueues[_event.server]
  166. next "Nothing is playing." unless _event.voice
  167. format_title(queue.now_playing)
  168. end
  169. bot.command(:stop, description: "Puts an end to your misery.") do |_event|
  170. if _event.voice
  171. _event.voice.destroy
  172. "Stopped playing."
  173. else
  174. "Nothing is playing."
  175. end
  176. end
  177. bot.command(:queue, description: "Lists the impending torture.") do |_event|
  178. queue = fairqueues[_event.server]
  179. next "Nothing is playing." unless queue
  180. next "The queue is empty." if queue.queue.empty?
  181. queue.queue.each_with_index.map{|video, i| "#{i + 1}. #{format_title(video)}"}.join("\n")
  182. end
  183. bot.command(:volume, description: "Sets the severity of currently playing 'music'.") do |_event, volume|
  184. next "Nothing is playing." unless _event.voice
  185. $settings['volume'] = _event.voice.volume = [volume.to_f, 1.5].min
  186. save_settings
  187. ""
  188. end
  189. bot.command(:give, description: "Give a user some Good Boy Points (GBP).") do |_event|
  190. receiver = _event.message.mentions[0]
  191. sender = _event.author
  192. next 'You cannot give GBP to yourself.' if sender.id == receiver.id
  193. result = db.query "SELECT remaining FROM gbp WHERE user_id=?", sender.id
  194. first_result = result.next
  195. if first_result
  196. remaining = first_result[0]
  197. else
  198. remaining = $settings['gbp_per_week']
  199. db.execute "INSERT INTO gbp VALUES (?, ?, ?)", sender.id, 0, $settings['gbp_per_week']
  200. end
  201. next 'No GBP left to give this week.' unless remaining > 0
  202. result = db.query "SELECT received FROM gbp WHERE user_id=?", receiver.id
  203. first_result = result.next
  204. if first_result
  205. received = first_result[0]
  206. else
  207. received = 0
  208. db.execute "INSERT INTO gbp VALUES (?, ?, ?)", receiver.id, 0, $settings['gbp_per_week']
  209. end
  210. db.execute "UPDATE gbp SET received = ? WHERE user_id = ?", received + 1, receiver.id
  211. db.execute "UPDATE gbp SET remaining = ? WHERE user_id = ?", remaining - 1, sender.id
  212. "#{sender.mention} gave 1 GBP to #{receiver.mention}."
  213. end
  214. bot.command(:leaderboard, description: "Show who's got the most Good Boy Points (GBP).") do |_event|
  215. results = db.query "SELECT user_id, received FROM gbp ORDER BY received DESC"
  216. "**Good Boy Points (GBP) Leaderboard**\n" + results.each_with_index.map{|row, i| "#{i + 1}. #{_event.server.member(row[0]).nick} - #{row[1]}"}.join("\n")
  217. end
  218. bot.voice_state_update() do |_event|
  219. user = _event.user
  220. channel = _event.channel
  221. if channel.id == $settings['naughty_corner']
  222. # Deduct GBP
  223. result = db.query "SELECT received FROM gbp WHERE user_id=?", user.id
  224. first_result = result.next
  225. if first_result
  226. received = first_result[0]
  227. else
  228. received = 0
  229. db.execute "INSERT INTO gbp VALUES (?, ?, ?)", receiver.id, 0, $settings['gbp_per_week']
  230. end
  231. next unless received > 0
  232. db.execute "UPDATE gbp SET received = ? WHERE user_id = ?", received - 1, user.id
  233. end
  234. end
  235. bot.run