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

Allow for custom Imgur Client Ids #149

Merged
merged 1 commit into from
Jul 16, 2022
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
22 changes: 16 additions & 6 deletions DiscordBee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -75,19 +77,16 @@ 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;
_settingsWindow = new SettingsWindow(this, _settings);

_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<IAssetUploader>{ 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);

Expand All @@ -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<IAssetUploader> { actualUploader });
}

private void ShowUploaderHealth(object sender, EventArgs e)
{
_uploaderStatusWindow?.Show();
Expand All @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SettingChangedEventArgs> SettingChanged;
Expand Down Expand Up @@ -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
Expand Down
39 changes: 32 additions & 7 deletions UI/SettingsWindow.Designer.cs

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

19 changes: 17 additions & 2 deletions UI/SettingsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -184,6 +198,7 @@ bool validateUri()
private void ResetErrorIndications()
{
textBoxDiscordAppId.BackColor = SystemColors.Window;
textBoxImgurClientId.BackColor = SystemColors.Window;
customButtonUrl.BackColor = Color.FromArgb(114, 137, 218);
}

Expand Down