public async Task <DiscordRole> GetOrCreateMuteRoleAsync(DiscordGuild guild) { DiscordRole muteRole = null; await this.csem.WaitAsync(); try { using (var dc = this.shard.Database.CreateContext()) { DatabaseGuildConfiguration gcfg = guild.GetGuildConfiguration(dc); muteRole = guild.GetRole(gcfg.MuteRoleId); if (muteRole is null) { muteRole = guild.Roles.Values.FirstOrDefault(r => r.Name.ToLowerInvariant() == "f_mute"); } if (muteRole is null) { muteRole = await guild.CreateRoleAsync("f_mute", hoist : false, mentionable : false); foreach (var channel in guild.Channels.Values.Where(c => c.Type == ChannelType.Text)) { await channel.AddOverwriteAsync(muteRole, deny : Permissions.SendMessages | Permissions.SendTtsMessages | Permissions.AddReactions); await Task.Delay(100); } gcfg.MuteRoleId = muteRole.Id; dc.GuildConfiguration.Update(gcfg); await dc.SaveChangesAsync(); } } } finally { this.csem.Release(); } return(muteRole); }
public async Task ExecuteGroupAsync(CommandContext ctx, [Description("Enable?")] bool enable, [Description("Log channel.")] DiscordChannel channel = null) { channel = channel ?? ctx.Channel; if (channel.Type != ChannelType.Text) { throw new CommandFailedException("Action logging channel must be a text channel."); } DatabaseGuildConfiguration gcfg = await this.ModifyGuildConfigurationAsync(ctx.Guild.Id, cfg => { cfg.LogChannelIdDb = enable ? (long?)channel.Id : null; }); var logchn = this.Shared.GetLogChannelForGuild(ctx.Client, ctx.Guild); if (!(logchn is null)) { var emb = new DiscordEmbedBuilder { Title = "Guild config changed", Color = this.ModuleColor }; emb.AddField("User responsible", ctx.User.Mention, inline: true); emb.AddField("Invoked in", ctx.Channel.Mention, inline: true); emb.AddField("Logging channel set to", gcfg.LogChannelId.ToString(), inline: true); await logchn.SendMessageAsync(embed : emb.Build()); } await this.InformAsync(ctx, $"{Formatter.Bold(gcfg.LoggingEnabled ? "Enabled" : "Disabled")} action logs.", important : false); }
public static DatabaseGuildConfiguration GetGuildSettings(this DiscordGuild guild, DatabaseContextBuilder dcb) { DatabaseGuildConfiguration gcfg = null; using (var dc = dcb.CreateContext()) gcfg = guild.GetGuildConfiguration(dc); return(gcfg); }
private static async Task RegisterGuildAsync(SharedData shared, DatabaseContextBuilder dcb, ulong gid) { shared.GuildConfigurations.TryAdd(gid, CachedGuildConfiguration.Default); using (var dc = dcb.CreateContext()) { var gcfg = new DatabaseGuildConfiguration { GuildId = gid }; if (!dc.GuildConfiguration.Contains(gcfg)) { dc.GuildConfiguration.Add(gcfg); await dc.SaveChangesAsync(); } } }