Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #64 from pvzig/swift3
Browse files Browse the repository at this point in the history
Update to Swift 3
  • Loading branch information
pvzig authored Dec 30, 2016
2 parents b8f20d7 + b877fac commit b00bf0b
Show file tree
Hide file tree
Showing 37 changed files with 2,307 additions and 2,266 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DEVELOPMENT-SNAPSHOT-2016-05-09-a
3.0.2
42 changes: 9 additions & 33 deletions Examples/echobot/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,4 @@
//
// main.swift
//
// Copyright © 2016 Peter Zignego. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Foundation
import SlackKit

class Echobot: MessageEventsDelegate {
Expand All @@ -33,27 +11,25 @@ class Echobot: MessageEventsDelegate {
}

// MARK: MessageEventsDelegate
func messageReceived(message: Message) {
func sent(_ message: Message, client: SlackClient) {}
func changed(_ message: Message, client: SlackClient) {}
func deleted(_ message: Message?, client: SlackClient) {}
func received(_ message: Message, client: SlackClient) {
listen(message: message)
}

func messageSent(message: Message){}
func messageChanged(message: Message){}
func messageDeleted(message: Message?){}

// MARK: Echobot Internal Logic
private func listen(message: Message) {
if let channel = message.channel, text = message.text, id = client.authenticatedUser?.id {
if let channel = message.channel, let text = message.text, let id = client.authenticatedUser?.id {
if id != message.user && message.user != nil {
client.webAPI.sendMessage(channel:channel, text: text, linkNames: true, success: {(response) in
}, failure: { (error) in
print("Echobot failed to reply due to error:\(error)")
}, failure: { (error) in
print("Echobot failed to reply due to error:\(error)")
})
}
}
}

}

let echobot = Echobot(token: "xoxb-SLACK_API_TOKEN")
echobot.client.connect()
echobot.client.connect()
98 changes: 36 additions & 62 deletions Examples/leaderboard/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
//
// main.swift
//
// Copyright © 2016 Peter Zignego. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import String
import Foundation
import SlackKit

class Leaderboard: MessageEventsDelegate {

var leaderboard: [String: Int] = [String: Int]()
let atSet = CharacterSet(characters: ["@"])
let atSet = CharacterSet(charactersIn: "@")

let client: SlackClient

Expand All @@ -37,42 +14,39 @@ class Leaderboard: MessageEventsDelegate {
}

enum Command: String {
case Leaderboard = "leaderboard"
case leaderboard = "leaderboard"
}

enum Trigger: String {
case PlusPlus = "++"
case MinusMinus = "--"
case plusPlus = "++"
case minusMinus = "--"
}

// MARK: MessageEventsDelegate
func messageReceived(message: Message) {
func sent(_ message: Message, client: SlackClient) {}
func changed(_ message: Message, client: SlackClient) {}
func deleted(_ message: Message?, client: SlackClient) {}
func received(_ message: Message, client: SlackClient) {
listen(message: message)
}

func messageSent(message: Message){}
func messageChanged(message: Message){}
func messageDeleted(message: Message?){}

// MARK: Leaderboard Internal Logic
private func listen(message: Message) {
if let id = client.authenticatedUser?.id, text = message.text {
if text.lowercased().contains(query: Command.Leaderboard.rawValue) && text.contains(query: id) {
handleCommand(command: .Leaderboard, channel: message.channel)
if let id = client.authenticatedUser?.id, let text = message.text {
if text.lowercased().contains(Command.leaderboard.rawValue) && text.contains(id) {
handleCommand(command: .leaderboard, channel: message.channel)
}
}
if message.text?.contains(query: Trigger.PlusPlus.rawValue) == true {
handleMessageWithTrigger(message: message, trigger: .PlusPlus)
if message.text?.contains(Trigger.plusPlus.rawValue) == true {
handleMessageWithTrigger(message: message, trigger: .plusPlus)
}
if message.text?.contains(query: Trigger.MinusMinus.rawValue) == true {
handleMessageWithTrigger(message: message, trigger: .MinusMinus)
if message.text?.contains(Trigger.minusMinus.rawValue) == true {
handleMessageWithTrigger(message: message, trigger: .minusMinus)
}
}

private func handleMessageWithTrigger(message: Message, trigger: Trigger) {
if let text = message.text,
start = text.index(of: "@"),
end = text.index(of: trigger.rawValue) {
if let text = message.text, let start = text.range(of: "@")?.lowerBound, let end = text.range(of: trigger.rawValue)?.lowerBound {
let string = String(text.characters[start...end].dropLast().dropFirst())
let users = client.users.values.filter{$0.id == self.userID(string: string)}
if users.count > 0 {
Expand All @@ -88,12 +62,12 @@ class Leaderboard: MessageEventsDelegate {

private func handleCommand(command: Command, channel:String?) {
switch command {
case .Leaderboard:
case .leaderboard:
if let id = channel {
client.webAPI.sendMessage(channel:id, text: "Leaderboard", linkNames: true, attachments: [constructLeaderboardAttachment()], success: {(response) in

}, failure: { (error) in
print("Leaderboard failed to post due to error:\(error)")
print(response)
}, failure: { (error) in
print("Leaderboard failed to post due to error:\(error)")
})
}
}
Expand All @@ -107,9 +81,9 @@ class Leaderboard: MessageEventsDelegate {

private func scoringForValue( dictionary: inout [String: Int], value: String, trigger: Trigger) {
switch trigger {
case .PlusPlus:
case .plusPlus:
dictionary[value]?+=1
case .MinusMinus:
case .minusMinus:
dictionary[value]?-=1
}
}
Expand All @@ -118,18 +92,22 @@ class Leaderboard: MessageEventsDelegate {
private func constructLeaderboardAttachment() -> Attachment? {
let 💯 = AttachmentField(title: "💯", value: swapIDsForNames(string: topItems(dictionary: &leaderboard)), short: true)
let 💩 = AttachmentField(title: "💩", value: swapIDsForNames(string: bottomItems(dictionary: &leaderboard)), short: true)
return Attachment(fallback: "Leaderboard", title: "Leaderboard", colorHex: AttachmentColor.Good.rawValue, text: "", fields: [💯, 💩])
return Attachment(fallback: "Leaderboard", title: "Leaderboard", colorHex: AttachmentColor.good.rawValue, text: "", fields: [💯, 💩])
}

private func topItems(dictionary: inout [String: Int]) -> String {
let sortedKeys = dictionary.keys.sorted(isOrderedBefore: ({dictionary[$0] > dictionary[$1]})).filter({dictionary[$0] > 0})
let sortedValues = dictionary.values.sorted(isOrderedBefore: {$0 > $1}).filter({$0 > 0})
let sortedKeys = dictionary.keys.sorted(by: { (k1: String, k2: String) -> Bool in
return dictionary[k1]! > dictionary[k2]!
}).filter({ dictionary[$0]! > 0})
let sortedValues = dictionary.values.sorted(by: {$0 > $1}).filter({$0 > 0})
return leaderboardString(keys: sortedKeys, values: sortedValues)
}

private func bottomItems( dictionary: inout [String: Int]) -> String {
let sortedKeys = dictionary.keys.sorted(isOrderedBefore: ({dictionary[$0] < dictionary[$1]})).filter({dictionary[$0] < 0})
let sortedValues = dictionary.values.sorted(isOrderedBefore: {$0 < $1}).filter({$0 < 0})
let sortedKeys = dictionary.keys.sorted(by: { (k1: String, k2: String) -> Bool in
return dictionary[k1]! < dictionary[k2]!
}).filter({ dictionary[$0]! < 0})
let sortedValues = dictionary.values.sorted(by: {$0 < $1}).filter({$0 < 0})
return leaderboardString(keys: sortedKeys, values: sortedValues)
}

Expand All @@ -146,22 +124,18 @@ class Leaderboard: MessageEventsDelegate {
var returnString = string
for key in client.users.keys {
if let name = client.users[key]?.name {
if returnString.contains(query: key) {
returnString.replace(string: key, with: "@"+name)
if returnString.contains(key) {
returnString = returnString.replacingOccurrences(of: key, with: "@"+name)
}
}
}
return returnString
}

private func userID(string: String) -> String {
let alphanumericSet = CharacterSet(characters:
["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"0","1","2","3","4","5","6","7","8","9"])
return string.trim(alphanumericSet.inverted)
return string.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
}
}

let leaderboard = Leaderboard(token: "xoxb-SLACK_API_TOKEN")
leaderboard.client.connect()
leaderboard.client.connect()
Loading

0 comments on commit b00bf0b

Please sign in to comment.