Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Web;
- using Microsoft.Xna.Framework;
- using StardewModdingAPI;
- using StardewModdingAPI.Events;
- using StardewModdingAPI.Utilities;
- using StardewValley;
- using DiscordRPC;
- using DiscordRPC.IO;
- using DiscordRPC.Message;
- using System.Text;
- using System.Threading;
- // 428108945446535178
- namespace SDVRP
- {
- public class ModEntry : Mod
- {
- private static readonly int DiscordPipe = -1;
- private static readonly string ClientID = "428108945446535178";
- private static DiscordRpcClient client;
- private static RichPresence presence = new RichPresence()
- {
- Details = "SDV Test",
- State = "SDV dev",
- Assets = new Assets()
- {
- LargeImageKey = "image_large",
- LargeImageText = "Test",
- SmallImageKey = "image_small"
- }
- };
- public override void Entry(IModHelper helper)
- {
- GameEvents.FirstUpdateTick += GameEvents_FirstUpdateTick;
- GameEvents.UpdateTick += GameEvents_UpdateTick;
- }
- private void GameEvents_UpdateTick(object sender, EventArgs e)
- {
- if (client != null)
- client.Invoke();
- }
- private void GameEvents_FirstUpdateTick(object sender, EventArgs e)
- {
- using (client = new DiscordRpcClient(ClientID, true, DiscordPipe)) //This will create a new client that will register itself a URI scheme (for join / spectate)
- {
- //Register to the events we care about. We are registering to everyone just to show off the events
- client.OnReady += OnReady;
- client.OnClose += OnClose;
- client.OnError += OnError;
- client.OnConnectionEstablished += OnConnectionEstablished;
- client.OnConnectionFailed += OnConnectionFailed;
- client.OnPresenceUpdate += OnPresenceUpdate;
- client.OnSubscribe += OnSubscribe;
- client.OnUnsubscribe += OnUnsubscribe;
- client.OnJoin += OnJoin;
- client.OnSpectate += OnSpectate;
- client.OnJoinRequested += OnJoinRequested;
- //Before we send a initial presence, we will generate a random "game ID" for this example.
- // For a real game, this "game ID" can be a unique ID that your Match Maker / Master Server generates.
- // This is used for the Join / Specate feature. This can be ignored if you do not plan to implement that feature.
- presence.Secrets = new Secrets()
- {
- //These secrets should contain enough data for external clients to be able to know which
- // game to connect too. A simple approach would be just to use IP address, but this is highly discouraged
- // and can leave your players vulnerable!
- JoinSecret = "join_myuniquegameid",
- SpectateSecret = "spectate_myuniquegameid"
- };
- presence.Timestamps = new Timestamps()
- {
- Start = DateTime.UtcNow
- };
- //We also need to generate a initial party. This is because Join requires the party to be created too.
- // If no party is set, the join feature will not work and may cause errors within the discord client itself.
- presence.Party = new Party()
- {
- ID = Secrets.CreateFriendlySecret(new Random()),
- Size = 1,
- Max = 4
- };
- //Set some new presence to tell Discord we are in a game.
- // If the connection is not yet available, this will be queued until a Ready event is called,
- // then it will be sent. All messages are queued until Discord is ready to receive them.
- client.SetPresence(presence);
- //Subscribe to the join / spectate feature.
- //These require the RegisterURI to be true.
- client.SetSubscription(EventType.Join | EventType.Spectate | EventType.JoinRequest); //This will alert us if discord wants to join a game
- //Initialize the connection. This must be called ONLY once.
- //It must be called before any updates are sent or received from the discord client.
- client.Initialize();
- }
- }
- #region Events
- #region State Events
- private static void OnReady(object sender, ReadyMessage args)
- {
- //This is called when we are all ready to start receiving and sending discord events.
- // It will give us some basic information about discord to use in the future.
- //It can be a good idea to send a inital presence update on this event too, just to setup the inital game state.
- Console.WriteLine("On Ready. RPC Version: {0}", args.Version);
- }
- private static void OnClose(object sender, CloseMessage args)
- {
- //This is called when our client has closed. The client can no longer send or receive events after this message.
- // Connection will automatically try to re-establish and another OnReady will be called (unless it was disposed).
- Console.WriteLine("Lost Connection with client because of '{0}'", args.Reason);
- }
- private static void OnError(object sender, ErrorMessage args)
- {
- //Some error has occured from one of our messages. Could be a malformed presence for example.
- // Discord will give us one of these events and its upto us to handle it
- Console.WriteLine("Error occured within discord. ({1}) {0}", args.Message, args.Code);
- }
- #endregion
- #region Pipe Connection Events
- private static void OnConnectionEstablished(object sender, ConnectionEstablishedMessage args)
- {
- //This is called when a pipe connection is established. The connection is not ready yet, but we have at least found a valid pipe.
- Console.WriteLine("Pipe Connection Established. Valid on pipe #{0}", args.ConnectedPipe);
- }
- private static void OnConnectionFailed(object sender, ConnectionFailedMessage args)
- {
- //This is called when the client fails to establish a connection to discord.
- // It can be assumed that Discord is unavailable on the supplied pipe.
- Console.WriteLine("Pipe Connection Failed. Could not connect to pipe #{0}", args.FailedPipe);
- }
- #endregion
- private static void OnPresenceUpdate(object sender, PresenceMessage args)
- {
- //This is called when the Rich Presence has been updated in the discord client.
- // Use this to keep track of the rich presence and validate that it has been sent correctly.
- Console.WriteLine("Rich Presence Updated. Playing {0}", args.Presence == null ? "Nothing (NULL)" : args.Presence.State);
- }
- #region Subscription Events
- private static void OnSubscribe(object sender, SubscribeMessage args)
- {
- //This is called when the subscription has been made succesfully. It will return the event you subscribed too.
- Console.WriteLine("Subscribed: {0}", args.Event);
- }
- private static void OnUnsubscribe(object sender, UnsubscribeMessage args)
- {
- //This is called when the unsubscription has been made succesfully. It will return the event you unsubscribed from.
- Console.WriteLine("Unsubscribed: {0}", args.Event);
- }
- #endregion
- #region Join / Spectate feature
- private static void OnJoin(object sender, JoinMessage args)
- {
- /*
- * This is called when the Discord Client wants to join a online game to play.
- * It can be triggered from a invite that your user has clicked on within discord or from an accepted invite.
- *
- * The secret should be some sort of encrypted data that will give your game the nessary information to connect.
- * For example, it could be the Game ID and the Game Password which will allow you to look up from the Master Server.
- * Please avoid using IP addresses within these fields, its not secure and defeats the Discord security measures.
- *
- * This feature requires the RegisterURI to be true on the client.
- */
- Console.WriteLine("Joining Game '{0}'", args.Secret);
- }
- private static void OnSpectate(object sender, SpectateMessage args)
- { /*
- * This is called when the Discord Client wants to join a online game to watch and spectate.
- * It can be triggered from a invite that your user has clicked on within discord.
- *
- * The secret should be some sort of encrypted data that will give your game the nessary information to connect.
- * For example, it could be the Game ID and the Game Password which will allow you to look up from the Master Server.
- * Please avoid using IP addresses within these fields, its not secure and defeats the Discord security measures.
- *
- * This feature requires the RegisterURI to be true on the client.
- */
- Console.WriteLine("Spectating Game '{0}'", args.Secret);
- }
- private static void OnJoinRequested(object sender, JoinRequestMessage args)
- {
- /*
- * This is called when the Discord Client has received a request from another external Discord User to join your game.
- * 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
- * other information about the user such as their avatar (which this library will provide a useful link) and their nickname to
- * make it more personalised. You can combine this with more API if you wish. Check the Discord API documentation.
- *
- * Once a user clicks on a response, call the Respond function, passing the message, to respond to the request.
- * A example is provided below.
- *
- * This feature requires the RegisterURI to be true on the client.
- */
- //We have received a request, dump a bunch of information for the user
- Console.WriteLine("'{0}' has requested to join our game.", args.User.Username);
- Console.WriteLine(" - User's Avatar: {0}", args.User.GetAvatarURL(User.AvatarFormat.GIF, User.AvatarSize.x2048));
- Console.WriteLine(" - User's Descrim: {0}", args.User.Discriminator);
- Console.WriteLine(" - User's Snowflake: {0}", args.User.ID);
- Console.WriteLine();
- //Ask the user if they wish to accept the join request.
- Console.Write("Do you give this user permission to join? [Y / n]: ");
- bool accept = Console.ReadKey().Key == ConsoleKey.Y; Console.WriteLine();
- //Tell the client if we accept or not.
- DiscordRpcClient client = (DiscordRpcClient)sender;
- client.Respond(args, accept);
- //All done.
- Console.WriteLine(" - Sent a {0} invite to the client {1}", accept ? "ACCEPT" : "REJECT", args.User.Username);
- }
- #endregion
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement