private async Task updateConfigIfRequired(IGuildConfig Cfg)
        {
            if (!String.IsNullOrEmpty(Cfg.BotVersion) && Double.TryParse(Cfg.BotVersion, out double CurrentVersion))
            {
                if (CurrentVersion == CurrentUpdate.Version)
                {
                    //The bot is up to date, nothing to do.
                    return;
                }
                else
                {
                    foreach (IBotUpdate update in Updates.Where(o => o.Version > CurrentVersion).OrderBy(o => o.Version))
                    {
                        //Apply the update.
                        update.Apply(Cfg);

                        //Determine if we should send an update to the guild. If so, send the update.
                        bool updateWasSent = false;

                        if (Cfg.GetGuildChannel(WarBotChannelType.WARBOT_UPDATES).IsNotNull(out var CH)
                            //Validate this update, should sent a message out.
                            && update.SendUpdateToGuild
                            //Validate the guild is opt-in to receive update messages.
                            && Cfg[Setting_Key.WARBOT_UPDATES].Enabled
                            //Validate I have permissions to send to this channel.
                            && Cfg.CurrentUser.GetPermissions(CH).SendMessages)
                        {
                            updateWasSent = true;
                            var eb = new EmbedBuilder()
                                     .WithTitle($"WarBot updated to version {update.Version}")
                                     .WithDescription($"I have been updated to version {update.Version} 👏")
                                     .WithFooter("To view my patch notes, click this embed.")
                                     .WithUrl(update.ReleaseNotesURL);

                            await CH.SendMessageAsync(embed : eb.Build());
                        }

                        await bot.Log.GuildUpdated(Cfg.Guild.Name, CurrentVersion, update.Version, updateWasSent);

                        //Update the local current version.
                        CurrentVersion = update.Version;
                    }

                    Cfg.BotVersion = CurrentUpdate.Version.ToString();
                }
            }
            else //Unable to parse the version. Assume no updates are required.
            {
                Cfg.BotVersion = CurrentUpdate.Version.ToString();
            }


            //Save the changes.
            await Cfg.SaveConfig();
        }
示例#2
0
        /// <summary>
        /// Sends an embed to the selected channel, if we have the proper permissions.
        /// Else- it will DM the owner of the guild.
        /// </summary>
        /// <param name="cfg"></param>
        /// <param name="embed"></param>
        ///
        /// <returns></returns>
        private static async Task sendWarMessage(IGuildConfig cfg, string Message)
        {
            SocketTextChannel ch = cfg.GetGuildChannel(WarBotChannelType.WAR) as SocketTextChannel;

            //If there is no channel configured, abort.
            if (ch == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(Message))
            {
                throw new NullReferenceException("War message is empty?");
            }

            //If we can send to the WAR channel, and we have permissions.
            if (PermissionHelper.TestBotPermission(ch, ChannelPermission.SendMessages))
            {
                await ch.SendMessageAsync(Message);

                return;
            }

            //Handle missing permissions below this line.
            await cfg.Log.Error(cfg.Guild, new Exception($"Missing SEND_PERMISSIONS for channel {ch.Name} for guild {cfg.Guild.Name}"));

            StringBuilder sb = new StringBuilder()
                               .AppendLine("ERROR: Missing Permissions")
                               .AppendLine($"You are receiving this error, because I do not have the proper permissions to send the war notification to channel {ch.Name}.")
                               .AppendLine("Please validate I have the 'SEND_MESSAGES' permission for the specified channel.");

            bool messageSent = await cfg.Log.MessageServerLeadership(cfg, sb.ToString());

            //If we were unsuccessful in delivaring a message to the guid's leadership, disable the message.
            if (!messageSent)
            {
                //Well, out of options. Lets disable this channel for the guild.
                cfg.SetGuildChannel(WarBotChannelType.WAR, null);
                await cfg.SaveConfig();

                UnauthorizedAccessException error = new UnauthorizedAccessException("Missing permissions to send to WAR Channel. WAR messages disabled for this guild.");
                await cfg.Log.Error(cfg.Guild, error, nameof(sendWarMessage));
            }
        }