Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autologin, fix friends displaying #20

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Naticord/Loading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Naticord
{
public partial class Loading : Form
{
private Naticord parentForm;
//private Naticord parentForm;
private Random random = new Random();
private List<string> quotes = new List<string>
{
Expand All @@ -20,10 +20,10 @@ public partial class Loading : Form
"i have severe brain damage afte reading this code - pat"
};

public Loading(Naticord parentForm)
public Loading(/*Naticord parentForm*/)
{
InitializeComponent();
this.parentForm = parentForm;
//this.parentForm = parentForm;
}

public void UpdateProgress(string message, int progress)
Expand Down
40 changes: 31 additions & 9 deletions Naticord/Login.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;

namespace Naticord
{
Expand All @@ -13,6 +14,7 @@ public partial class Login : Form
public Login()
{
InitializeComponent();
CheckToken();
}

private void loginButton_Click(object sender, EventArgs e)
Expand All @@ -28,7 +30,7 @@ private void loginButton_Click(object sender, EventArgs e)
PerformLogin(accessToken);
}

private void PerformLogin(string accessToken)
private void PerformLogin(string accessToken, bool isAutomated = false)
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; // TLS 1.2

Expand All @@ -40,12 +42,10 @@ private void PerformLogin(string accessToken)
{
string userProfileJson = webClient.DownloadString("https://discord.com/api/v9/users/@me");

SaveToken(accessToken);
if(!isAutomated) SaveToken(accessToken);

Naticord naticordForm = new Naticord(accessToken);
Naticord naticordForm = new Naticord(this, accessToken);
naticordForm.Show();

this.Hide();
}
catch (WebException ex)
{
Expand All @@ -54,25 +54,47 @@ private void PerformLogin(string accessToken)
}
}

private void SaveToken(string accessToken)
private void CheckToken()
{
try
{

string homeDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

string filePath = Path.Combine(homeDirectory, TokenFileName);

File.WriteAllText(filePath, "token=" + accessToken);
if (File.Exists(filePath))
{
foreach (string line in File.ReadLines(filePath))
{
if (line.Contains("token="))
{
PerformLogin(line.Replace("token=", ""), true);
return;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Failed to save token: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void Login_Load(object sender, EventArgs e)
private void SaveToken(string accessToken)
{
// this isn't really needed but at the same time it isnt?
try
{
string homeDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

string filePath = Path.Combine(homeDirectory, TokenFileName);

File.WriteAllText(filePath, "token=" + accessToken);
}
catch (Exception ex)
{
MessageBox.Show("Failed to save token: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
Expand Down
18 changes: 13 additions & 5 deletions Naticord/Naticord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public partial class Naticord : Form
{
private const string DiscordApiBaseUrl = "https://discord.com/api/v9/";
private WebSocketClient websocketClient;
private Login parentLogin;
private string accessToken;
private string currentChannelId;
private Dictionary<string, string> userCache = new Dictionary<string, string>();
Expand All @@ -26,9 +27,10 @@ public partial class Naticord : Form
public string AccessToken { get { return accessToken; } set => accessToken = value; }
public string CurrentChannelId { get => currentChannelId; set => currentChannelId = value; }

public Naticord(string accessToken)
public Naticord(Login parentLogin, string accessToken)
{
InitializeComponent();
this.parentLogin = parentLogin;
AccessToken = accessToken;
tabControl.SelectedIndexChanged += TabControl_SelectedIndexChanged;
friendListBox.SelectedIndexChanged += FriendList_SelectedIndexChanged;
Expand All @@ -40,6 +42,12 @@ public Naticord(string accessToken)
websocketClient = new WebSocketClient(accessToken, this);
}

protected override void OnShown(EventArgs e)
{
base.OnShown(e);
parentLogin.Hide();
}

private void Naticord_Load(object sender, EventArgs e)
{
PopulateUserProfile();
Expand Down Expand Up @@ -73,10 +81,9 @@ private void PopulateFriendsTab()
dynamic friends = GetApiResponse("users/@me/relationships");
foreach (var friend in friends)
{
string username = friend.user.global_name ?? friend.user.username;
string nickname = friend.nickname;
string displayUsername = string.IsNullOrEmpty(nickname) ? username : nickname;
var friendItem = new ListViewItem($"{displayUsername}")
string username;
if(friend.nickname != null) { username = friend.nickname; } else if(friend.user.global_name != null) { username = friend.user.global_name; } else { username = friend.user.username; } // His old implementation wasnt working for some reason
var friendItem = new ListViewItem(username)
{
Tag = (string)friend.user.id
};
Expand Down Expand Up @@ -412,6 +419,7 @@ protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
websocketClient.CloseWebSocket();
parentLogin.Close();
}

public void UpdateMessageBoxWithFormatting(string message)
Expand Down
Loading