C#:
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Core.Libraries.Covalence;
using System.Collections.Generic;
using System;
using System.Linq;
namespace Oxide.Plugins {
[Info("Chat Triggers", "Drxp", "1.0.0")]
[Description("Help players based on keywords in chat")]
public class ChatTriggers : CovalencePlugin {
private PluginConfig config;
#region Config
private PluginConfig GetDefaultConfig() {
return new PluginConfig {
Triggers = new List<PluginConfig.Trigger> {
new PluginConfig.Trigger {
Keywords = new List<string> {
"admin", "help"
}
, ResponseMessage = "AutoHelp: Need an admin? Open up a ticket on our Discord: discord.gg/invite", DiscordWebhookUrls = new List<string> {
"https://discord.com/api/webhooks/your-webhook-url-for-admin"
}
, WebhookTitle = "This player may need an admin!", WebhookDescriptionTemplate = "[{0}](https://steamcommunity.com/id/{1}) was automagically helped on how to contact an admin.\n```{2}```\n**Server Reply**\n```Need an admin? Open up a ticket on our Discord: discord.gg/kSyQfFAZPD```", WebhookColor = 4160982
}
, new PluginConfig.Trigger {
Keywords = new List<string> {
"group", "limit"
}
, ResponseMessage = "AutoHelp: This server is 6man only.", DiscordWebhookUrls = new List<string> {
"https://discord.com/api/webhooks/your-webhook-url-for-group"
}
, WebhookTitle = "Player helped with group limits!", WebhookDescriptionTemplate = "[{0}](https://steamcommunity.com/id/{1}) was automagically helped with the group limits.\n```{2}```\n** Server Reply**\n```This server is 6man only```", WebhookColor = 4160982
}
, new PluginConfig.Trigger {
Keywords = new List<string> {
"Cheating", "Hacking", "Hacks", "AimBot"
}
, ResponseMessage = "AutoHelp: If you think someone is cheating F7 report them and open a ticket up in our discord.", DiscordWebhookUrls = new List<string> {
"https://discord.com/api/webhooks/your-webhook-url-for-cheating"
}
, WebhookTitle = "Player helped with reporting", WebhookDescriptionTemplate = "[{0}](https://steamcommunity.com/id/{1}) was automagically help with reporting cheaters limits.\n```{2}```\n**Server Reply**\n```AutoHelp: If you think someone is cheating F7 report them and open a ticket up in our discord.```", WebhookColor = 4160982
}
, new PluginConfig.Trigger {
Keywords = new List<string> {
"!pop"
}
, ResponseMessage = "AutoHelp: Players online: {playerCount}", DiscordWebhookUrls = new List<string> {
"https://discord.com/api/webhooks/your-webhook-url-for-ANYTHING"
}
, WebhookTitle = "Pop Trigger Activated!", WebhookDescriptionTemplate = "[{0}](https://steamcommunity.com/id/{1}) activated the pop trigger.\n```{2}```", WebhookColor = 4160982
}
}
}
;
}
protected override void LoadDefaultConfig() {
PrintWarning("Creating a new configuration file");
config = GetDefaultConfig();
SaveConfig();
}
protected override void SaveConfig() => Config.WriteObject(config);
private void Init() {
try {
config = Config.ReadObject<PluginConfig>();
}
catch {
PrintError("Could not read the configuration file. A new one will be generated.");
LoadDefaultConfig();
}
if (config == null) {
PrintError("Configuration file is missing or invalid. A new one will be generated.");
LoadDefaultConfig();
}
}
#endregion
#region Hooks
private int GetOnlinePlayerCount() {
return covalence.Players.Connected.Count();
}
private void OnUserChat(IPlayer player, string message) {
Puts($"Received message from {player.Name}: {message}");
foreach (var trigger in config.Triggers) {
foreach (var keyword in trigger.Keywords) {
if (message.ToLower().Contains(keyword.ToLower())) {
if (keyword.Equals("!pop", StringComparison.OrdinalIgnoreCase)) {
timer.Once(1.0f, () => {
int playerCount = GetOnlinePlayerCount();
string responseMessage = trigger.ResponseMessage .Replace("{playerCount}", playerCount.ToString());
player.Message(responseMessage);
SendWebhookMessage(trigger, player.Name, player.Id, message);
}
);
}
else {
timer.Once(3.0f, () => {
player.Message(trigger.ResponseMessage);
SendWebhookMessage(trigger, player.Name, player.Id, message);
}
);
}
return;
}
}
}
}
#endregion
#region Discord Webhook
private class Embed {
public string title {
get;
set;
}
public string description {
get;
set;
}
public int color {
get;
set;
}
}
private class DiscordWebhookPayload {
public List<Embed> embeds {
get;
set;
}
}
private void SendWebhookMessage(PluginConfig.Trigger trigger, string playerName, string playerId, string message) {
var embed = new Embed {
title = trigger.WebhookTitle, description = string.Format(trigger.WebhookDescriptionTemplate, playerName, playerId, message), color = trigger.WebhookColor
}
;
var payload = new DiscordWebhookPayload {
embeds = new List<Embed> {
embed
}
}
;
var payloadJson = JsonConvert.SerializeObject(payload);
var headers = new Dictionary<string, string> {
{
"Content-Type", "application/json"
}
}
;
foreach (var url in trigger.DiscordWebhookUrls) {
if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(payloadJson)) {
webrequest.EnqueuePost(url, payloadJson, (code, response) => ValidateResponse(code, response, url), this, headers);
}
}
}
private void ValidateResponse(int code, string response, string url) {
if (code != 204 && code != 200) {
Puts($"Webhook to {url} failed with code {code}: {response}");
}
}
#endregion
#region Configuration
private class PluginConfig {
public List<Trigger> Triggers {
get;
set;
}
public class Trigger {
public List<string> Keywords {
get;
set;
}
public string ResponseMessage {
get;
set;
}
public List<string> DiscordWebhookUrls {
get;
set;
}
public string WebhookTitle {
get;
set;
}
public string WebhookDescriptionTemplate {
get;
set;
}
public int WebhookColor {
get;
set;
}
}
}
#endregion
}
}