Skip to content

Commit

Permalink
Make webhook looks more better, fix small bug with banning player tha…
Browse files Browse the repository at this point in the history
…t not on server
  • Loading branch information
Mr0maks committed Aug 11, 2023
1 parent 2ad0fb1 commit 90156c3
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 25 deletions.
9 changes: 6 additions & 3 deletions Content.Server/Administration/Managers/BanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public async void CreateServerBan(NetUserId? target, string? targetUsername, Net
_sawmill.Info(logMessage);
_chat.SendAdminAlert(logMessage);

_arumoonBans.RaiseLocalBanEvent(targetUsername ?? Loc.GetString("system-user"), expires, reason, severity, adminName);

// If we're not banning a player we don't care about disconnecting people
if (target == null)
return;
Expand All @@ -179,8 +181,6 @@ public async void CreateServerBan(NetUserId? target, string? targetUsername, Net
// If they are, kick them
var message = banDef.FormatBanMessage(_cfg, _localizationManager);
targetPlayer.ConnectedClient.Disconnect(message);

_arumoonBans.RaiseLocalBanEvent(targetUsername ?? "null", expires, reason);
}
#endregion

Expand All @@ -204,6 +204,9 @@ public async void CreateRoleBan(NetUserId? target, string? targetUsername, NetUs
_systems.TryGetEntitySystem(out GameTicker? ticker);
int? roundId = ticker == null || ticker.RoundId == 0 ? null : ticker.RoundId;
var playtime = target == null ? TimeSpan.Zero : (await _db.GetPlayTimes(target.Value)).Find(p => p.Tracker == PlayTimeTrackingShared.TrackerOverall)?.TimeSpent ?? TimeSpan.Zero;
var adminName = banningAdmin == null
? Loc.GetString("system-user")
: (await _db.GetPlayerRecordByUserId(banningAdmin.Value))?.LastSeenUserName ?? Loc.GetString("system-user");

var banDef = new ServerRoleBanDef(
null,
Expand Down Expand Up @@ -234,7 +237,7 @@ public async void CreateRoleBan(NetUserId? target, string? targetUsername, NetUs
SendRoleBans(target.Value);
}

_arumoonBans.RaiseLocalJobBanEvent(targetUsername ?? "null", expires, jobPrototype, reason);
_arumoonBans.RaiseLocalJobBanEvent(targetUsername ?? "null", expires, jobPrototype, reason, severity, adminName);
}

public HashSet<string>? GetJobBans(NetUserId playerUserId)
Expand Down
164 changes: 147 additions & 17 deletions Content.Server/AruMoon/BansNotificationsSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Content.Shared.CCVar;
using Content.Shared.Database;
using Content.Shared.GameTicking;
using Content.Shared.Roles;
using Robust.Shared.Configuration;
Expand All @@ -16,10 +17,9 @@ namespace Content.Server.Arumoon.BansNotifications
/// </summary>
public interface IBansNotificationsSystem
{
void RaiseLocalBanEvent(string username, DateTimeOffset? expires, string reason);

void RaiseLocalJobBanEvent(string username, DateTimeOffset? expires, JobPrototype job, string reason);
void RaiseLocalDepartmentBanEvent(string username, DateTimeOffset? expires, DepartmentPrototype department, string reason);
void RaiseLocalBanEvent(string username, DateTimeOffset? expires, string reason, NoteSeverity severity, string adminusername);
void RaiseLocalJobBanEvent(string username, DateTimeOffset? expires, JobPrototype job, string reason, NoteSeverity severity, string adminusername);
void RaiseLocalDepartmentBanEvent(string username, DateTimeOffset? expires, DepartmentPrototype department, string reason, NoteSeverity severity, string adminusername);
}

public sealed class BansNotificationsSystem : EntitySystem, IBansNotificationsSystem
Expand All @@ -37,19 +37,19 @@ public override void Initialize()
_config.OnValueChanged(CCVars.DiscordBanWebhook, value => _webhookUrl = value, true);
}

public void RaiseLocalBanEvent(string username, DateTimeOffset? expires, string reason)
public void RaiseLocalBanEvent(string username, DateTimeOffset? expires, string reason, NoteSeverity severity, string adminusername)
{
RaiseLocalEvent(new BanEvent(username, expires, reason));
RaiseLocalEvent(new BanEvent(username, expires, reason, severity, adminusername));
}

public void RaiseLocalJobBanEvent(string username, DateTimeOffset? expires, JobPrototype job, string reason)
public void RaiseLocalJobBanEvent(string username, DateTimeOffset? expires, JobPrototype job, string reason, NoteSeverity severity, string adminusername)
{
RaiseLocalEvent(new JobBanEvent(username, expires, job, reason));
RaiseLocalEvent(new JobBanEvent(username, expires, job, reason, severity, adminusername));
}

public void RaiseLocalDepartmentBanEvent(string username, DateTimeOffset? expires, DepartmentPrototype department, string reason)
public void RaiseLocalDepartmentBanEvent(string username, DateTimeOffset? expires, DepartmentPrototype department, string reason, NoteSeverity severity, string adminusername)
{
RaiseLocalEvent(new DepartmentBanEvent(username, expires, department, reason));
RaiseLocalEvent(new DepartmentBanEvent(username, expires, department, reason, severity, adminusername));
}

private async void SendDiscordMessage(WebhookPayload payload)
Expand All @@ -70,14 +70,43 @@ public void OnBan(BanEvent e)
if (String.IsNullOrEmpty(_webhookUrl))
return;

var payload = new WebhookPayload();
var expires = e.Expires == null ? Loc.GetString("discord-permanent") : Loc.GetString("discord-expires-at", ("date", e.Expires));
var text = Loc.GetString("discord-ban-msg",
var message = Loc.GetString("discord-ban-msg",
("username", e.Username),
("expires", expires),
("reason", e.Reason));

payload.Content = text;
var color = e.Severity switch
{
NoteSeverity.None => 0x6aa84f,
NoteSeverity.Minor => 0x45818e,
NoteSeverity.Medium => 0xf1c232,
NoteSeverity.High => 0xff0000,
_ => 0xff0000,
};

var payload = new WebhookPayload
{
/*
Username = username,
AvatarUrl = string.IsNullOrWhiteSpace(_avatarUrl) ? null : _avatarUrl,
*/
Embeds = new List<Embed>
{
new()
{
Description = message,
Color = color,
Footer = new EmbedFooter
{
Text = $"{e.AdminUsername}",
/*
IconUrl = string.IsNullOrWhiteSpace(_footerIconUrl) ? null : _footerIconUrl
*/
},
},
},
};

SendDiscordMessage(payload);
}
Expand All @@ -87,23 +116,54 @@ public void OnJobBan(JobBanEvent e)
if (String.IsNullOrEmpty(_webhookUrl))
return;

var payload = new WebhookPayload();
var expires = e.Expires == null ? Loc.GetString("discord-permanent") : Loc.GetString("discord-expires-at", ("date", e.Expires));
var text = Loc.GetString("discord-jobban-msg",
var message = Loc.GetString("discord-jobban-msg",
("username", e.Username),
("role", e.Job.LocalizedName),
("expires", expires),
("reason", e.Reason));

payload.Content = text;

var color = e.Severity switch
{
NoteSeverity.None => 0x6aa84f,
NoteSeverity.Minor => 0x45818e,
NoteSeverity.Medium => 0xf1c232,
NoteSeverity.High => 0xff0000,
_ => 0xff0000,
};

var payload = new WebhookPayload
{
/*
Username = username,
AvatarUrl = string.IsNullOrWhiteSpace(_avatarUrl) ? null : _avatarUrl,
*/
Embeds = new List<Embed>
{
new()
{
Description = message,
Color = color,
Footer = new EmbedFooter
{
Text = $"{e.AdminUsername}",
/*
IconUrl = string.IsNullOrWhiteSpace(_footerIconUrl) ? null : _footerIconUrl
*/
},
},
},
};

SendDiscordMessage(payload);
}

public void OnDepartmentBan(DepartmentBanEvent e)
{
if (String.IsNullOrEmpty(_webhookUrl))
return;

/*
var payload = new WebhookPayload();
var departamentLocName = Loc.GetString(string.Concat("department-", e.Department.ID));
var expires = e.Expires == null ? Loc.GetString("discord-permanent") : Loc.GetString("discord-expires-at", ("date", e.Expires));
Expand All @@ -115,8 +175,10 @@ public void OnDepartmentBan(DepartmentBanEvent e)
payload.Content = text;
SendDiscordMessage(payload);
*/
}

/*
private struct WebhookPayload
{
[JsonPropertyName("content")]
Expand All @@ -130,5 +192,73 @@ private struct WebhookPayload
public WebhookPayload() {}
}
*/
private struct WebhookPayload
{
[JsonPropertyName("username")]
public string Username { get; set; } = "";

[JsonPropertyName("avatar_url")]
public string? AvatarUrl { get; set; } = "";

[JsonPropertyName("embeds")]
public List<Embed>? Embeds { get; set; } = null;

[JsonPropertyName("allowed_mentions")]
public Dictionary<string, string[]> AllowedMentions { get; set; } =
new()
{
{ "parse", Array.Empty<string>() },
};

public WebhookPayload()
{
}
}

// https://discord.com/developers/docs/resources/channel#embed-object-embed-structure
private struct Embed
{
[JsonPropertyName("description")]
public string Description { get; set; } = "";

[JsonPropertyName("color")]
public int Color { get; set; } = 0;

[JsonPropertyName("footer")]
public EmbedFooter? Footer { get; set; } = null;

public Embed()
{
}
}

// https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure
private struct EmbedFooter
{
[JsonPropertyName("text")]
public string Text { get; set; } = "";

[JsonPropertyName("icon_url")]
public string? IconUrl { get; set; }

public EmbedFooter()
{
}
}

// https://discord.com/developers/docs/resources/webhook#webhook-object-webhook-structure
private struct WebhookData
{
[JsonPropertyName("guild_id")]
public string? GuildId { get; set; } = null;

[JsonPropertyName("channel_id")]
public string? ChannelId { get; set; } = null;

public WebhookData()
{
}
}
}
}
9 changes: 7 additions & 2 deletions Content.Server/GameTicking/Events/BanEvent.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using Content.Shared.Database;

namespace Content.Shared.GameTicking;

public sealed class BanEvent : EntityEventArgs
{
public string Username { get; }
public DateTimeOffset? Expires { get; }
public string Reason { get; }
public NoteSeverity Severity { get; }
public string AdminUsername { get; }


public BanEvent(string username, DateTimeOffset? expires, string reason)
public BanEvent(string username, DateTimeOffset? expires, string reason, NoteSeverity severity, string adminusername)
{
Username = username;
Expires = expires;
Reason = reason;
Severity = severity;
AdminUsername = adminusername;
}
}
7 changes: 6 additions & 1 deletion Content.Server/GameTicking/Events/DepartmentBanEvent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Content.Shared.Database;
using Content.Shared.Roles;

namespace Content.Shared.GameTicking;
Expand All @@ -8,13 +9,17 @@ public sealed class DepartmentBanEvent : EntityEventArgs
public DepartmentPrototype Department { get; }
public DateTimeOffset? Expires { get; }
public string Reason { get; }
public NoteSeverity Severity { get; }
public string AdminUsername { get; }


public DepartmentBanEvent(string username, DateTimeOffset? expires, DepartmentPrototype department, string reason)
public DepartmentBanEvent(string username, DateTimeOffset? expires, DepartmentPrototype department, string reason, NoteSeverity severity, string adminusername)
{
Username = username;
Department = department;
Expires = expires;
Reason = reason;
Severity = severity;
AdminUsername = adminusername;
}
}
8 changes: 6 additions & 2 deletions Content.Server/GameTicking/Events/JobBanEvent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Content.Shared.Database;
using Content.Shared.Roles;

namespace Content.Shared.GameTicking;
Expand All @@ -8,13 +9,16 @@ public sealed class JobBanEvent : EntityEventArgs
public JobPrototype Job { get; }
public DateTimeOffset? Expires { get; }
public string Reason { get; }
public NoteSeverity Severity { get; }
public string AdminUsername { get; }


public JobBanEvent(string username, DateTimeOffset? expires, JobPrototype job, string reason)
public JobBanEvent(string username, DateTimeOffset? expires, JobPrototype job, string reason, NoteSeverity severity, string adminusername)
{
Username = username;
Job = job;
Expires = expires;
Reason = reason;
Severity = severity;
AdminUsername = adminusername;
}
}

0 comments on commit 90156c3

Please sign in to comment.