Compare commits
No commits in common. "master" and "1.0" have entirely different histories.
@ -1,27 +0,0 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DiscordMediaRP;
|
||||
|
||||
public struct Config
|
||||
{
|
||||
public required string DiscordKey;
|
||||
public LogLevel? LogLevel;
|
||||
public string? LargeImageKey;
|
||||
public bool? UseSpotify;
|
||||
public string[]? WebbrowserIgnoreSites;
|
||||
|
||||
public Config WithDiscordKey(string key)
|
||||
{
|
||||
return this with { DiscordKey = key };
|
||||
}
|
||||
|
||||
public Config WithLargeImageKey(string key)
|
||||
{
|
||||
return this with { LargeImageKey = key };
|
||||
}
|
||||
|
||||
public Config WithLogLevel(LogLevel level)
|
||||
{
|
||||
return this with { LogLevel = level };
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Reflection;
|
||||
using Windows.Media;
|
||||
using Windows.Media.Control;
|
||||
using DiscordRPC;
|
||||
@ -21,9 +20,8 @@ public class DisMediaRP : IDisposable
|
||||
private readonly DiscordRpcClient _discordRpcClient;
|
||||
private RichPresence _currentStatus;
|
||||
private bool _running = true;
|
||||
private Config config;
|
||||
|
||||
private static RichPresence DefaultPresence(string? largeImageKey)
|
||||
private static RichPresence DefaultPresence(string largeImageKey)
|
||||
{
|
||||
return new RichPresence()
|
||||
{
|
||||
@ -31,7 +29,7 @@ public class DisMediaRP : IDisposable
|
||||
State = "https://github.com/C9Glax/DiscordMediaRichPresence",
|
||||
Assets = new()
|
||||
{
|
||||
LargeImageKey = largeImageKey ?? "",
|
||||
LargeImageKey = largeImageKey,
|
||||
SmallImageKey = "music",
|
||||
LargeImageText = "C9Glax/DiscordMediaRichPresence",
|
||||
SmallImageText = "https://www.flaticon.com/de/autoren/alfanz"
|
||||
@ -39,12 +37,15 @@ public class DisMediaRP : IDisposable
|
||||
};
|
||||
}
|
||||
|
||||
public DisMediaRP(Config config)
|
||||
public DisMediaRP(string applicationId, LogLevel? logLevel, string largeImageKey = "cat") : this(applicationId, new Logger(logLevel ?? LogLevel.Information), largeImageKey)
|
||||
{
|
||||
this.config = config;
|
||||
this._logger = new Logger(config.LogLevel ?? LogLevel.Information);
|
||||
this._currentStatus = DefaultPresence(config.LargeImageKey);
|
||||
this._discordRpcClient = new DiscordRpcClient(config.DiscordKey, logger: new DisLogger(this._logger));
|
||||
}
|
||||
|
||||
public DisMediaRP(string applicationId, ILogger? logger = null, string largeImageKey = "cat")
|
||||
{
|
||||
this._logger = logger;
|
||||
this._currentStatus = DefaultPresence(largeImageKey);
|
||||
this._discordRpcClient = new DiscordRpcClient(applicationId, logger: new DisLogger(this._logger));
|
||||
this._discordRpcClient.Initialize();
|
||||
this._discordRpcClient.OnError += (sender, args) =>
|
||||
{
|
||||
@ -60,7 +61,7 @@ public class DisMediaRP : IDisposable
|
||||
{
|
||||
if (mediaSession is null)
|
||||
{
|
||||
this._currentStatus = DefaultPresence(config.LargeImageKey);
|
||||
this._currentStatus = DefaultPresence(largeImageKey);
|
||||
}
|
||||
|
||||
this._discordRpcClient.SetPresence(this._currentStatus);
|
||||
@ -92,9 +93,6 @@ public class DisMediaRP : IDisposable
|
||||
this._logger?.LogDebug(ObjectToString(mediaSession));
|
||||
this._logger?.LogDebug(ObjectToString(mediaProperties));
|
||||
|
||||
if (!UseMediaSession(mediaSession))
|
||||
return;
|
||||
|
||||
string details = $"{mediaProperties.Title}";
|
||||
if (mediaProperties.Artist.Length > 0)
|
||||
details += $" - {mediaProperties.Artist}";
|
||||
@ -110,9 +108,6 @@ public class DisMediaRP : IDisposable
|
||||
this._logger?.LogDebug(ObjectToString(mediaSession));
|
||||
this._logger?.LogDebug(ObjectToString(playbackInfo));
|
||||
|
||||
if (!UseMediaSession(mediaSession))
|
||||
return;
|
||||
|
||||
string playbackState = playbackInfo.PlaybackStatus switch
|
||||
{
|
||||
GlobalSystemMediaTransportControlsSessionPlaybackStatus.Paused => "pause",
|
||||
@ -141,9 +136,6 @@ public class DisMediaRP : IDisposable
|
||||
this._logger?.LogDebug(ObjectToString(mediaSession));
|
||||
this._logger?.LogDebug(ObjectToString(timelineProperties));
|
||||
|
||||
if (!UseMediaSession(mediaSession))
|
||||
return;
|
||||
|
||||
if (timelineProperties.LastUpdatedTime < DateTimeOffset.UnixEpoch)
|
||||
return;
|
||||
|
||||
@ -173,27 +165,6 @@ public class DisMediaRP : IDisposable
|
||||
this._discordRpcClient.SetPresence(this._currentStatus);
|
||||
}
|
||||
|
||||
private bool UseMediaSession(MediaManager.MediaSession mediaSession)
|
||||
{
|
||||
string processId = mediaSession.ControlSession.SourceAppUserModelId;
|
||||
if (processId == "spotify.exe")
|
||||
return config.UseSpotify ?? true;
|
||||
|
||||
if (processId == "firefox.exe")
|
||||
{
|
||||
string[] windowNames = Process.GetProcesses().Where(proc => processId.Contains(proc.ProcessName, StringComparison.InvariantCultureIgnoreCase)).Select(proc => proc.MainWindowTitle).ToArray();
|
||||
return !windowNames.Any(name =>
|
||||
{
|
||||
foreach (string site in config.WebbrowserIgnoreSites ?? Array.Empty<string>())
|
||||
if (name.Contains(site, StringComparison.InvariantCultureIgnoreCase))
|
||||
return true;
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_mediaManager.Dispose();
|
||||
|
@ -11,7 +11,6 @@
|
||||
<PackageReference Include="DiscordRichPresence" Version="1.2.1.24" />
|
||||
<PackageReference Include="Dubya.WindowsMediaController" Version="2.5.3" />
|
||||
<PackageReference Include="GlaxLogger" Version="1.0.6" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -2,40 +2,31 @@
|
||||
|
||||
using DiscordMediaRP;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
Config? c = null;
|
||||
if (File.Exists("config.json"))
|
||||
c = JsonConvert.DeserializeObject<Config>(File.ReadAllText("config.json"));
|
||||
|
||||
int discordKeyIndex = Array.IndexOf(args, "-d");
|
||||
if (discordKeyIndex > -1)
|
||||
if (discordKeyIndex + 1 < args.Length)
|
||||
if (c is null)
|
||||
c = new Config()
|
||||
{
|
||||
DiscordKey = args[discordKeyIndex + 1]
|
||||
};
|
||||
else
|
||||
c = c.Value.WithDiscordKey(args[discordKeyIndex + 1]);
|
||||
else
|
||||
throw new IndexOutOfRangeException("No Discord ApplicationKey provided");
|
||||
else if(c is null)
|
||||
throw new ArgumentNullException(nameof(Config.DiscordKey));
|
||||
|
||||
|
||||
int loglevelIndex = Array.IndexOf(args, "-l");
|
||||
LogLevel? level = null;
|
||||
if(loglevelIndex > -1)
|
||||
if (loglevelIndex + 1 < args.Length)
|
||||
c = c.Value.WithLogLevel(Enum.Parse<LogLevel>(args[loglevelIndex + 1]));
|
||||
if(loglevelIndex + 1 < args.Length)
|
||||
level = loglevelIndex < args.Length ? Enum.Parse<LogLevel>(args[loglevelIndex + 1]) : null;
|
||||
else
|
||||
throw new IndexOutOfRangeException(nameof(loglevelIndex));
|
||||
|
||||
int discordKeyIndex = Array.IndexOf(args, "-d");
|
||||
string discordKey;
|
||||
if (discordKeyIndex > -1)
|
||||
if(discordKeyIndex + 1 < args.Length)
|
||||
discordKey = args[discordKeyIndex + 1];
|
||||
else
|
||||
throw new IndexOutOfRangeException("No Discord ApplicationKey provided");
|
||||
else
|
||||
throw new ArgumentNullException(nameof(discordKey));
|
||||
|
||||
int imageKeyIndex = Array.IndexOf(args, "-i");
|
||||
string imageKey = "cat";
|
||||
if(imageKeyIndex > -1)
|
||||
if (imageKeyIndex + 1 < args.Length)
|
||||
c = c.Value.WithLargeImageKey(args[imageKeyIndex + 1]);
|
||||
imageKey = args[imageKeyIndex + 1];
|
||||
else
|
||||
throw new IndexOutOfRangeException(nameof(imageKeyIndex));
|
||||
|
||||
DisMediaRP _ = new (c.Value);
|
||||
DisMediaRP _ = new (discordKey, level, imageKey);
|
28
README.md
28
README.md
@ -1,25 +1,11 @@
|
||||
# DiscordMediaRichPresence
|
||||
|
||||
## Usage
|
||||
`DiscordMediaRP.exe [-d <Your ApplicationKey>] [-l Debug]`
|
||||
`DiscordMediaRP.exe -d <Your AppliocationKey> [-l Debug]`
|
||||
|
||||
If you omit the ApplicationKey as argument, you need to use a `config.json`:
|
||||
```json
|
||||
{
|
||||
"DiscordKey": "<Your ApplicationKey>",
|
||||
"UseSpotify": false,
|
||||
"WebbrowserIgnoreSites": [
|
||||
"Youtube",
|
||||
"Reddit"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Your Discord-Application should have 5 Assets:
|
||||
* cat
|
||||
* play
|
||||
* pause
|
||||
* stop
|
||||
* music
|
||||
|
||||
![image](https://github.com/C9Glax/DiscordMediaRichPresence/assets/13404778/f28c1c4a-8297-4d99-a0ec-0538ffdd427b)
|
||||
Your Application should have 5 Assets:
|
||||
- cat
|
||||
- play
|
||||
- pause
|
||||
- stop
|
||||
- music
|
Loading…
Reference in New Issue
Block a user