From 17620da74c56f55e12d3e5fd7999daed06ae04bb Mon Sep 17 00:00:00 2001 From: Stefan Lengauer Date: Sat, 16 Jul 2022 23:31:36 +0200 Subject: [PATCH] Allow for custom Imgur Client Ids --- DiscordBee.cs | 22 ++++++++++++++------ README.md | 9 ++++++++ Settings.cs | 17 +++++++++++++++ UI/SettingsWindow.Designer.cs | 39 ++++++++++++++++++++++++++++------- UI/SettingsWindow.cs | 19 +++++++++++++++-- 5 files changed, 91 insertions(+), 15 deletions(-) diff --git a/DiscordBee.cs b/DiscordBee.cs index b865a73..798d1f9 100644 --- a/DiscordBee.cs +++ b/DiscordBee.cs @@ -28,6 +28,8 @@ public partial class Plugin private Settings _settings; private SettingsWindow _settingsWindow; private readonly Timer _updateTimer = new Timer(300); + private string _imgurAssetCachePath; + private string _imgurAlbum; public Plugin() { @@ -75,8 +77,8 @@ public PluginInfo Initialise(IntPtr apiInterfacePtr) var workingDir = _mbApiInterface.Setting_GetPersistentStoragePath() + _about.Name; var settingsFilePath = $"{workingDir}\\{_about.Name}.settings"; - var imgurAssetCachePath = $"{workingDir}\\{_about.Name}-Imgur.cache"; - var imgurAlbum = $"{workingDir}\\{_about.Name}-Imgur.album"; + _imgurAssetCachePath = $"{workingDir}\\{_about.Name}-Imgur.cache"; + _imgurAlbum = $"{workingDir}\\{_about.Name}-Imgur.album"; _settings = Settings.GetInstance(settingsFilePath); _settings.SettingChanged += SettingChangedCallback; @@ -84,10 +86,7 @@ public PluginInfo Initialise(IntPtr apiInterfacePtr) _discordClient.ArtworkUploadEnabled = _settings.UploadArtwork; _discordClient.DiscordId = _settings.DiscordAppId; - ImgurUploader assetUploader = new ImgurUploader(imgurAlbum, "34debbc0ae4077e"); - _discordClient.AssetManager = new AssetManager(new ResizingUploader(new CachingUploader(imgurAssetCachePath, assetUploader))); - - _uploaderStatusWindow = new UploaderHealth(new List{ assetUploader }); + UpdateAssetManager(_imgurAssetCachePath, new ImgurUploader(_imgurAlbum, _settings.ImgurClientId)); ToolStripMenuItem mainMenuItem = (ToolStripMenuItem)_mbApiInterface.MB_AddMenuItem($"mnuTools/{_about.Name}", null, null); mainMenuItem.DropDown.Items.Add("Uploader Health", null, ShowUploaderHealth); @@ -103,6 +102,13 @@ public PluginInfo Initialise(IntPtr apiInterfacePtr) return _about; } + private void UpdateAssetManager(string cachePath, IAssetUploader actualUploader) + { + _discordClient.AssetManager = new AssetManager(new ResizingUploader(new CachingUploader(cachePath, actualUploader))); + _uploaderStatusWindow?.Dispose(); + _uploaderStatusWindow = new UploaderHealth(new List { actualUploader }); + } + private void ShowUploaderHealth(object sender, EventArgs e) { _uploaderStatusWindow?.Show(); @@ -124,6 +130,10 @@ private void SettingChangedCallback(object sender, Settings.SettingChangedEventA { _discordClient.ArtworkUploadEnabled = _settings.UploadArtwork; } + if (e.SettingProperty.Equals("ImgurClientId")) + { + UpdateAssetManager(_imgurAssetCachePath, new ImgurUploader(_imgurAlbum, _settings.ImgurClientId)); + } } public string GetVersionString() diff --git a/README.md b/README.md index 88c792d..dfc5a21 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,15 @@ That means you need to upload one picture for each asset, all assets must be pre When you are done uploading the assets you can take your `Application ID` from the dashboard and insert it in the DiscordBee settings. Now DiscordBee will use your custom Name and assets. +### Custom Imgur Client ID + +To circumvent the global rate limits you can use your own Imgur Client ID. This is only recommended if you experience that the uploader health is immediately (after first upload) false even if you haven't used DiscordBee for a day. + +To get a client ID you must register an application at Imgur. You need an account for that. +Go to the [Registration Page](https://api.imgur.com/oauth2/addclient) and fill out the form by selecting the 3rd option for `Authorization type` (Anonymous). You will receive a Client Id and a secret, you will only need the ID. + +Copy the ID and insert it in the DiscordBee Settings. The new Client ID will be used after changing the current song. + ## Screenshots ![Small presence](Screenshots/small_presence.png) diff --git a/Settings.cs b/Settings.cs index 2bd23e9..1c8a653 100644 --- a/Settings.cs +++ b/Settings.cs @@ -26,6 +26,7 @@ public class Settings {"ButtonUrl", "https://www.last.fm/music/[Artist]/_/[TrackTitle]"}, {"DiscordAppId", "409394531948298250"}, // prod //{"DiscordAppId", "408977077799354379"}, // dev + {"ImgurClientId", "09bef4c058080cd"}, }; public event EventHandler SettingChanged; @@ -106,6 +107,22 @@ public string DiscordAppId } } + [DataMember] private string _imgurClientId; + + public string ImgurClientId + { + get => _imgurClientId ?? defaults["ImgurClientId"]; + set + { + if (value?.Equals(defaults["ImgurClientId"]) == true) + { + _imgurClientId = null; + return; + } + SetIfChanged("_imgurClientId", value); + } + } + [DataMember] private bool? _updatePresenceWhenStopped; public bool UpdatePresenceWhenStopped diff --git a/UI/SettingsWindow.Designer.cs b/UI/SettingsWindow.Designer.cs index 4a891a5..db49a67 100644 --- a/UI/SettingsWindow.Designer.cs +++ b/UI/SettingsWindow.Designer.cs @@ -60,6 +60,8 @@ private void InitializeComponent() this.customButtonUrl = new System.Windows.Forms.TextBox(); this.label8 = new System.Windows.Forms.Label(); this.customButtonLabel = new System.Windows.Forms.TextBox(); + this.labelImgurClientId = new System.Windows.Forms.Label(); + this.textBoxImgurClientId = new System.Windows.Forms.TextBox(); this.panel2.SuspendLayout(); this.panel3.SuspendLayout(); this.panel4.SuspendLayout(); @@ -293,6 +295,8 @@ private void InitializeComponent() this.panel2.AutoSize = true; this.panel2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.panel2.BackColor = System.Drawing.Color.Transparent; + this.panel2.Controls.Add(this.labelImgurClientId); + this.panel2.Controls.Add(this.textBoxImgurClientId); this.panel2.Controls.Add(this.customButtonToggle); this.panel2.Controls.Add(this.checkBoxShowOnlyNonPlayingState); this.panel2.Controls.Add(this.checkBoxShowPlayState); @@ -309,7 +313,7 @@ private void InitializeComponent() this.panel2.Location = new System.Drawing.Point(0, 157); this.panel2.MinimumSize = new System.Drawing.Size(0, 100); this.panel2.Name = "panel2"; - this.panel2.Size = new System.Drawing.Size(524, 159); + this.panel2.Size = new System.Drawing.Size(524, 204); this.panel2.TabIndex = 1; // // customButtonToggle @@ -331,7 +335,7 @@ private void InitializeComponent() this.checkBoxShowOnlyNonPlayingState.Checked = true; this.checkBoxShowOnlyNonPlayingState.CheckState = System.Windows.Forms.CheckState.Checked; this.checkBoxShowOnlyNonPlayingState.ForeColor = System.Drawing.Color.Black; - this.checkBoxShowOnlyNonPlayingState.Location = new System.Drawing.Point(17, 118); + this.checkBoxShowOnlyNonPlayingState.Location = new System.Drawing.Point(17, 145); this.checkBoxShowOnlyNonPlayingState.Margin = new System.Windows.Forms.Padding(4); this.checkBoxShowOnlyNonPlayingState.Name = "checkBoxShowOnlyNonPlayingState"; this.checkBoxShowOnlyNonPlayingState.Padding = new System.Windows.Forms.Padding(2); @@ -346,7 +350,7 @@ private void InitializeComponent() this.checkBoxShowPlayState.Checked = true; this.checkBoxShowPlayState.CheckState = System.Windows.Forms.CheckState.Checked; this.checkBoxShowPlayState.ForeColor = System.Drawing.Color.Black; - this.checkBoxShowPlayState.Location = new System.Drawing.Point(17, 91); + this.checkBoxShowPlayState.Location = new System.Drawing.Point(17, 118); this.checkBoxShowPlayState.Margin = new System.Windows.Forms.Padding(4); this.checkBoxShowPlayState.Name = "checkBoxShowPlayState"; this.checkBoxShowPlayState.Padding = new System.Windows.Forms.Padding(2); @@ -375,7 +379,7 @@ private void InitializeComponent() this.checkBoxArtworkUpload.AutoSize = true; this.checkBoxArtworkUpload.BackColor = System.Drawing.Color.Transparent; this.checkBoxArtworkUpload.ForeColor = System.Drawing.Color.Black; - this.checkBoxArtworkUpload.Location = new System.Drawing.Point(17, 64); + this.checkBoxArtworkUpload.Location = new System.Drawing.Point(17, 91); this.checkBoxArtworkUpload.Margin = new System.Windows.Forms.Padding(4); this.checkBoxArtworkUpload.Name = "checkBoxArtworkUpload"; this.checkBoxArtworkUpload.Padding = new System.Windows.Forms.Padding(2); @@ -415,7 +419,7 @@ private void InitializeComponent() this.panel3.Controls.Add(this.buttonPlaceholders); this.panel3.Controls.Add(this.buttonSaveClose); this.panel3.Dock = System.Windows.Forms.DockStyle.Bottom; - this.panel3.Location = new System.Drawing.Point(0, 316); + this.panel3.Location = new System.Drawing.Point(0, 361); this.panel3.MinimumSize = new System.Drawing.Size(0, 30); this.panel3.Name = "panel3"; this.panel3.Size = new System.Drawing.Size(524, 30); @@ -490,13 +494,32 @@ private void InitializeComponent() this.customButtonLabel.Size = new System.Drawing.Size(177, 25); this.customButtonLabel.TabIndex = 10; // + // labelImgurClientId + // + this.labelImgurClientId.AutoSize = true; + this.labelImgurClientId.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.labelImgurClientId.ForeColor = System.Drawing.Color.Black; + this.labelImgurClientId.Location = new System.Drawing.Point(14, 65); + this.labelImgurClientId.Name = "labelImgurClientId"; + this.labelImgurClientId.Size = new System.Drawing.Size(88, 15); + this.labelImgurClientId.TabIndex = 19; + this.labelImgurClientId.Text = "Imgur Client ID"; + // + // textBoxImgurClientId + // + this.textBoxImgurClientId.Font = new System.Drawing.Font("Arial", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.textBoxImgurClientId.Location = new System.Drawing.Point(108, 62); + this.textBoxImgurClientId.Name = "textBoxImgurClientId"; + this.textBoxImgurClientId.Size = new System.Drawing.Size(151, 25); + this.textBoxImgurClientId.TabIndex = 20; + // // SettingsWindow // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoSize = true; this.BackColor = System.Drawing.Color.White; - this.ClientSize = new System.Drawing.Size(524, 346); + this.ClientSize = new System.Drawing.Size(524, 391); this.Controls.Add(this.panel2); this.Controls.Add(this.panel3); this.Controls.Add(this.panel4); @@ -549,5 +572,7 @@ private void InitializeComponent() private System.Windows.Forms.CheckBox checkBoxShowTime; private System.Windows.Forms.CheckBox checkBoxShowPlayState; private System.Windows.Forms.CheckBox checkBoxShowOnlyNonPlayingState; - } + private System.Windows.Forms.Label labelImgurClientId; + private System.Windows.Forms.TextBox textBoxImgurClientId; + } } diff --git a/UI/SettingsWindow.cs b/UI/SettingsWindow.cs index 0324df6..bec0a93 100644 --- a/UI/SettingsWindow.cs +++ b/UI/SettingsWindow.cs @@ -58,6 +58,7 @@ private void UpdateValues(Settings settings) textBoxSmallImage.Text = settings.SmallImageText; textBoxSeparator.Text = settings.Separator; textBoxDiscordAppId.Text = settings.DiscordAppId.Equals(Settings.defaults["DiscordAppId"]) ? "" : settings.DiscordAppId; + textBoxImgurClientId.Text = settings.ImgurClientId.Equals(Settings.defaults["ImgurClientId"]) ? "" : settings.ImgurClientId; checkBoxPresenceUpdate.Checked = settings.UpdatePresenceWhenStopped; checkBoxShowTime.Checked = settings.ShowTime; checkBoxShowRemainingTime.Checked = settings.ShowRemainingTime; @@ -101,6 +102,7 @@ private void buttonSaveClose_Click(object sender, EventArgs e) _settings.SmallImageText = textBoxSmallImage.Text; _settings.Separator = textBoxSeparator.Text; _settings.DiscordAppId = string.IsNullOrWhiteSpace(textBoxDiscordAppId.Text) ? null : textBoxDiscordAppId.Text; + _settings.ImgurClientId = string.IsNullOrWhiteSpace(textBoxImgurClientId.Text) ? null : textBoxImgurClientId.Text; _settings.UpdatePresenceWhenStopped = checkBoxPresenceUpdate.Checked; _settings.ShowTime = checkBoxShowTime.Checked; _settings.ShowRemainingTime = checkBoxShowRemainingTime.Checked; @@ -149,12 +151,24 @@ bool validateDiscordId() return true; } - if (checkBoxArtworkUpload.Checked && !validateDiscordId()) + if (textBoxDiscordAppId.Text.Length > 0 && !validateDiscordId()) { return false; } - if (textBoxDiscordAppId.Text.Length > 0 && !validateDiscordId()) + bool validateImgurClientId() + { + if (textBoxImgurClientId.Text.Length != Settings.defaults["ImgurClientId"].Length + || textBoxImgurClientId.Text.Equals(Settings.defaults["ImgurClientId"])) + { + textBoxImgurClientId.BackColor = Color.PaleVioletRed; + return false; + } + textBoxImgurClientId.BackColor = Color.White; + return true; + } + + if (checkBoxArtworkUpload.Checked && textBoxImgurClientId.Text.Length > 0 && !validateImgurClientId()) { return false; } @@ -184,6 +198,7 @@ bool validateUri() private void ResetErrorIndications() { textBoxDiscordAppId.BackColor = SystemColors.Window; + textBoxImgurClientId.BackColor = SystemColors.Window; customButtonUrl.BackColor = Color.FromArgb(114, 137, 218); }