C#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Core.Libraries.Covalence;
namespace Oxide.Plugins
{
[Info("SuggestionPlugin", "Drxp", "1.0.0")]
[Description("Sends player suggestions to Discord using webhooks.")]
public class SuggestionPlugin : RustPlugin
{
private string WebhookUrl;
private string ServerName;
private int CooldownSeconds;
private string content;
private Dictionary<string, DateTime> cooldowns = new Dictionary<string, DateTime>();
protected override void LoadDefaultConfig()
{
Config["WebhookUrl"] = "YOUR_DISCORD_WEBHOOK_URL";
Config["ServerName"] = "Your Server Name";
Config["CooldownSeconds"] = 60;
Config["content"] = "<ROLEHERE>";
}
void Init()
{
WebhookUrl = Config.Get("WebhookUrl") as string ?? "YOUR_DISCORD_WEBHOOK_URL";
ServerName = Config.Get("ServerName") as string ?? "Your Server Name";
CooldownSeconds = Convert.ToInt32(Config.Get("CooldownSeconds") ?? "60");
content = Config.Get("content") as string ?? "<ROLEHERE>";
AddCovalenceCommand("suggest", "HandleSuggestionCommand");
}
[Command("suggest")]
void HandleSuggestionCommand(IPlayer player, string command, string[] args)
{
if (IsCommandOnCooldown(player.Id))
{
player.Reply("You need to wait before sending another suggestion.");
return;
}
string suggestion = string.Join(" ", args);
SendSuggestionToDiscord(player.Name, suggestion, player.Id);
player.Reply("Thanks for your suggestion! It has been sent to Discord.");
StartCooldown(player.Id);
}
private bool IsCommandOnCooldown(string playerId)
{
DateTime lastUsage;
if (!cooldowns.TryGetValue(playerId, out lastUsage))
return false;
TimeSpan elapsed = DateTime.UtcNow - lastUsage;
return elapsed.TotalSeconds < CooldownSeconds;
}
private void StartCooldown(string playerId)
{
cooldowns[playerId] = DateTime.UtcNow;
}
private async void SendSuggestionToDiscord(string playerName, string suggestion, string playerId)
{
try
{
string jsonPayload = CreateEmbedPayload(playerName, suggestion, playerId);
await SendJsonPayloadToDiscordAsync(jsonPayload);
}
catch (Exception ex)
{
Puts($"Error sending suggestion to Discord: {ex.Message}");
}
}
private string CreateEmbedPayload(string playerName, string suggestion, string playerId)
{
var payload = new
{
content = content,
embeds = new[]
{
new
{
title = "New Suggestion",
description = $"A new suggestion has been sent from [{playerName}](https://steamcommunity.com/id/{playerId})",
color = 3447003,
fields = new[]
{
new {name = "New Suggestion", value = $"```{suggestion}```", inline = true},
},
footer = new
{
text = ServerName
}
}
}
};
return JsonConvert.SerializeObject(payload);
}
private async Task SendJsonPayloadToDiscordAsync(string jsonPayload)
{
byte[] data = Encoding.UTF8.GetBytes(jsonPayload);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(WebhookUrl);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
using (Stream stream = await request.GetRequestStreamAsync())
{
await stream.WriteAsync(data, 0, data.Length);
}
}
}
}