using Newtonsoft.Json; using Oxide.Core.Libraries.Covalence; using Oxide.Core.Plugins; using Oxide.Ext.Discord.Clients; using Oxide.Ext.Discord.Entities; using System.Collections.Generic; using System; using Oxide.Ext.Discord.Constants; using System.Linq; using Oxide.Core.Libraries; using Oxide.Ext.Discord.Connections; using Oxide.Ext.Discord.Interfaces; using Oxide.Ext.Discord.Libraries; namespace Oxide.Plugins { [Info("Discord Auth Logger", "YaMang -w-", "1.0.6")] [Description("Discord Auth, Core Linked, UnLink Message Alarm")] internal class DiscordAuthLogger : RustPlugin, IDiscordPlugin { #region Class Fields public DiscordClient Client { get; set; } private DiscordGuild _guild; private readonly BotConnection _discordSettings = new BotConnection(); #endregion [HookMethod(DiscordExtHooks.OnDiscordGatewayReady)] private void OnDiscordGatewayReady(GatewayReadyEvent ready) { Puts($"Bot connected to:{ready.Guilds.FirstOrDefault().Value.Name}"); DiscordGuild guild = null; if (ready.Guilds.Count == 1) { guild = ready.Guilds.Values.FirstOrDefault(); } if (guild == null) { guild = ready.Guilds[_config.guildId]; } if (guild == null) { PrintError("Failed to find a matching guild for the Discord Server Id. " + "Please make sure your guild Id is correct and the bot is in the discord server."); return; } _guild = guild; } #region Hook private void OnServerInitialized() { if (string.IsNullOrEmpty(_config.token)) { PrintWarning("Need a discord bot token\nNot loaded"); return; } _discordSettings.ApiToken = _config.token; _discordSettings.Intents = GatewayIntents.GuildMessages | GatewayIntents.Guilds | GatewayIntents.GuildMembers; _discordSettings.LogLevel = Ext.Discord.Logging.DiscordLogLevel.Info; Client.Connect(_discordSettings); } void OnDiscordPlayerLinked(IPlayer player, DiscordUser discord) { webrequest.Enqueue(SteamApi(player.Id), null, (code, response) => { var summaries = Deserialise(response); if (summaries == null) { PrintWarning($"(Connect Devloper)Api Response Can't Deserialise:\n{response}"); return; } DiscordEmbed embed = new DiscordEmbed(); embed.Author = new EmbedAuthor(player.Name, $"https://steamcommunity.com/profiles/{player.Id}/", GetAvatarFull(summaries)); embed.Description = Format(_config.AuthMsg.descriptionmsg, player, discord); List fields = new List(); fields.Clear(); foreach (var field in _config.AuthMsg.EmbedsFields) { var value = Format(field.Value, player, discord); fields.Add(new EmbedField() { Name = field.Name, Value = value, Inline = field.Inline }); } embed.Fields = fields; if (!string.IsNullOrEmpty(_config.AuthMsg.footermsg)) embed.Footer = new EmbedFooter() { Text = _config.AuthMsg.footermsg, IconUrl = _config.AuthMsg.footericon }; _guild.Channels[_config.AuthMsg.channelId].CreateMessage(Client, embed); }, this, RequestMethod.GET); } void OnDiscordPlayerUnlinked(IPlayer player, DiscordUser discord) { webrequest.Enqueue(SteamApi(player.Id), null, (code, response) => { var summaries = Deserialise(response); if (summaries == null) { PrintWarning($"(Connect Devloper)Api Response Can't Deserialise:\n{response}"); return; } DiscordEmbed embed = new DiscordEmbed(); embed.Author = new EmbedAuthor(player.Name, $"https://steamcommunity.com/profiles/{player.Id}/", GetAvatarFull(summaries)); embed.Description = Format(_config.DeAuthMsg.descriptionmsg, player, discord); List fields = new List(); fields.Clear(); foreach (var field in _config.DeAuthMsg.EmbedsFields) { var value = Format(field.Value, player, discord); fields.Add(new EmbedField() { Name = field.Name, Value = value, Inline = field.Inline }); } embed.Fields = fields; if (!string.IsNullOrEmpty(_config.DeAuthMsg.footermsg)) embed.Footer = new EmbedFooter() { Text = _config.DeAuthMsg.footermsg, IconUrl = _config.DeAuthMsg.footericon }; _guild.Channels[_config.DeAuthMsg.channelId].CreateMessage(Client, embed); }, this, RequestMethod.GET); } #endregion #region Helper private string SteamApi(string id) { return $"http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={_config.steamApiKey}&steamids={id}"; } private string SteamGetAvatarUrl(IPlayer player) { string url = ""; webrequest.Enqueue(string.Format(SteamApi(player.Id)), null, (code, response) => { if (code != 200) { PrintWarning($"Steam Api Can't {response}"); return; } var summaries = Deserialise(response); url = GetAvatarFull(summaries); }, this, RequestMethod.GET); return url; } private string Format(string msg, IPlayer player, DiscordUser discord) { msg = msg .Replace("{steamId}", player.Id) .Replace("{steamName}", player.Name) .Replace("{steamGetAvatarUrl}", SteamGetAvatarUrl(player)) .Replace("{discordId}", discord.Id) .Replace("{discordName}", discord.DisplayName.Replace("#0", "")) .Replace("{discordGetAvatarUrl}", discord.GetAvatarUrl) .Replace("{datetime}", DateTime.Now.ToString(_config.TimeFormat)); return msg; } #endregion #region Config private ConfigData _config; private class ConfigData { [JsonProperty(PropertyName = "Discord Token (Auth, Core Same Token recommended)")] public string token { get; set; } [JsonProperty(PropertyName = "Steam API Key (https://steamcommunity.com/dev/apikey)")] public string steamApiKey { get; set; } [JsonProperty(PropertyName = "Auth (Linked) Message Embed")] public Embed AuthMsg { get; set; } [JsonProperty(PropertyName = "DeAuth (UnLinked) Message Embed")] public Embed DeAuthMsg { get; set; } [JsonProperty(PropertyName = "Time Formatting")] public string TimeFormat { get; set; } [JsonProperty(PropertyName = "Guild Id")] public Snowflake guildId { get; set; } public Oxide.Core.VersionNumber Version { get; set; } } public class Embed { [JsonProperty(PropertyName = "Channel Logger Id")] public Snowflake channelId { get; set; } [JsonProperty(PropertyName = "Description")] public string descriptionmsg { get; set; } [JsonProperty(PropertyName = "Footer Message")] public string footermsg { get; set; } [JsonProperty(PropertyName = "Footer Icon")] public string footericon { get; set; } [JsonProperty(PropertyName = "Embed Field")] public List EmbedsFields { get; set; } } protected override void LoadConfig() { base.LoadConfig(); _config = Config.ReadObject(); if (_config.Version < Version) UpdateConfigValues(); Config.WriteObject(_config, true); } protected override void LoadDefaultConfig() => _config = GetBaseConfig(); private ConfigData GetBaseConfig() { return new ConfigData { token = "", steamApiKey = "", TimeFormat = "MM/dd/yyyy hh:mm:ss tt", guildId = new Snowflake(), AuthMsg = new Embed { channelId = new Snowflake(), descriptionmsg = "Linked", footermsg = "Linked", footericon = "", EmbedsFields = new List() { new EmbedField() { Name = "Steam", Value = "[{steamName}](https://steamcommunity.com/profiles/{steamId}) ({steamId})" }, new EmbedField() { Name = "Discord", Value = "<@{discordId}> ({discordName} | {discordId})" } }, }, DeAuthMsg = new Embed { channelId = new Snowflake(), descriptionmsg = "Unlinked", footermsg = "Unlinked", footericon = "", EmbedsFields = new List() { new EmbedField() { Name = "Steam", Value = "[{steamName}](https://steamcommunity.com/profiles/{steamId}) ({steamId})" }, new EmbedField() { Name = "Discord", Value = "<@{discordId}> ({discordName} | {discordId})" } }, }, Version = Version }; } protected override void SaveConfig() => Config.WriteObject(_config, true); private void UpdateConfigValues() { PrintWarning("Config update detected! Updating config values..."); _config.Version = Version; PrintWarning("Config update completed!"); } #endregion #region SteamAPI private T Deserialise(string json) => JsonConvert.DeserializeObject(json); private string GetAvatarFull(Summaries s) => s.Response.Players[0].AvatarFull; private class Summaries { [JsonProperty("response")] public Content Response; public class Content { [JsonProperty("players")] public Player[] Players; public class Player { [JsonProperty("steamid")] public string SteamId; [JsonProperty("communityvisibilitystate")] public int CommunityVisibilityState; [JsonProperty("profilestate")] public int ProfileState; [JsonProperty("personaname")] public string PersonaName; [JsonProperty("lastlogoff")] public double LastLogOff; [JsonProperty("commentpermission")] public int CommentPermission; [JsonProperty("profileurl")] public string ProfileUrl; [JsonProperty("avatar")] public string Avatar; [JsonProperty("avatarmedium")] public string AvatarMedium; [JsonProperty("avatarfull")] public string AvatarFull; [JsonProperty("personastate")] public int PersonaState; [JsonProperty("realname")] public string RealName; [JsonProperty("primaryclanid")] public string PrimaryClanId; [JsonProperty("timecreated")] public double TimeCreated; [JsonProperty("personastateflags")] public int PersonaStateFlags; [JsonProperty("loccountrycode")] public string LocCountryCode; [JsonProperty("locstatecode")] public string LocStateCode; } } } #endregion } }