async Task MessageCreated(MessageCreateEventArgs __) { if (!IsInitialized) { return; } var channel = __.Channel; var author = __.Author; var authorId = author.Id; var message = __.Message; var content = string.Join(' ', message.Content.Split(' ').Where(_ => _ != "")); if (author.IsCurrent) { return; } if (message.MessageType != MessageType.Default) { return; } if (channel.Type == ChannelType.Private) { var game = GamesByUsers.GetValueOrDefault(authorId); if (game is DisboardGameUsingDM) { var player = game.InitialPlayers.Where(_ => _.Id == authorId).FirstOrDefault(); if (player != null) { await OnDM(game, player, content); } } else { await channel.SendMessageAsync("`진행중인 게임이 없습니다.`"); } } else if (channel.Type == ChannelType.Text) { var game = Games.GetValueOrDefault(channel.Id); var split = content.Split(" ").ToList(); if (split.Count > 0 && split[0].ToLower() == "bot") { var mentionedUsers = __.MentionedUsers.Distinct().Where(_ => !_.IsCurrent); await OnBot(game, channel, authorId, content, mentionedUsers); } else { if (game != null) { var player = game.InitialPlayers.Where(_ => _.Id == authorId).FirstOrDefault(); if (player != null) { await OnGroup(game, player, content); } } } } }
async Task MessageReactionAdded(MessageReactionAddEventArgs __) { if (!IsInitialized) { return; } var channel = __.Channel; var user = __.User; var userId = __.User.Id; var message = __.Message; var emoji = __.Emoji; if (user.IsCurrent) { return; } if (message.MessageType != MessageType.Default) { return; } if (false == message.Author.IsCurrent) { return; } if (channel.Type == ChannelType.Private) { var game = GamesByUsers.GetValueOrDefault(userId); if (game is DisboardGameUsingDM) { var player = game.InitialPlayers.Where(_ => _.Id == userId).FirstOrDefault(); if (player != null) { await OnDMReaction(game, player, emoji); } } else { await channel.SendMessageAsync("`진행중인 게임이 없습니다.`"); } } else if (channel.Type == ChannelType.Text) { var game = Games.GetValueOrDefault(channel.Id); if (game != null) { var player = game.InitialPlayers.Where(_ => _.Id == userId).FirstOrDefault(); if (player != null) { await OnGroupReaction(game, player, emoji); } } } }
async Task OnBot(DisboardGame?game, DiscordChannel channel, UserIdType authorId, string content, IEnumerable <DiscordUser> mentionedUsers) { var split = content.Split(" ").ToList(); GuildIdType guildId = channel.GuildId; if (split.Count > 1 && split[1].ToLower() == "start") { if (game == null) { if (mentionedUsers.Count() == split.Count - 2) { await NewGame(channel, mentionedUsers); } else { await channel.SendMessageAsync("`BOT start @참가인원1 @참가인원2... 로 게임을 시작합니다.`"); } } else { await channel.SendMessageAsync("`진행중인 게임이 있습니다. BOT restart @참가인원1 @참가인원2...는 어떨까요?`"); } } else if (split.Count > 1 && split[1].ToLower() == "restart") { if (game == null) { await channel.SendMessageAsync("`진행중인 게임이 없습니다. BOT start @참가인원1 @참가인원2...는 어떨까요?`"); } else { if (mentionedUsers.Count() == split.Count - 2) { await NewGame(channel, mentionedUsers); } else { await channel.SendMessageAsync("`BOT restart @참가인원1 @참가인원2... 로 게임을 시작합니다.`"); } } } else if (split.Count == 2 && split[1].ToLower() == "help") { if (game == null) { await channel.SendMessageAsync("`BOT start @참가인원1 @참가인원2...로 게임을 시작합니다.`"); } else { var player = game.InitialPlayers.Where(_ => _.Id == authorId).FirstOrDefault(); if (player == null) { await channel.SendMessageAsync("`BOT restart @참가인원1 @참가인원2... 로 게임을 시작합니다.`"); } } var messageQueue = new ConcurrentQueue <Func <Task> >(); void task() => GameFactory.OnHelp(new DisboardChannel(channel, messageQueue, STADispatcher)); await RunAndProcessMessage(task, messageQueue); } else if (split.Count > 1 && split[1].ToLower() == "restoredm") { if (game == null) { await channel.SendMessageAsync("`진행중인 게임이 없습니다. BOT start @참가인원1 @참가인원2...는 어떨까요?`"); } else if (false == game is DisboardGameUsingDM) { await channel.SendMessageAsync("`DM을 사용하지 않는 게임입니다.`"); } else { var player = game.InitialPlayers.Where(_ => _.Id == authorId).FirstOrDefault(); if (player == null) { await channel.SendMessageAsync("`게임에 참여하고 있지 않습니다. 게임에 참여하려면 BOT restart @참가인원1 @참가인원2...로 게임을 다시 시작해야 합니다.`"); } else { var gameUsesDM = game as DisboardGameUsingDM; var member = await channel.Guild.GetMemberAsync(authorId); var dMChannel = await member.CreateDmChannelAsync(); if (GamesByUsers.GetValueOrDefault(authorId) == game) { await dMChannel.SendMessageAsync("`이곳에 입력하는 메시지는 해당 채널의 게임으로 전달됩니다.`"); } else { GamesByUsers.Remove(authorId, out _); GamesByUsers.TryAdd(authorId, gameUsesDM !); await dMChannel.SendMessageAsync("`복원되었습니다. 이제부터 이곳에 입력하는 메시지는 해당 채널의 게임으로 전달됩니다.`"); } } } } else { await channel.SendMessageAsync("`명령어: BOT start, BOT restart, BOT help, BOT restoredm`"); } }