Git Product home page Git Product logo

twitchirc's Introduction

TwitchIRC

Build Status

A dependancy-free Swift package that takes of parsing and serializing Twitch IRC messages.

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
Twitch has deprecated sending chat commands over websocket, and you need to use the REST API instead.
You can still respond to messages over Websocket.
For more info: https://discuss.dev.twitch.tv/t/deprecation-of-chat-commands-through-irc/40486/83

I don't see some info in some messages

TwitchIRC contains all info sent by Twitch. If you don't see something in message types, its probably deprecated.
Some of the deprecated keys are subscriber, mod or user-type. You need to instead use the badges for those 3 mentioned keys.
I'm personally actively looking for any non-parsed info using the parsingLeftOvers property that some message types have, and will update the package with the new info as soon as I catch them.

How To Add TwitchIRC To Your Project

To use the TwitchIRC library in a SwiftPM project, add the following line to the dependencies in your Package.swift file:

.package(url: "https://github.com/MahdiBM/TwitchIRC", from: "1.0.0"),

Include "TwitchIRC" as a dependency for your executable target:

.target(name: "<target>", dependencies: [
    .product(name: "TwitchIRC", package: "TwitchIRC"),
]),

Finally, add import TwitchIRC to your source code.

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 :)

twitchirc's People

Contributors

kevinrpb avatar mahdibm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

muzix false kevinrpb

twitchirc's Issues

Send custom tags with PRIVMSG

Is it possible to add custom tags to outgoing PRIVMSGs? There is the messageIdToReply argument, but I'd specifically want to send a custom client-nonce to verify messages being sent successfully.

I can't see an option to add any additional tags, afaik client-once is the only one that even affects anything when sent to TMI, but it would be nice to have either a way of sending that specifically or an option for completely custom tags (IncomingMessage already contains the client-nonce tag).

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.