public static async Task War_Prep_Started(IGuildConfig cfg, byte WarNo) { //Guild has elected out for this notification. if (!cfg[Setting_Key.WAR_PREP_STARTED].Enabled) { return; } //Guild elected out of this specific war. else if (!shouldSendSpecificWar(cfg, WarNo)) { return; } ///Determine the message to send. string Message = ""; if (cfg[Setting_Key.WAR_PREP_STARTED].HasValue) { Message = getMessageForSpecificWar(cfg[Setting_Key.WAR_PREP_STARTED].Value, WarNo); if (!string.IsNullOrEmpty(Message)) { await sendWarMessage(cfg, Message); return; } //If the message is empty- will revert to using the default message instead. } //Send a default message. if (cfg.GetGuildRole(RoleLevel.Member).IsNotNull(out IRole role) && role.IsMentionable) { Message = $"{role.Mention}, WAR Placement has started! Please place your troops in the next two hours!"; }
/// <summary> /// This will return a guild user's current WarBot Role. /// Only used inside of guild-context. /// </summary> /// <param name="User"></param> /// <param name="cfg"></param> /// <returns></returns> public static RoleLevel GetRole(this SocketGuildUser User, IGuildConfig cfg) { //Yup, this is my statically defined userID. Its stupid, and it works. Will update one day. //ToDo - Update global admin selection. if (User.Id == 381654208073433091) { return(RoleLevel.GlobalAdmin); } //If the user has Administrator permissions for the guild, return ServerAdmin role. if (User.GuildPermissions.Administrator) { return(RoleLevel.ServerAdmin); } //Loop through all Guild Roles, Highest to Lowest. //None is excluded, because it should never have a guild role assigned. //Global admin is excluded, because it is only specified by the first statement in this method. foreach (var Role in GetRoleValues().Where(o => o != RoleLevel.GlobalAdmin && o != RoleLevel.None).OrderByDescending(o => (int)o)) { ulong?guildRoleId = cfg.GetGuildRole(Role)?.Id; if (guildRoleId.HasValue && User.Roles.Any(o => o.Id == guildRoleId)) { return(Role); } } //No roles found. return(RoleLevel.None); }
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(); }
public void Apply(IGuildConfig cfg) { //3.7 added a new portal channel. For guilds we update- make sure to set the portal channel, to the old war channel. cfg.SetGuildChannel(WarBotChannelType.PORTAL, cfg.GetGuildChannel(WarBotChannelType.WAR)); //The new user notifications do not include the mention by default. Lets mimmick the setting. var uj = cfg[Setting_Key.USER_JOIN]; if (uj.Enabled && uj.HasValue) { uj.Value = "{user}, " + uj.Value; } }
public static async Task Portal_Opened(IGuildConfig cfg) { ///Determine the message to send. string Message = ""; if (!cfg[Setting_Key.PORTAL_STARTED].HasValue) { if (cfg.GetGuildRole(RoleLevel.Member).IsNotNull(out var role) && role.IsMentionable) { Message = $"{role.Mention}, The portal has opened!"; } else { Message = "The portal has opened!"; } }
/// <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)); } }
/// <summary> /// This command will attempt to get a message to a guild's owner either via a channel in the guild, or via direct DM. /// </summary> /// <param name="cfg"></param> /// <param name="ErrorMessage"></param> /// <returns></returns> public async Task <bool> MessageServerLeadership(IGuildConfig cfg, string ErrorMessage) { try { //Else, we don't have permissions to the WAR Channel. Send a notification to the officers channel. SocketTextChannel och = cfg.GetGuildChannel(WarBotChannelType.CH_Officers) as SocketTextChannel; if (och != null && PermissionHelper.TestBotPermission(och, ChannelPermission.SendMessages)) { await och.SendMessageAsync(ErrorMessage); return(true); } } catch (Exception ex) { await Error(cfg.Guild, ex, nameof(MessageServerLeadership) + ".Officer_Channel"); } //Either the officers channel is not configured, or we do not have permissions to send to it. //Lets attempt to DM the guild's owner instead. try { IDMChannel dm = await cfg.Guild.Owner.GetOrCreateDMChannelAsync(); await dm.SendMessageAsync(ErrorMessage); return(true); } catch (Exception ex) { await Error(cfg.Guild, ex, nameof(MessageServerLeadership) + ".DM_Owner"); } //We were unsuccessful in sending a message. return(false); }
/// <summary> /// Determine is a guild is elected into a specific war. /// </summary> /// <param name="cfg"></param> /// <param name="WarNo"></param> /// <returns></returns> private static bool shouldSendSpecificWar(IGuildConfig cfg, byte WarNo) { if (WarNo == 1) { return(cfg[Setting_Key.WAR_1].Enabled); } if (WarNo == 2) { return(cfg[Setting_Key.WAR_2].Enabled); } if (WarNo == 3) { return(cfg[Setting_Key.WAR_3].Enabled); } if (WarNo == 4) { return(cfg[Setting_Key.WAR_4].Enabled); } else { throw new ArgumentOutOfRangeException("There are only 4 wars. The value passed was not between 1 and 4."); } }
public string GetUsage(CommandInfo cmd, IGuildConfig cfg) { return(Usage .Replace("{command}", cmd.Name, StringComparison.OrdinalIgnoreCase) .Replace("{prefix}", cfg.Prefix, StringComparison.OrdinalIgnoreCase)); }
public void Apply(IGuildConfig config) { //No changes. }
public GuildCommandContext(DiscordSocketClient Client, SocketUserMessage Message, IGuildConfig Config, IWARBOT WarBot) : base(Client, Message, WarBot) { this.cfg = Config; }