Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Embed functionality #1

Merged
merged 11 commits into from
Aug 9, 2020
144 changes: 121 additions & 23 deletions GHooks/settings.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,138 @@
{
"General": {
"Version": "0.1.0",
"MaxTicketsPerUser" : "5"
},
"Chat": {
"General": {
"Version": "0.2.0"
},
"Chat": {
/*
chat custom names:
{0} = Message
{1} = Sender username
*/

//please put the web hook link here, leave blank for disable
"GlobalChat" : "",
"GlobalFormat": "{0}",

//please put the web hook link here, leave blank for disable
"LocalChat": "",
"LocalFormat": "local: {0}",
//please put the web hook link here, leave blank for disable
"GlobalChat": "https://discordapp.com/api/webhooks/741815178844176416/2XL_m-BMsr6n2DpRaOHNanjgo-B8rG8gRrV_Yhxeko2R06dswsUTCscgbiOaHbmgyyrz",
xiluisx marked this conversation as resolved.
Show resolved Hide resolved
"GlobalFormat": "{0}",
"GlobalUseEmbed":true,
"GlobalEmbed": [{
"title": "Global Chat",
"type": "rich",
"description": "{1} : {0}",
"url": null,
"timestamp": null,
"color": 15158332,
"footer": null,
"image": null,
"thumbnail": null,
"video": null,
"provider": null,
"author": null,
"fields": []
}],

//please put the web hook link here, leave blank for disable
"LocalChat": "https://discordapp.com/api/webhooks/741815178844176416/2XL_m-BMsr6n2DpRaOHNanjgo-B8rG8gRrV_Yhxeko2R06dswsUTCscgbiOaHbmgyyrz",
xiluisx marked this conversation as resolved.
Show resolved Hide resolved
"LocalFormat": "local: {0}",
"LocalUseEmbed":true,
"LocalEmbed": [{
"title": "Local Chat",
"type": "rich",
"description": "{1} : {0}",
"url": null,
"timestamp": null,
"color": 1752220,
"footer": null,
"image": null,
"thumbnail": null,
"video": null,
"provider": null,
"author": null,
"fields": []
}],

//please put the web hook link here, leave blank for disable
"CommandsLog" : "",
"CommandsLogFormat" : "{0}"
//please put the web hook link here, leave blank for disable
"CommandsLog": "https://discordapp.com/api/webhooks/741815178844176416/2XL_m-BMsr6n2DpRaOHNanjgo-B8rG8gRrV_Yhxeko2R06dswsUTCscgbiOaHbmgyyrz",
xiluisx marked this conversation as resolved.
Show resolved Hide resolved
"CommandsLogFormat": "{0}",
"CommandsUseEmbed":true,
"CommandsEmbed": [{
"title": "Commands",
"type": "rich",
"description": "{1} : {0}",
"url": null,
"timestamp": null,
"color": 1146986,
"footer": null,
"image": null,
"thumbnail": null,
"video": null,
"provider": null,
"author": null,
"fields": []
}]
},

//please put the web hook link here, leave blank for disable
"Server": {
/*
chat custom names:
{0} = User
*/
//please put the web hook link here, leave blank for disable
"PlayerJoinLeave" : "",
"PlayerJoinFormat": "{0} has joined the server",
"PlayerLeaveFormat": "{0} has left the server",

//please put the web hook link here, leave blank for disable
"ServerStart" : "",
//cant use {0} here
"ServerStartMessage": "Server started"
//please put the web hook link here, leave blank for disable
"PlayerJoinLeave": "https://discordapp.com/api/webhooks/741815178844176416/2XL_m-BMsr6n2DpRaOHNanjgo-B8rG8gRrV_Yhxeko2R06dswsUTCscgbiOaHbmgyyrz",
"PlayerJoinFormat": "{0} has joined the server",
"PlayerJoinUseEmbed":true,
"PlayerJoinEmbed": [{
"title": "SERVER JOIN",
"type": "rich",
"description": "{0} has joined",
"url": null,
"timestamp": null,
"color": 0,
"footer": null,
"image": null,
"thumbnail": null,
"video": null,
"provider": null,
"author": null,
"fields": []
}],
"PlayerLeaveFormat": "{0} has left the server",
"PlayerLeaveUseEmbed":true,
"PlayerLeaveEmbed": [{
"title": "SERVER LEFT",
"type": "rich",
"description": "{0} has left",
"url": null,
"timestamp": null,
"color": 0,
"footer": null,
"image": null,
"thumbnail": null,
"video": null,
"provider": null,
"author": null,
"fields": []
}],

//please put the web hook link here, leave blank for disable
"ServerStart": "https://discordapp.com/api/webhooks/741815178844176416/2XL_m-BMsr6n2DpRaOHNanjgo-B8rG8gRrV_Yhxeko2R06dswsUTCscgbiOaHbmgyyrz",
//cant use {0} here
"ServerStartMessage": "Server started",
"ServerStartUseEmbed":true,
"ServerStartEmbed": [{
"title": "Server started",
"type": "rich",
"description": "Server started",
"url": null,
"timestamp": null,
"color": 2899536,
"footer": null,
"image": null,
"thumbnail": null,
"video": null,
"provider": null,
"author": null,
"fields": []
}]
}
}
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: 7 additions & 3 deletions src/Webhooks/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using BrokeProtocol.API;
using BrokeProtocol.Entities;
using BrokeProtocol.Managers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;//remove
xiluisx marked this conversation as resolved.
Show resolved Hide resolved
using System.Reflection;
using Webhooks.Configuration.Models;
using Webhooks.Configuration.Models.SettingsModel;
Expand Down Expand Up @@ -61,8 +63,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))
{
Comment on lines +64 to +67
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might wanna add embeds to that soon... not urgent though.

return;
}
Logger.LogInfo($"cutstom event {customEvent.Event} was tiriggered");
Expand All @@ -77,7 +81,7 @@ public void SetConfigurationFilePaths()
CustomEventReader.Path = Paths.EventsFile;
}

public void ReadConfigurationFiles()
public void ReadConfigurationFiles()
{
SettingsReader.ReadAndParse();
CustomEventReader.ReadAndParse();
Expand Down
20 changes: 17 additions & 3 deletions src/Webhooks/Events/OnGlobalChatMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BrokeProtocol.API;
using BrokeProtocol.Entities;
using System.Collections.Generic;

namespace Webhooks.RegisteredEvents
{
Expand All @@ -8,11 +9,24 @@ public class OnGlobalChatMessage : IScript
[Target(GameSourceEvent.PlayerGlobalChatMessage, ExecutionMode.Event)]
public void OnEvent(ShPlayer player, string message)
{
List<Embed> commandsEmb = new List<Embed>();
List<Embed> globalEmb = new List<Embed>();
xiluisx marked this conversation as resolved.
Show resolved Hide resolved

foreach (var em in Core.Instance.Settings.Chat.CommandsEmbed)
{
commandsEmb.Add(new Embed { Title = string.Format(em.Title, message, player.username), Description = string.Format(em.Description, message, player.username) });
}
foreach(var em in Core.Instance.Settings.Chat.GlobalEmbed)
{
globalEmb.Add(new Embed { Title = string.Format(em.Title, message, player.username), Description = string.Format(em.Description, message, player.username) });
}
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? commandsEmb: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? globalEmb :null);

}
}
}
}
10 changes: 9 additions & 1 deletion src/Webhooks/Events/OnLeave.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BrokeProtocol.API;
using BrokeProtocol.Entities;
using System.Collections.Generic;

namespace Webhooks.RegisteredEvents
{
Expand All @@ -8,7 +9,14 @@ 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);
List<Embed> leaveEmb = new List<Embed>();
xiluisx marked this conversation as resolved.
Show resolved Hide resolved

foreach (var em in Core.Instance.Settings.Server.PlayerLeaveEmbed)
{
leaveEmb.Add(new Embed { Title = string.Format(em.Title, player.username), Description = string.Format(em.Description, player.username) });
}
Core.Instance.joinWebhook.Send(string.Format(Core.Instance.Settings.Server.PlayerLeaveFormat, player.username), player.username,
embeds: Core.Instance.Settings.Server.PlayerLeaveUseEmbed? leaveEmb:null);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this is a lot of duplicate code, maybe we would have it in a function that takes in the embed settings and returns it back:

public List<Embed> CreateAllEmbeds(List<Embed> embeds) {
          var emb = new List<Embed>();
          foreach (var em in Core.Instance.Settings.Server.PlayerLeaveEmbed)
          {
              emb.Add(new Embed { Title = string.Format(em.Title, player.username), Description = string.Format(em.Description, player.username) }); // add more things to format and make docs on what each mean in the json
          }
}

}
}
}
17 changes: 15 additions & 2 deletions src/Webhooks/Events/OnLocalChatMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BrokeProtocol.API;
using BrokeProtocol.Entities;
using System.Collections.Generic;

namespace Webhooks.RegisteredEvents
{
Expand All @@ -8,11 +9,23 @@ public class OnLocalChatMessage : IScript
[Target(GameSourceEvent.PlayerLocalChatMessage, ExecutionMode.Event)]
public void OnEvent(ShPlayer player, string message)
{
List<Embed> commandsEmb = new List<Embed>();
List<Embed> localEmb = new List<Embed>();

foreach (var em in Core.Instance.Settings.Chat.CommandsEmbed)
{
commandsEmb.Add(new Embed { Title = string.Format(em.Title, message, player.username), Description = string.Format(em.Description, message, player.username) });
}
foreach (var em in Core.Instance.Settings.Chat.LocalEmbed)
{
localEmb.Add(new Embed { Title = string.Format(em.Title, message, player.username), Description = string.Format(em.Description, message, player.username) });
}
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?commandsEmb: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?localEmb:null);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be:

            if (message.StartsWith("/"))
            {
            foreach (var em in Core.Instance.Settings.Chat.CommandsEmbed)
            {
                commandsEmb.Add(new Embed { Title = string.Format(em.Title, message, player.username), Description = string.Format(em.Description, message, player.username) });
            }
Core.Instance.commandWebhook.Send(string.Format(Core.Instance.Settings.Chat.CommandsLogFormat, message, player.username), player.username,embeds: Core.Instance.Settings.Chat.CommandsUseEmbed?commandsEmb:null);
                return;
            }
            foreach (var em in Core.Instance.Settings.Chat.LocalEmbed)
            {
                localEmb.Add(new Embed { Title = string.Format(em.Title, message, player.username), Description = string.Format(em.Description, message, player.username) });
            }
            Core.Instance.localWebhook.Send(string.Format(Core.Instance.Settings.Chat.LocalFormat, message, player.username), player.username,embeds: Core.Instance.Settings.Chat.LocalUseEmbed?localEmb:null);

I guess

}
}
}
11 changes: 8 additions & 3 deletions src/Webhooks/Events/OnLogin.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using BrokeProtocol.API;
using BrokeProtocol.Entities;
using System.Collections.Specialized;
using System.Net;
using System.Collections.Generic;

namespace Webhooks.RegisteredEvents
{
Expand All @@ -10,7 +9,13 @@ 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);
List<Embed> joinEmb = new List<Embed>();
xiluisx marked this conversation as resolved.
Show resolved Hide resolved

foreach (var em in Core.Instance.Settings.Server.PlayerJoinEmbed)
{
joinEmb.Add(new Embed { Title = string.Format(em.Title, player.username), Description = string.Format(em.Description, player.username) });
}
Core.Instance.joinWebhook.Send(string.Format(Core.Instance.Settings.Server.PlayerJoinFormat, player.username), player.username, embeds: Core.Instance.Settings.Server.PlayerJoinUseEmbed? joinEmb:null);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Webhooks/Events/OnStarted.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BrokeProtocol.API;
using BrokeProtocol.Managers;
using UnityEngine;

namespace Webhooks.RegisteredEvents
{
Expand All @@ -8,8 +9,7 @@ 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("2.0.0.0")]
xiluisx marked this conversation as resolved.
Show resolved Hide resolved