コード例 #1
0
        public async Task <RuntimeResult> CancelDateAsync(
            [Summary("The date on which the session will take place.")] string date)
        {
            var campaign = await _campaignService.GetByTextChannelId(Context.Channel.Id);

            if (campaign == null)
            {
                return(GameMasterResult.ErrorResult("you are not in a campaign text channel."));
            }

            if (!DateTime.TryParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var parsedDate))
            {
                return(GameMasterResult.ErrorResult("the date you entered was invalid."));
            }

            // Check to make sure that this user is the game master of the campaign
            var commandIssuer = Context.Guild.GetUser(Context.User.Id);

            if (campaign.GameMaster.User.DiscordId != Context.User.Id && !commandIssuer.GuildPermissions.Administrator)
            {
                return(GameMasterResult.ErrorResult("you do not have permission to cancel a session for this campaign. You must either be the Game Master of this campaign or a Server Administrator."));
            }

            try
            {
                await _sessionService.CancelForDate(campaign.Id, parsedDate);
                await ReplyAsync($"Session planned for {parsedDate.ToShortDateString()} cancelled successfully.");

                return(GameMasterResult.SuccessResult());
            }
            catch (Exception e)
            {
                return(GameMasterResult.ErrorResult(e.Message));
            }
        }