C#:
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
namespace Oxide.Plugins
{
[Info("RustToDiscordSuggestions", "Toccima", "1.0.0")]
class RustToDiscordSuggestions : RustPlugin
{
private PluginConfig config;
private Dictionary<string, DateTime> cooldowns = new Dictionary<string, DateTime>();
private class PluginConfig
{
public string WebhookUrl { get; set; }
public int CooldownSeconds { get; set; }
public string ServerName { get; set; }
public string AfterSentFeedback { get; set; }
public int EmbedColor { get; set; }
public static PluginConfig DefaultConfig()
{
return new PluginConfig
{
WebhookUrl = "",
CooldownSeconds = 60,
ServerName = "My Server",
AfterSentFeedback = "Thanks for your feedback!",
EmbedColor = 3447003
};
}
}
protected override void LoadDefaultConfig()
{
Config.WriteObject(PluginConfig.DefaultConfig(), true);
}
protected override void LoadConfig()
{
base.LoadConfig();
config = Config.ReadObject<PluginConfig>();
}
void Init()
{
permission.RegisterPermission("rusttodiscordsuggestions.use", this);
}
[ChatCommand("suggest")]
private void OnSuggestCommand(BasePlayer player, string command, string[] args)
{
if (!permission.UserHasPermission(player.UserIDString, "rusttodiscordsuggestions.use"))
{
player.ChatMessage("You don't have permission to use this command.");
return;
}
ProcessCommand(player, "suggestion", args);
}
[ChatCommand("suggestion")]
private void OnSuggestionCommand(BasePlayer player, string command, string[] args)
{
if (!permission.UserHasPermission(player.UserIDString, "rusttodiscordsuggestions.use"))
{
player.ChatMessage("You don't have permission to use this command.");
return;
}
ProcessCommand(player, "suggestion", args);
}
[ChatCommand("complain")]
private void OnComplainCommand(BasePlayer player, string command, string[] args)
{
if (!permission.UserHasPermission(player.UserIDString, "rusttodiscordsuggestions.use"))
{
player.ChatMessage("You don't have permission to use this command.");
return;
}
ProcessCommand(player, "complaint", args);
}
[ChatCommand("complaint")]
private void OnComplaintCommand(BasePlayer player, string command, string[] args)
{
if (!permission.UserHasPermission(player.UserIDString, "rusttodiscordsuggestions.use"))
{
player.ChatMessage("You don't have permission to use this command.");
return;
}
ProcessCommand(player, "complaint", args);
}
private void ProcessCommand(BasePlayer player, string commandType, string[] args)
{
var suggestion = string.Join(" ", args);
if (string.IsNullOrEmpty(suggestion))
{
player.ChatMessage("You must provide a suggestion or complaint.");
return;
}
if (cooldowns.ContainsKey(player.UserIDString) && cooldowns[player.UserIDString] > DateTime.UtcNow)
{
var timeLeft = (int)(cooldowns[player.UserIDString] - DateTime.UtcNow).TotalSeconds;
player.ChatMessage($"Please wait {timeLeft} seconds before sending another feedback.");
return;
}
cooldowns[player.UserIDString] = DateTime.UtcNow.AddSeconds(config.CooldownSeconds);
try
{
string descCommandType = commandType == "suggestion" ? "suggestion" : "complaint";
SendToDiscord(config.WebhookUrl, $"{player.displayName}'s {descCommandType}", suggestion, player.UserIDString);
player.ChatMessage(config.AfterSentFeedback);
}
catch (Exception e)
{
player.ChatMessage("There was an error processing your command.");
Puts($"Error sending Discord message: {e.Message}");
return;
}
}
private void SendToDiscord(string url, string title, string content, string userID)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var embed = new
{
title = title,
description = $"```{content}```",
color = config.EmbedColor,
fields = new[]
{
new
{
name = "Server",
value = config.ServerName,
inline = true
},
new
{
name = "Steam64ID",
value = $"[{userID}](https://steamcommunity.com/profiles/{userID})",
inline = true
}
}
};
var json = new
{
embeds = new[] { embed }
};
streamWriter.Write(JsonConvert.SerializeObject(json));
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
}
}