- Updated editorconfig rules to hopefully look a bit nicer.

- Removed configureawait(false) from everywhere as it doesnt' do anything in a console app and just makes the code look ugly
- Started using .WhenAll extension instead of Task.WhenAll to make it look nicer when chaining methods
This commit is contained in:
Kwoth
2021-12-28 21:14:26 +01:00
parent d093f7eed7
commit 723447c7d4
171 changed files with 1523 additions and 1594 deletions

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Database.Models;
using NadekoBot.Db;
@@ -52,14 +52,14 @@ public class AdministrationService : INService
if (state && cmd.Name != "prune" && cmd.Name != "pick")
{
_logService.AddDeleteIgnore(msg.Id);
try { await msg.DeleteAsync().ConfigureAwait(false); } catch { }
try { await msg.DeleteAsync(); } catch { }
}
//if state is false, that means do not do it
}
else if (DeleteMessagesOnCommand.Contains(channel.Guild.Id) && cmd.Name != "prune" && cmd.Name != "pick")
{
_logService.AddDeleteIgnore(msg.Id);
try { await msg.DeleteAsync().ConfigureAwait(false); } catch { }
try { await msg.DeleteAsync(); } catch { }
}
});
return Task.CompletedTask;
@@ -128,7 +128,7 @@ public class AdministrationService : INService
{
try
{
await u.ModifyAsync(usr => usr.Deaf = value).ConfigureAwait(false);
await u.ModifyAsync(usr => usr.Deaf = value);
}
catch
{

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System.Threading.Channels;
using LinqToDB;
using Microsoft.EntityFrameworkCore;
@@ -50,8 +50,8 @@ public sealed class AutoAssignRoleService : INService
if (roleIds.Any())
{
await user.AddRolesAsync(roleIds).ConfigureAwait(false);
await Task.Delay(250).ConfigureAwait(false);
await user.AddRolesAsync(roleIds);
await Task.Delay(250);
}
else
{

View File

@@ -118,7 +118,7 @@ public class GameVoiceChannelService : INService
if (vch is null)
return;
await Task.Delay(1000).ConfigureAwait(false);
await gUser.ModifyAsync(gu => gu.Channel = vch).ConfigureAwait(false);
await Task.Delay(1000);
await gUser.ModifyAsync(gu => gu.Channel = vch);
}
}

View File

@@ -90,7 +90,7 @@ public sealed class LogCommandService : ILogCommandService
{
var keys = PresenceUpdates.Keys.ToList();
await Task.WhenAll(keys.Select(key =>
await keys.Select(key =>
{
if (!((SocketGuild) key.Guild).CurrentUser.GetPermissions(key).SendMessages)
return Task.CompletedTask;
@@ -102,7 +102,7 @@ public sealed class LogCommandService : ILogCommandService
}
return Task.CompletedTask;
})).ConfigureAwait(false);
}).WhenAll();
}, null, TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(15));
//_client.MessageReceived += _client_MessageReceived;
@@ -229,7 +229,7 @@ public sealed class LogCommandService : ILogCommandService
ITextChannel logChannel;
if ((logChannel =
await TryGetLogChannel(g, logSetting, LogType.UserUpdated).ConfigureAwait(false)) is null)
await TryGetLogChannel(g, logSetting, LogType.UserUpdated)) is null)
return;
var embed = _eb.Create();
@@ -263,7 +263,7 @@ public sealed class LogCommandService : ILogCommandService
return;
}
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
catch
{
@@ -360,8 +360,7 @@ public sealed class LogCommandService : ILogCommandService
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.VoicePresenceTTS)
.ConfigureAwait(false)) is null)
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.VoicePresenceTTS)) is null)
return;
var str = string.Empty;
@@ -378,7 +377,7 @@ public sealed class LogCommandService : ILogCommandService
str = GetText(logChannel.Guild, strs.log_vc_left(usr.Username, beforeVch.Name));
}
var toDelete = await logChannel.SendMessageAsync(str, true).ConfigureAwait(false);
var toDelete = await logChannel.SendMessageAsync(str, true);
toDelete.DeleteAfter(5);
}
catch
@@ -400,8 +399,7 @@ public sealed class LogCommandService : ILogCommandService
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserMuted)
.ConfigureAwait(false)) is null)
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserMuted)) is null)
return;
var mutes = string.Empty;
var mutedLocalized = GetText(logChannel.Guild, strs.muted_sn);
@@ -424,7 +422,7 @@ public sealed class LogCommandService : ILogCommandService
.WithFooter(CurrentTime(usr.Guild))
.WithOkColor();
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
catch
{
@@ -444,8 +442,7 @@ public sealed class LogCommandService : ILogCommandService
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserMuted)
.ConfigureAwait(false)) is null)
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserMuted)) is null)
return;
var mutes = string.Empty;
@@ -472,7 +469,7 @@ public sealed class LogCommandService : ILogCommandService
if (!string.IsNullOrWhiteSpace(reason))
embed.WithDescription(reason);
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
catch
{
@@ -495,8 +492,7 @@ public sealed class LogCommandService : ILogCommandService
|| logSetting.LogOtherId is null)
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(users.First().Guild, logSetting, LogType.Other)
.ConfigureAwait(false)) is null)
if ((logChannel = await TryGetLogChannel(users.First().Guild, logSetting, LogType.Other)) is null)
return;
var punishment = string.Empty;
@@ -525,7 +521,7 @@ public sealed class LogCommandService : ILogCommandService
.WithFooter(CurrentTime(logChannel.Guild))
.WithOkColor();
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
catch
{
@@ -570,8 +566,7 @@ public sealed class LogCommandService : ILogCommandService
ITextChannel logChannel;
if (logSetting.UserUpdatedId != null &&
(logChannel = await TryGetLogChannel(before.Guild, logSetting, LogType.UserUpdated)
.ConfigureAwait(false)) != null)
(logChannel = await TryGetLogChannel(before.Guild, logSetting, LogType.UserUpdated)) != null)
{
var embed = _eb.Create().WithOkColor()
.WithFooter(CurrentTime(before.Guild))
@@ -584,7 +579,7 @@ public sealed class LogCommandService : ILogCommandService
.AddField(GetText(logChannel.Guild, strs.new_nick)
, $"{after.Nickname}#{after.Discriminator}");
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
else if (!before.Roles.SequenceEqual(after.Roles))
{
@@ -594,7 +589,7 @@ public sealed class LogCommandService : ILogCommandService
embed.WithAuthor("⚔ " + GetText(logChannel.Guild, strs.user_role_add))
.WithDescription(string.Join(", ", diffRoles).SanitizeMentions());
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
else if (before.Roles.Count > after.Roles.Count)
{
@@ -609,7 +604,7 @@ public sealed class LogCommandService : ILogCommandService
embed.WithAuthor("⚔ " + GetText(logChannel.Guild, strs.user_role_rem))
.WithDescription(string.Join(", ", diffRoles).SanitizeMentions());
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
}
}
@@ -617,8 +612,7 @@ public sealed class LogCommandService : ILogCommandService
logChannel = null;
if (!before.IsBot && logSetting.LogUserPresenceId != null && (logChannel =
await TryGetLogChannel(before.Guild, logSetting, LogType.UserPresence)
.ConfigureAwait(false)) != null)
await TryGetLogChannel(before.Guild, logSetting, LogType.UserPresence)) != null)
{
if (before.Status != after.Status)
{
@@ -671,8 +665,7 @@ public sealed class LogCommandService : ILogCommandService
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(before.Guild, logSetting, LogType.ChannelUpdated)
.ConfigureAwait(false)) is null)
if ((logChannel = await TryGetLogChannel(before.Guild, logSetting, LogType.ChannelUpdated)) is null)
return;
var embed = _eb.Create().WithOkColor()
@@ -697,7 +690,7 @@ public sealed class LogCommandService : ILogCommandService
else
return;
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
catch
{
@@ -722,8 +715,7 @@ public sealed class LogCommandService : ILogCommandService
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(ch.Guild, logSetting, LogType.ChannelDestroyed)
.ConfigureAwait(false)) is null)
if ((logChannel = await TryGetLogChannel(ch.Guild, logSetting, LogType.ChannelDestroyed)) is null)
return;
string title;
if (ch is IVoiceChannel)
@@ -737,7 +729,7 @@ public sealed class LogCommandService : ILogCommandService
.WithOkColor()
.WithTitle("🆕 " + title)
.WithDescription($"{ch.Name} | {ch.Id}")
.WithFooter(CurrentTime(ch.Guild))).ConfigureAwait(false);
.WithFooter(CurrentTime(ch.Guild)));
}
catch
{
@@ -761,8 +753,7 @@ public sealed class LogCommandService : ILogCommandService
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(ch.Guild, logSetting, LogType.ChannelCreated)
.ConfigureAwait(false)) is null)
if ((logChannel = await TryGetLogChannel(ch.Guild, logSetting, LogType.ChannelCreated)) is null)
return;
string title;
if (ch is IVoiceChannel)
@@ -776,7 +767,7 @@ public sealed class LogCommandService : ILogCommandService
.WithOkColor()
.WithTitle("🆕 " + title)
.WithDescription($"{ch.Name} | {ch.Id}")
.WithFooter(CurrentTime(ch.Guild))).ConfigureAwait(false);
.WithFooter(CurrentTime(ch.Guild)));
}
catch (Exception)
{
@@ -807,8 +798,7 @@ public sealed class LogCommandService : ILogCommandService
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.VoicePresence)
.ConfigureAwait(false)) is null)
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.VoicePresence)) is null)
return;
string str = null;
@@ -860,8 +850,7 @@ public sealed class LogCommandService : ILogCommandService
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(guild, logSetting, LogType.UserLeft)
.ConfigureAwait(false)) is null)
if ((logChannel = await TryGetLogChannel(guild, logSetting, LogType.UserLeft)) is null)
return;
var embed = _eb.Create()
.WithOkColor()
@@ -873,7 +862,7 @@ public sealed class LogCommandService : ILogCommandService
if (Uri.IsWellFormedUriString(usr.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(usr.GetAvatarUrl());
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
catch
{
@@ -894,8 +883,7 @@ public sealed class LogCommandService : ILogCommandService
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserJoined)
.ConfigureAwait(false)) is null)
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserJoined)) is null)
return;
var embed = _eb.Create()
@@ -914,7 +902,7 @@ public sealed class LogCommandService : ILogCommandService
if (Uri.IsWellFormedUriString(usr.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(usr.GetAvatarUrl());
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
catch (Exception)
{
@@ -936,8 +924,7 @@ public sealed class LogCommandService : ILogCommandService
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(guild, logSetting, LogType.UserUnbanned)
.ConfigureAwait(false)) is null)
if ((logChannel = await TryGetLogChannel(guild, logSetting, LogType.UserUnbanned)) is null)
return;
var embed = _eb.Create()
.WithOkColor()
@@ -949,7 +936,7 @@ public sealed class LogCommandService : ILogCommandService
if (Uri.IsWellFormedUriString(usr.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(usr.GetAvatarUrl());
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
catch (Exception)
{
@@ -972,7 +959,7 @@ public sealed class LogCommandService : ILogCommandService
ITextChannel logChannel;
if ((logChannel =
await TryGetLogChannel(guild, logSetting, LogType.UserBanned).ConfigureAwait(false)) ==
await TryGetLogChannel(guild, logSetting, LogType.UserBanned)) ==
null)
return;
var embed = _eb.Create()
@@ -987,7 +974,7 @@ public sealed class LogCommandService : ILogCommandService
if (Uri.IsWellFormedUriString(avatarUrl, UriKind.Absolute))
embed.WithThumbnailUrl(usr.GetAvatarUrl());
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
catch (Exception)
{
@@ -1019,8 +1006,7 @@ public sealed class LogCommandService : ILogCommandService
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(channel.Guild, logSetting, LogType.MessageDeleted)
.ConfigureAwait(false)) is null || logChannel.Id == msg.Id)
if ((logChannel = await TryGetLogChannel(channel.Guild, logSetting, LogType.MessageDeleted)) is null || logChannel.Id == msg.Id)
return;
var resolvedMessage = msg.Resolve(userHandling: TagHandling.FullName);
@@ -1038,7 +1024,7 @@ public sealed class LogCommandService : ILogCommandService
string.Join(", ", msg.Attachments.Select(a => a.Url)),
false);
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
catch (Exception)
{
@@ -1076,8 +1062,7 @@ public sealed class LogCommandService : ILogCommandService
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(channel.Guild, logSetting, LogType.MessageUpdated)
.ConfigureAwait(false)) is null || logChannel.Id == after.Channel.Id)
if ((logChannel = await TryGetLogChannel(channel.Guild, logSetting, LogType.MessageUpdated)) is null || logChannel.Id == after.Channel.Id)
return;
var embed = _eb.Create()
@@ -1098,7 +1083,7 @@ public sealed class LogCommandService : ILogCommandService
.AddField("Id", after.Id.ToString(), false)
.WithFooter(CurrentTime(channel.Guild));
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
await logChannel.EmbedAsync(embed);
}
catch
{
@@ -1166,7 +1151,7 @@ public sealed class LogCommandService : ILogCommandService
return null;
}
var channel = await guild.GetTextChannelAsync(id.Value).ConfigureAwait(false);
var channel = await guild.GetTextChannelAsync(id.Value);
if (channel is null)
{

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Database.Models;
using NadekoBot.Db;
@@ -152,7 +152,7 @@ public class MuteService : INService
if (muted is null || !muted.Contains(usr.Id))
return Task.CompletedTask;
var _ = Task.Run(() => MuteUser(usr, _client.CurrentUser, reason: "Sticky mute").ConfigureAwait(false));
var _ = Task.Run(() => MuteUser(usr, _client.CurrentUser, reason: "Sticky mute"));
}
catch (Exception ex)
{
@@ -174,10 +174,10 @@ public class MuteService : INService
{
if (type == MuteType.All)
{
try { await usr.ModifyAsync(x => x.Mute = true).ConfigureAwait(false); } catch { }
var muteRole = await GetMuteRole(usr.Guild).ConfigureAwait(false);
try { await usr.ModifyAsync(x => x.Mute = true); } catch { }
var muteRole = await GetMuteRole(usr.Guild);
if (!usr.RoleIds.Contains(muteRole.Id))
await usr.AddRoleAsync(muteRole).ConfigureAwait(false);
await usr.AddRoleAsync(muteRole);
StopTimer(usr.GuildId, usr.Id, TimerType.Mute);
await using (var uow = _db.GetDbContext())
{
@@ -201,14 +201,14 @@ public class MuteService : INService
{
try
{
await usr.ModifyAsync(x => x.Mute = true).ConfigureAwait(false);
await usr.ModifyAsync(x => x.Mute = true);
UserMuted(usr, mod, MuteType.Voice, reason);
}
catch { }
}
else if (type == MuteType.Chat)
{
await usr.AddRoleAsync(await GetMuteRole(usr.Guild).ConfigureAwait(false)).ConfigureAwait(false);
await usr.AddRoleAsync(await GetMuteRole(usr.Guild));
UserMuted(usr, mod, MuteType.Chat, reason);
}
}
@@ -241,8 +241,8 @@ public class MuteService : INService
}
if (usr != null)
{
try { await usr.ModifyAsync(x => x.Mute = false).ConfigureAwait(false); } catch { }
try { await usr.RemoveRoleAsync(await GetMuteRole(usr.Guild).ConfigureAwait(false)).ConfigureAwait(false); } catch { /*ignore*/ }
try { await usr.ModifyAsync(x => x.Mute = false); } catch { }
try { await usr.RemoveRoleAsync(await GetMuteRole(usr.Guild)); } catch { /*ignore*/ }
UserUnmuted(usr, mod, MuteType.All, reason);
}
}
@@ -252,7 +252,7 @@ public class MuteService : INService
return;
try
{
await usr.ModifyAsync(x => x.Mute = false).ConfigureAwait(false);
await usr.ModifyAsync(x => x.Mute = false);
UserUnmuted(usr, mod, MuteType.Voice, reason);
}
catch { }
@@ -261,7 +261,7 @@ public class MuteService : INService
{
if (usr is null)
return;
await usr.RemoveRoleAsync(await GetMuteRole(usr.Guild).ConfigureAwait(false)).ConfigureAwait(false);
await usr.RemoveRoleAsync(await GetMuteRole(usr.Guild));
UserUnmuted(usr, mod, MuteType.Chat, reason);
}
}
@@ -280,26 +280,25 @@ public class MuteService : INService
{
//if it doesn't exist, create it
try { muteRole = await guild.CreateRoleAsync(muteRoleName, isMentionable: false).ConfigureAwait(false); }
try { muteRole = await guild.CreateRoleAsync(muteRoleName, isMentionable: 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, isMentionable: false).ConfigureAwait(false);
await guild.CreateRoleAsync(defaultMuteRoleName, isMentionable: false);
}
}
foreach (var toOverwrite in await guild.GetTextChannelsAsync().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 toOverwrite.AddPermissionOverwriteAsync(muteRole, denyOverwrite);
await Task.Delay(200).ConfigureAwait(false);
await Task.Delay(200);
}
}
catch
@@ -313,7 +312,7 @@ public class MuteService : INService
public async Task TimedMute(IGuildUser user, IUser mod, TimeSpan after, MuteType muteType = MuteType.All, string reason = "")
{
await MuteUser(user, mod, muteType, reason).ConfigureAwait(false); // mute the user. This will also remove any previous unmute timers
await MuteUser(user, mod, muteType, reason); // mute the user. This will also remove any previous unmute timers
await using (var uow = _db.GetDbContext())
{
var config = uow.GuildConfigsForId(user.GuildId, set => set.Include(x => x.UnmuteTimers));
@@ -330,7 +329,7 @@ public class MuteService : INService
public async Task TimedBan(IGuild guild, IUser user, TimeSpan after, string reason)
{
await guild.AddBanAsync(user.Id, 0, reason).ConfigureAwait(false);
await guild.AddBanAsync(user.Id, 0, reason);
await using (var uow = _db.GetDbContext())
{
var config = uow.GuildConfigsForId(guild.Id, set => set.Include(x => x.UnbanTimer));
@@ -347,7 +346,7 @@ public class MuteService : INService
public async Task TimedRole(IGuildUser user, TimeSpan after, string reason, IRole role)
{
await user.AddRoleAsync(role).ConfigureAwait(false);
await user.AddRoleAsync(role);
await using (var uow = _db.GetDbContext())
{
var config = uow.GuildConfigsForId(user.GuildId, set => set.Include(x => x.UnroleTimer));
@@ -380,7 +379,7 @@ public class MuteService : INService
var guild = _client.GetGuild(guildId); // load the guild
if (guild != null)
{
await guild.RemoveBanAsync(userId).ConfigureAwait(false);
await guild.RemoveBanAsync(userId);
}
}
catch (Exception ex)
@@ -399,7 +398,7 @@ public class MuteService : INService
var role = guild.GetRole(roleId.Value);
if (guild != null && user != null && user.Roles.Contains(role))
{
await user.RemoveRoleAsync(role).ConfigureAwait(false);
await user.RemoveRoleAsync(role);
}
}
catch (Exception ex)
@@ -412,7 +411,7 @@ public class MuteService : INService
try
{
// unmute the user, this will also remove the timer from the db
await UnmuteUser(guildId, userId, _client.CurrentUser, reason: "Timed mute expired").ConfigureAwait(false);
await UnmuteUser(guildId, userId, _client.CurrentUser, reason: "Timed mute expired");
}
catch (Exception ex)
{

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System.Threading.Channels;
using NadekoBot.Modules.Administration.Common;
using NadekoBot.Services.Database.Models;
@@ -181,9 +181,9 @@ public class ProtectionService : INService
var settings = stats.AntiRaidSettings;
await PunishUsers(settings.Action, ProtectionType.Raiding,
settings.PunishDuration, null, users).ConfigureAwait(false);
settings.PunishDuration, null, users);
}
await Task.Delay(1000 * stats.AntiRaidSettings.Seconds).ConfigureAwait(false);
await Task.Delay(1000 * stats.AntiRaidSettings.Seconds);
stats.RaidUsers.TryRemove(user);
--stats.UsersCount;
@@ -228,8 +228,7 @@ public class ProtectionService : INService
stats.Dispose();
var settings = spamSettings.AntiSpamSettings;
await PunishUsers(settings.Action, ProtectionType.Spamming, settings.MuteTime,
settings.RoleId, (IGuildUser)msg.Author)
.ConfigureAwait(false);
settings.RoleId, (IGuildUser)msg.Author);
}
}
}
@@ -270,7 +269,7 @@ public class ProtectionService : INService
PunishmentAction action, int minutesDuration)
{
var g = _client.GetGuild(guildId);
await _mute.GetMuteRole(g).ConfigureAwait(false);
await _mute.GetMuteRole(g);
if (action == PunishmentAction.AddRole)
return null;
@@ -338,7 +337,7 @@ public class ProtectionService : INService
int punishDurationMinutes, ulong? roleId)
{
var g = _client.GetGuild(guildId);
await _mute.GetMuteRole(g).ConfigureAwait(false);
await _mute.GetMuteRole(g);
if (!IsDurationAllowed(action))
punishDurationMinutes = 0;

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
namespace NadekoBot.Modules.Administration.Services;
public class PruneService : INService
@@ -25,7 +25,7 @@ public class PruneService : INService
{
IMessage[] msgs;
IMessage lastMessage = null;
msgs = (await channel.GetMessagesAsync(50).FlattenAsync().ConfigureAwait(false)).Where(predicate).Take(amount).ToArray();
msgs = (await channel.GetMessagesAsync(50).FlattenAsync()).Where(predicate).Take(amount).ToArray();
while (amount > 0 && msgs.Any())
{
lastMessage = msgs[msgs.Length - 1];
@@ -43,16 +43,20 @@ public class PruneService : INService
}
if (bulkDeletable.Count > 0)
await Task.WhenAll(Task.Delay(1000), channel.DeleteMessagesAsync(bulkDeletable)).ConfigureAwait(false);
await Task.WhenAll(Task.Delay(1000), channel.DeleteMessagesAsync(bulkDeletable));
foreach (var group in singleDeletable.Chunk(5))
await Task.WhenAll(Task.Delay(1000), Task.WhenAll(group.Select(x => x.DeleteAsync()))).ConfigureAwait(false);
await Task.WhenAll(
Task.Delay(1000),
group.Select(x => x.DeleteAsync())
.WhenAll()
);
//this isn't good, because this still work as if i want to remove only specific user's messages from the last
//100 messages, Maybe this needs to be reduced by msgs.Length instead of 100
amount -= 50;
if(amount > 0)
msgs = (await channel.GetMessagesAsync(lastMessage, Direction.Before, 50).FlattenAsync().ConfigureAwait(false)).Where(predicate).Take(amount).ToArray();
msgs = (await channel.GetMessagesAsync(lastMessage, Direction.Before, 50).FlattenAsync()).Where(predicate).Take(amount).ToArray();
}
}
catch

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using Microsoft.EntityFrameworkCore;
using NadekoBot.Common.Collections;
using NadekoBot.Services.Database.Models;
@@ -72,7 +72,7 @@ public class RoleCommandsService : INService
var removeExclusiveTask = RemoveExclusiveReactionRoleAsync(msg, gusr, reaction, conf, reactionRole, CancellationToken.None);
var addRoleTask = AddReactionRoleAsync(gusr, reactionRole);
await Task.WhenAll(removeExclusiveTask, addRoleTask).ConfigureAwait(false);
await Task.WhenAll(removeExclusiveTask, addRoleTask);
}
finally
{
@@ -82,12 +82,12 @@ public class RoleCommandsService : INService
}
else
{
var dl = await msg.GetOrDownloadAsync().ConfigureAwait(false);
var dl = await msg.GetOrDownloadAsync();
await dl.RemoveReactionAsync(reaction.Emote, dl.Author,
new()
{
RetryMode = RetryMode.RetryRatelimit | RetryMode.Retry502
}).ConfigureAwait(false);
});
Log.Warning("User {0} is adding unrelated reactions to the reaction roles message.", dl.Author);
}
});
@@ -126,7 +126,7 @@ public class RoleCommandsService : INService
var role = gusr.Guild.GetRole(reactionRole.RoleId);
if (role is null)
return;
await gusr.RemoveRoleAsync(role).ConfigureAwait(false);
await gusr.RemoveRoleAsync(role);
}
}
catch { }
@@ -231,13 +231,13 @@ public class RoleCommandsService : INService
//if the role is exclusive,
// remove all other reactions user added to the message
var dl = await reactionMessage.GetOrDownloadAsync().ConfigureAwait(false);
var dl = await reactionMessage.GetOrDownloadAsync();
foreach (var r in dl.Reactions)
{
if (r.Key.Name == reaction.Emote.Name)
continue;
try { await dl.RemoveReactionAsync(r.Key, user).ConfigureAwait(false); } catch { }
await Task.Delay(100, cToken).ConfigureAwait(false);
try { await dl.RemoveReactionAsync(r.Key, user); } catch { }
await Task.Delay(100, cToken);
}
}
}

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using NadekoBot.Services.Database.Models;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Db;
@@ -99,8 +99,8 @@ public class SelfAssignedRolesService : INService
{
try
{
await guildUser.RemoveRoleAsync(sameRole).ConfigureAwait(false);
await Task.Delay(300).ConfigureAwait(false);
await guildUser.RemoveRoleAsync(sameRole);
await Task.Delay(300);
}
catch
{
@@ -111,7 +111,7 @@ public class SelfAssignedRolesService : INService
}
try
{
await guildUser.AddRoleAsync(role).ConfigureAwait(false);
await guildUser.AddRoleAsync(role);
}
catch (Exception ex)
{
@@ -167,7 +167,7 @@ public class SelfAssignedRolesService : INService
}
try
{
await guildUser.RemoveRoleAsync(role).ConfigureAwait(false);
await guildUser.RemoveRoleAsync(role);
}
catch (Exception)
{

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System.Collections.Immutable;
using NadekoBot.Common.ModuleBehaviors;
using Microsoft.EntityFrameworkCore;
@@ -79,12 +79,12 @@ public sealed class SelfService : ILateExecutor, IReadyExecutor, INService
if (server.OwnerId != _client.CurrentUser.Id)
{
await server.LeaveAsync().ConfigureAwait(false);
await server.LeaveAsync();
Log.Information($"Left server {server.Name} [{server.Id}]");
}
else
{
await server.DeleteAsync().ConfigureAwait(false);
await server.DeleteAsync();
Log.Information($"Deleted server {server.Name} [{server.Id}]");
}
});
@@ -110,7 +110,7 @@ public sealed class SelfService : ILateExecutor, IReadyExecutor, INService
{
try
{
await ExecuteCommand(cmd).ConfigureAwait(false);
await ExecuteCommand(cmd);
}
catch
{
@@ -119,12 +119,12 @@ public sealed class SelfService : ILateExecutor, IReadyExecutor, INService
if (_client.ShardId == 0)
{
await LoadOwnerChannels().ConfigureAwait(false);
await LoadOwnerChannels();
}
}
private Timer TimerFromAutoCommand(AutoCommand x)
=> new(async obj => await ExecuteCommand((AutoCommand) obj).ConfigureAwait(false),
=> new(async obj => await ExecuteCommand((AutoCommand) obj),
x,
x.Interval * 1000,
x.Interval * 1000);
@@ -143,7 +143,7 @@ public sealed class SelfService : ILateExecutor, IReadyExecutor, INService
//if someone already has .die as their startup command, ignore it
if (cmd.CommandText.StartsWith(prefix + "die", StringComparison.InvariantCulture))
return;
await _cmdHandler.ExecuteExternal(cmd.GuildId, cmd.ChannelId, cmd.CommandText).ConfigureAwait(false);
await _cmdHandler.ExecuteExternal(cmd.GuildId, cmd.ChannelId, cmd.CommandText);
}
catch (Exception ex)
{
@@ -201,7 +201,7 @@ public sealed class SelfService : ILateExecutor, IReadyExecutor, INService
return Task.FromResult<IDMChannel>(null);
return user.CreateDMChannelAsync();
})).ConfigureAwait(false);
}));
ownerChannels = channels.Where(x => x != null)
.ToDictionary(x => x.Recipient.Id, x => x)
@@ -244,7 +244,7 @@ public sealed class SelfService : ILateExecutor, IReadyExecutor, INService
{
try
{
await ownerCh.SendConfirmAsync(_eb, title, toSend).ConfigureAwait(false);
await ownerCh.SendConfirmAsync(_eb, title, toSend);
}
catch
{
@@ -259,7 +259,7 @@ public sealed class SelfService : ILateExecutor, IReadyExecutor, INService
{
try
{
await firstOwnerChannel.SendConfirmAsync(_eb, title, toSend).ConfigureAwait(false);
await firstOwnerChannel.SendConfirmAsync(_eb, title, toSend);
}
catch
{
@@ -322,14 +322,14 @@ public sealed class SelfService : ILateExecutor, IReadyExecutor, INService
var uri = new Uri(img);
using var http = _httpFactory.CreateClient();
using var sr = await http.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
using var sr = await http.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);
if (!sr.IsImage())
return false;
// i can't just do ReadAsStreamAsync because dicord.net's image poops itself
var imgData = await sr.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
var imgData = await sr.Content.ReadAsByteArrayAsync();
await using var imgStream = imgData.ToStream();
await _client.CurrentUser.ModifyAsync(u => u.Avatar = new Image(imgStream)).ConfigureAwait(false);
await _client.CurrentUser.ModifyAsync(u => u.Avatar = new Image(imgStream));
return true;
}

View File

@@ -73,7 +73,7 @@ public class UserPunishService : INService
if (p != null)
{
var user = await guild.GetUserAsync(userId).ConfigureAwait(false);
var user = await guild.GetUserAsync(userId);
if (user is null)
return null;
@@ -95,50 +95,45 @@ public class UserPunishService : INService
{
case PunishmentAction.Mute:
if (minutes == 0)
await _mute.MuteUser(user, mod, reason: reason).ConfigureAwait(false);
await _mute.MuteUser(user, mod, reason: reason);
else
await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(minutes), reason: reason)
.ConfigureAwait(false);
await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(minutes), reason: reason);
break;
case PunishmentAction.VoiceMute:
if (minutes == 0)
await _mute.MuteUser(user, mod, MuteType.Voice, reason).ConfigureAwait(false);
await _mute.MuteUser(user, mod, MuteType.Voice, reason);
else
await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(minutes), MuteType.Voice, reason)
.ConfigureAwait(false);
await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(minutes), MuteType.Voice, reason);
break;
case PunishmentAction.ChatMute:
if (minutes == 0)
await _mute.MuteUser(user, mod, MuteType.Chat, reason).ConfigureAwait(false);
await _mute.MuteUser(user, mod, MuteType.Chat, reason);
else
await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(minutes), MuteType.Chat, reason)
.ConfigureAwait(false);
await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(minutes), MuteType.Chat, reason);
break;
case PunishmentAction.Kick:
await user.KickAsync(reason).ConfigureAwait(false);
await user.KickAsync(reason);
break;
case PunishmentAction.Ban:
if (minutes == 0)
await guild.AddBanAsync(user, reason: reason, pruneDays: 7).ConfigureAwait(false);
await guild.AddBanAsync(user, reason: reason, pruneDays: 7);
else
await _mute.TimedBan(user.Guild, user, TimeSpan.FromMinutes(minutes), reason)
.ConfigureAwait(false);
await _mute.TimedBan(user.Guild, user, TimeSpan.FromMinutes(minutes), reason);
break;
case PunishmentAction.Softban:
await guild.AddBanAsync(user, 7, reason: $"Softban | {reason}").ConfigureAwait(false);
await guild.AddBanAsync(user, 7, reason: $"Softban | {reason}");
try
{
await guild.RemoveBanAsync(user).ConfigureAwait(false);
await guild.RemoveBanAsync(user);
}
catch
{
await guild.RemoveBanAsync(user).ConfigureAwait(false);
await guild.RemoveBanAsync(user);
}
break;
case PunishmentAction.RemoveRoles:
await user.RemoveRolesAsync(user.GetRoles().Where(x => !x.IsManaged && x != x.Guild.EveryoneRole))
.ConfigureAwait(false);
await user.RemoveRolesAsync(user.GetRoles().Where(x => !x.IsManaged && x != x.Guild.EveryoneRole));
break;
case PunishmentAction.AddRole:
if (roleId is null)
@@ -147,10 +142,9 @@ public class UserPunishService : INService
if (role is not null)
{
if (minutes == 0)
await user.AddRoleAsync(role).ConfigureAwait(false);
await user.AddRoleAsync(role);
else
await _mute.TimedRole(user, TimeSpan.FromMinutes(minutes), reason, role)
.ConfigureAwait(false);
await _mute.TimedRole(user, TimeSpan.FromMinutes(minutes), reason, role);
}
else
{

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Database.Models;
using NadekoBot.Db;
@@ -21,49 +21,60 @@ public class VcRoleService : INService
_client.UserVoiceStateUpdated += ClientOnUserVoiceStateUpdated;
VcRoles = new();
ToAssign = new();
var missingRoles = new ConcurrentBag<VcRoleInfo>();
using (var uow = db.GetDbContext())
{
var guildIds = client.Guilds.Select(x => x.Id).ToList();
var configs = uow.Set<GuildConfig>()
uow.Set<GuildConfig>()
.AsQueryable()
.Include(x => x.VcRoleInfos)
.Where(x => guildIds.Contains(x.GuildId))
.ToList();
Task.WhenAll(configs.Select(InitializeVcRole));
.AsEnumerable()
.Select(InitializeVcRole)
.WhenAll();
}
Task.Run(async () =>
{
while (true)
{
var tasks = ToAssign.Values.Select(queue => Task.Run(async () =>
{
while (queue.TryDequeue(out var item))
{
var (add, user, role) = item;
if (add)
Task Selector(ConcurrentQueue<(bool, IGuildUser, IRole)> queue)
=> Task.Run(async () =>
{
if (!user.RoleIds.Contains(role.Id))
while (queue.TryDequeue(out var item))
{
try { await user.AddRoleAsync(role).ConfigureAwait(false); } catch { }
}
}
else
{
if (user.RoleIds.Contains(role.Id))
{
try { await user.RemoveRoleAsync(role).ConfigureAwait(false); } catch { }
var (add, user, role) = item;
try
{
if (add)
{
if (!user.RoleIds.Contains(role.Id))
{
await user.AddRoleAsync(role);
}
}
else
{
if (user.RoleIds.Contains(role.Id))
{
await user.RemoveRoleAsync(role);
}
}
}
catch
{
}
await Task.Delay(250);
}
}
);
await Task.Delay(250).ConfigureAwait(false);
}
}));
await Task.WhenAll(tasks.Append(Task.Delay(1000))).ConfigureAwait(false);
await ToAssign.Values.Select(Selector)
.Append(Task.Delay(1000))
.WhenAll();
}
});
@@ -119,7 +130,10 @@ public class VcRoleService : INService
if (missingRoles.Any())
{
await using var uow = _db.GetDbContext();
Log.Warning($"Removing {missingRoles.Count} missing roles from {nameof(VcRoleService)}");
Log.Warning("Removing {MissingRoleCount} missing roles from {ServiceName}",
missingRoles.Count,
nameof(VcRoleService)
);
uow.RemoveRange(missingRoles);
await uow.SaveChangesAsync();
}
@@ -132,7 +146,7 @@ public class VcRoleService : INService
var guildVcRoles = VcRoles.GetOrAdd(guildId, new ConcurrentDictionary<ulong, IRole>());
guildVcRoles.AddOrUpdate(vcId, role, (key, old) => role);
guildVcRoles.AddOrUpdate(vcId, role, (_, _) => role);
using var uow = _db.GetDbContext();
var conf = uow.GuildConfigsForId(guildId, set => set.Include(x => x.VcRoleInfos));
var toDelete = conf.VcRoleInfos.FirstOrDefault(x => x.VoiceChannelId == vcId); // remove old one