TwitchIRC - A Swift package for parsing/serializing Twitch IRC messages

Overview

TwitchIRC

A Swift package to take of parsing/serializing Twitch IRC messages for you.

How To Parse

To parse Twitch messages, use IncomingMessage.parse(ircOutput:):

/// `websocketOutput` here represents the `String` Twitch sent you over IRC.
let twitchMessages: [(message: IncomingMessage?, text: String)] = IncomingMessage.parse(ircOutput: websocketOutput)

Then to use:

for (message, text) in twitchMessages {
    if let message = message {
        switch message {
        /// Switch over each case of the `IncomingMessage` and do whatever you want.
        }
    }
}

As an example, this will print any normal chat messages viewers send, in the channels you've joined:

for (message, _) in twitchMessages {
    if let message = message {
        switch message {
        case let .privateMessage(privateMessage):
            print("In channel \(privateMessage.channel), user \(privateMessage.displayName) sent a message: \(privateMessage.message)")
        default: break
        }
    }
}

How To Serialize

To serialize your messages to a proper text form, use OutgoingMessage.serialize():

let outgoingMessage: OutgoingMessage = ...
let serialized = outgoingMessage.serialize()
/// Now send `serialized` to Twitch over IRC.

As an example of serializing, this will send a normal chat message to channel mahdimmbm, saying Testing TwitchIRC :):

let outgoingMessage: OutgoingMessage = .privateMessage(to: "mahdimmbm", message: "Testing TwitchIRC :)")
let serialized = outgoingMessage.serialize()
/// Now send `serialized` to Twitch over IRC.

Warning

This package includes both official and unofficial info sent to you over IRC by Twitch.
Twitch only gurantees the official stuff, and using the unofficial info might result in code breakage in the future.
To see what's official and what's not, you can take a look at the official documentation.

Communication and Support

If you have any questions, TwitchDev Discord server will likely prove helpful to you. I'm available there @Mahdi BM#0517.

Feel free to make PRs or open Issues as well :)

You might also like...
A Swift mailing list client for iPhone and iPad
A Swift mailing list client for iPhone and iPad

Due to costs and lack of interest, I’ve had to take down the Charter service. If you’re interested in running your own copy, get in touch and I can se

2048 for Swift

swift-2048 A working port of iOS-2048 to Apple's new Swift language. Like the original Objective-C version, swift-2048 does not rely upon SpriteKit. S

Learn Swift interactively on your iPhone.
Learn Swift interactively on your iPhone.

Unwrap is an app that helps you learn Swift faster and more effectively. At its core lies almost 100 video lessons that teach all the fundamentals of

▶️ video player in Swift, simple way to play and stream media on iOS/tvOS

Player Player is a simple iOS video player library written in Swift. Looking for an obj-c video player? Check out PBJVideoPlayer (obj-c). Looking for

SushiMarketingApp - Sushi Marketing App Built With Swift
SushiMarketingApp - Sushi Marketing App Built With Swift

SushiMarketingApp Sushi E-Commerce App Language: Swift and Objective-C Database:

A small SwiftUI based chat client for IRC, using swift-nio-irc
A small SwiftUI based chat client for IRC, using swift-nio-irc

NeoIRC A simple Internet Relay Chat client implemented using SwiftNIO and SwiftUI. Inspired by: For maximum NIO someone (I’m tempted) should adopt NIO

The SwiftUI Messages Clone consists of layout and composition clones of the iOS Messages app.
The SwiftUI Messages Clone consists of layout and composition clones of the iOS Messages app.

The SwiftUI Messages Clone consists of layout and composition clones of the iOS Messages app. It has Messages-like bubble and screen effects, reactions, and animations, all created with SwiftUI.

A tool for fast serializing & deserializing of JSON
A tool for fast serializing & deserializing of JSON

FastEasyMapping Overview Requirements Installation CocoaPods Carthage Usage Deserialization Serialization Mapping Attributes Relationships Assignment

Colloquy is an advanced IRC, SILC & ICB client for macOS and iOS!

Colloquy Dependencies This repository uses git submodules for some of its dependencies, so you will have to check those out as well. You can do this f

BTTV and Twitch Emotes App
BTTV and Twitch Emotes App

NativeEmote BTTV and Twitch Emotes App Their API is not documented and is not supported (officially) Usage Download and open the the app from releases

BTTV-for-Safari - Unofficial BTTV/ FFZ Safari Extension for Twitch
BTTV-for-Safari - Unofficial BTTV/ FFZ Safari Extension for Twitch

BTTV for Safari This unofficial Safari exention adds support for BTTV and FFZ emotes on Twitch. The extension simply injects the BTTV script from the

React Native Twitch application
React Native Twitch application

Notes I'm going to rewrite this project 😉 . Be ready to new Twitch application. Twitch This project was built for The Rolling Scopes #18 meetup. As o

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable. Elevate should no longer be used for

CoreML-Face-Parsing - how to use face-parsing CoreML model in iOS
CoreML-Face-Parsing - how to use face-parsing CoreML model in iOS

CoreML-Face-Parsing The simple sample how to use face-parsing CoreML model in iO

Swift Markdown is a Swift package for parsing, building, editing, and analyzing Markdown documents.

Swift Markdown is a Swift package for parsing, building, editing, and analyzing Markdown documents.

A Swift package for parsing Clang module map files

Clangler is a Swift package used to parse Clang module map files into an abstract syntax tree (AST) representation. Once parsed, you can inspect or manipulate the nodes in the file, then generate and save a new file reflecting your changes.

Math expression parser built with Point•Free's swift-parsing package

swift-math-parser Basic math expression parser built with Point•Free's swift-parsing package. NOTE: currently, this uses a fork of that fixes a parsin

A simple style messages/notifications, in Swift.
A simple style messages/notifications, in Swift.

Demo Example To show notifications use the following code: self.showMessage("Something success", type: .success) To display a notice on a view: view.s

A fully customizable library to easily display Animated Toast Messages in iOS using Swift!
A fully customizable library to easily display Animated Toast Messages in iOS using Swift!

CustomToastView-swift A fully customizable library to easily display Animated Toast Messages in iOS using Swift! Preview - All the custom toasts you c

Comments
  • Improved emote parsing in PrivateMessage

    Improved emote parsing in PrivateMessage

    Hi there!

    I've been using your package for a personal project and it is great! Thank you very much for all the work. Since it has been very helpful, I thought I'd give back by improving something I found was not as straight forward as other aspects of the message.

    Up to now, the parsing of the emotes in the message was a bit odd, specially for handling messages where the same emote repeats multiple times.

    I made a small utility function that takes care of better parsing the information from the message into an array containing a new struct EmoteReference. This struct includes the id of the emote, the name (aka the text used to represent the emote), and the indices in the message that correspond to the emote. The idea is that having all this information it is easier for a user of the package to handle emotes.

    Please, let me know if there's anything out of place or if I should include some more test cases (although I feel like the one included already handles all cases).

    Cheers!

    opened by kevinrpb 5
  • Message types in `switch` not working

    Message types in `switch` not working

    Hi @MahdiBM, first of all, thank you for the work on that library!

    For some reason i cannot resolve the message type in a switch statement like in the examples.

    This is my code:

    print(rawString)
    
    let twitchMessages: [(message: IncomingMessage?, text: String)] = IncomingMessage.parse(ircOutput: rawString)
    
    for (message, _) in twitchMessages {
      if let message = message {
        
        print(message)
        
        switch message {
        case .privateMessage(let privateMessage):
          print("In channel \(privateMessage.channel), user \(privateMessage.displayName) sent a message: \(privateMessage.message)")
        default: break
        }
      }
    }
    

    It does not go into the case let .privateMessage(privateMessage): and always falls through to the default.

    The print statements i've added print the following output to the console:

    // print(rawString)
    @badge-info=subscriber/22;badges=subscriber/18;client-nonce=a2d200e925041b5e800bf32c47039ed7;color=#D2691E;display-name=MNT7_Matze;emotes=306934184:79-88;first-msg=0;flags=;id=42e74668-a2a0-4f26-85b6-a2c00563b926;mod=0;returning-chatter=0;room-id=73437396;subscriber=1;tmi-sent-ts=1670779577985;turbo=0;user-id=24647541;user-type= :mnt7_matze!mnt7_matze@mnt7_matze.tmi.twitch.tv PRIVMSG #bonjwa :@Omphis weiß nicht ob die StarCraft-Ehre das zulassen würde extra zu verlieren bonjwaCute
    
    // print(message)
    privateMessage(TwitchIRC.PrivateMessage(channel: "bonjwa", message: "@Omphis weiß nicht ob die StarCraft-Ehre das zulassen würde extra zu verlieren bonjwaCute", badgeInfo: ["subscriber/22"], badges: ["subscriber/18"], bits: "", color: "#D2691E", displayName: "MNT7_Matze", userLogin: "mnt7_matze", emotes: "306934184:79-88", emoteOnly: false, flags: [], firstMessage: false, returningChatter: false, messageId: "", id: "42e74668-a2a0-4f26-85b6-a2c00563b926", crowdChantParentMessageId: "", customRewardId: "", roomId: "73437396", tmiSentTs: 1670779577985, clientNonce: "a2d200e925041b5e800bf32c47039ed7", userId: "24647541", replyParent: TwitchIRC.PrivateMessage.ReplyParent(displayName: "", userLogin: "", message: "", id: "", userId: ""), pinnedChat: TwitchIRC.PrivateMessage.PinnedChat(amount: 0, canonicalAmount: 0, currency: "", exponent: 0), parsingLeftOvers: TwitchIRC.ParsingLeftOvers(unusedPairs: [], unavailableKeys: [], unparsedKeys: [])))
    

    So it looks like it has the proper type but why doesn't it work?

    Additional info:

    Lib Version: v1.0.7
    Platform: iOS 16 / tvOS 16
    Swift: 5.7
    XCode: 14.1
    

    If you need additional info just hit me up here and i will follow your request.

    opened by markhaehnel 1
Releases(v1.0.7)
Owner
Mahdi Bahrami
Swift both server-side and client-side :) Server-side Swift author @ raywenderlich.com
Mahdi Bahrami
Swift Library based on AVFoundation that allow to easily add camera feature with custom UI into your project.

Would you like to create your camera same as Snapchat or Instagram? I think this library could help you. Basicly it is a wrapper above AVFoundation th

Taras Chernyshenko 14 Sep 21, 2022
Fully open source text editor for iOS written in Swift.

Edhita Fully open source text editor for iOS written in Swift. http://edhita.bornneet.com/ What Edhita means? Edhita (Romaji) == エディタ (Katakana) == Ed

Tatsuya Tobioka 1.2k Jan 7, 2023
30 mini Swift Apps for self-study

Swift 30 Projects Contents This repo is updated with Swift 5 and compatible with iPhone X: Simple UIKit components UIScrollView, UITableView, UICollec

Soap 7.5k Jan 3, 2023
A Hacker News reader in Swift

SwiftHN A Hacker News reader in Swift using the best features of both the language and iOS 8 latest API (well, that's the end goal) SwiftHN is now ava

Thomas Ricouard 1.7k Dec 24, 2022
Build a Swift App as a designer

DesignerNewsApp Simple iOS client for Designer News, by the creator of Design+Code and the team, written in Swift. Usage Download the repository $ git

Meng To 2.4k Dec 24, 2022
swift implementation of flappy bird. More at fullstackedu.com

FlappySwift An implementation of Flappy Bird in Swift for iOS 8. Notes We're launching a course Game Programming with Swift If you are interested in e

newline (formerly Fullstack.io) 9.5k Dec 29, 2022
Basic app to show how to login with Facebook, Google, Twitter. Created for learning purpose :) using Xcode 9 and Swift 4.0

Social Logins iOS Basic app to show how to login with Facebook, Google, Twitter. Created for learning purpose :) using Xcode 9 and Swift 4.0 Note: Bef

Jogendra 12 Nov 4, 2022
Applozic UI Kit in Swift

Official iOS Swift SDK for Chat ?? Introduction ?? Applozic brings real-time engagement with chat, video, and voice to your web, mobile, and conversat

Applozic 50 Mar 21, 2022
Simple sample of using the VIP (Clean Swift) architecture for iOS

MyAnimeList Simple sample of using the VIP (Clean Swift) architecture for iOS. ViewController: controls the event handling, view life cycle and displa

null 24 Oct 12, 2022
Swift audio synthesis, processing, & analysis platform for iOS, macOS and tvOS

AudioKit AudioKit is an audio synthesis, processing, and analysis platform for iOS, macOS (including Catalyst), and tvOS. Installation To add AudioKit

AudioKit 9.5k Jan 5, 2023