Untitled

 avatar
unknown
plain_text
6 days ago
4.7 kB
28
No Index
using GTANetworkAPI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;

namespace Infinity.sistemler
{
    internal class Discord : Script
    {
        private static HttpListener listener;
        private static Dictionary<string, string> authCodes = new Dictionary<string, string>();
        public static Dictionary<string, string> linkedAccounts = new Dictionary<string, string>();

        [ServerEvent(Event.ResourceStart)]
        public void OnResourceStart()
        {
            listener = new HttpListener();
            listener.Prefixes.Add("http://localhost:3000/link/");
            listener.Start();
            NAPI.Util.ConsoleOutput("[Discord] HTTP endpoint aktif: http://localhost:3000/link/");

            Thread listenerThread = new Thread(StartListener);
            listenerThread.Start();
        }

        private void StartListener()
        {
            while (listener.IsListening)
            {
                try
                {
                    HttpListenerContext context = listener.GetContext();
                    ProcessRequest(context);
                }
                catch (Exception ex)
                {
                    NAPI.Util.ConsoleOutput("[Discord] Listener hatası: " + ex.Message);
                }
            }
        }

        private void ProcessRequest(HttpListenerContext context)
        {
            string body;
            using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
            {
                body = reader.ReadToEnd(); 
            }

            string code = null;
            string discordId = null;

            string[] parts = body.Split('&');
            foreach (string part in parts)
            {
                if (part.StartsWith("code=")) code = part.Substring(5);
                else if (part.StartsWith("discordId=")) discordId = part.Substring(10);
            }

            string playerName;
            bool success = TryLinkAccount(code, discordId, out playerName);

            string jsonResponse = success
                ? $"{{\"success\":true,\"playerName\":\"{playerName}\"}}"
                : "{\"success\":false}";

            byte[] buffer = Encoding.UTF8.GetBytes(jsonResponse);
            context.Response.ContentType = "application/json";
            context.Response.ContentLength64 = buffer.Length;
            context.Response.OutputStream.Write(buffer, 0, buffer.Length);
            context.Response.Close();
        }

        [Command("discord")]
        public void DiscordCommand(Player player)
        {
            string playerName = player.Name;

            if (linkedAccounts.ContainsValue(playerName))
            {
                Main.SendErrorMessage(player, "Zaten bir Discord hesabıyla eşleştirilmişsiniz. Kaldırmak için /baglantikes yazın.");
                return;
            }

            string code = GenerateAuthCode();
            authCodes[playerName] = code;

            Main.SendSuccessMessage(player, $"Discord hesabınızı bağlamak için Discord kanalına bu kodu yazın: {code}");
        }

        [Command("baglantikes")]
        public void BaglantiKesCommand(Player player)
        {
            string playerName = player.Name;
            string discordId = null;

            foreach (var pair in linkedAccounts)
            {
                if (pair.Value == playerName)
                {
                    discordId = pair.Key;
                    break;
                }
            }

            if (discordId != null)
            {
                linkedAccounts.Remove(discordId);
                Main.SendSuccessMessage(player, "Discord hesabınızla olan bağlantı başarıyla kaldırıldı.");
            }
            else
            {
                Main.SendErrorMessage(player, "Bu oyun hesabı şu anda herhangi bir Discord hesabıyla bağlantılı değil.");
            }
        }

        public static bool TryLinkAccount(string code, string discordId, out string playerName)
        {
            playerName = null;

            foreach (var pair in authCodes)
            {
                if (pair.Value == code)
                {
                    playerName = pair.Key;
                    linkedAccounts[discordId] = playerName;
                    authCodes.Remove(pair.Key);
                    return true;
                }
            }

            return false;
        }

        private string GenerateAuthCode()
        {
            Random rnd = new Random();
            int randomNumber = rnd.Next(100000, 1000000);
            return $"LSNRP{randomNumber}";
        }
    }
}
Editor is loading...
Leave a Comment