Skip to content

Commit

Permalink
Merge pull request #116 from winglessraven:dev
Browse files Browse the repository at this point in the history
Add variable for external IP address
  • Loading branch information
winglessraven authored Nov 12, 2024
2 parents 5b00c42 + 8251669 commit af967fb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 217 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,4 @@ ASALocalRun/
# BeatPulse healthcheck temp database
healthchecksdb
#/DiscordBotPlugin/DiscordBotPlugin.csproj
/DiscordBotPlugin/DiscordBotPlugin.csproj
212 changes: 0 additions & 212 deletions DiscordBotPlugin/DiscordBotPlugin.csproj

This file was deleted.

22 changes: 22 additions & 0 deletions DiscordBotPlugin/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using static DiscordBotPlugin.PluginMain;

Expand Down Expand Up @@ -326,5 +327,26 @@ public List<string> SplitOutputIntoCodeBlocks(List<string> messages)

return outputStrings;
}

public async Task<string> GetExternalIpAddressAsync()
{
using (HttpClient client = new HttpClient())
{
try
{
// Use a service that returns the IP address in plain text
HttpResponseMessage response = await client.GetAsync("https://api.ipify.org");
response.EnsureSuccessStatusCode();

string ipAddress = await response.Content.ReadAsStringAsync();
return ipAddress;
}
catch (Exception ex)
{
log.Error("Error fetching IP: " + ex.Message);
return null;
}
}
}
}
}
8 changes: 7 additions & 1 deletion DiscordBotPlugin/InfoPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ public async Task GetServerInfo(bool updateExisting, SocketSlashCommand msg, boo
}

embed.AddField("Server Name", "```" + settings?.MainSettings?.ServerDisplayName + "```", false);
embed.AddField("Server IP", "```" + settings?.MainSettings?.ServerConnectionURL + "```", false);
string connectionURL = settings.MainSettings.ServerConnectionURL;
if (connectionURL.ToLower().Contains("{publicip}"))
{
string ipAddress = await helper.GetExternalIpAddressAsync();
connectionURL = connectionURL.ToLower().Replace("{publicip}", ipAddress);
}
embed.AddField("Server IP", "```" + connectionURL + "```", false);

if (!string.IsNullOrEmpty(settings?.MainSettings?.ServerPassword))
{
Expand Down
8 changes: 4 additions & 4 deletions DiscordBotPlugin/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DiscordBotSettings : SettingSectionStore
[WebSetting("Server Display Name", "Displayed in the bot info panel", false, Subcategory: "Server Info:page_info:2")]
public string ServerDisplayName = "";

[WebSetting("Server Connection URL", "Displayed in bot info panel", false, Subcategory: "Server Info:page_info:2")]
[WebSetting("Server Connection URL", "Displayed in bot info panel. Use {PublicIP} variable to show your external IP address.", false, Subcategory: "Server Info:page_info:2")]
public string ServerConnectionURL = "";

[WebSetting("Server Password", "Displayed in bot info panel", false, Subcategory: "Server Info:page_info:2")]
Expand Down Expand Up @@ -143,7 +143,7 @@ public class DiscordBotSettings : SettingSectionStore
}

public DiscordBotSettings MainSettings = new DiscordBotSettings();

[Description("Discord Bot:smart_toy")]
[SettingsGroupName("Discord Bot")]
[Serializable]
Expand Down Expand Up @@ -191,7 +191,7 @@ public class DiscordBotColoursSettings : SettingSectionStore
public class DiscordBotGameSpecificSettings : SettingSectionStore
{
[WebSetting("Valheim Join Code", "Look for Valheim join code and display it in the info panel (restart the server after enabling)", false, Subcategory: "Game Specific:sports_esports:6")]
public bool ValheimJoinCode = false;
public bool ValheimJoinCode = false;
}

public DiscordBotGameSpecificSettings GameSpecificSettings = new DiscordBotGameSpecificSettings();
Expand All @@ -204,7 +204,7 @@ public class DiscordBotAboutSettings : SettingSectionStore
[WebSetting("Support", "For support, or to submit issues and feature requests visit the [GitHub](https://github.com/winglessraven/AMP-Discord-Bot) page.", false, Subcategory: "About:info:8")]
public int? AboutMessage = null;

[WebSetting("Donations", "If you appreciate my work, donations are gratefully received! Visit my [Sponsor](https://github.com/sponsors/winglessraven) page.", false, Subcategory: "About:info:8")]
[WebSetting("Donations", "If you appreciate my work, donations are gratefully received! Visit my [Sponsor](https://github.com/sponsors/winglessraven) page.", false, Subcategory: "About:info:8")]
public int? DonationsMessage = null;

[WebSetting("AMP Discord Bot", "Created by winglessraven", false, Subcategory: "About:info:8")]
Expand Down

0 comments on commit af967fb

Please sign in to comment.