public static async Task <IRole> GetMuteRole(IGuild guild) { const string defaultMuteRoleName = "nadeko-mute"; var muteRoleName = GuildMuteRoles.GetOrAdd(guild.Id, defaultMuteRoleName); var muteRole = guild.Roles.FirstOrDefault(r => r.Name == muteRoleName); if (muteRole == null) { //if it doesn't exist, create it try { muteRole = await guild.CreateRoleAsync(muteRoleName, GuildPermissions.None).ConfigureAwait(false); } catch { //if creations fails, maybe the name is not correct, find default one, if doesn't work, create default one muteRole = guild.Roles.FirstOrDefault(r => r.Name == muteRoleName) ?? await guild.CreateRoleAsync(defaultMuteRoleName, GuildPermissions.None).ConfigureAwait(false); } foreach (var toOverwrite in guild.GetTextChannels()) { try { await toOverwrite.AddPermissionOverwriteAsync(muteRole, new OverwritePermissions(sendMessages : PermValue.Deny, attachFiles : PermValue.Deny)) .ConfigureAwait(false); } catch { } await Task.Delay(200).ConfigureAwait(false); } } return(muteRole); }
public async Task <IRole> GetMuteRole(IGuild guild) { if (guild == null) { throw new ArgumentNullException(nameof(guild)); } const string defaultMuteRoleName = "evilMorty-mute"; var muteRoleName = GuildMuteRoles.GetOrAdd(guild.Id, defaultMuteRoleName); var muteRole = guild.Roles.FirstOrDefault(r => r.Name == muteRoleName); if (muteRole == null) { //if it doesn't exist, create it try { muteRole = await guild.CreateRoleAsync(muteRoleName, GuildPermissions.None).ConfigureAwait(false); } catch { //if creations fails, maybe the name is not correct, find default one, if doesn't work, create default one muteRole = guild.Roles.FirstOrDefault(r => r.Name == muteRoleName) ?? await guild.CreateRoleAsync(defaultMuteRoleName, GuildPermissions.None).ConfigureAwait(false); } } foreach (var toOverwrite in (await guild.GetTextChannelsAsync())) { try { if (!toOverwrite.PermissionOverwrites.Any(x => x.TargetId == muteRole.Id && x.TargetType == PermissionTarget.Role)) { await toOverwrite.AddPermissionOverwriteAsync(muteRole, denyOverwrite) .ConfigureAwait(false); await Task.Delay(200).ConfigureAwait(false); } } catch { // ignored } } return(muteRole); }
public async Task <IRole> GetMuteRole(IGuild guild) { const string defaultMuteRoleName = "Muted"; var muteRoleName = GuildMuteRoles.GetOrAdd(guild.Id, defaultMuteRoleName); var muteRole = guild.Roles.FirstOrDefault(r => r.Name == muteRoleName); if (muteRole != null) { return(muteRole); } //if it doesn't exist, create it try { muteRole = await guild.CreateRoleAsync(muteRoleName, GuildPermissions.None).ConfigureAwait(false); } catch { //if creations fails, maybe the name is not correct, find default one, if doesn't work, create default one muteRole = guild.Roles.FirstOrDefault(r => r.Name == muteRoleName) ?? await guild.CreateRoleAsync(defaultMuteRoleName, GuildPermissions.None).ConfigureAwait(false); } //foreach (var toOverwrite in (await guild.GetTextChannelsAsync())) //{ // try // { // if (!toOverwrite.PermissionOverwrites.Select(x => x.Permissions).Contains(denyOverwrite)) // { // await toOverwrite.AddPermissionOverwriteAsync(muteRole, denyOverwrite) // .ConfigureAwait(false); // // await Task.Delay(200).ConfigureAwait(false); // } // } // catch // { // // ignored // } //} return(muteRole); }