职业网络成瘾者 • 游戏爱好者 • 技术创造者
职业网络成瘾者 • 游戏爱好者 • 技术创造者

如何让 Twitch 聊天通过频道点控制 RGB 灯

以下是关于如何创建 Twitch 频道积分奖励的快速指南,该奖励允许通过聊天改变我房间中的 RGB 灯!
为了方便您使用,本页面由我热情高涨的 AI 实习生从英文翻译而来。他们仍在学习中,因此可能存在一些错误。为了获得最准确的信息,请参考英文版本。
博客 如何让 Twitch 聊天通过频道点控制 RGB 灯

请注意,本博文发布于 2021 年 2 月,因此根据您阅读的时间,某些部分可能已过时。很遗憾,我无法始终保持这些文章的完全更新,以确保信息的准确性。

    So I recently created a Twitch channel reward that would allow viewers to control the colors of my RGB lights I have in my room. It is actually pretty simple and straightforward to create such a reward, you basically just need to connect the dots and tie everything together. So in this blog post I will show you how I did it.

    A demonstration

    But before I go through how I did it, I just want to show a short demo. So below is a quick video demonstration!

    Prerequisites

    In order to follow my guide there are some certain prerequisites, meaning prior things you have to have and set up in order to connect all the dots.
    1. You need to be at least a Twitch Affiliate in order to gain access to channel points
    2. You need some kind of smart lights. I personally have invested in in the Philips Hue smart lights system, but any other type of smart lights should work
    3. You need to have an IFTTT (If This Then That) or Zapier account set up. I will personally use IFTTT in this demo
    4. And finally, you will need to have a StreamElements account in order to create an (OBS Browser Source) overlay
    If you have all those prerequisites, or something equivalent, then you are good to go.
    So let's start connecting all these things together!

    Step 1 - Creating your Twitch channel point reward

    Simply head over to your Manage Rewards & Challenges page and create a new custom reward.
    When creating the reward, it is important that you toggle "Require Viewer to Enter Text". It might also be a good idea to add cooldown limits as well. Apart from those two, you can customize the reward as you wish!
    Here is a video tutorial of the specific step.

    Step 2 - Get the reward ID

    Each channel Twitch channel point reward has a long unique ID. This ID is hidden in the UI, but we need this ID in order to single out which reward was redeemed by the user in order to control the lights.
    This step is a bit more tricky if you are non-technical, but if you just do this slowly you should be able to get the ID.
    The first thing you need to do is go to your Manage Rewards & Challenges page, then press F12 to open up the browser's Developer Tools. Navigate to the "Console" tab and insert this piece of JavaScript code below and hit enter.
    But before you do that, please note; You should generally never actually inject code into your browser from a random site, as it is both a security and privacy risk.
    With that said, this is the only way I can help you fetch the reward ID in the most easy way, but please review the code before you run it. If you can't, maybe you have a friend that knows basic coding and can confirm that the code is safe to use.
    This code will create new red buttons onto the page. Simply click on the appropriate button to see the reward ID and copy the ID, as you will be needing this later on.
    Also don't worry, these are just temporarily changes to the page. If you simply refresh F5 the page, you will see the UI changes are gone.
    document.querySelectorAll("button[data-reward-id]").forEach((e) => { let element = document.createElement("BUTTON"); element.innerText = "Click to get reward ID"; element.style.backgroundColor = "red"; element.style.margin = "5px"; element.addEventListener("click", () => alert(e.getAttribute("data-reward-id"))); e.parentElement.appendChild(element); });
    Here is a video tutorial of the specific step.

    Step 3 - Creating the IFTTT webhooks

    As I mentioned before, you need to have prior setup your IFTTT account to use your Philips Hue smart system. It is pretty straightforward to do that, but requires you to authenticate and allow IFTTT to control your lights.
    Once your account is configured, simply create a Webhook applet as your trigger and as an action to change your Philips Hue lights.
    In the video below, I create two webhooks; one to set a specific light color and another for a random color.

    Step 4 - Creating the StreamElements custom widget

    The final step is to create a StreamElements custom widget that ties everything together.
    I would recommend creating a new overlay, but it is fully possible to use an existing overlay. Regardless, press on the big blue plus button, navigate to "STATIC / CUSTOM" and then add a "Custom widget".
    Next select your widget and click "Open editor". In the "JS" tab, replace the current JavaScript code to the one below.
    Remember to set the variables that are in the beginning of the code. REWARD_ID should contain the reward ID from step 2, and the WEBHOOK_ URLs to the ones you created in step 3.
    const REWARD_ID = ""; const WEBOOK_URL_SPECIFIC_COLOR = ""; const WEBOOK_URL_RANDOM_COLOR = ""; window.addEventListener("onEventReceived", async (obj) => { if (!obj || !obj.detail || !obj.detail.event || !obj.detail.event.data) { return; } const data = obj.detail.event.data; const rewardID = data.tags["custom-reward-id"]; const text = data.text; console.warn("[ChangeColorReward] Reward ID is "" + rewardID + ""."); if (!rewardID) { console.warn("[ChangeColorReward] No reward ID was found in chat message."); return; } if (rewardID !== REWARD_ID) { console.warn("[ChangeColorReward] Reward ID "" + rewardID + "" did not match expected "" + REWARD_ID + ""."); return; } const requestData = { value1: text, } let url = (text === "random") ? WEBOOK_URL_RANDOM_COLOR : WEBOOK_URL_SPECIFIC_COLOR; url += "?value1=" + encodeURIComponent(text); console.log("[ChangeColorReward] Final URL is "" + url + ""."); window.fetch(url, { mode: "no-cors", }); });
    Here is a video tutorial of the specific step.

    Step 5 - Trying it all out

    In theory you should have connected everything together now to create a Twitch channel reward that controls your RGB lights. Simply try it out by redeeming a reward!
    Also remember to add the StreamElements overlay as a Browser Source in your OBS Studio!

    作者:Special Agent Squeaky。首次发布于2021年2月21日。最后更新于2021年2月21日。

    📺 快来看看 Squeaky 的最新视频!

    如何为您的直播添加简单的实时字幕