Skip to content

Commit

Permalink
Merge pull request #1 from xiluisx/master
Browse files Browse the repository at this point in the history
Added Embed functionality + Works with bp v1.07
  • Loading branch information
ggggg authored Aug 9, 2020
2 parents de4e12f + 37ec94a commit f373fb7
Show file tree
Hide file tree
Showing 10 changed files with 650 additions and 41 deletions.
565 changes: 541 additions & 24 deletions GHooks/settings.json

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion src/Webhooks/Configuration/Models/SettingsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,41 @@ public class Server
{
public string PlayerJoinLeave { get; set; }
public string PlayerJoinFormat { get; set; }
public bool PlayerJoinUseEmbed { get; set; }
public List<Embed> PlayerJoinEmbed { get; set; }
public string PlayerLeaveFormat { get; set; }

public bool PlayerLeaveUseEmbed { get; set; }
public List<Embed> PlayerLeaveEmbed { get; set; }
public string ServerStart { get; set; }
public string ServerStartMessage { get; set; }
public bool ServerStartUseEmbed { get; set; }
public List<Embed> ServerStartEmbed { get; set; }
}
public class Chat
{
public string GlobalChat { get; set; }

public string GlobalFormat { get; set; }

public bool GlobalUseEmbed { get; set; }

public List<Embed> GlobalEmbed { get; set; }

public string LocalChat { get; set; }

public string LocalFormat { get; set; }

public bool LocalUseEmbed { get; set; }

public List<Embed> LocalEmbed { get; set; }

public string CommandsLog { get; set; }

public string CommandsLogFormat { get; set; }

public bool CommandsUseEmbed { get; set; }

public List<Embed> CommandsEmbed { get; set; }
}
public class General
{
Expand Down
10 changes: 6 additions & 4 deletions src/Webhooks/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public void RegisterCustomCommands()
foreach (var customEvent in CustomEventReader.Parsed)
{
Logger.LogInfo($"[CC] Registering custom event(s) for webhook {string.Join(", ", customEvent.Event)} by name '{customEvent.Event}'..");
EventsHandler.Add(customEvent.Event, new Action<ShPlayer, string>((player, eventName) => {
if (player.HasPermission("webhook." + eventName)){
EventsHandler.Add(customEvent.Event, new Action<ShPlayer, string>((player, eventName) =>
{
if (player.svPlayer.HasPermission("webhook." + eventName))
{
return;
}
Logger.LogInfo($"cutstom event {customEvent.Event} was tiriggered");
Expand All @@ -77,10 +79,10 @@ public void SetConfigurationFilePaths()
CustomEventReader.Path = Paths.EventsFile;
}

public void ReadConfigurationFiles()
public void ReadConfigurationFiles()
{
SettingsReader.ReadAndParse();
CustomEventReader.ReadAndParse();
}
}
}
}
68 changes: 68 additions & 0 deletions src/Webhooks/EmbedCrafter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using BrokeProtocol.Entities;
using System.Collections.Generic;
using System.Linq;

namespace Webhooks
{
public static class EmbedCrafter
{
public static List<Embed> CreateAllEmbeds(List<Embed> embeds, ShPlayer player)
{
var allEmbeds = new List<Embed>();
foreach (var embed in embeds)
{
allEmbeds.Add(new Embed
{
Title = string.Format(embed.Title, player.username),
Description = string.Format(embed.Description, player.username),
Color = embed.Color,
Footer = new EmbedFooter() { Text = string.Format(embed.Footer.Text, player.username), IconUrl = embed.Footer.IconUrl, ProxyIconUrl = embed.Footer.ProxyIconUrl },
Author = new EmbedAuthor() { Name = string.Format(embed.Author.Name, player.username), IconUrl = embed.Author.IconUrl, ProxyIconUrl = embed.Author.ProxyIconUrl, Url = embed.Author.Url },
Fields = new List<EmbedField>(embed.Fields.Select(c =>
{
c.Name = string.Format(c.Name, player.username);
c.Value = string.Format(c.Value, player.username);
return c;
}).ToList()),
Image = embed.Image,
Provider = embed.Provider,
Url = embed.Url,
Thumbnail = embed.Thumbnail,
TimeStamp = embed.TimeStamp,
Type = embed.Type,
Video = embed.Video
});
}
return allEmbeds;
}
public static List<Embed> CreateAllEmbeds(List<Embed> embeds, ShPlayer player,string message)
{
var allEmbeds = new List<Embed>();
foreach (var embed in embeds)
{
allEmbeds.Add(new Embed
{
Title = string.Format(embed.Title,message,player.username),
Description = string.Format(embed.Description, message, player.username),
Color = embed.Color,
Footer = new EmbedFooter() { Text = string.Format(embed.Footer.Text, message, player.username), IconUrl = embed.Footer.IconUrl, ProxyIconUrl = embed.Footer.ProxyIconUrl },
Author = new EmbedAuthor() { Name = string.Format(embed.Author.Name, message, player.username), IconUrl = embed.Author.IconUrl, ProxyIconUrl = embed.Author.ProxyIconUrl, Url = embed.Author.Url },
Fields = new List<EmbedField>(embed.Fields.Select(c =>
{
c.Name = string.Format(c.Name, message, player.username);
c.Value = string.Format(c.Value, message, player.username);
return c;
}).ToList()),
Image = embed.Image,
Provider = embed.Provider,
Url = embed.Url,
Thumbnail = embed.Thumbnail,
TimeStamp = embed.TimeStamp,
Type = embed.Type,
Video = embed.Video
});
}
return allEmbeds;
}
}
}
7 changes: 5 additions & 2 deletions src/Webhooks/Events/OnGlobalChatMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ public void OnEvent(ShPlayer player, string message)
{
if (message.StartsWith("/"))
{
Core.Instance.commandWebhook.Send(string.Format(Core.Instance.Settings.Chat.CommandsLogFormat, message, player.username), player.username);
Core.Instance.commandWebhook.Send(string.Format(Core.Instance.Settings.Chat.CommandsLogFormat, message, player.username), player.username,
embeds: Core.Instance.Settings.Chat.CommandsUseEmbed ? EmbedCrafter.CreateAllEmbeds(Core.Instance.Settings.Chat.CommandsEmbed,player,message) : null);
return;
}
Core.Instance.globalWebhook.Send(string.Format(Core.Instance.Settings.Chat.GlobalFormat , message, player.username), player.username);
Core.Instance.globalWebhook.Send(string.Format(Core.Instance.Settings.Chat.GlobalFormat, message, player.username), player.username,
embeds: Core.Instance.Settings.Chat.GlobalUseEmbed ? EmbedCrafter.CreateAllEmbeds(Core.Instance.Settings.Chat.GlobalEmbed,player,message) : null);
}
}
}
3 changes: 2 additions & 1 deletion src/Webhooks/Events/OnLeave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public class OnLeave : IScript
[Target(GameSourceEvent.PlayerDestroy, ExecutionMode.Event)]
public void OnEvent(ShPlayer player)
{
Core.Instance.joinWebhook.Send(string.Format(Core.Instance.Settings.Server.PlayerLeaveFormat, player.username), player.username);
Core.Instance.joinWebhook.Send(string.Format(Core.Instance.Settings.Server.PlayerLeaveFormat, player.username), player.username,
embeds: Core.Instance.Settings.Server.PlayerLeaveUseEmbed? EmbedCrafter.CreateAllEmbeds(Core.Instance.Settings.Server.PlayerLeaveEmbed,player):null);
}
}
}
7 changes: 5 additions & 2 deletions src/Webhooks/Events/OnLocalChatMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ public void OnEvent(ShPlayer player, string message)
{
if (message.StartsWith("/"))
{
Core.Instance.commandWebhook.Send(string.Format(Core.Instance.Settings.Chat.CommandsLogFormat, message, player.username), player.username);
Core.Instance.commandWebhook.Send(string.Format(Core.Instance.Settings.Chat.CommandsLogFormat, message, player.username), player.username,
embeds: Core.Instance.Settings.Chat.CommandsUseEmbed ? EmbedCrafter.CreateAllEmbeds(Core.Instance.Settings.Chat.CommandsEmbed,player,message) : null);
return;
}
Core.Instance.localWebhook.Send(string.Format(Core.Instance.Settings.Chat.LocalFormat, message, player.username), player.username);
Core.Instance.localWebhook.Send(string.Format(Core.Instance.Settings.Chat.LocalFormat, message, player.username), player.username,
embeds: Core.Instance.Settings.Chat.LocalUseEmbed ? EmbedCrafter.CreateAllEmbeds(Core.Instance.Settings.Chat.LocalEmbed, player,message) : null);
}
}
}
6 changes: 2 additions & 4 deletions src/Webhooks/Events/OnLogin.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using BrokeProtocol.API;
using BrokeProtocol.Entities;
using System.Collections.Specialized;
using System.Net;

namespace Webhooks.RegisteredEvents
{
Expand All @@ -10,9 +8,9 @@ public class OnLogin : IScript
[Target(GameSourceEvent.PlayerInitialize, ExecutionMode.Event)]
public void OnEvent(ShPlayer player)
{
Core.Instance.joinWebhook.Send(string.Format(Core.Instance.Settings.Server.PlayerJoinFormat, player.username), player.username);
Core.Instance.joinWebhook.Send(string.Format(Core.Instance.Settings.Server.PlayerJoinFormat, player.username), player.username,
embeds: Core.Instance.Settings.Server.PlayerJoinUseEmbed? EmbedCrafter.CreateAllEmbeds(Core.Instance.Settings.Server.PlayerJoinEmbed,player) :null);
}
}

}

4 changes: 2 additions & 2 deletions src/Webhooks/Events/OnStarted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class OnStarted : IScript
[Target(GameSourceEvent.ManagerStart, ExecutionMode.Event)]
public void OnEvent(SvManager svManager)
{
Core.Instance.joinWebhook.Send(string.Format(Core.Instance.Settings.Server.ServerStartMessage));

Core.Instance.joinWebhook.Send(string.Format(Core.Instance.Settings.Server.ServerStartMessage),
embeds: Core.Instance.Settings.Server.ServerStartUseEmbed? Core.Instance.Settings.Server.ServerStartEmbed:null);
}
}
}
2 changes: 1 addition & 1 deletion src/Webhooks/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
[assembly: ComVisible(false)]
[assembly: Guid("6898544b-a280-4866-ba6f-9d0dc8f62ca8")]

[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]

0 comments on commit f373fb7

Please sign in to comment.