Skip to content

Commit e80660b

Browse files
committed
feat: 新增 Twitch Bot 自動登入功能及相關 UI 調整
1 parent 4e4500f commit e80660b

File tree

5 files changed

+105
-38
lines changed

5 files changed

+105
-38
lines changed

OBSNowPlayingOverlay/OBSNowPlayingOverlay.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
<ImplicitUsings>enable</ImplicitUsings>
88
<UseWPF>true</UseWPF>
99
<ApplicationIcon>Assets\icon.ico</ApplicationIcon>
10-
<Version>1.1.4</Version>
10+
<Version>1.1.5</Version>
1111
<PackageIcon>icon.png</PackageIcon>
1212
<PackageReadmeFile>README.md</PackageReadmeFile>
1313
<RepositoryUrl>https://github.com/konnokai/OBSNowPlayingOverlay</RepositoryUrl>
1414
<RepositoryType>git</RepositoryType>
1515
<StartupObject>OBSNowPlayingOverlay.App</StartupObject>
16-
<Platforms>AnyCPU;x64</Platforms>
16+
<Platforms>x64</Platforms>
1717
</PropertyGroup>
1818

1919
<ItemGroup>

OBSNowPlayingOverlay/SettingWindow.xaml.cs

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Windows;
1010
using System.Windows.Markup;
1111
using System.Windows.Media;
12+
using TwitchLib.Api;
1213
using WebSocketSharp.Server;
1314
using FontFamily = System.Windows.Media.FontFamily;
1415

@@ -31,35 +32,6 @@ public SettingWindow()
3132
{
3233
InitializeComponent();
3334

34-
try
35-
{
36-
AutoUpdater.RunUpdateAsAdmin = false;
37-
AutoUpdater.HttpUserAgent = "OBSNowPlayingOverlay";
38-
AutoUpdater.SetOwner(this);
39-
AutoUpdater.CheckForUpdateEvent += (e) =>
40-
{
41-
if (e.Error != null)
42-
{
43-
AnsiConsole.WriteException(e.Error);
44-
}
45-
else if (e.IsUpdateAvailable)
46-
{
47-
AnsiConsole.MarkupLine("檢查更新: [green]發現更新![/]");
48-
Dispatcher.Invoke(() => AutoUpdater.ShowUpdateForm(e));
49-
}
50-
else
51-
{
52-
AnsiConsole.MarkupLine("檢查更新: [darkorange3]沒有需要更新[/]");
53-
}
54-
};
55-
56-
Task.Run(() => AutoUpdater.Start("https://raw.githubusercontent.com/konnokai/OBSNowPlayingOverlay/refs/heads/master/Docs/Update.xml"));
57-
}
58-
catch (Exception ex)
59-
{
60-
AnsiConsole.WriteException(ex);
61-
}
62-
6335
if (File.Exists("Config.json"))
6436
{
6537
try
@@ -98,6 +70,43 @@ public SettingWindow()
9870
}
9971
}
10072

73+
try
74+
{
75+
AutoUpdater.RunUpdateAsAdmin = false;
76+
AutoUpdater.HttpUserAgent = "OBSNowPlayingOverlay";
77+
AutoUpdater.SetOwner(this);
78+
AutoUpdater.CheckForUpdateEvent += (e) =>
79+
{
80+
if (e.Error != null)
81+
{
82+
AnsiConsole.WriteException(e.Error);
83+
}
84+
else if (e.IsUpdateAvailable)
85+
{
86+
AnsiConsole.MarkupLine("檢查更新: [green]發現更新![/]");
87+
Dispatcher.Invoke(() => AutoUpdater.ShowUpdateForm(e));
88+
}
89+
else
90+
{
91+
AnsiConsole.MarkupLine("檢查更新: [darkorange3]沒有需要更新[/]");
92+
}
93+
94+
if (!e.IsUpdateAvailable)
95+
{
96+
ContinueAfterCheckUpdate();
97+
}
98+
};
99+
100+
Task.Run(() => AutoUpdater.Start("https://raw.githubusercontent.com/konnokai/OBSNowPlayingOverlay/refs/heads/master/Docs/Update.xml"));
101+
}
102+
catch (Exception ex)
103+
{
104+
AnsiConsole.WriteException(ex);
105+
}
106+
}
107+
108+
private void ContinueAfterCheckUpdate()
109+
{
101110
try
102111
{
103112
_wsServer = new WebSocketServer(IPAddress.Loopback, 52998);
@@ -118,8 +127,6 @@ public SettingWindow()
118127
return;
119128
}
120129

121-
_mainWindow.Show();
122-
123130
chkb_LoadSystemFonts.Dispatcher.Invoke(() =>
124131
{
125132
chkb_LoadSystemFonts.IsChecked = _config.IsLoadSystemFonts;
@@ -156,6 +163,48 @@ public SettingWindow()
156163
});
157164

158165
MainWindow.IsUseBlackAsTitleColor = _config.OBSUseBlackAsTitleColor;
166+
167+
Dispatcher.Invoke(() => _mainWindow.Show());
168+
169+
if (!string.IsNullOrEmpty(TwitchBotConfig.AccessToken) && TwitchBotConfig.AutoLogin)
170+
{
171+
var twitchAPI = new TwitchAPI()
172+
{
173+
Helix =
174+
{
175+
Settings =
176+
{
177+
AccessToken = TwitchBotConfig.AccessToken,
178+
ClientId = TwitchBotConfig.ClientId
179+
}
180+
}
181+
};
182+
183+
Task.Run(async () =>
184+
{
185+
var accessTokenResponse = await twitchAPI.Auth.ValidateAccessTokenAsync();
186+
if (accessTokenResponse == null)
187+
{
188+
AnsiConsole.MarkupLine("[red]Twitch AccessToken 驗證失敗,請重新登入[/]");
189+
TwitchBotConfig.AccessToken = "";
190+
return;
191+
}
192+
193+
AnsiConsole.MarkupLineInterpolated($"Twitch AccessToken 驗證成功,過期時間: [darkorange3]{DateTime.Now.AddSeconds(accessTokenResponse.ExpiresIn)}[/]");
194+
TwitchBotConfig.UserLogin = accessTokenResponse.Login;
195+
196+
try
197+
{
198+
File.WriteAllText("TwitchBotConfig.json", JsonConvert.SerializeObject(TwitchBotConfig, Formatting.Indented));
199+
}
200+
catch (Exception)
201+
{
202+
}
203+
204+
TwitchBot.Bot.SetBotCred(TwitchBotConfig.AccessToken, accessTokenResponse.Login);
205+
TwitchBot.Bot.StartBot();
206+
});
207+
}
159208
}
160209

161210
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
@@ -248,7 +297,7 @@ private void ReloadFonts(bool isLoadSystemFonts = false)
248297
{
249298
AnsiConsole.MarkupLineInterpolated($"[red]字型載入失敗: {Path.GetFileName(item)}[/]");
250299
AnsiConsole.WriteException(ex);
251-
}
300+
}
252301
}
253302
}
254303

OBSNowPlayingOverlay/TwitchBotConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ public class TwitchBotConfig
55
public string AccessToken { get; set; } = "";
66
public string ClientId { get; set; } = "uo4hdesne87wg7jut89awcur8fy048";
77
public string UserLogin { get; set; } = "";
8+
public bool AutoLogin { get; set; } = true;
89
}
910
}

OBSNowPlayingOverlay/TwitchBotWindow.xaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<RowDefinition Height="50"/>
1818
<RowDefinition Height="50"/>
1919
<RowDefinition Height="50"/>
20+
<RowDefinition Height="50"/>
2021
<RowDefinition Height="*"/>
2122
</Grid.RowDefinitions>
2223
<Grid.ColumnDefinitions>
@@ -25,7 +26,7 @@
2526
<ColumnDefinition Width="2*"/>
2627
</Grid.ColumnDefinitions>
2728

28-
<Wpf:WebView2 x:Name="webView" Grid.Row="0" Grid.ColumnSpan="1" Grid.Column="2" Grid.RowSpan="8" Margin="5,10,10,10" />
29+
<Wpf:WebView2 x:Name="webView" Grid.Row="0" Grid.ColumnSpan="1" Grid.Column="2" Grid.RowSpan="9" Margin="5,10,10,10" />
2930

3031
<Label VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="10,0,5,0" Grid.ColumnSpan="2">
3132
<Hyperlink NavigateUri="https://github.com/konnokai/OBSNowPlayingOverlay/blob/master/HOW_TO_USE_TWITCH_BOT.md" RequestNavigate="Hyperlink_RequestNavigate">
@@ -44,9 +45,11 @@
4445
<Label Content="UserLogin" Grid.Row="4" HorizontalAlignment="Stretch" Margin="10,0,5,0"/>
4546
<TextBox x:Name="txt_UserLogin" Grid.Column="1" Margin="5,0,5,0" Grid.Row="4" VerticalAlignment="Center" IsEnabled="False" Grid.ColumnSpan="1"/>
4647

47-
<Button x:Name="btn_CheckAccessToken" Content="驗證 AccessToken 是否有效" Grid.RowSpan="1" Grid.Row="5" HorizontalAlignment="Stretch" Margin="10,0,5,0" Grid.ColumnSpan="2" IsEnabled="False" Click="btn_CheckAccessToken_Click"/>
48+
<CheckBox x:Name="cb_AutoLoginBot" Content="自動登入 Twitch Bot" Grid.Row="5" Grid.ColumnSpan="2" Margin="10,0,10,0" HorizontalAlignment="Center" IsChecked="True" Checked="cb_AutoLoginBot_Checked" Unchecked="cb_AutoLoginBot_Unchecked"/>
49+
50+
<Button x:Name="btn_CheckAccessToken" Content="驗證 AccessToken 是否有效" Grid.RowSpan="1" Grid.Row="6" HorizontalAlignment="Stretch" Margin="10,0,5,0" Grid.ColumnSpan="2" IsEnabled="False" Click="btn_CheckAccessToken_Click"/>
4851

49-
<Button x:Name="btn_StartBot" Content="啟動 Bot" HorizontalAlignment="Stretch" Grid.Row="6" Margin="10,0,5,0" IsEnabled="False" Click="btn_StartBot_Click"/>
50-
<Button x:Name="btn_StopBot" Grid.Column="1" Content="停止 Bot" HorizontalAlignment="Stretch" Grid.RowSpan="1" Grid.Row="6" VerticalAlignment="Center" Margin="5,0,5,0" IsEnabled="False" Click="btn_StopBot_Click"/>
52+
<Button x:Name="btn_StartBot" Content="啟動 Bot" HorizontalAlignment="Stretch" Grid.Row="7" Margin="10,0,5,0" IsEnabled="False" Click="btn_StartBot_Click"/>
53+
<Button x:Name="btn_StopBot" Grid.Column="1" Content="停止 Bot" HorizontalAlignment="Stretch" Grid.RowSpan="1" Grid.Row="7" VerticalAlignment="Center" Margin="5,0,5,0" IsEnabled="False" Click="btn_StopBot_Click"/>
5154
</Grid>
5255
</Window>

OBSNowPlayingOverlay/TwitchBotWindow.xaml.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public TwitchBotWindow(TwitchBotConfig twitchBotConfig)
4949
{
5050
txt_UserLogin.Text = _twitchBotConfig.UserLogin;
5151
});
52+
cb_AutoLoginBot.Dispatcher.Invoke(() =>
53+
{
54+
cb_AutoLoginBot.IsChecked = _twitchBotConfig.AutoLogin;
55+
});
5256

5357
if (TwitchBot.Bot.IsConnect.HasValue && TwitchBot.Bot.IsConnect.Value)
5458
{
@@ -258,5 +262,15 @@ private void btn_StopBot_Click(object sender, RoutedEventArgs e)
258262
btn_CheckAccessToken.IsEnabled = true;
259263
});
260264
}
265+
266+
private void cb_AutoLoginBot_Checked(object sender, RoutedEventArgs e)
267+
{
268+
SettingWindow.TwitchBotConfig.AutoLogin = true;
269+
}
270+
271+
private void cb_AutoLoginBot_Unchecked(object sender, RoutedEventArgs e)
272+
{
273+
SettingWindow.TwitchBotConfig.AutoLogin = false;
274+
}
261275
}
262276
}

0 commit comments

Comments
 (0)