Skip to content

Commit

Permalink
Closed #30 Only Replace Phrases for Followers
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr34mR committed Jan 20, 2020
1 parent 404324f commit 6d8dc90
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 13 deletions.
14 changes: 13 additions & 1 deletion MisterDoctor/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
4 changes: 3 additions & 1 deletion MisterDoctor/Classes/TokenFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ internal class Token

public string Username { get; set; }
public string UserOAuthKey { get; set; }


public string ClientId { get; set; }

public override string ToString()
{
return $"{Id} - {Username}";
Expand Down
79 changes: 75 additions & 4 deletions MisterDoctor/Forms/FormMain.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
using MisterDoctor.Classes;
using MisterDoctor.Helpers;
using MisterDoctor.Managers;
using MisterDoctor.Properties;
using Newtonsoft.Json.Linq;
using RestSharp;
using TwitchLib.Client;
using TwitchLib.Client.Events;
using TwitchLib.Client.Models;
Expand All @@ -28,6 +31,8 @@ internal partial class FormMain : Form
private bool _procNext;
private bool _closing;

private string _channelId = string.Empty;

public FormMain()
{
InitializeComponent();
Expand Down Expand Up @@ -294,6 +299,13 @@ private void Connect()
return;
}

if (string.IsNullOrEmpty(token.ClientId))
{
ShowError("No Client ID");
ShowToken();
return;
}

if (DbHelper.WordRandom() == null)
{
ShowError("No Substitution Words Set");
Expand Down Expand Up @@ -353,7 +365,7 @@ private void Twitch_OnMessageReceived(object sender, OnMessageReceivedArgs e)
if (message == null) return;
if (message.IsMe) return;
if (message.Username.Equals(_twitchClient.TwitchUsername, StringComparison.CurrentCultureIgnoreCase)) return;

// Check for command

if (message.Message.StartsWith(_settings.CommandIgnore, StringComparison.CurrentCultureIgnoreCase))
Expand Down Expand Up @@ -396,9 +408,9 @@ private void Twitch_OnMessageReceived(object sender, OnMessageReceivedArgs e)
var send = GoodBadCheck(parts, _twitchClient.TwitchUsername);

if (string.IsNullOrEmpty(send)) send = PhraseCheck(parts, message.Username);
if (string.IsNullOrEmpty(send)) send = RandomReplace(parts);
if (string.IsNullOrEmpty(send)) send = RandomReplace(message.Username, message.Channel, parts);
if (string.IsNullOrEmpty(send)) return;

var sendParts = new MessageParts(send);
sendParts.UpdateWildcards(message);

Expand Down Expand Up @@ -430,7 +442,7 @@ private static string PhraseCheck(MessageParts parts, string username)
return PhraseManager.CheckMessage(parts, username);
}

private string RandomReplace(MessageParts parts)
private string RandomReplace(string user, string channel, MessageParts parts)
{
// Cooldown ?
if (_coolDownTime.HasValue)
Expand All @@ -450,6 +462,10 @@ private string RandomReplace(MessageParts parts)

_procNext = true;

// First check if the user is following (check after proc)

if (!IsFollowing(user, channel)) return string.Empty;

// Get the indexes of the nouns

if (!parts.HasNoun()) return string.Empty;
Expand All @@ -470,5 +486,60 @@ private string RandomReplace(MessageParts parts)

return messageToSend;
}

private bool IsFollowing(string messageUsername, string channelName)
{
if (messageUsername.Equals(channelName, StringComparison.CurrentCultureIgnoreCase)) return true;

// get https://api.twitch.tv/kraken/users/<userId>/follows/channels/<channelId>
// Accept: application/vnd.twitchtv.v5+json
// Client-ID: xxxxxxxxxxxx

var client = new RestClient("https://api.twitch.tv/kraken/");
var id = DbHelper.TokenGet().ClientId;

// Get the channels id number (if hasn't been pulled yet)

if (string.IsNullOrEmpty(_channelId))
{
var channelRequest = new RestRequest($"users?login={channelName}");
channelRequest.AddHeader("Accept", "application/vnd.twitchtv.v5+json");
channelRequest.AddHeader("Client-ID", id);

var channelResponse = client.Get(channelRequest);

var crObj = JObject.Parse(channelResponse.Content);
if (!crObj.ContainsKey("_total")) return false;
if (crObj["_total"].ToObject<int>() != 1) return false;

_channelId = ((JArray)crObj.GetValue("users")).First()["_id"].ToString();
}

// Get the users id number

var userRequest = new RestRequest($"users?login={messageUsername}");
userRequest.AddHeader("Accept", "application/vnd.twitchtv.v5+json");
userRequest.AddHeader("Client-ID", id);

var userResponse = client.Get(userRequest);

var urObj = JObject.Parse(userResponse.Content);
if (!urObj.ContainsKey("_total")) return false;
if (urObj["_total"].ToObject<int>() != 1) return false;
var userId = ((JArray)urObj.GetValue("users")).First()["_id"].ToString();

// Get the follow information

var followRequest = new RestRequest($"users/{userId}/follows/channels/{_channelId}");
followRequest.AddHeader("Accept", "application/vnd.twitchtv.v5+json");
followRequest.AddHeader("Client-ID", id);

var followResponse = client.Get(followRequest);

var statusCode = followResponse.StatusCode;

return statusCode == HttpStatusCode.OK;

}
}
}
43 changes: 40 additions & 3 deletions MisterDoctor/Forms/FormToken.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion MisterDoctor/Forms/FormToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ private void FormToken_Load(object sender, EventArgs e)
{
txtUsername.Text = Token.Username;
txtOAuth.Text = Token.UserOAuthKey;
txtClientId.Text = Token.ClientId;
}

CancelButton = btnCancel;

lnkGetOAuth.LinkClicked += lnkGetOAuth_LinkClicked;
lnkGetClient.LinkClicked += lnkGetClient_LinkClicked;
btnSave.Click += btnSave_Click;
btnCancel.Click += btnCancel_Click;

Expand All @@ -37,6 +39,11 @@ private void FormToken_Load(object sender, EventArgs e)
ToggleSave();
}

private static void lnkGetClient_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("https://dev.twitch.tv/console/apps/");
}

private static void lnkGetOAuth_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("https://twitchapps.com/tmi/");
Expand Down Expand Up @@ -77,7 +84,8 @@ private void btnSave_Click(object sender, EventArgs e)
Token = new Token
{
Username = txtUsername.Text.Trim(),
UserOAuthKey = txtOAuth.Text.Trim()
UserOAuthKey = txtOAuth.Text.Trim(),
ClientId = txtClientId.Text.Trim()
};

Save = true;
Expand Down
1 change: 1 addition & 0 deletions MisterDoctor/Helpers/DbHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static void TokenSet(Token token)
{
origToken.Username = token.Username;
origToken.UserOAuthKey = token.UserOAuthKey;
origToken.ClientId = token.ClientId;

collection.Update(origToken);
}
Expand Down
67 changes: 65 additions & 2 deletions MisterDoctor/MisterDoctor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,75 @@
<Reference Include="LiteDB, Version=4.1.4.0, Culture=neutral, PublicKeyToken=4ee40123013c9f27, processorArchitecture=MSIL">
<HintPath>..\packages\LiteDB.4.1.4\lib\net40\LiteDB.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Logging.Abstractions, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Extensions.Logging.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Extensions.Logging.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Extensions.Logging.Abstractions.2.2.0\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="RestSharp, Version=106.0.0.0, Culture=neutral, PublicKeyToken=598062e77f915f75, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.106.10.1\lib\net452\RestSharp.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Net.Http.4.3.3\lib\net46\System.Net.Http.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Encoding, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.X509Certificates, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="TwitchLib.Api, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TwitchLib.Api.3.1.2\lib\netstandard2.0\TwitchLib.Api.dll</HintPath>
</Reference>
<Reference Include="TwitchLib.Api.Core, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TwitchLib.Api.Core.3.1.2\lib\netstandard2.0\TwitchLib.Api.Core.dll</HintPath>
</Reference>
<Reference Include="TwitchLib.Api.Core.Enums, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TwitchLib.Api.Core.Enums.3.1.2\lib\netstandard2.0\TwitchLib.Api.Core.Enums.dll</HintPath>
</Reference>
<Reference Include="TwitchLib.Api.Core.Interfaces, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TwitchLib.Api.Core.Interfaces.3.1.2\lib\netstandard2.0\TwitchLib.Api.Core.Interfaces.dll</HintPath>
</Reference>
<Reference Include="TwitchLib.Api.Core.Models, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TwitchLib.Api.Core.Models.3.1.2\lib\netstandard2.0\TwitchLib.Api.Core.Models.dll</HintPath>
</Reference>
<Reference Include="TwitchLib.Api.Helix, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TwitchLib.Api.Helix.3.1.2\lib\netstandard2.0\TwitchLib.Api.Helix.dll</HintPath>
</Reference>
<Reference Include="TwitchLib.Api.Helix.Models, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TwitchLib.Api.Helix.Models.3.1.2\lib\netstandard2.0\TwitchLib.Api.Helix.Models.dll</HintPath>
</Reference>
<Reference Include="TwitchLib.Api.V5, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TwitchLib.Api.V5.3.1.2\lib\netstandard2.0\TwitchLib.Api.V5.dll</HintPath>
</Reference>
<Reference Include="TwitchLib.Api.V5.Models, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TwitchLib.Api.V5.Models.3.1.2\lib\netstandard2.0\TwitchLib.Api.V5.Models.dll</HintPath>
</Reference>
<Reference Include="TwitchLib.Client, Version=3.1.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TwitchLib.Client.3.1.4\lib\netstandard2.0\TwitchLib.Client.dll</HintPath>
</Reference>
Expand All @@ -66,6 +126,9 @@
<Reference Include="TwitchLib.Communication, Version=1.0.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TwitchLib.Communication.1.0.3\lib\netstandard2.0\TwitchLib.Communication.dll</HintPath>
</Reference>
<Reference Include="TwitchLib.PubSub, Version=3.1.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TwitchLib.PubSub.3.1.3\lib\netstandard2.0\TwitchLib.PubSub.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\AppSettings.cs" />
Expand Down
5 changes: 4 additions & 1 deletion MisterDoctor/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
<package id="Costura.Fody" version="4.1.0" targetFramework="net462" />
<package id="Fody" version="6.0.0" targetFramework="net462" developmentDependency="true" />
<package id="LiteDB" version="4.1.4" targetFramework="net462" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="2.1.1" targetFramework="net462" />
<package id="Microsoft.CSharp" version="4.5.0" targetFramework="net462" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="2.2.0" targetFramework="net462" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net462" />
<package id="RestSharp" version="106.10.1" targetFramework="net462" />
<package id="TwitchLib.Client" version="3.1.4" targetFramework="net462" />
<package id="TwitchLib.Client.Enums" version="3.1.4" targetFramework="net462" />
<package id="TwitchLib.Client.Models" version="3.1.4" targetFramework="net462" />
Expand Down

0 comments on commit 6d8dc90

Please sign in to comment.