Ejemplo n.º 1
0
        private async Task ExecuteGameInputAsync(IReactionsGame game, SocketReaction reaction, IUserMessage message, ISocketMessageChannel channel)
        {
            var userId = reaction.UserId;
            var user   = reaction.User.GetValueOrDefault() ?? client.GetUser(reaction.UserId);
            var guild  = (channel as SocketTextChannel)?.Guild;

            log.Verbose(
                $"Input {reaction.Emote.ReadableName()} by {user?.FullName()} in {channel.FullName()}",
                game.GameName);

            await game.InputAsync(reaction.Emote, userId);

            if (game.State != GameState.Active)
            {
                if (!(game is IUserGame))
                {
                    games.Remove(game);
                }

                if (game is PacManGame pmGame && pmGame.State != GameState.Cancelled && !pmGame.custom)
                {
                    storage.AddScore(new ScoreEntry(pmGame.score, userId, pmGame.State, pmGame.Time,
                                                    user?.NameandDisc(), $"{guild?.Name}/{channel.Name}", DateTime.Now));
                }

                if (channel.BotCan(ChannelPermission.ManageMessages))
                {
                    await message.RemoveAllReactionsAsync(PmBot.DefaultOptions);
                }
            }

            game.CancelRequests();
            try { await message.ModifyAsync(game.GetMessageUpdate(), game.GetRequestOptions()); }
            catch (OperationCanceledException) { }
        }
Ejemplo n.º 2
0
        private async void HandleReaction(SocketReaction reaction, Cacheable <IUserMessage, ulong> messageData, ISocketMessageChannel channel)
        {
            try
            {
                if (!channel.BotCan(ChannelPermission.SendMessages | ChannelPermission.ReadMessageHistory))
                {
                    return;
                }

                var message = reaction.Message.GetValueOrDefault() ?? await messageData.GetOrDownloadAsync();

                if (reaction.UserId != client.CurrentUser.Id && message?.Author.Id == client.CurrentUser.Id)
                {
                    await ReactionGameInputAsync(reaction, message, channel);
                }
            }
            catch (Exception e)
            {
                log.Exception($"In {channel.FullName()}", e);
            }
        }
Ejemplo n.º 3
0
        /// <summary>Tries to find a game and execute reaction input. Returns whether it is successful.</summary>
        private async ValueTask <bool> ReactionGameInputAsync(SocketReaction reaction, IUserMessage message, ISocketMessageChannel channel)
        {
            var game = games.AllGames
                       .OfType <IReactionsGame>()
                       .FirstOrDefault(g => g.MessageId == message.Id && g.IsInput(reaction.Emote, reaction.UserId));

            if (game == null)
            {
                return(false);
            }

            try
            {
                await ExecuteGameInputAsync(game, reaction, message, channel);
            }
            catch (Exception e)
            {
                log.Exception($"During input \"{reaction.Emote.ReadableName()}\" in {channel.FullName()}", e, game.GameName);
            }

            return(true);
        }