Skip to content

Commit

Permalink
Merge pull request #170 from nwesterhausen/dev
Browse files Browse the repository at this point in the history
closes #24
  • Loading branch information
nwesterhausen authored Feb 15, 2023
2 parents 7b3173f + 9f5edfd commit 36db004
Show file tree
Hide file tree
Showing 17 changed files with 505 additions and 67 deletions.
18 changes: 18 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

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

## Version 2.1.9

Adds a requested feature for a second webhook. Both webhooks can be configured to accept messages of any type that Discord
Connector sends, and by default (to be non-breaking) they will send all messages (which is the behavior of 2.1.8 and previous).
Some plugins which may make use of Discord Connector's webhook to send messages can use the same method for sending and will
be tagged as 'other' webhook events. For a full list of what webhook events can be configured, see the
[documentation](https://discordconnector.valheim.nwest.games/config/main.html#webhook-events).

Features:

- Adds a second webhook entry
- Adds webhook events configuration entries (webhooks can be configured to only send certain messages)

Fixes:

- Empty leaderboards now send an empty leaderboard (instead of not sending anything)
- Configuration for how to differentiate players may have swapped the Name and NameAndPlayerId definitions

## Version 2.1.8

Fixes:
Expand Down
64 changes: 64 additions & 0 deletions docs/config/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Filename `discordconnector.cfg`
| Option | Default | Description |
| ------------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| Webhook URL | (none) | The main Discord webhook URL to send notifications/messages to. |
| Webhook Events | ALL | Semi-colon separated list of which [events](#webhook-events) to send to the primary webhook. |
| Secondary Webhook URL | (none) | The main Discord webhook URL to send notifications/messages to. |
| Secondary Webhook Events | (none) | Semi-colon separated list of which [events](#webhook-events) to send to the primary webhook. |
| Use fancier discord messages | false | Set to true to enable using embeds in the Discord messages. If left false, all messages will remain plain strings (except for the leaderboard). |
| Allow positions to be sent | true | Set to false to prevent any positions/coordinates from being sent. If this is true, it can be overridden per message in the toggles config file. |
| Ignored players | (none) | List of player names to never send a discord message for (they also won't be tracked in stats). This list should be semicolon (`;`) separated. |
Expand All @@ -25,3 +28,64 @@ If this is false, it takes precedent over the "Send leader board updates" settin

The stat collection database uses the [LiteDB](https://www.litedb.org/) library and if you are so inclined they offer a database gui which you can use to view/modify this database. (Find the LiteDB Editor on their site.)
:::

## Webhook Events

Here is a list of every accepted event for inclusion in the 'webhook event' settings. These correspond pretty directly to the configurations in the messages config.

::: info "Specifying Events ≠ Enabling Messages"
Adding an event to be send for a webhook does not enable Discord Connector to send that event. It only tells Discord Connector *where* to send the event message.

Messages for these events have to be enabled in the toggles config file (as they were previously). Any of these messages which are enabled by default are notated as such in the 'Enabled by Default' column.

Take note that by default, the webhooks are set to 'ALL' which takes any messages which happen and sends them.
:::

| Event Code | Corresponding Trigger | Enabled by Default |
| ---------------- | --------------------------------------------- | ------------------ |
| serverLaunch | Server begins starting up | Yes |
| serverStart | World has been loaded | Yes |
| serverStop | Server beings stopping | Yes |
| serverShutdown | Server has stopped | Yes |
| serverSave | The world has been saved | Yes |
| eventStart | An event starts | Yes |
| eventPaused | An event pauses | Yes |
| eventResumed | An event resumes | Yes |
| eventStop | An event finishes | Yes |
| playerJoin | A player joins the server | Yes |
| playerLeave | A player joins the server | Yes |
| playerShout | A player joins the server | Yes |
| playerPing | A player joins the server | Yes |
| playerDeath | A player joins the server | Yes |
| playerFirstJoin | A new player joins the server | Yes |
| playerFirstLeave | A player leaves the server for the first time | No |
| playerFirstShout | A player shouts in chat for the first time | No |
| playerFirstPing | A player pings the map for the first time | No |
| playerFirstDeath | A player dies for the first time | Yes |

### Leaderboard Events

These are events which represent sending the leaderboards

| Event Code | Corresponding Trigger | Enabled by Default |
| ------------- | ------------------------------ | ------------------ |
| activePlayers | The active players leaderboard | No |
| leaderboard1 | The custom leaderboard 1 | No |
| leaderboard2 | The custom leaderboard 2 | No |
| leaderboard3 | The custom leaderboard 3 | No |
| leaderboard4 | The custom leaderboard 4 | No |
| leaderboard5 | The custom leaderboard 5 | No |

### Special Case Events

These are shorthand to represent multiple events.

| Event Code | Corresponding Trigger | Enabled by Default |
| --------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------ |
| ALL | All messages get sent to this webhook | N/A |
| serverLifecycle | Any server launch, start, stop, shutdown messages are sent to this webhook | N/A |
| eventLifecycle | Any event start, pause, resume, or stop/end messages are sent to this webhook | N/A |
| playerAll | Any messages about player's activity are sent to this webhook | N/A |
| playerFirstAll | Any message about a player's first X are sent to this webhook | N/A |
| leaderboardsAll | Any message for a leaderboard are sent to this webhook | N/A |
| other | A few other mods/plugins utilize Discord Connector's webhook to send messages. Those get tagged as 'Other' events. | N/A |
5 changes: 5 additions & 0 deletions src/Config/CustomLeaderBoardConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,9 @@ public class LeaderBoardConfigReference
public bool Pings;
public bool TimeOnline;
public string DisplayedHeading;

/// <summary>
/// The event to use when sending to the webhook.
/// </summary>
public Webhook.Event WebhookEvent;
}
5 changes: 5 additions & 0 deletions src/Config/LeaderboardConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private void LoadConfig()
Shouts = leaderBoard1.shouts.Value,
Pings = leaderBoard1.pings.Value,
TimeOnline = leaderBoard1.timeOnline.Value,
WebhookEvent = Webhook.Event.Leaderboard1,
},
new LeaderBoardConfigReference
{
Expand All @@ -72,6 +73,7 @@ private void LoadConfig()
Shouts = leaderBoard2.shouts.Value,
Pings = leaderBoard2.pings.Value,
TimeOnline = leaderBoard2.timeOnline.Value,
WebhookEvent = Webhook.Event.Leaderboard2,
},
new LeaderBoardConfigReference
{
Expand All @@ -86,6 +88,7 @@ private void LoadConfig()
Shouts = leaderBoard3.shouts.Value,
Pings = leaderBoard3.pings.Value,
TimeOnline = leaderBoard3.timeOnline.Value,
WebhookEvent = Webhook.Event.Leaderboard3,
},
new LeaderBoardConfigReference
{
Expand All @@ -100,6 +103,7 @@ private void LoadConfig()
Shouts = leaderBoard4.shouts.Value,
Pings = leaderBoard4.pings.Value,
TimeOnline = leaderBoard4.timeOnline.Value,
WebhookEvent = Webhook.Event.Leaderboard4,
},
new LeaderBoardConfigReference
{
Expand All @@ -114,6 +118,7 @@ private void LoadConfig()
Shouts = leaderBoard5.shouts.Value,
Pings = leaderBoard5.pings.Value,
TimeOnline = leaderBoard5.timeOnline.Value,
WebhookEvent = Webhook.Event.Leaderboard5,
}};

}
Expand Down
44 changes: 40 additions & 4 deletions src/Config/MainConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public enum RetrievalDiscernmentMethods
{
[System.ComponentModel.Description(RetrieveBySteamID)]
PlayerId,
[System.ComponentModel.Description(RetrieveByNameAndSteamID)]
Name,
[System.ComponentModel.Description(RetrieveByName)]
Name,
[System.ComponentModel.Description(RetrieveByNameAndSteamID)]
NameAndPlayerId,
}
public const string RetrieveBySteamID = "PlayerId: Treat each PlayerId as a separate player";
Expand All @@ -29,6 +29,9 @@ public enum RetrievalDiscernmentMethods

// Main Settings
private ConfigEntry<string> webhookUrl;
private ConfigEntry<string> webhookUrl2;
private ConfigEntry<string> webhookEvents;
private ConfigEntry<string> webhook2Events;
private ConfigEntry<bool> discordEmbedMessagesToggle;
private ConfigEntry<string> mutedDiscordUserList;
private ConfigEntry<string> mutedDiscordUserListRegex;
Expand All @@ -38,6 +41,9 @@ public enum RetrievalDiscernmentMethods
private ConfigEntry<RetrievalDiscernmentMethods> playerLookupPreference;
private ConfigEntry<bool> allowNonPlayerShoutLogging;

private WebhookEntry primaryWebhook;
private WebhookEntry secondaryWebhook;

public MainConfig(ConfigFile configFile)
{
config = configFile;
Expand All @@ -51,6 +57,9 @@ public MainConfig(ConfigFile configFile)
{
mutedPlayersRegex = new Regex(mutedDiscordUserListRegex.Value);
}

primaryWebhook = new WebhookEntry { Url = webhookUrl.Value, FireOnEvents = Webhook.StringToEventList(webhookEvents.Value) };
secondaryWebhook = new WebhookEntry { Url = webhookUrl2.Value, FireOnEvents = Webhook.StringToEventList(webhook2Events.Value) };
}

public void ReloadConfig()
Expand All @@ -67,6 +76,9 @@ public void ReloadConfig()
{
mutedPlayersRegex = new Regex(mutedDiscordUserListRegex.Value);
}

primaryWebhook = new WebhookEntry { Url = webhookUrl.Value, FireOnEvents = Webhook.StringToEventList(webhookEvents.Value) };
secondaryWebhook = new WebhookEntry { Url = webhookUrl2.Value, FireOnEvents = Webhook.StringToEventList(webhook2Events.Value) };
}

private void LoadConfig()
Expand All @@ -78,6 +90,26 @@ private void LoadConfig()
"Discord channel webhook URL. For instructions, reference the 'MAKING A WEBHOOK' section of " + Environment.NewLine +
"Discord's documentation: https://support.Discord.com/hc/en-us/articles/228383668-Intro-to-Webhook");

webhookEvents = config.Bind<string>(MAIN_SETTINGS,
"Webhook Events",
"ALL",
"Specify a subset of possible events to send to the primary webhook. Previously all events would go to the primary webhook." + Environment.NewLine +
"Format should be the keyword 'ALL' or a semi-colon separated list, e.g. 'serverLifecycle;playerAll;playerFirstAll;leaderboardsAll;'" + Environment.NewLine +
"Full list of valid options here: https://discordconnector.valheim.nwest.games/config/main.html#webhook-events");

webhookUrl2 = config.Bind<string>(MAIN_SETTINGS,
"Secondary Webhook URL",
"",
"Discord channel webhook URL. For instructions, reference the 'MAKING A WEBHOOK' section of " + Environment.NewLine +
"Discord's documentation: https://support.Discord.com/hc/en-us/articles/228383668-Intro-to-Webhook");

webhook2Events = config.Bind<string>(MAIN_SETTINGS,
"Secondary Webhook Events",
"ALL",
"Specify a subset of possible events to send to the primary webhook. Previously all events would go to the primary webhook." + Environment.NewLine +
"Format should be the keyword 'ALL' or a semi-colon separated list, e.g. 'serverLaunch;serverStart;serverSave;'" + Environment.NewLine +
"Full list of valid options here: https://discordconnector.valheim.nwest.games/config/main.html#webhook-events");

discordEmbedMessagesToggle = config.Bind<bool>(MAIN_SETTINGS,
"Use fancier discord messages",
false,
Expand Down Expand Up @@ -133,7 +165,10 @@ public string ConfigAsJson()
{
string jsonString = "{";
jsonString += "\"discord\":{";
jsonString += $"\"webhook\":\"{(string.IsNullOrEmpty(WebHookURL) ? "unset" : "REDACTED")}\",";
jsonString += $"\"webhook\":\"{(string.IsNullOrEmpty(webhookUrl.Value) ? "unset" : "REDACTED")}\",";
jsonString += $"\"webhookEvents\":\"{webhookEvents.Value}\",";
jsonString += $"\"webhook2\":\"{(string.IsNullOrEmpty(webhookUrl2.Value) ? "unset" : "REDACTED")}\",";
jsonString += $"\"webhookEvents\":\"{webhook2Events.Value}\",";
jsonString += $"\"fancierMessages\":\"{DiscordEmbedsEnabled}\",";
jsonString += $"\"ignoredPlayers\":\"{mutedDiscordUserList.Value}\",";
jsonString += $"\"ignoredPlayersRegex\":\"{mutedDiscordUserListRegex.Value}\"";
Expand All @@ -146,7 +181,8 @@ public string ConfigAsJson()
return jsonString;
}

public string WebHookURL => webhookUrl.Value;
public WebhookEntry PrimaryWebhook => primaryWebhook;
public WebhookEntry SecondaryWebhook => secondaryWebhook;
public bool CollectStatsEnabled => collectStatsToggle.Value;
public bool DiscordEmbedsEnabled => discordEmbedMessagesToggle.Value;
public bool SendPositionsEnabled => sendPositionsToggle.Value;
Expand Down
3 changes: 2 additions & 1 deletion src/Config/PluginConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ public void ReloadConfig(string configExt)
public bool EventResumedPosEnabled => mainConfig.SendPositionsEnabled && togglesConfig.EventResumedPosEnabled;

// Main Config
public string WebHookURL => mainConfig.WebHookURL;
public WebhookEntry PrimaryWebhook => mainConfig.PrimaryWebhook;
public WebhookEntry SecondaryWebhook => mainConfig.SecondaryWebhook;
public bool CollectStatsEnabled => mainConfig.CollectStatsEnabled;
public bool DiscordEmbedsEnabled => mainConfig.DiscordEmbedsEnabled;
public bool SendPositionsEnabled => mainConfig.SendPositionsEnabled;
Expand Down
Loading

0 comments on commit 36db004

Please sign in to comment.