public static Exception ToError(DiscordApp.Result result)
 {
     if (result == DiscordApp.Result.Ok)
     {
         return(null);
     }
     return(new Exception($"Discord Error: {result}"));
 }
        public static void LogIfError(DiscordApp.Result result)
        {
            Exception error = ToError(result);

            if (error != null)
            {
                Debug.LogException(error);
            }
        }
        public static void RaiseIfError(DiscordApp.Result result)
        {
            Exception error = ToError(result);

            if (error != null)
            {
                throw error;
            }
        }
        public static bool CancelIfError <T>(TaskCompletionSource <T> source,
                                             DiscordApp.Result result)
        {
            Exception error = ToError(result);

            if (error != null)
            {
                source.SetException(error);
            }
            return(error != null);
        }
        private void UpdateActivity(int VersionNumber, Microsoft.AspNetCore.Http.IFormCollection collection)
        {
            //var clientID = Environment.GetEnvironmentVariable("DISCORD_CLIENT_ID");
            string clientID = collection["ClientID"].ToString();

            if (clientID == "")
            {
                clientID = "418559331265675294";
                clientID = "636181032353136640";
            }
            var discord         = new Discord.Discord(long.Parse(clientID), (ulong)Discord.CreateFlags.Default);
            var activityManager = discord.GetActivityManager();
            var activity        = new Discord.Activity
            {
                State   = collection["State"],
                Details = collection["Details"],
                Assets  =
                {
                    LargeImage = collection["LargeImage"],
                    LargeText  = collection["LargeText"],
                    SmallImage = collection["SmallImage"],
                    SmallText  = collection["SmallText"],
                },


                Instance = true,
            };

            if (collection["timeStart"] != "")
            {
                activity.Timestamps.Start = long.Parse(collection["timeStart"]);
            }
            if (collection["timeEnd"] != "")
            {
                activity.Timestamps.End = long.Parse(collection["timeEnd"]);
            }
            ;
            if (collection["EnableParty"].ToString() == "on")
            {
                activity.Party.Id   = Guid.NewGuid().ToString();
                activity.Party.Size = new PartySize
                {
                    CurrentSize = collection["CurrentSize"] == "" ? 1 : int.Parse(collection["CurrentSize"]),
                    MaxSize     = collection["MaxSize"] == "" ? 1 : int.Parse(collection["MaxSize"])
                };
                activity.Secrets = new Discord.ActivitySecrets
                {
                    Join     = Guid.NewGuid().ToString(),
                    Match    = Guid.NewGuid().ToString(),
                    Spectate = Guid.NewGuid().ToString(),
                };
            }
            Discord.Result result1 = new Discord.Result();
            activityManager.UpdateActivity(activity, result =>
            {
                Console.WriteLine("Update Activity {0}", result);
                result1 = result;
                // Send an invite to another user for this activity.
                // Receiver should see an invite in their DM.
                // Use a relationship user's ID for this.
                // activityManager
                //   .SendInvite(
                //       364843917537050624,
                //       Discord.ActivityActionType.Join,
                //       "",
                //       inviteResult =>
                //       {
                //           Console.WriteLine("Invite {0}", inviteResult);
                //       }
                //   );
            });
            try
            {
                while (VersionNumber == (int)_cache.Get("DiscordActivityVersion"))
                {
                    discord.RunCallbacks();
                    Thread.Sleep(1000 / 10);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                discord.Dispose();
                _cache.Set("DisposeVersionCode", VersionNumber);
            }
        }
 private static void UpdateActivityCallback(Discord.Result result)
 {
     Log.Info("Updated Activity. Result: " + result);
 }
 private static void ClearActivityCallback(Discord.Result result)
 {
     Log.Info("Cleared Activity. Result: " + result);
 }