Advertisement
Guest User

Untitled

a guest
May 27th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.40 KB | None | 0 0
  1. using System;
  2.  
  3. using System.Web;
  4. using Microsoft.Xna.Framework;
  5. using StardewModdingAPI;
  6. using StardewModdingAPI.Events;
  7. using StardewModdingAPI.Utilities;
  8. using StardewValley;
  9. using DiscordRPC;
  10. using DiscordRPC.IO;
  11. using DiscordRPC.Message;
  12. using System.Text;
  13. using System.Threading;
  14.  
  15.  
  16. // 428108945446535178
  17. namespace SDVRP
  18. {
  19.     public class ModEntry : Mod
  20.     {
  21.         private static readonly int DiscordPipe = -1;
  22.         private static readonly string ClientID = "428108945446535178";
  23.         private static DiscordRpcClient client;
  24.         private static RichPresence presence = new RichPresence()
  25.  
  26.         {
  27.             Details = "SDV Test",
  28.             State = "SDV dev",
  29.             Assets = new Assets()
  30.             {
  31.                 LargeImageKey = "image_large",
  32.                 LargeImageText = "Test",
  33.                 SmallImageKey = "image_small"
  34.             }
  35.         };
  36.  
  37.         public override void Entry(IModHelper helper)
  38.         {
  39.             GameEvents.FirstUpdateTick += GameEvents_FirstUpdateTick;
  40.             GameEvents.UpdateTick += GameEvents_UpdateTick;
  41.         }
  42.  
  43.         private void GameEvents_UpdateTick(object sender, EventArgs e)
  44.         {
  45.             if (client != null)
  46.                 client.Invoke();
  47.         }
  48.  
  49.         private void GameEvents_FirstUpdateTick(object sender, EventArgs e)
  50.         {
  51.             using (client = new DiscordRpcClient(ClientID, true, DiscordPipe))                                          //This will create a new client that will register itself a URI scheme (for join / spectate)
  52.             {
  53.  
  54.  
  55.                 //Register to the events we care about. We are registering to everyone just to show off the events
  56.                 client.OnReady += OnReady;
  57.                 client.OnClose += OnClose;
  58.                 client.OnError += OnError;
  59.  
  60.                 client.OnConnectionEstablished += OnConnectionEstablished;
  61.                 client.OnConnectionFailed += OnConnectionFailed;
  62.  
  63.                 client.OnPresenceUpdate += OnPresenceUpdate;
  64.  
  65.                 client.OnSubscribe += OnSubscribe;
  66.                 client.OnUnsubscribe += OnUnsubscribe;
  67.  
  68.                 client.OnJoin += OnJoin;
  69.                 client.OnSpectate += OnSpectate;
  70.                 client.OnJoinRequested += OnJoinRequested;
  71.  
  72.                 //Before we send a initial presence, we will generate a random "game ID" for this example.
  73.                 // For a real game, this "game ID" can be a unique ID that your Match Maker / Master Server generates.
  74.                 // This is used for the Join / Specate feature. This can be ignored if you do not plan to implement that feature.
  75.                 presence.Secrets = new Secrets()
  76.                 {
  77.                     //These secrets should contain enough data for external clients to be able to know which
  78.                     // game to connect too. A simple approach would be just to use IP address, but this is highly discouraged
  79.                     // and can leave your players vulnerable!
  80.                     JoinSecret = "join_myuniquegameid",
  81.                     SpectateSecret = "spectate_myuniquegameid"
  82.                 };
  83.  
  84.                 presence.Timestamps = new Timestamps()
  85.                 {
  86.                     Start = DateTime.UtcNow
  87.                 };
  88.  
  89.                 //We also need to generate a initial party. This is because Join requires the party to be created too.
  90.                 // If no party is set, the join feature will not work and may cause errors within the discord client itself.
  91.                 presence.Party = new Party()
  92.                 {
  93.                     ID = Secrets.CreateFriendlySecret(new Random()),
  94.                     Size = 1,
  95.                     Max = 4
  96.                 };
  97.  
  98.                 //Set some new presence to tell Discord we are in a game.
  99.                 // If the connection is not yet available, this will be queued until a Ready event is called,
  100.                 // then it will be sent. All messages are queued until Discord is ready to receive them.
  101.                 client.SetPresence(presence);
  102.  
  103.                 //Subscribe to the join / spectate feature.
  104.                 //These require the RegisterURI to be true.
  105.                 client.SetSubscription(EventType.Join | EventType.Spectate | EventType.JoinRequest);        //This will alert us if discord wants to join a game
  106.  
  107.                 //Initialize the connection. This must be called ONLY once.
  108.                 //It must be called before any updates are sent or received from the discord client.
  109.                 client.Initialize();
  110.  
  111.             }
  112.         }
  113.         #region Events
  114.  
  115.         #region State Events
  116.         private static void OnReady(object sender, ReadyMessage args)
  117.         {
  118.             //This is called when we are all ready to start receiving and sending discord events.
  119.             // It will give us some basic information about discord to use in the future.
  120.  
  121.             //It can be a good idea to send a inital presence update on this event too, just to setup the inital game state.
  122.             Console.WriteLine("On Ready. RPC Version: {0}", args.Version);
  123.  
  124.         }
  125.         private static void OnClose(object sender, CloseMessage args)
  126.         {
  127.             //This is called when our client has closed. The client can no longer send or receive events after this message.
  128.             // Connection will automatically try to re-establish and another OnReady will be called (unless it was disposed).
  129.             Console.WriteLine("Lost Connection with client because of '{0}'", args.Reason);
  130.         }
  131.         private static void OnError(object sender, ErrorMessage args)
  132.         {
  133.             //Some error has occured from one of our messages. Could be a malformed presence for example.
  134.             // Discord will give us one of these events and its upto us to handle it
  135.             Console.WriteLine("Error occured within discord. ({1}) {0}", args.Message, args.Code);
  136.         }
  137.         #endregion
  138.  
  139.         #region Pipe Connection Events
  140.         private static void OnConnectionEstablished(object sender, ConnectionEstablishedMessage args)
  141.         {
  142.             //This is called when a pipe connection is established. The connection is not ready yet, but we have at least found a valid pipe.
  143.             Console.WriteLine("Pipe Connection Established. Valid on pipe #{0}", args.ConnectedPipe);
  144.         }
  145.         private static void OnConnectionFailed(object sender, ConnectionFailedMessage args)
  146.         {
  147.             //This is called when the client fails to establish a connection to discord.
  148.             // It can be assumed that Discord is unavailable on the supplied pipe.
  149.             Console.WriteLine("Pipe Connection Failed. Could not connect to pipe #{0}", args.FailedPipe);
  150.         }
  151.         #endregion
  152.  
  153.         private static void OnPresenceUpdate(object sender, PresenceMessage args)
  154.         {
  155.             //This is called when the Rich Presence has been updated in the discord client.
  156.             // Use this to keep track of the rich presence and validate that it has been sent correctly.
  157.             Console.WriteLine("Rich Presence Updated. Playing {0}", args.Presence == null ? "Nothing (NULL)" : args.Presence.State);
  158.         }
  159.  
  160.         #region Subscription Events
  161.         private static void OnSubscribe(object sender, SubscribeMessage args)
  162.         {
  163.             //This is called when the subscription has been made succesfully. It will return the event you subscribed too.
  164.             Console.WriteLine("Subscribed: {0}", args.Event);
  165.         }
  166.         private static void OnUnsubscribe(object sender, UnsubscribeMessage args)
  167.         {
  168.             //This is called when the unsubscription has been made succesfully. It will return the event you unsubscribed from.
  169.             Console.WriteLine("Unsubscribed: {0}", args.Event);
  170.         }
  171.         #endregion
  172.  
  173.         #region Join / Spectate feature
  174.         private static void OnJoin(object sender, JoinMessage args)
  175.         {
  176.             /*
  177.              * This is called when the Discord Client wants to join a online game to play.
  178.              * It can be triggered from a invite that your user has clicked on within discord or from an accepted invite.
  179.              *
  180.              * The secret should be some sort of encrypted data that will give your game the nessary information to connect.
  181.              * For example, it could be the Game ID and the Game Password which will allow you to look up from the Master Server.
  182.              * Please avoid using IP addresses within these fields, its not secure and defeats the Discord security measures.
  183.              *
  184.              * This feature requires the RegisterURI to be true on the client.
  185.             */
  186.             Console.WriteLine("Joining Game '{0}'", args.Secret);
  187.         }
  188.  
  189.         private static void OnSpectate(object sender, SpectateMessage args)
  190.         {   /*
  191.              * This is called when the Discord Client wants to join a online game to watch and spectate.
  192.              * It can be triggered from a invite that your user has clicked on within discord.
  193.              *
  194.              * The secret should be some sort of encrypted data that will give your game the nessary information to connect.
  195.              * For example, it could be the Game ID and the Game Password which will allow you to look up from the Master Server.
  196.              * Please avoid using IP addresses within these fields, its not secure and defeats the Discord security measures.
  197.              *
  198.              * This feature requires the RegisterURI to be true on the client.
  199.             */
  200.             Console.WriteLine("Spectating Game '{0}'", args.Secret);
  201.         }
  202.  
  203.         private static void OnJoinRequested(object sender, JoinRequestMessage args)
  204.         {
  205.             /*
  206.              * This is called when the Discord Client has received a request from another external Discord User to join your game.
  207.              * You should trigger a UI prompt to your user sayings 'X wants to join your game' with a YES or NO button. You can also get
  208.              *  other information about the user such as their avatar (which this library will provide a useful link) and their nickname to
  209.              *  make it more personalised. You can combine this with more API if you wish. Check the Discord API documentation.
  210.              *  
  211.              *  Once a user clicks on a response, call the Respond function, passing the message, to respond to the request.
  212.              *  A example is provided below.
  213.              *  
  214.              * This feature requires the RegisterURI to be true on the client.
  215.             */
  216.  
  217.             //We have received a request, dump a bunch of information for the user
  218.             Console.WriteLine("'{0}' has requested to join our game.", args.User.Username);
  219.             Console.WriteLine(" - User's Avatar: {0}", args.User.GetAvatarURL(User.AvatarFormat.GIF, User.AvatarSize.x2048));
  220.             Console.WriteLine(" - User's Descrim: {0}", args.User.Discriminator);
  221.             Console.WriteLine(" - User's Snowflake: {0}", args.User.ID);
  222.             Console.WriteLine();
  223.  
  224.             //Ask the user if they wish to accept the join request.
  225.             Console.Write("Do you give this user permission to join? [Y / n]: ");
  226.             bool accept = Console.ReadKey().Key == ConsoleKey.Y; Console.WriteLine();
  227.  
  228.             //Tell the client if we accept or not.
  229.             DiscordRpcClient client = (DiscordRpcClient)sender;
  230.             client.Respond(args, accept);
  231.  
  232.             //All done.
  233.             Console.WriteLine(" - Sent a {0} invite to the client {1}", accept ? "ACCEPT" : "REJECT", args.User.Username);
  234.         }
  235.         #endregion
  236.  
  237.         #endregion
  238.     }
  239.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement