c# - Why discord net doesn't work when I open and close games fast - Stack Overflow

So basically game is getting added to users even if I open it fast and close but it doesn't get re

So basically game is getting added to users even if I open it fast and close but it doesn't get removed

if (oldPresence.Activities.Count(e => Convert.ToString(e.Type) == "Playing") >
    newPresence.Activities.Count(e => Convert.ToString(e.Type) == "Playing")) {
    foreach (var game in oldPresence.Activities) {
        if (!newPresence.Activities.Contains(game)) {
            gameName = game.Name;
            break;
        }
    }
    shouldDo = true;
}

// this is basically handler to closing game
if (oldPresence.Activities.Any() && shouldDo == true) {
    // we create User object here and also get time game was open
    User localUser =
        new User(Convert.ToString(user), Convert.ToString(gameName));
    if (users.Any(e => e.userName == localUser.userName &&
                       e.game == localUser.game)) {
        Console.WriteLine(gameName);
        int index = users.FindIndex(u => u.userName == localUser.userName &&
                                         u.game == localUser.game);
        Console.WriteLine(DateTime.Now.Subtract(users[index].date));
        // dataBase.AddPlayerGame(1, "saba",
        // DateTime.Now.Subtract(users[index].date));
        users.RemoveAt(index);
    }
}

if (shouldDo == false &&
    user.Activities.Any(e => Convert.ToString(e.Type) == "Playing")) {
    string newGame = Convert.ToString(newPresence.Activities.First().Name);
    User localUser = new User(Convert.ToString(user), newGame);
    if (!users.Any(e => e.userName == localUser.userName &&
                        e.game == localUser.game)) {
        users.Add(localUser);
        Console.WriteLine("game added");
    }
}

I also run method in dedicated thread like this

Thread test =
    new Thread(() => ActivityHandler(user, oldPresence, newPresence)) {
        Name = Convert.ToString(ThreadCount)
    };
ThreadCount++;
test.Start();

and I call this last method (ThreadManager) on PresenceUpdated

I have tried to use lock but it doesn't work, also Join doesn't work and running it on a single thread doesn't work too.

I don't have slow internet (Download Mbps 31.84 and Upload Mbps 2.80);

Also worth mentioning that most of the test was done with adding multiple games in discord with Registered games and removing them fast.

So basically game is getting added to users even if I open it fast and close but it doesn't get removed

if (oldPresence.Activities.Count(e => Convert.ToString(e.Type) == "Playing") >
    newPresence.Activities.Count(e => Convert.ToString(e.Type) == "Playing")) {
    foreach (var game in oldPresence.Activities) {
        if (!newPresence.Activities.Contains(game)) {
            gameName = game.Name;
            break;
        }
    }
    shouldDo = true;
}

// this is basically handler to closing game
if (oldPresence.Activities.Any() && shouldDo == true) {
    // we create User object here and also get time game was open
    User localUser =
        new User(Convert.ToString(user), Convert.ToString(gameName));
    if (users.Any(e => e.userName == localUser.userName &&
                       e.game == localUser.game)) {
        Console.WriteLine(gameName);
        int index = users.FindIndex(u => u.userName == localUser.userName &&
                                         u.game == localUser.game);
        Console.WriteLine(DateTime.Now.Subtract(users[index].date));
        // dataBase.AddPlayerGame(1, "saba",
        // DateTime.Now.Subtract(users[index].date));
        users.RemoveAt(index);
    }
}

if (shouldDo == false &&
    user.Activities.Any(e => Convert.ToString(e.Type) == "Playing")) {
    string newGame = Convert.ToString(newPresence.Activities.First().Name);
    User localUser = new User(Convert.ToString(user), newGame);
    if (!users.Any(e => e.userName == localUser.userName &&
                        e.game == localUser.game)) {
        users.Add(localUser);
        Console.WriteLine("game added");
    }
}

I also run method in dedicated thread like this

Thread test =
    new Thread(() => ActivityHandler(user, oldPresence, newPresence)) {
        Name = Convert.ToString(ThreadCount)
    };
ThreadCount++;
test.Start();

and I call this last method (ThreadManager) on PresenceUpdated

I have tried to use lock but it doesn't work, also Join doesn't work and running it on a single thread doesn't work too.

I don't have slow internet (Download Mbps 31.84 and Upload Mbps 2.80);

Also worth mentioning that most of the test was done with adding multiple games in discord with Registered games and removing them fast.

Share edited Mar 8 at 2:41 nalka 2,3882 gold badges18 silver badges34 bronze badges asked Mar 7 at 21:14 skami0_0skami0_0 413 bronze badges 1
  • Please share a minimal reproducible example. – mjwills Commented Mar 8 at 3:37
Add a comment  | 

1 Answer 1

Reset to default 0

My best guess is that you're unable to locate the correct user, possibly because the game property doesn't match the value you're expecting. I'd recommend adding an else statement after the if's where you're trying to find the user and logging the fact that you were unable to locate the user. This should help narrow down where it's going wrong.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744909310a4600448.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信