Skip to content

Commit

Permalink
Add autologin, fix friends displaying
Browse files Browse the repository at this point in the history
  • Loading branch information
jukfiuune committed May 19, 2024
1 parent 15d963e commit ee0ce8a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
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

0 comments on commit ee0ce8a

Please sign in to comment.