浏览代码

Updateded the Raffle Bot to use case statements

Neiro 6 年之前
父节点
当前提交
b60709378a
共有 2 个文件被更改,包括 47 次插入39 次删除
  1. 25 24
      app/models/char_app_responses.rb
  2. 22 15
      bot.rb

+ 25 - 24
app/models/char_app_responses.rb

@@ -1,24 +1,25 @@
-# frozen_string_literal: true
-
-module CharAppResponses
-  GRAMMAR = "Please check your grammar and\ncapitalization"
-  UNITS = "Please specify your units in\nImperial or Metric"
-  IMAGE = "Your image is inappropriate for\ndefault use"
-  LORE = "One or more responses are\nconflicting with server lore"
-  UNDER_AGE = "Your age conflicts with the\nspecified rating"
-  INVALID = "One or more responses are\ninvalid"
-  VULGAR = "Your application is too vulgar,\nor conflicts with server rules"
-  DM_NOTES = "Please elaborate on your\nDM Notes"
-  INLINE_SPACE = "------------------------------------"
-
-  REJECT_MESSAGES = {
-    Emoji::SPEECH_BUBBLE => GRAMMAR,
-    Emoji::SCALE => UNITS,
-    Emoji::PICTURE => IMAGE,
-    Emoji::BOOKS => LORE,
-    Emoji::BABY => UNDER_AGE,
-    Emoji::SKULL => INVALID,
-    Emoji::VULGAR => VULGAR,
-    Emoji::NOTE => DM_NOTES
-  }
-end
+# frozen_string_literal: true
+require_relative '../../lib/emoji.rb'
+
+module CharAppResponses
+  GRAMMAR = "Please check your grammar and\ncapitalization"
+  UNITS = "Please specify your units in\nImperial or Metric"
+  IMAGE = "Your image is inappropriate for\ndefault use"
+  LORE = "One or more responses are\nconflicting with server lore"
+  UNDER_AGE = "Your age conflicts with the\nspecified rating"
+  INVALID = "One or more responses are\ninvalid"
+  VULGAR = "Your application is too vulgar,\nor conflicts with server rules"
+  DM_NOTES = "Please elaborate on your\nDM Notes"
+  INLINE_SPACE = "------------------------------------"
+
+  REJECT_MESSAGES = {
+    Emoji::SPEECH_BUBBLE => GRAMMAR,
+    Emoji::SCALE => UNITS,
+    Emoji::PICTURE => IMAGE,
+    Emoji::BOOKS => LORE,
+    Emoji::BABY => UNDER_AGE,
+    Emoji::SKULL => INVALID,
+    Emoji::VULGAR => VULGAR,
+    Emoji::NOTE => DM_NOTES
+  }
+end

+ 22 - 15
bot.rb

@@ -147,23 +147,30 @@ opts = { "participants" => "May accept Everyone, Here, or a comma seperated list
 raffle = Command.new(:raffle, "Creates a raffle and picks a winner", opts) do |event, participant|
   user_channel = event.author.dm
 
-  case participant
-  when 'everyone'
-    participants = event.server.members
-    participant = participants[rand(0...participants.count)]
-    participant = participant.nickname || participant.username
-  when 'here'
-    participants = event.message.channel.users
-    participant = participants[rand(0...participants.count)]
-    participant = participant.nickname || participant.username
-  else
-    participants = participant.split(/\s?,\s?/)
-    participant = participants[rand(0...participants.count)]
-  end
+  participants =
+    case participant
+    when /^everyone$/i
+      event.server.members
+    when /^here$/i
+      event.message.channel.users
+    else
+      participant.split(/\s?,\s?/)
+    end
 
-  new_generic_embed(event, "Raffle Results!", "Winner: " + participant) if participant
+  winner = participants.sample
+  winner_name = 
+    case winner
+    when String
+      winner
+    else
+      winner.nickname || winner.username
+    end
 
-  command_error_embed("There was an error creating your raffle!", raffle) unless participant
+    if winner_name
+      new_generic_embed(event, "Raffle Results!", "Winner: " + winner_name)
+    else
+      command_error_embed("There was an error creating your raffle!", raffle)
+    end
 end
 
 # ---