C#:
using Oxide.Core.Libraries.Covalence;
using Oxide.Core.Plugins;
using System;
using System.Net;
using System.IO;
using System.Threading.Tasks;
// ____ _ _ _
// / ___|(_) __ _(_) | ___
// \___ \| |/ _` | | |/ _ \
// ___) | | (_| | | | (_) |
// |____/|_|\__, |_|_|\___/
// |___/
// ☽✧✵✧✵✧✵✧✵✧✵✧✵✧☾
// ✦ RUST PLUGINS ✦
// ✦ Sigilo.dev ✦
// ☽✧✵✧✵✧✵✧✵✧✵✧✵✧☾
namespace Oxide.Plugins
{
[Info("DiscordBanNotification", "Sigilo", "1.0.1")]
class DiscordBanNotification : CovalencePlugin
{
private string webhookUrl;
private bool notifyBans;
private bool notifyKicks;
private void Init()
{
LoadConfig();
}
protected override void LoadDefaultConfig()
{
Config["WebhookURL"] = "Your Discord Webhook URL here";
Config["NotifyBans"] = true;
Config["NotifyKicks"] = true;
}
protected override void LoadConfig()
{
base.LoadConfig();
webhookUrl = Config["WebhookURL"].ToString();
notifyBans = Convert.ToBoolean(Config["NotifyBans"]);
notifyKicks = Convert.ToBoolean(Config["NotifyKicks"]);
}
void OnUserBanned(string name, string id, string ip, string reason)
{
if (notifyBans)
{
SendDiscordNotification(name, reason, "banned", id, ConVar.Server.hostname);
}
}
void OnUserUnbanned(string name, string id, string reason)
{
SendDiscordNotification(name, "", "unbanned", id, ConVar.Server.hostname);
}
void OnUserKicked(IPlayer player, string reason)
{
if (notifyKicks)
{
if (reason.StartsWith("Kicked: "))
{
reason = reason.Substring(8);
}
SendDiscordNotification(player.Name, reason, "kicked", player.Id, ConVar.Server.hostname);
}
}
private void SendDiscordNotification(string userName, string reason, string action, string id, string serverName)
{
Task.Run(() =>
{
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webhookUrl);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string reasonField = "";
if (action != "unbanned")
{
reasonField = $@",
{{
""name"": ""Reason"",
""value"": ""{reason}"",
""inline"": false
}}";
}
string json = $@"{{
""embeds"": [{{
""title"": ""{serverName}"",
""fields"": [
{{
""name"": ""User"",
""value"": ""**{userName}** | [Steam](https://steamcommunity.com/profiles/{id}) | [BM](https://www.battlemetrics.com/rcon/players?filter%5Bsearch%5D={id}) | [RA](https://www.rustadmin.com/player-search-{id})"",
""inline"": false
}},
{{
""name"": ""SteamID"",
""value"": ""{id}"",
""inline"": false
}},
{{
""name"": ""Action"",
""value"": ""{action}"",
""inline"": false
}}{reasonField}
]
}}]
}}";
streamWriter.Write(json);
streamWriter.Flush();
}
httpWebRequest.GetResponse();
}
catch (Exception ex)
{
Puts($"Error sending Discord notification: {ex.Message}");
}
});
}
}
}