Skip to content

Commit

Permalink
Merge pull request #153 from nwesterhausen/dev
Browse files Browse the repository at this point in the history
2.1.3 fix
  • Loading branch information
nwesterhausen authored Dec 8, 2022
2 parents 45063af + 78b3890 commit bf15b71
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"release",
"build",
"docs",
"media"
"media",
"configs",
"leaderboards"
]
}
6 changes: 6 additions & 0 deletions Metadata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ See the [current roadmap](https://github.com/nwesterhausen/valheim-discordconnec

## Changelog

### Version 2.1.3

Fixes:

- Extra JSON data was getting sent to Discord when some LiteDB lookups were happening

### Version 2.1.2

Mistlands update.
Expand Down
2 changes: 1 addition & 1 deletion Metadata/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "DiscordConnector",
"version_number": "2.1.2",
"version_number": "2.1.3",
"website_url": "https://discordconnector.valheim.nwest.games/",
"description": "Connects your Valheim server to a Discord webhook. Works for both dedicated and client-hosted servers.",
"dependencies": ["denikson-BepInExPack_Valheim-5.4.1901"]
Expand Down
2 changes: 1 addition & 1 deletion Metadata/thunderstore.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ schemaVersion = "0.0.1"
[package]
namespace = "nwesterhausen"
name = "DiscordConnector"
versionNumber = "2.1.2"
versionNumber = "2.1.3"
description = "Connects your Valheim server to a Discord webhook. Works for both dedicated and client-hosted servers."
websiteUrl = "https://discordconnector.valheim.nwest.games/"
containsNsfwContent = false
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Build](https://github.com/nwesterhausen/valheim-discordconnector/actions/workflows/dotnet.yml/badge.svg)](https://github.com/nwesterhausen/valheim-discordconnector/actions/workflows/dotnet.yml)
[![Publish](https://github.com/nwesterhausen/valheim-discordconnector/actions/workflows/publish.yml/badge.svg)](https://github.com/nwesterhausen/valheim-discordconnector/actions/workflows/publish.yml)
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/nwesterhausen/valheim-discordconnector?label=Github%20Release&style=flat&labelColor=%2332393F)](https://github.com/nwesterhausen/valheim-discordconnector/releases/latest)
[![Thunderstore.io](https://img.shields.io/badge/Thunderstore.io-2.1.2-%23375a7f?style=flat&labelColor=%2332393F)](https://valheim.thunderstore.io/package/nwesterhausen/DiscordConnector/)
[![Thunderstore.io](https://img.shields.io/badge/Thunderstore.io-2.1.3-%23375a7f?style=flat&labelColor=%2332393F)](https://valheim.thunderstore.io/package/nwesterhausen/DiscordConnector/)
[![NexusMods](https://img.shields.io/badge/NexusMods-2.0.6-%23D98F40?style=flat&labelColor=%2332393F)](https://www.nexusmods.com/valheim/mods/1551/)

Connect your Valheim server to Discord. ([See website for installation or configuration instructions](https://discordconnector.valheim.nwest.games/)). This plugin is largely based on [valheim-discord-notifier](https://github.com/aequasi/valheim-discord-notifier), but this plugin supports randomized messages, muting players, and Discord message embeds.
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

A full changelog of changes, dating all the way back to the first release.

## Version 2.1.3

Fixes:

- Extra JSON data was getting sent to Discord when some LiteDB lookups were happening

## Version 2.1.2

Mistlands update.
Expand Down
5 changes: 3 additions & 2 deletions src/Config/PluginConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class PluginConfig
private VariableConfig variableConfig;
private LeaderBoardConfig leaderBoardConfig;
public readonly string configPath;
const string ConfigJsonFilename = "config-dump.json";

/// <summary>
/// Valid extensions for the config files, plus a reference for main.
Expand Down Expand Up @@ -273,9 +274,9 @@ public void DumpConfigAsJson()

System.Threading.Tasks.Task.Run(() =>
{
string configDump = Path.Combine(configPath, "config-debug.json");
string configDump = Path.Combine(configPath, ConfigJsonFilename);
File.WriteAllText(configDump, jsonString);
Plugin.StaticLogger.LogDebug("Dumped configuration files to JSON for debugging (if needed).");
Plugin.StaticLogger.LogDebug($"Dumped configuration files to {ConfigJsonFilename}");
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Config/TogglesConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public string ConfigAsJson()
jsonString += $"\"debugEventChanges\":\"{DebugEveryEventChange}\",";
jsonString += $"\"debugDatabaseMethods\":\"{DebugDatabaseMethods}\",";
jsonString += $"\"debugHttpRequestResponses\":\"{DebugHttpRequestResponse}\"";
jsonString += "},";
jsonString += "}";

jsonString += "}";
return jsonString;
Expand Down
24 changes: 22 additions & 2 deletions src/DiscordApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,21 @@ public static void SendMessage(string message)
/// <param name="fields">Discord fields as defined in the API, as Tuples (field name, value)</param>
public static void SendMessageWithFields(string content = null, List<Tuple<string, string>> fields = null)
{

// Guard against null/empty calls
if (string.IsNullOrEmpty(content) && fields == null)
{
content = "Uh-oh! An unexpectedly empty message was sent!";
}

// Begin the payload object
string payloadString = "{";
// If we have fields at all, put them as embedded fields
if (fields != null)
{
// Fields go under embed as array
payloadString += "\"embeds\":[{\"fields\":[";

// Convert the fields into JSON Strings
List<string> fieldStrings = new List<string>();
foreach (Tuple<string, string> t in fields)
{
Expand All @@ -72,19 +77,29 @@ public static void SendMessageWithFields(string content = null, List<Tuple<strin
value = t.Item2
}));
}

// Put the field JSON strings into our payload object
payloadString += string.Join(",", fieldStrings.ToArray());
payloadString += "]}]";

// Cautiously put a comma if there is content to add to the payload as well
if (content != null)
{
payloadString += ",";
}
}

// If there is any content
if (content != null)
{
// Append the content to the payload
payloadString += $"\"content\":\"{content}\"";
}

// Finish the payload JSON
payloadString += "}";

// Use our pre-existing method to send serialized JSON to discord
SendSerializedJson(payloadString);
}

Expand All @@ -95,15 +110,18 @@ public static void SendMessageWithFields(string content = null, List<Tuple<strin
internal static void SendSerializedJson(string serializedJson)
{
Plugin.StaticLogger.LogDebug($"Trying webhook with payload: {serializedJson}");

// Guard against unset webhook
if (string.IsNullOrEmpty(Plugin.StaticConfig.WebHookURL))
{
Plugin.StaticLogger.LogInfo("No webhook set, not sending message.");
return;
}

// Responsible for sending a JSON string to the webhook.
byte[] byteArray = Encoding.UTF8.GetBytes(serializedJson);


// Create a web request to send the payload to discord
WebRequest request = WebRequest.Create(Plugin.StaticConfig.WebHookURL);
request.Method = "POST";
request.ContentType = "application/json";
Expand All @@ -112,10 +130,12 @@ internal static void SendSerializedJson(string serializedJson)
// Dispatch the request to discord and the response processing to an async task
Task.Run(() =>
{
// We have to write the data to the request
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

// Wait for a response to the web request
WebResponse response = request.GetResponse();
if (Plugin.StaticConfig.DebugHttpRequestResponse)
{
Expand Down
2 changes: 1 addition & 1 deletion src/PluginInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static class PluginInfo
{
public const string PLUGIN_ID = "games.nwest.valheim.discordconnector";
public const string PLUGIN_NAME = "Valheim Discord Connector";
public const string PLUGIN_VERSION = "2.1.2";
public const string PLUGIN_VERSION = "2.1.3";
public const string PLUGIN_REPO_SHORT = "github: nwesterhausen/valheim-discordconnector";
public const string PLUGIN_AUTHOR = "Nicholas Westerhausen";
public const string SHORT_PLUGIN_ID = "discordconnector";
Expand Down
10 changes: 0 additions & 10 deletions src/Records/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,6 @@ private List<CountResult> AllTimeOnlineRecordsGrouped()
results.Add(new CountResult(player.CharacterName, (int)onlineTime.TotalSeconds));
}

Task.Run(() =>
{
DiscordApi.SendMessage(JsonConvert.SerializeObject(results));
});

return results;
}

Expand Down Expand Up @@ -531,11 +526,6 @@ private List<CountResult> TimeOnlineRecordsWhereDate(System.DateTime startDate,
results.Add(new CountResult(player.CharacterName, (int)onlineTime.TotalSeconds));
}

Task.Run(() =>
{
DiscordApi.SendMessage(JsonConvert.SerializeObject(results));
});

return results;
}

Expand Down

0 comments on commit bf15b71

Please sign in to comment.