1.4-alpha builds and runs, for now

This commit is contained in:
Kwoth 2017-04-15 02:54:19 +02:00
parent 70a5d6603d
commit b0f555eaea
28 changed files with 270 additions and 340 deletions

0
.gitmodules vendored
View File

View File

@ -1,7 +1,7 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14 # Visual Studio 15
VisualStudioVersion = 14.0.25420.1 VisualStudioVersion = 15.0.26228.4
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{04929013-5BAB-42B0-B9B2-8F2BB8F16AF2}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{04929013-5BAB-42B0-B9B2-8F2BB8F16AF2}"
EndProject EndProject
@ -10,15 +10,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
global.json = global.json global.json = global.json
EndProjectSection EndProjectSection
EndProject EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "NadekoBot", "src\NadekoBot\NadekoBot.xproj", "{45EC1473-C678-4857-A544-07DFE0D0B478}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NadekoBot", "src\NadekoBot\NadekoBot.csproj", "{45EC1473-C678-4857-A544-07DFE0D0B478}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Discord.Net.Commands", "discord.net\src\Discord.Net.Commands\Discord.Net.Commands.xproj", "{078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Discord.Net.Core", "discord.net\src\Discord.Net.Core\Discord.Net.Core.xproj", "{E5F4786F-58F3-469E-8C87-1908A95436B7}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Discord.Net.Rest", "discord.net\src\Discord.Net.Rest\Discord.Net.Rest.xproj", "{63F5B5C8-56FE-4B53-8003-B58CEB451EF9}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Discord.Net.WebSocket", "discord.net\src\Discord.Net.WebSocket\Discord.Net.WebSocket.xproj", "{E9800F7A-3354-41B1-BDBB-2D59F8124EC9}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -33,30 +25,6 @@ Global
{45EC1473-C678-4857-A544-07DFE0D0B478}.GlobalNadeko|Any CPU.Build.0 = Release|Any CPU {45EC1473-C678-4857-A544-07DFE0D0B478}.GlobalNadeko|Any CPU.Build.0 = Release|Any CPU
{45EC1473-C678-4857-A544-07DFE0D0B478}.Release|Any CPU.ActiveCfg = Release|Any CPU {45EC1473-C678-4857-A544-07DFE0D0B478}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45EC1473-C678-4857-A544-07DFE0D0B478}.Release|Any CPU.Build.0 = Release|Any CPU {45EC1473-C678-4857-A544-07DFE0D0B478}.Release|Any CPU.Build.0 = Release|Any CPU
{078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}.GlobalNadeko|Any CPU.ActiveCfg = Debug|Any CPU
{078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}.GlobalNadeko|Any CPU.Build.0 = Debug|Any CPU
{078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}.Release|Any CPU.Build.0 = Release|Any CPU
{E5F4786F-58F3-469E-8C87-1908A95436B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5F4786F-58F3-469E-8C87-1908A95436B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5F4786F-58F3-469E-8C87-1908A95436B7}.GlobalNadeko|Any CPU.ActiveCfg = Debug|Any CPU
{E5F4786F-58F3-469E-8C87-1908A95436B7}.GlobalNadeko|Any CPU.Build.0 = Debug|Any CPU
{E5F4786F-58F3-469E-8C87-1908A95436B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5F4786F-58F3-469E-8C87-1908A95436B7}.Release|Any CPU.Build.0 = Release|Any CPU
{63F5B5C8-56FE-4B53-8003-B58CEB451EF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{63F5B5C8-56FE-4B53-8003-B58CEB451EF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63F5B5C8-56FE-4B53-8003-B58CEB451EF9}.GlobalNadeko|Any CPU.ActiveCfg = Debug|Any CPU
{63F5B5C8-56FE-4B53-8003-B58CEB451EF9}.GlobalNadeko|Any CPU.Build.0 = Debug|Any CPU
{63F5B5C8-56FE-4B53-8003-B58CEB451EF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63F5B5C8-56FE-4B53-8003-B58CEB451EF9}.Release|Any CPU.Build.0 = Release|Any CPU
{E9800F7A-3354-41B1-BDBB-2D59F8124EC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E9800F7A-3354-41B1-BDBB-2D59F8124EC9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9800F7A-3354-41B1-BDBB-2D59F8124EC9}.GlobalNadeko|Any CPU.ActiveCfg = Debug|Any CPU
{E9800F7A-3354-41B1-BDBB-2D59F8124EC9}.GlobalNadeko|Any CPU.Build.0 = Debug|Any CPU
{E9800F7A-3354-41B1-BDBB-2D59F8124EC9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9800F7A-3354-41B1-BDBB-2D59F8124EC9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -1,3 +1,3 @@
{ {
"sdk": { "version": "1.0.0-rc3-004350" } "sdk": { "version": "1.0.1" }
} }

View File

@ -33,8 +33,7 @@ namespace NadekoBot.Attributes
private static string GetModulePrefix(string moduleName, string defaultPrefix) private static string GetModulePrefix(string moduleName, string defaultPrefix)
{ {
string prefix = null; if (!ModulePrefixes.TryGetValue(moduleName, out string prefix))
if (!ModulePrefixes.TryGetValue(moduleName, out prefix))
{ {
NadekoBot.ModulePrefixes.TryAdd(moduleName, defaultPrefix); NadekoBot.ModulePrefixes.TryAdd(moduleName, defaultPrefix);
NLog.LogManager.GetCurrentClassLogger().Warn("Prefix not found for {0}. Will use default one: {1}", moduleName, defaultPrefix); NLog.LogManager.GetCurrentClassLogger().Warn("Prefix not found for {0}. Will use default one: {1}", moduleName, defaultPrefix);

View File

@ -19,7 +19,7 @@ namespace NadekoBot.Modules.Administration
[NadekoModule("Administration", ".")] [NadekoModule("Administration", ".")]
public partial class Administration : NadekoTopLevelModule public partial class Administration : NadekoTopLevelModule
{ {
private static ConcurrentHashSet<ulong> deleteMessagesOnCommand { get; } private static readonly ConcurrentHashSet<ulong> deleteMessagesOnCommand;
private new static readonly Logger _log; private new static readonly Logger _log;
@ -122,7 +122,7 @@ namespace NadekoBot.Modules.Administration
return; return;
try try
{ {
await usr.AddRolesAsync(role).ConfigureAwait(false); await usr.AddRoleAsync(role).ConfigureAwait(false);
await ReplyConfirmLocalized("setrole", Format.Bold(role.Name), Format.Bold(usr.ToString())) await ReplyConfirmLocalized("setrole", Format.Bold(role.Name), Format.Bold(usr.ToString()))
.ConfigureAwait(false); .ConfigureAwait(false);
} }
@ -144,7 +144,7 @@ namespace NadekoBot.Modules.Administration
return; return;
try try
{ {
await usr.RemoveRolesAsync(role).ConfigureAwait(false); await usr.RemoveRoleAsync(role).ConfigureAwait(false);
await ReplyConfirmLocalized("remrole", Format.Bold(role.Name), Format.Bold(usr.ToString())).ConfigureAwait(false); await ReplyConfirmLocalized("remrole", Format.Bold(role.Name), Format.Bold(usr.ToString())).ConfigureAwait(false);
} }
catch catch

View File

@ -17,20 +17,19 @@ namespace NadekoBot.Modules.Administration
public class AutoAssignRoleCommands : NadekoSubmodule public class AutoAssignRoleCommands : NadekoSubmodule
{ {
//guildid/roleid //guildid/roleid
private static ConcurrentDictionary<ulong, ulong> autoAssignedRoles { get; } private static ConcurrentDictionary<ulong, ulong> AutoAssignedRoles { get; }
static AutoAssignRoleCommands() static AutoAssignRoleCommands()
{ {
var log = LogManager.GetCurrentClassLogger(); var log = LogManager.GetCurrentClassLogger();
autoAssignedRoles = new ConcurrentDictionary<ulong, ulong>(NadekoBot.AllGuildConfigs.Where(x => x.AutoAssignRoleId != 0) AutoAssignedRoles = new ConcurrentDictionary<ulong, ulong>(NadekoBot.AllGuildConfigs.Where(x => x.AutoAssignRoleId != 0)
.ToDictionary(k => k.GuildId, v => v.AutoAssignRoleId)); .ToDictionary(k => k.GuildId, v => v.AutoAssignRoleId));
NadekoBot.Client.UserJoined += async (user) => NadekoBot.Client.UserJoined += async (user) =>
{ {
try try
{ {
ulong roleId; AutoAssignedRoles.TryGetValue(user.Guild.Id, out ulong roleId);
autoAssignedRoles.TryGetValue(user.Guild.Id, out roleId);
if (roleId == 0) if (roleId == 0)
return; return;
@ -38,7 +37,7 @@ namespace NadekoBot.Modules.Administration
var role = user.Guild.Roles.FirstOrDefault(r => r.Id == roleId); var role = user.Guild.Roles.FirstOrDefault(r => r.Id == roleId);
if (role != null) if (role != null)
await user.AddRolesAsync(role).ConfigureAwait(false); await user.AddRoleAsync(role).ConfigureAwait(false);
} }
catch (Exception ex) { log.Warn(ex); } catch (Exception ex) { log.Warn(ex); }
}; };
@ -60,13 +59,12 @@ namespace NadekoBot.Modules.Administration
if (role == null) if (role == null)
{ {
conf.AutoAssignRoleId = 0; conf.AutoAssignRoleId = 0;
ulong throwaway; AutoAssignedRoles.TryRemove(Context.Guild.Id, out ulong throwaway);
autoAssignedRoles.TryRemove(Context.Guild.Id, out throwaway);
} }
else else
{ {
conf.AutoAssignRoleId = role.Id; conf.AutoAssignRoleId = role.Id;
autoAssignedRoles.AddOrUpdate(Context.Guild.Id, role.Id, (key, val) => role.Id); AutoAssignedRoles.AddOrUpdate(Context.Guild.Id, role.Id, (key, val) => role.Id);
} }
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);

View File

@ -22,20 +22,20 @@ namespace NadekoBot.Modules.Administration
[Group] [Group]
public class LogCommands : NadekoSubmodule public class LogCommands : NadekoSubmodule
{ {
private static DiscordShardedClient client { get; } private static DiscordShardedClient Client { get; }
private new static Logger _log { get; } private new static Logger _log { get; }
private static string prettyCurrentTime => $"【{DateTime.Now:HH:mm:ss}】"; private static string PrettyCurrentTime => $"【{DateTime.Now:HH:mm:ss}】";
private static string currentTime => $"{DateTime.Now:HH:mm:ss}"; private static string CurrentTime => $"{DateTime.Now:HH:mm:ss}";
public static ConcurrentDictionary<ulong, LogSetting> GuildLogSettings { get; } public static ConcurrentDictionary<ulong, LogSetting> GuildLogSettings { get; }
private static ConcurrentDictionary<ITextChannel, List<string>> presenceUpdates { get; } = new ConcurrentDictionary<ITextChannel, List<string>>(); private static ConcurrentDictionary<ITextChannel, List<string>> PresenceUpdates { get; } = new ConcurrentDictionary<ITextChannel, List<string>>();
private static readonly Timer _timerReference; private static readonly Timer _timerReference;
static LogCommands() static LogCommands()
{ {
client = NadekoBot.Client; Client = NadekoBot.Client;
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
var sw = Stopwatch.StartNew(); var sw = Stopwatch.StartNew();
@ -46,12 +46,11 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
var keys = presenceUpdates.Keys.ToList(); var keys = PresenceUpdates.Keys.ToList();
await Task.WhenAll(keys.Select(async key => await Task.WhenAll(keys.Select(async key =>
{ {
List<string> messages; if (PresenceUpdates.TryRemove(key, out List<string> messages))
if (presenceUpdates.TryRemove(key, out messages))
try { await key.SendConfirmAsync(key.Guild.GetLogText("presence_updates"), string.Join(Environment.NewLine, messages)); } try { await key.SendConfirmAsync(key.Guild.GetLogText("presence_updates"), string.Join(Environment.NewLine, messages)); }
catch catch
{ {
@ -69,22 +68,22 @@ namespace NadekoBot.Modules.Administration
_log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s"); _log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s");
//_client.MessageReceived += _client_MessageReceived; //_client.MessageReceived += _client_MessageReceived;
client.MessageUpdated += _client_MessageUpdated; Client.MessageUpdated += _client_MessageUpdated;
client.MessageDeleted += _client_MessageDeleted; Client.MessageDeleted += _client_MessageDeleted;
client.UserBanned += _client_UserBanned; Client.UserBanned += _client_UserBanned;
client.UserUnbanned += _client_UserUnbanned; Client.UserUnbanned += _client_UserUnbanned;
client.UserJoined += _client_UserJoined; Client.UserJoined += _client_UserJoined;
client.UserLeft += _client_UserLeft; Client.UserLeft += _client_UserLeft;
client.UserPresenceUpdated += _client_UserPresenceUpdated; Client.UserPresenceUpdated += _client_UserPresenceUpdated;
client.UserVoiceStateUpdated += _client_UserVoiceStateUpdated; Client.UserVoiceStateUpdated += _client_UserVoiceStateUpdated;
client.UserVoiceStateUpdated += _client_UserVoiceStateUpdated_TTS; Client.UserVoiceStateUpdated += _client_UserVoiceStateUpdated_TTS;
client.GuildMemberUpdated += _client_GuildUserUpdated; Client.GuildMemberUpdated += _client_GuildUserUpdated;
#if !GLOBAL_NADEKO #if !GLOBAL_NADEKO
client.UserUpdated += _client_UserUpdated; Client.UserUpdated += _client_UserUpdated;
#endif #endif
client.ChannelCreated += _client_ChannelCreated; Client.ChannelCreated += _client_ChannelCreated;
client.ChannelDestroyed += _client_ChannelDestroyed; Client.ChannelDestroyed += _client_ChannelDestroyed;
client.ChannelUpdated += _client_ChannelUpdated; Client.ChannelUpdated += _client_ChannelUpdated;
MuteCommands.UserMuted += MuteCommands_UserMuted; MuteCommands.UserMuted += MuteCommands_UserMuted;
MuteCommands.UserUnmuted += MuteCommands_UserUnmuted; MuteCommands.UserUnmuted += MuteCommands_UserUnmuted;
@ -101,8 +100,7 @@ namespace NadekoBot.Modules.Administration
var g = after.Guild; var g = after.Guild;
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(g.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(g.Id, out logSetting)
|| (logSetting.UserUpdatedId == null)) || (logSetting.UserUpdatedId == null))
return; return;
@ -119,16 +117,16 @@ namespace NadekoBot.Modules.Administration
.WithDescription($"{before.Username}#{before.Discriminator} | {before.Id}") .WithDescription($"{before.Username}#{before.Discriminator} | {before.Id}")
.AddField(fb => fb.WithName("Old Name").WithValue($"{before.Username}").WithIsInline(true)) .AddField(fb => fb.WithName("Old Name").WithValue($"{before.Username}").WithIsInline(true))
.AddField(fb => fb.WithName("New Name").WithValue($"{after.Username}").WithIsInline(true)) .AddField(fb => fb.WithName("New Name").WithValue($"{after.Username}").WithIsInline(true))
.WithFooter(fb => fb.WithText(currentTime)) .WithFooter(fb => fb.WithText(CurrentTime))
.WithOkColor(); .WithOkColor();
} }
else if (before.AvatarUrl != after.AvatarUrl) else if (before.AvatarId != after.AvatarId)
{ {
embed.WithTitle("👥" + g.GetLogText("avatar_changed")) embed.WithTitle("👥" + g.GetLogText("avatar_changed"))
.WithDescription($"{before.Username}#{before.Discriminator} | {before.Id}") .WithDescription($"{before.Username}#{before.Discriminator} | {before.Id}")
.WithThumbnailUrl(before.AvatarUrl) .WithThumbnailUrl(before.GetAvatarUrl())
.WithImageUrl(after.AvatarUrl) .WithImageUrl(after.GetAvatarUrl())
.WithFooter(fb => fb.WithText(currentTime)) .WithFooter(fb => fb.WithText(CurrentTime))
.WithOkColor(); .WithOkColor();
} }
else else
@ -173,8 +171,7 @@ namespace NadekoBot.Modules.Administration
if (beforeVch == afterVch) if (beforeVch == afterVch)
return; return;
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out logSetting)
|| (logSetting.LogVoicePresenceTTSId == null)) || (logSetting.LogVoicePresenceTTSId == null))
return; return;
@ -208,8 +205,7 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out logSetting)
|| (logSetting.UserMutedId == null)) || (logSetting.UserMutedId == null))
return; return;
@ -233,7 +229,7 @@ namespace NadekoBot.Modules.Administration
var embed = new EmbedBuilder().WithAuthor(eab => eab.WithName(mutes)) var embed = new EmbedBuilder().WithAuthor(eab => eab.WithName(mutes))
.WithTitle($"{usr.Username}#{usr.Discriminator} | {usr.Id}") .WithTitle($"{usr.Username}#{usr.Discriminator} | {usr.Id}")
.WithFooter(fb => fb.WithText(currentTime)) .WithFooter(fb => fb.WithText(CurrentTime))
.WithOkColor(); .WithOkColor();
await logChannel.EmbedAsync(embed).ConfigureAwait(false); await logChannel.EmbedAsync(embed).ConfigureAwait(false);
@ -248,8 +244,7 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out logSetting)
|| (logSetting.UserMutedId == null)) || (logSetting.UserMutedId == null))
return; return;
@ -274,7 +269,7 @@ namespace NadekoBot.Modules.Administration
var embed = new EmbedBuilder().WithAuthor(eab => eab.WithName(mutes)) var embed = new EmbedBuilder().WithAuthor(eab => eab.WithName(mutes))
.WithTitle($"{usr.Username}#{usr.Discriminator} | {usr.Id}") .WithTitle($"{usr.Username}#{usr.Discriminator} | {usr.Id}")
.WithFooter(fb => fb.WithText($"{currentTime}")) .WithFooter(fb => fb.WithText($"{CurrentTime}"))
.WithOkColor(); .WithOkColor();
await logChannel.EmbedAsync(embed).ConfigureAwait(false); await logChannel.EmbedAsync(embed).ConfigureAwait(false);
@ -292,8 +287,7 @@ namespace NadekoBot.Modules.Administration
if (users.Length == 0) if (users.Length == 0)
return; return;
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(users.First().Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(users.First().Guild.Id, out logSetting)
|| (logSetting.LogOtherId == null)) || (logSetting.LogOtherId == null))
return; return;
ITextChannel logChannel; ITextChannel logChannel;
@ -320,7 +314,7 @@ namespace NadekoBot.Modules.Administration
var embed = new EmbedBuilder().WithAuthor(eab => eab.WithName($"🛡 Anti-{protection}")) var embed = new EmbedBuilder().WithAuthor(eab => eab.WithName($"🛡 Anti-{protection}"))
.WithTitle(logChannel.Guild.GetLogText("users") + " " + punishment) .WithTitle(logChannel.Guild.GetLogText("users") + " " + punishment)
.WithDescription(string.Join("\n", users.Select(u => u.ToString()))) .WithDescription(string.Join("\n", users.Select(u => u.ToString())))
.WithFooter(fb => fb.WithText($"{currentTime}")) .WithFooter(fb => fb.WithText($"{CurrentTime}"))
.WithOkColor(); .WithOkColor();
await logChannel.EmbedAsync(embed).ConfigureAwait(false); await logChannel.EmbedAsync(embed).ConfigureAwait(false);
@ -335,15 +329,14 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(before.Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(before.Guild.Id, out logSetting)
|| (logSetting.UserUpdatedId == null)) || (logSetting.UserUpdatedId == null))
return; return;
ITextChannel logChannel; ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(before.Guild, logSetting, LogType.UserUpdated)) == null) if ((logChannel = await TryGetLogChannel(before.Guild, logSetting, LogType.UserUpdated)) == null)
return; return;
var embed = new EmbedBuilder().WithOkColor().WithFooter(efb => efb.WithText(currentTime)) var embed = new EmbedBuilder().WithOkColor().WithFooter(efb => efb.WithText(CurrentTime))
.WithTitle($"{before.Username}#{before.Discriminator} | {before.Id}"); .WithTitle($"{before.Username}#{before.Discriminator} | {before.Id}");
if (before.Nickname != after.Nickname) if (before.Nickname != after.Nickname)
{ {
@ -352,17 +345,17 @@ namespace NadekoBot.Modules.Administration
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("old_nick")).WithValue($"{before.Nickname}#{before.Discriminator}")) .AddField(efb => efb.WithName(logChannel.Guild.GetLogText("old_nick")).WithValue($"{before.Nickname}#{before.Discriminator}"))
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("new_nick")).WithValue($"{after.Nickname}#{after.Discriminator}")); .AddField(efb => efb.WithName(logChannel.Guild.GetLogText("new_nick")).WithValue($"{after.Nickname}#{after.Discriminator}"));
} }
else if (!before.RoleIds.SequenceEqual(after.RoleIds)) else if (!before.Roles.SequenceEqual(after.Roles))
{ {
if (before.RoleIds.Count < after.RoleIds.Count) if (before.Roles.Count < after.Roles.Count)
{ {
var diffRoles = after.RoleIds.Where(r => !before.RoleIds.Contains(r)).Select(r => before.Guild.GetRole(r).Name); var diffRoles = after.Roles.Where(r => !before.Roles.Contains(r)).Select(r => r.Name);
embed.WithAuthor(eab => eab.WithName("⚔ " + logChannel.Guild.GetLogText("user_role_add"))) embed.WithAuthor(eab => eab.WithName("⚔ " + logChannel.Guild.GetLogText("user_role_add")))
.WithDescription(string.Join(", ", diffRoles).SanitizeMentions()); .WithDescription(string.Join(", ", diffRoles).SanitizeMentions());
} }
else if (before.RoleIds.Count > after.RoleIds.Count) else if (before.Roles.Count > after.Roles.Count)
{ {
var diffRoles = before.RoleIds.Where(r => !after.RoleIds.Contains(r)).Select(r => before.Guild.GetRole(r).Name); var diffRoles = before.Roles.Where(r => !after.Roles.Contains(r)).Select(r => r.Name);
embed.WithAuthor(eab => eab.WithName("⚔ " + logChannel.Guild.GetLogText("user_role_rem"))) embed.WithAuthor(eab => eab.WithName("⚔ " + logChannel.Guild.GetLogText("user_role_rem")))
.WithDescription(string.Join(", ", diffRoles).SanitizeMentions()); .WithDescription(string.Join(", ", diffRoles).SanitizeMentions());
} }
@ -386,8 +379,7 @@ namespace NadekoBot.Modules.Administration
return; return;
var after = (IGuildChannel)cafter; var after = (IGuildChannel)cafter;
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(before.Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(before.Guild.Id, out logSetting)
|| (logSetting.ChannelUpdatedId == null) || (logSetting.ChannelUpdatedId == null)
|| logSetting.IgnoredChannels.Any(ilc => ilc.ChannelId == after.Id)) || logSetting.IgnoredChannels.Any(ilc => ilc.ChannelId == after.Id))
return; return;
@ -396,7 +388,7 @@ namespace NadekoBot.Modules.Administration
if ((logChannel = await TryGetLogChannel(before.Guild, logSetting, LogType.ChannelUpdated)) == null) if ((logChannel = await TryGetLogChannel(before.Guild, logSetting, LogType.ChannelUpdated)) == null)
return; return;
var embed = new EmbedBuilder().WithOkColor().WithFooter(efb => efb.WithText(currentTime)); var embed = new EmbedBuilder().WithOkColor().WithFooter(efb => efb.WithText(CurrentTime));
var beforeTextChannel = cbefore as ITextChannel; var beforeTextChannel = cbefore as ITextChannel;
var afterTextChannel = cafter as ITextChannel; var afterTextChannel = cafter as ITextChannel;
@ -433,8 +425,7 @@ namespace NadekoBot.Modules.Administration
if (ch == null) if (ch == null)
return; return;
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(ch.Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(ch.Guild.Id, out logSetting)
|| (logSetting.ChannelDestroyedId == null) || (logSetting.ChannelDestroyedId == null)
|| logSetting.IgnoredChannels.Any(ilc => ilc.ChannelId == ch.Id)) || logSetting.IgnoredChannels.Any(ilc => ilc.ChannelId == ch.Id))
return; return;
@ -453,7 +444,7 @@ namespace NadekoBot.Modules.Administration
.WithOkColor() .WithOkColor()
.WithTitle("🆕 " + title) .WithTitle("🆕 " + title)
.WithDescription($"{ch.Name} | {ch.Id}") .WithDescription($"{ch.Name} | {ch.Id}")
.WithFooter(efb => efb.WithText(currentTime))).ConfigureAwait(false); .WithFooter(efb => efb.WithText(CurrentTime))).ConfigureAwait(false);
} }
catch catch
{ {
@ -469,8 +460,7 @@ namespace NadekoBot.Modules.Administration
if (ch == null) if (ch == null)
return; return;
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(ch.Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(ch.Guild.Id, out logSetting)
|| (logSetting.ChannelCreatedId == null)) || (logSetting.ChannelCreatedId == null))
return; return;
@ -488,7 +478,7 @@ namespace NadekoBot.Modules.Administration
.WithOkColor() .WithOkColor()
.WithTitle("🆕 " + title) .WithTitle("🆕 " + title)
.WithDescription($"{ch.Name} | {ch.Id}") .WithDescription($"{ch.Name} | {ch.Id}")
.WithFooter(efb => efb.WithText(currentTime))).ConfigureAwait(false); .WithFooter(efb => efb.WithText(CurrentTime))).ConfigureAwait(false);
} }
catch (Exception ex) { _log.Warn(ex); } catch (Exception ex) { _log.Warn(ex); }
} }
@ -507,8 +497,7 @@ namespace NadekoBot.Modules.Administration
if (beforeVch == afterVch) if (beforeVch == afterVch)
return; return;
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out logSetting)
|| (logSetting.LogVoicePresenceId == null)) || (logSetting.LogVoicePresenceId == null))
return; return;
@ -519,24 +508,24 @@ namespace NadekoBot.Modules.Administration
string str = null; string str = null;
if (beforeVch?.Guild == afterVch?.Guild) if (beforeVch?.Guild == afterVch?.Guild)
{ {
str = "🎙" + Format.Code(prettyCurrentTime) + logChannel.Guild.GetLogText("user_vmoved", str = "🎙" + Format.Code(PrettyCurrentTime) + logChannel.Guild.GetLogText("user_vmoved",
"👤" + Format.Bold(usr.Username + "#" + usr.Discriminator), "👤" + Format.Bold(usr.Username + "#" + usr.Discriminator),
Format.Bold(beforeVch?.Name ?? ""), Format.Bold(afterVch?.Name ?? "")); Format.Bold(beforeVch?.Name ?? ""), Format.Bold(afterVch?.Name ?? ""));
} }
else if (beforeVch == null) else if (beforeVch == null)
{ {
str = "🎙" + Format.Code(prettyCurrentTime) + logChannel.Guild.GetLogText("user_vjoined", str = "🎙" + Format.Code(PrettyCurrentTime) + logChannel.Guild.GetLogText("user_vjoined",
"👤" + Format.Bold(usr.Username + "#" + usr.Discriminator), "👤" + Format.Bold(usr.Username + "#" + usr.Discriminator),
Format.Bold(afterVch.Name ?? "")); Format.Bold(afterVch.Name ?? ""));
} }
else if (afterVch == null) else if (afterVch == null)
{ {
str = "🎙" + Format.Code(prettyCurrentTime) + logChannel.Guild.GetLogText("user_vleft", str = "🎙" + Format.Code(PrettyCurrentTime) + logChannel.Guild.GetLogText("user_vleft",
"👤" + Format.Bold(usr.Username + "#" + usr.Discriminator), "👤" + Format.Bold(usr.Username + "#" + usr.Discriminator),
Format.Bold(beforeVch.Name ?? "")); Format.Bold(beforeVch.Name ?? ""));
} }
if (str != null) if (str != null)
presenceUpdates.AddOrUpdate(logChannel, new List<string>() { str }, (id, list) => { list.Add(str); return list; }); PresenceUpdates.AddOrUpdate(logChannel, new List<string>() { str }, (id, list) => { list.Add(str); return list; });
} }
catch catch
{ {
@ -553,8 +542,7 @@ namespace NadekoBot.Modules.Administration
if (guild == null) if (guild == null)
return; return;
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(guild.Id, out logSetting)
|| (logSetting.LogUserPresenceId == null) || (logSetting.LogUserPresenceId == null)
|| before.Status == after.Status) || before.Status == after.Status)
return; return;
@ -564,7 +552,7 @@ namespace NadekoBot.Modules.Administration
return; return;
string str = ""; string str = "";
if (before.Status != after.Status) if (before.Status != after.Status)
str = "🎭" + Format.Code(prettyCurrentTime) + str = "🎭" + Format.Code(PrettyCurrentTime) +
logChannel.Guild.GetLogText("user_status_change", logChannel.Guild.GetLogText("user_status_change",
"👤" + Format.Bold(usr.Username), "👤" + Format.Bold(usr.Username),
Format.Bold(after.Status.ToString())); Format.Bold(after.Status.ToString()));
@ -576,7 +564,7 @@ namespace NadekoBot.Modules.Administration
// str += $"👾`{prettyCurrentTime}`👤__**{usr.Username}**__ is now playing **{after.Game?.Name}**."; // str += $"👾`{prettyCurrentTime}`👤__**{usr.Username}**__ is now playing **{after.Game?.Name}**.";
//} //}
presenceUpdates.AddOrUpdate(logChannel, new List<string>() { str }, (id, list) => { list.Add(str); return list; }); PresenceUpdates.AddOrUpdate(logChannel, new List<string>() { str }, (id, list) => { list.Add(str); return list; });
} }
catch catch
{ {
@ -588,8 +576,7 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out logSetting)
|| (logSetting.UserLeftId == null)) || (logSetting.UserLeftId == null))
return; return;
@ -600,10 +587,10 @@ namespace NadekoBot.Modules.Administration
await logChannel.EmbedAsync(new EmbedBuilder() await logChannel.EmbedAsync(new EmbedBuilder()
.WithOkColor() .WithOkColor()
.WithTitle("❌ " + logChannel.Guild.GetLogText("user_left")) .WithTitle("❌ " + logChannel.Guild.GetLogText("user_left"))
.WithThumbnailUrl(usr.AvatarUrl) .WithThumbnailUrl(usr.GetAvatarUrl())
.WithDescription(usr.ToString()) .WithDescription(usr.ToString())
.AddField(efb => efb.WithName("Id").WithValue(usr.Id.ToString())) .AddField(efb => efb.WithName("Id").WithValue(usr.Id.ToString()))
.WithFooter(efb => efb.WithText(currentTime))).ConfigureAwait(false); .WithFooter(efb => efb.WithText(CurrentTime))).ConfigureAwait(false);
} }
catch catch
{ {
@ -615,8 +602,7 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out logSetting)
|| (logSetting.UserJoinedId == null)) || (logSetting.UserJoinedId == null))
return; return;
@ -627,10 +613,10 @@ namespace NadekoBot.Modules.Administration
await logChannel.EmbedAsync(new EmbedBuilder() await logChannel.EmbedAsync(new EmbedBuilder()
.WithOkColor() .WithOkColor()
.WithTitle("✅ " + logChannel.Guild.GetLogText("user_joined")) .WithTitle("✅ " + logChannel.Guild.GetLogText("user_joined"))
.WithThumbnailUrl(usr.AvatarUrl) .WithThumbnailUrl(usr.GetAvatarUrl())
.WithDescription($"{usr}") .WithDescription($"{usr}")
.AddField(efb => efb.WithName("Id").WithValue(usr.Id.ToString())) .AddField(efb => efb.WithName("Id").WithValue(usr.Id.ToString()))
.WithFooter(efb => efb.WithText(currentTime))).ConfigureAwait(false); .WithFooter(efb => efb.WithText(CurrentTime))).ConfigureAwait(false);
} }
catch (Exception ex) { _log.Warn(ex); } catch (Exception ex) { _log.Warn(ex); }
} }
@ -639,8 +625,7 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(guild.Id, out logSetting)
|| (logSetting.UserUnbannedId == null)) || (logSetting.UserUnbannedId == null))
return; return;
@ -651,10 +636,10 @@ namespace NadekoBot.Modules.Administration
await logChannel.EmbedAsync(new EmbedBuilder() await logChannel.EmbedAsync(new EmbedBuilder()
.WithOkColor() .WithOkColor()
.WithTitle("♻️ " + logChannel.Guild.GetLogText("user_unbanned")) .WithTitle("♻️ " + logChannel.Guild.GetLogText("user_unbanned"))
.WithThumbnailUrl(usr.AvatarUrl) .WithThumbnailUrl(usr.GetAvatarUrl())
.WithDescription(usr.ToString()) .WithDescription(usr.ToString())
.AddField(efb => efb.WithName("Id").WithValue(usr.Id.ToString())) .AddField(efb => efb.WithName("Id").WithValue(usr.Id.ToString()))
.WithFooter(efb => efb.WithText(currentTime))).ConfigureAwait(false); .WithFooter(efb => efb.WithText(CurrentTime))).ConfigureAwait(false);
} }
catch (Exception ex) { _log.Warn(ex); } catch (Exception ex) { _log.Warn(ex); }
} }
@ -663,8 +648,7 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(guild.Id, out logSetting)
|| (logSetting.UserBannedId == null)) || (logSetting.UserBannedId == null))
return; return;
@ -674,29 +658,28 @@ namespace NadekoBot.Modules.Administration
await logChannel.EmbedAsync(new EmbedBuilder() await logChannel.EmbedAsync(new EmbedBuilder()
.WithOkColor() .WithOkColor()
.WithTitle("🚫 " + logChannel.Guild.GetLogText("user_banned")) .WithTitle("🚫 " + logChannel.Guild.GetLogText("user_banned"))
.WithThumbnailUrl(usr.AvatarUrl) .WithThumbnailUrl(usr.GetAvatarUrl())
.WithDescription(usr.ToString()) .WithDescription(usr.ToString())
.AddField(efb => efb.WithName("Id").WithValue(usr.Id.ToString())) .AddField(efb => efb.WithName("Id").WithValue(usr.Id.ToString()))
.WithFooter(efb => efb.WithText(currentTime))).ConfigureAwait(false); .WithFooter(efb => efb.WithText(CurrentTime))).ConfigureAwait(false);
} }
catch (Exception ex) { _log.Warn(ex); } catch (Exception ex) { _log.Warn(ex); }
} }
private static async Task _client_MessageDeleted(ulong arg1, Optional<SocketMessage> imsg) private static async Task _client_MessageDeleted(Cacheable<IMessage, ulong> optMsg, ISocketMessageChannel ch)
{ {
try try
{ {
var msg = (imsg.IsSpecified ? imsg.Value : null) as IUserMessage; var msg = (optMsg.HasValue ? optMsg.Value : null) as IUserMessage;
if (msg == null || msg.IsAuthor()) if (msg == null || msg.IsAuthor())
return; return;
var channel = msg.Channel as ITextChannel; var channel = ch as ITextChannel;
if (channel == null) if (channel == null)
return; return;
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(channel.Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(channel.Guild.Id, out logSetting)
|| (logSetting.MessageDeletedId == null) || (logSetting.MessageDeletedId == null)
|| logSetting.IgnoredChannels.Any(ilc => ilc.ChannelId == channel.Id)) || logSetting.IgnoredChannels.Any(ilc => ilc.ChannelId == channel.Id))
return; return;
@ -710,7 +693,7 @@ namespace NadekoBot.Modules.Administration
.WithDescription(msg.Author.ToString()) .WithDescription(msg.Author.ToString())
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("content")).WithValue(string.IsNullOrWhiteSpace(msg.Content) ? "-" : msg.Resolve(userHandling: TagHandling.FullName)).WithIsInline(false)) .AddField(efb => efb.WithName(logChannel.Guild.GetLogText("content")).WithValue(string.IsNullOrWhiteSpace(msg.Content) ? "-" : msg.Resolve(userHandling: TagHandling.FullName)).WithIsInline(false))
.AddField(efb => efb.WithName("Id").WithValue(msg.Id.ToString()).WithIsInline(false)) .AddField(efb => efb.WithName("Id").WithValue(msg.Id.ToString()).WithIsInline(false))
.WithFooter(efb => efb.WithText(currentTime)); .WithFooter(efb => efb.WithText(CurrentTime));
if (msg.Attachments.Any()) if (msg.Attachments.Any())
embed.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("attachments")).WithValue(string.Join(", ", msg.Attachments.Select(a => a.Url))).WithIsInline(false)); embed.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("attachments")).WithValue(string.Join(", ", msg.Attachments.Select(a => a.Url))).WithIsInline(false));
@ -723,7 +706,7 @@ namespace NadekoBot.Modules.Administration
} }
} }
private static async Task _client_MessageUpdated(Optional<SocketMessage> optmsg, SocketMessage imsg2) private static async Task _client_MessageUpdated(Cacheable<IMessage, ulong> optmsg, SocketMessage imsg2, ISocketMessageChannel ch)
{ {
try try
{ {
@ -731,19 +714,18 @@ namespace NadekoBot.Modules.Administration
if (after == null || after.IsAuthor()) if (after == null || after.IsAuthor())
return; return;
var before = (optmsg.IsSpecified ? optmsg.Value : null) as IUserMessage; var before = (optmsg.HasValue ? optmsg.Value : null) as IUserMessage;
if (before == null) if (before == null)
return; return;
var channel = after.Channel as ITextChannel; var channel = ch as ITextChannel;
if (channel == null) if (channel == null)
return; return;
if (before.Content == after.Content) if (before.Content == after.Content)
return; return;
LogSetting logSetting; if (!GuildLogSettings.TryGetValue(channel.Guild.Id, out LogSetting logSetting)
if (!GuildLogSettings.TryGetValue(channel.Guild.Id, out logSetting)
|| (logSetting.MessageUpdatedId == null) || (logSetting.MessageUpdatedId == null)
|| logSetting.IgnoredChannels.Any(ilc => ilc.ChannelId == channel.Id)) || logSetting.IgnoredChannels.Any(ilc => ilc.ChannelId == channel.Id))
return; return;
@ -759,7 +741,7 @@ namespace NadekoBot.Modules.Administration
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("old_msg")).WithValue(string.IsNullOrWhiteSpace(before.Content) ? "-" : before.Resolve(userHandling: TagHandling.FullName)).WithIsInline(false)) .AddField(efb => efb.WithName(logChannel.Guild.GetLogText("old_msg")).WithValue(string.IsNullOrWhiteSpace(before.Content) ? "-" : before.Resolve(userHandling: TagHandling.FullName)).WithIsInline(false))
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("new_msg")).WithValue(string.IsNullOrWhiteSpace(after.Content) ? "-" : after.Resolve(userHandling: TagHandling.FullName)).WithIsInline(false)) .AddField(efb => efb.WithName(logChannel.Guild.GetLogText("new_msg")).WithValue(string.IsNullOrWhiteSpace(after.Content) ? "-" : after.Resolve(userHandling: TagHandling.FullName)).WithIsInline(false))
.AddField(efb => efb.WithName("Id").WithValue(after.Id.ToString()).WithIsInline(false)) .AddField(efb => efb.WithName("Id").WithValue(after.Id.ToString()).WithIsInline(false))
.WithFooter(efb => efb.WithText(currentTime)); .WithFooter(efb => efb.WithText(CurrentTime));
await logChannel.EmbedAsync(embed).ConfigureAwait(false); await logChannel.EmbedAsync(embed).ConfigureAwait(false);
} }

View File

@ -19,9 +19,9 @@ namespace NadekoBot.Modules.Administration
[Group] [Group]
public class MuteCommands : NadekoSubmodule public class MuteCommands : NadekoSubmodule
{ {
private static ConcurrentDictionary<ulong, string> guildMuteRoles { get; } private static ConcurrentDictionary<ulong, string> GuildMuteRoles { get; }
private static ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>> mutedUsers { get; } private static ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>> MutedUsers { get; }
private static ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, Timer>> unmuteTimers { get; } private static ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, Timer>> UnmuteTimers { get; }
= new ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, Timer>>(); = new ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, Timer>>();
public static event Action<IGuildUser, MuteType> UserMuted = delegate { }; public static event Action<IGuildUser, MuteType> UserMuted = delegate { };
@ -40,11 +40,11 @@ namespace NadekoBot.Modules.Administration
static MuteCommands() static MuteCommands()
{ {
var configs = NadekoBot.AllGuildConfigs; var configs = NadekoBot.AllGuildConfigs;
guildMuteRoles = new ConcurrentDictionary<ulong, string>(configs GuildMuteRoles = new ConcurrentDictionary<ulong, string>(configs
.Where(c => !string.IsNullOrWhiteSpace(c.MuteRoleName)) .Where(c => !string.IsNullOrWhiteSpace(c.MuteRoleName))
.ToDictionary(c => c.GuildId, c => c.MuteRoleName)); .ToDictionary(c => c.GuildId, c => c.MuteRoleName));
mutedUsers = new ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>>(configs.ToDictionary( MutedUsers = new ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>>(configs.ToDictionary(
k => k.GuildId, k => k.GuildId,
v => new ConcurrentHashSet<ulong>(v.MutedUsers.Select(m => m.UserId)) v => new ConcurrentHashSet<ulong>(v.MutedUsers.Select(m => m.UserId))
)); ));
@ -73,8 +73,7 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
ConcurrentHashSet<ulong> muted; MutedUsers.TryGetValue(usr.Guild.Id, out ConcurrentHashSet<ulong> muted);
mutedUsers.TryGetValue(usr.Guild.Id, out muted);
if (muted == null || !muted.Contains(usr.Id)) if (muted == null || !muted.Contains(usr.Id))
return; return;
@ -92,7 +91,7 @@ namespace NadekoBot.Modules.Administration
await usr.ModifyAsync(x => x.Mute = true).ConfigureAwait(false); await usr.ModifyAsync(x => x.Mute = true).ConfigureAwait(false);
var muteRole = await GetMuteRole(usr.Guild); var muteRole = await GetMuteRole(usr.Guild);
if (!usr.RoleIds.Contains(muteRole.Id)) if (!usr.RoleIds.Contains(muteRole.Id))
await usr.AddRolesAsync(muteRole).ConfigureAwait(false); await usr.AddRoleAsync(muteRole).ConfigureAwait(false);
StopUnmuteTimer(usr.GuildId, usr.Id); StopUnmuteTimer(usr.GuildId, usr.Id);
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
@ -103,8 +102,7 @@ namespace NadekoBot.Modules.Administration
{ {
UserId = usr.Id UserId = usr.Id
}); });
ConcurrentHashSet<ulong> muted; if (MutedUsers.TryGetValue(usr.Guild.Id, out ConcurrentHashSet<ulong> muted))
if (mutedUsers.TryGetValue(usr.Guild.Id, out muted))
muted.Add(usr.Id); muted.Add(usr.Id);
config.UnmuteTimers.RemoveWhere(x => x.UserId == usr.Id); config.UnmuteTimers.RemoveWhere(x => x.UserId == usr.Id);
@ -118,7 +116,7 @@ namespace NadekoBot.Modules.Administration
{ {
StopUnmuteTimer(usr.GuildId, usr.Id); StopUnmuteTimer(usr.GuildId, usr.Id);
try { await usr.ModifyAsync(x => x.Mute = false).ConfigureAwait(false); } catch { } try { await usr.ModifyAsync(x => x.Mute = false).ConfigureAwait(false); } catch { }
try { await usr.RemoveRolesAsync(await GetMuteRole(usr.Guild)).ConfigureAwait(false); } catch { /*ignore*/ } try { await usr.RemoveRoleAsync(await GetMuteRole(usr.Guild)).ConfigureAwait(false); } catch { /*ignore*/ }
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var config = uow.GuildConfigs.For(usr.Guild.Id, set => set.Include(gc => gc.MutedUsers) var config = uow.GuildConfigs.For(usr.Guild.Id, set => set.Include(gc => gc.MutedUsers)
@ -127,8 +125,7 @@ namespace NadekoBot.Modules.Administration
{ {
UserId = usr.Id UserId = usr.Id
}); });
ConcurrentHashSet<ulong> muted; if (MutedUsers.TryGetValue(usr.Guild.Id, out ConcurrentHashSet<ulong> muted))
if (mutedUsers.TryGetValue(usr.Guild.Id, out muted))
muted.TryRemove(usr.Id); muted.TryRemove(usr.Id);
config.UnmuteTimers.RemoveWhere(x => x.UserId == usr.Id); config.UnmuteTimers.RemoveWhere(x => x.UserId == usr.Id);
@ -142,7 +139,7 @@ namespace NadekoBot.Modules.Administration
{ {
const string defaultMuteRoleName = "nadeko-mute"; const string defaultMuteRoleName = "nadeko-mute";
var muteRoleName = guildMuteRoles.GetOrAdd(guild.Id, defaultMuteRoleName); var muteRoleName = GuildMuteRoles.GetOrAdd(guild.Id, defaultMuteRoleName);
var muteRole = guild.Roles.FirstOrDefault(r => r.Name == muteRoleName); var muteRole = guild.Roles.FirstOrDefault(r => r.Name == muteRoleName);
if (muteRole == null) if (muteRole == null)
@ -198,7 +195,7 @@ namespace NadekoBot.Modules.Administration
public static void StartUnmuteTimer(ulong guildId, ulong userId, TimeSpan after) public static void StartUnmuteTimer(ulong guildId, ulong userId, TimeSpan after)
{ {
//load the unmute timers for this guild //load the unmute timers for this guild
var userUnmuteTimers = unmuteTimers.GetOrAdd(guildId, new ConcurrentDictionary<ulong, Timer>()); var userUnmuteTimers = UnmuteTimers.GetOrAdd(guildId, new ConcurrentDictionary<ulong, Timer>());
//unmute timer to be added //unmute timer to be added
var toAdd = new Timer(async _ => var toAdd = new Timer(async _ =>
@ -232,11 +229,9 @@ namespace NadekoBot.Modules.Administration
public static void StopUnmuteTimer(ulong guildId, ulong userId) public static void StopUnmuteTimer(ulong guildId, ulong userId)
{ {
ConcurrentDictionary<ulong, Timer> userUnmuteTimers; if (!UnmuteTimers.TryGetValue(guildId, out ConcurrentDictionary<ulong, Timer> userUnmuteTimers)) return;
if (!unmuteTimers.TryGetValue(guildId, out userUnmuteTimers)) return;
Timer removed; if (userUnmuteTimers.TryRemove(userId, out Timer removed))
if(userUnmuteTimers.TryRemove(userId, out removed))
{ {
removed.Change(Timeout.Infinite, Timeout.Infinite); removed.Change(Timeout.Infinite, Timeout.Infinite);
} }
@ -266,7 +261,7 @@ namespace NadekoBot.Modules.Administration
{ {
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set); var config = uow.GuildConfigs.For(Context.Guild.Id, set => set);
config.MuteRoleName = name; config.MuteRoleName = name;
guildMuteRoles.AddOrUpdate(Context.Guild.Id, name, (id, old) => name); GuildMuteRoles.AddOrUpdate(Context.Guild.Id, name, (id, old) => name);
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);
} }
await ReplyConfirmLocalized("mute_role_set").ConfigureAwait(false); await ReplyConfirmLocalized("mute_role_set").ConfigureAwait(false);
@ -342,7 +337,7 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
await user.AddRolesAsync(await GetMuteRole(Context.Guild).ConfigureAwait(false)).ConfigureAwait(false); await user.AddRoleAsync(await GetMuteRole(Context.Guild).ConfigureAwait(false)).ConfigureAwait(false);
UserMuted(user, MuteType.Chat); UserMuted(user, MuteType.Chat);
await ReplyConfirmLocalized("user_chat_mute", Format.Bold(user.ToString())).ConfigureAwait(false); await ReplyConfirmLocalized("user_chat_mute", Format.Bold(user.ToString())).ConfigureAwait(false);
} }
@ -359,7 +354,7 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
await user.RemoveRolesAsync(await GetMuteRole(Context.Guild).ConfigureAwait(false)).ConfigureAwait(false); await user.RemoveRoleAsync(await GetMuteRole(Context.Guild).ConfigureAwait(false)).ConfigureAwait(false);
UserUnmuted(user, MuteType.Chat); UserUnmuted(user, MuteType.Chat);
await ReplyConfirmLocalized("user_chat_unmute", Format.Bold(user.ToString())).ConfigureAwait(false); await ReplyConfirmLocalized("user_chat_unmute", Format.Bold(user.ToString())).ConfigureAwait(false);
} }

View File

@ -73,8 +73,8 @@ namespace NadekoBot.Modules.Administration
public static Dictionary<string, Func<string>> PlayingPlaceholders { get; } = public static Dictionary<string, Func<string>> PlayingPlaceholders { get; } =
new Dictionary<string, Func<string>> { new Dictionary<string, Func<string>> {
{ "%servers%", () => NadekoBot.Client.GetGuildCount().ToString()}, { "%servers%", () => NadekoBot.Client.Guilds.Count.ToString()},
{ "%users%", () => NadekoBot.Client.GetGuilds().Sum(s => s.Users.Count).ToString()}, { "%users%", () => NadekoBot.Client.Guilds.Sum(s => s.Users.Count).ToString()},
{ "%playing%", () => { { "%playing%", () => {
var cnt = Music.Music.MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null); var cnt = Music.Music.MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null);
if (cnt != 1) return cnt.ToString(); if (cnt != 1) return cnt.ToString();

View File

@ -40,17 +40,14 @@ namespace NadekoBot.Modules.Administration
public int MaxMessages { get; set; } public int MaxMessages { get; set; }
public int PerSeconds { get; set; } public int PerSeconds { get; set; }
public CancellationTokenSource cancelSource { get; set; } = new CancellationTokenSource(); public CancellationTokenSource CancelSource { get; set; } = new CancellationTokenSource();
public ConcurrentDictionary<ulong, RatelimitedUser> Users { get; set; } = new ConcurrentDictionary<ulong, RatelimitedUser>(); public ConcurrentDictionary<ulong, RatelimitedUser> Users { get; set; } = new ConcurrentDictionary<ulong, RatelimitedUser>();
public bool CheckUserRatelimit(ulong id, ulong guildId, SocketGuildUser optUser) public bool CheckUserRatelimit(ulong id, ulong guildId, SocketGuildUser optUser)
{ {
HashSet<ulong> ignoreUsers; if ((IgnoredUsers.TryGetValue(guildId, out HashSet<ulong> ignoreUsers) && ignoreUsers.Contains(id)) ||
HashSet<ulong> ignoreRoles; (optUser != null && IgnoredRoles.TryGetValue(guildId, out HashSet<ulong> ignoreRoles) && optUser.Roles.Any(x => ignoreRoles.Contains(x.Id))))
if ((IgnoredUsers.TryGetValue(guildId, out ignoreUsers) && ignoreUsers.Contains(id)) ||
(optUser != null && IgnoredRoles.TryGetValue(guildId, out ignoreRoles) && optUser.RoleIds.Any(x => ignoreRoles.Contains(x))))
return false; return false;
var usr = Users.GetOrAdd(id, (key) => new RatelimitedUser() { UserId = id }); var usr = Users.GetOrAdd(id, (key) => new RatelimitedUser() { UserId = id });
@ -63,7 +60,7 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
await Task.Delay(PerSeconds * 1000, cancelSource.Token); await Task.Delay(PerSeconds * 1000, CancelSource.Token);
} }
catch (OperationCanceledException) { } catch (OperationCanceledException) { }
usr.MessageCount--; usr.MessageCount--;
@ -95,8 +92,7 @@ namespace NadekoBot.Modules.Administration
if (channel == null || usrMsg.IsAuthor()) if (channel == null || usrMsg.IsAuthor())
return; return;
Ratelimiter limiter; if (!RatelimitingChannels.TryGetValue(channel.Id, out Ratelimiter limiter))
if (!RatelimitingChannels.TryGetValue(channel.Id, out limiter))
return; return;
if (limiter.CheckUserRatelimit(usrMsg.Author.Id, channel.Guild.Id, usrMsg.Author as SocketGuildUser)) if (limiter.CheckUserRatelimit(usrMsg.Author.Id, channel.Guild.Id, usrMsg.Author as SocketGuildUser))
@ -111,10 +107,9 @@ namespace NadekoBot.Modules.Administration
[RequireUserPermission(GuildPermission.ManageMessages)] [RequireUserPermission(GuildPermission.ManageMessages)]
public async Task Slowmode() public async Task Slowmode()
{ {
Ratelimiter throwaway; if (RatelimitingChannels.TryRemove(Context.Channel.Id, out Ratelimiter throwaway))
if (RatelimitingChannels.TryRemove(Context.Channel.Id, out throwaway))
{ {
throwaway.cancelSource.Cancel(); throwaway.CancelSource.Cancel();
await ReplyConfirmLocalized("slowmode_disabled").ConfigureAwait(false); await ReplyConfirmLocalized("slowmode_disabled").ConfigureAwait(false);
} }
} }

View File

@ -185,7 +185,7 @@ namespace NadekoBot.Modules.Administration
var sameRole = Context.Guild.GetRole(sameRoleId); var sameRole = Context.Guild.GetRole(sameRoleId);
if (sameRole != null) if (sameRole != null)
{ {
await guildUser.RemoveRolesAsync(sameRole).ConfigureAwait(false); await guildUser.RemoveRoleAsync(sameRole).ConfigureAwait(false);
await Task.Delay(500).ConfigureAwait(false); await Task.Delay(500).ConfigureAwait(false);
} }
//await ReplyErrorLocalized("self_assign_already_excl", Format.Bold(sameRole?.Name)).ConfigureAwait(false); //await ReplyErrorLocalized("self_assign_already_excl", Format.Bold(sameRole?.Name)).ConfigureAwait(false);
@ -194,7 +194,7 @@ namespace NadekoBot.Modules.Administration
} }
try try
{ {
await guildUser.AddRolesAsync(role).ConfigureAwait(false); await guildUser.AddRoleAsync(role).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -236,7 +236,7 @@ namespace NadekoBot.Modules.Administration
} }
try try
{ {
await guildUser.RemoveRolesAsync(role).ConfigureAwait(false); await guildUser.RemoveRoleAsync(role).ConfigureAwait(false);
} }
catch (Exception) catch (Exception)
{ {

View File

@ -289,7 +289,7 @@ namespace NadekoBot.Modules.Administration
try try
{ {
await ReplyConfirmLocalized("shard_reconnecting", Format.Bold("#" + shardid)).ConfigureAwait(false); await ReplyConfirmLocalized("shard_reconnecting", Format.Bold("#" + shardid)).ConfigureAwait(false);
await shard.ConnectAsync().ConfigureAwait(false); await shard.StartAsync().ConfigureAwait(false);
await ReplyConfirmLocalized("shard_reconnected", Format.Bold("#" + shardid)).ConfigureAwait(false); await ReplyConfirmLocalized("shard_reconnected", Format.Bold("#" + shardid)).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)
@ -303,8 +303,8 @@ namespace NadekoBot.Modules.Administration
public async Task Leave([Remainder] string guildStr) public async Task Leave([Remainder] string guildStr)
{ {
guildStr = guildStr.Trim().ToUpperInvariant(); guildStr = guildStr.Trim().ToUpperInvariant();
var server = NadekoBot.Client.GetGuilds().FirstOrDefault(g => g.Id.ToString() == guildStr) ?? var server = NadekoBot.Client.Guilds.FirstOrDefault(g => g.Id.ToString() == guildStr) ??
NadekoBot.Client.GetGuilds().FirstOrDefault(g => g.Name.Trim().ToUpperInvariant() == guildStr); NadekoBot.Client.Guilds.FirstOrDefault(g => g.Name.Trim().ToUpperInvariant() == guildStr);
if (server == null) if (server == null)
{ {
@ -414,7 +414,7 @@ namespace NadekoBot.Modules.Administration
if (ids.Length != 2) if (ids.Length != 2)
return; return;
var sid = ulong.Parse(ids[0]); var sid = ulong.Parse(ids[0]);
var server = NadekoBot.Client.GetGuilds().FirstOrDefault(s => s.Id == sid); var server = NadekoBot.Client.Guilds.FirstOrDefault(s => s.Id == sid);
if (server == null) if (server == null)
return; return;
@ -451,7 +451,7 @@ namespace NadekoBot.Modules.Administration
[OwnerOnly] [OwnerOnly]
public async Task Announce([Remainder] string message) public async Task Announce([Remainder] string message)
{ {
var channels = NadekoBot.Client.GetGuilds().Select(g => g.DefaultChannel).ToArray(); var channels = NadekoBot.Client.Guilds.Select(g => g.DefaultChannel).ToArray();
if (channels == null) if (channels == null)
return; return;
await Task.WhenAll(channels.Where(c => c != null).Select(c => c.SendConfirmAsync(GetText("message_from_bo", Context.User.ToString()), message))) await Task.WhenAll(channels.Where(c => c != null).Select(c => c.SendConfirmAsync(GetText("message_from_bo", Context.User.ToString()), message)))

View File

@ -18,12 +18,12 @@ namespace NadekoBot.Modules.Administration
[Group] [Group]
public class VcRoleCommands : NadekoSubmodule public class VcRoleCommands : NadekoSubmodule
{ {
private static ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, IRole>> vcRoles { get; } private static ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, IRole>> VcRoles { get; }
static VcRoleCommands() static VcRoleCommands()
{ {
NadekoBot.Client.UserVoiceStateUpdated += ClientOnUserVoiceStateUpdated; NadekoBot.Client.UserVoiceStateUpdated += ClientOnUserVoiceStateUpdated;
vcRoles = new ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, IRole>>(); VcRoles = new ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, IRole>>();
foreach (var gconf in NadekoBot.AllGuildConfigs) foreach (var gconf in NadekoBot.AllGuildConfigs)
{ {
var g = NadekoBot.Client.GetGuild(gconf.GuildId); var g = NadekoBot.Client.GetGuild(gconf.GuildId);
@ -31,7 +31,7 @@ namespace NadekoBot.Modules.Administration
continue; //todo delete everything from db if guild doesn't exist? continue; //todo delete everything from db if guild doesn't exist?
var infos = new ConcurrentDictionary<ulong, IRole>(); var infos = new ConcurrentDictionary<ulong, IRole>();
vcRoles.TryAdd(gconf.GuildId, infos); VcRoles.TryAdd(gconf.GuildId, infos);
foreach (var ri in gconf.VcRoleInfos) foreach (var ri in gconf.VcRoleInfos)
{ {
var role = g.GetRole(ri.RoleId); var role = g.GetRole(ri.RoleId);
@ -62,24 +62,22 @@ namespace NadekoBot.Modules.Administration
ulong guildId; ulong guildId;
guildId = newVc?.Guild.Id ?? oldVc.Guild.Id; guildId = newVc?.Guild.Id ?? oldVc.Guild.Id;
ConcurrentDictionary<ulong, IRole> guildVcRoles; if (VcRoles.TryGetValue(guildId, out ConcurrentDictionary<ulong, IRole> guildVcRoles))
if (vcRoles.TryGetValue(guildId, out guildVcRoles))
{ {
IRole role;
//remove old //remove old
if (oldVc != null && guildVcRoles.TryGetValue(oldVc.Id, out role)) if (oldVc != null && guildVcRoles.TryGetValue(oldVc.Id, out IRole role))
{ {
if (gusr.RoleIds.Contains(role.Id)) if (gusr.Roles.Contains(role))
{ {
try try
{ {
await gusr.RemoveRolesAsync(role).ConfigureAwait(false); await gusr.RemoveRoleAsync(role).ConfigureAwait(false);
await Task.Delay(500).ConfigureAwait(false); await Task.Delay(500).ConfigureAwait(false);
} }
catch catch
{ {
await Task.Delay(200).ConfigureAwait(false); await Task.Delay(200).ConfigureAwait(false);
await gusr.RemoveRolesAsync(role).ConfigureAwait(false); await gusr.RemoveRoleAsync(role).ConfigureAwait(false);
await Task.Delay(500).ConfigureAwait(false); await Task.Delay(500).ConfigureAwait(false);
} }
} }
@ -87,8 +85,8 @@ namespace NadekoBot.Modules.Administration
//add new //add new
if (newVc != null && guildVcRoles.TryGetValue(newVc.Id, out role)) if (newVc != null && guildVcRoles.TryGetValue(newVc.Id, out role))
{ {
if (!gusr.RoleIds.Contains(role.Id)) if (!gusr.Roles.Contains(role))
await gusr.AddRolesAsync(role).ConfigureAwait(false); await gusr.AddRoleAsync(role).ConfigureAwait(false);
} }
} }
@ -120,7 +118,7 @@ namespace NadekoBot.Modules.Administration
return; return;
} }
var guildVcRoles = vcRoles.GetOrAdd(user.GuildId, new ConcurrentDictionary<ulong, IRole>()); var guildVcRoles = VcRoles.GetOrAdd(user.GuildId, new ConcurrentDictionary<ulong, IRole>());
if (role == null) if (role == null)
{ {
@ -159,8 +157,7 @@ namespace NadekoBot.Modules.Administration
{ {
var guild = (SocketGuild) Context.Guild; var guild = (SocketGuild) Context.Guild;
string text; string text;
ConcurrentDictionary<ulong, IRole> roles; if (VcRoles.TryGetValue(Context.Guild.Id, out ConcurrentDictionary<ulong, IRole> roles))
if (vcRoles.TryGetValue(Context.Guild.Id, out roles))
{ {
if (!roles.Any()) if (!roles.Any())
{ {

View File

@ -98,7 +98,7 @@ namespace NadekoBot.Modules.Administration
try try
{ {
_log.Info("Removing role " + beforeRoleName + " from user " + user.Username); _log.Info("Removing role " + beforeRoleName + " from user " + user.Username);
await user.RemoveRolesAsync(beforeRole).ConfigureAwait(false); await user.RemoveRoleAsync(beforeRole).ConfigureAwait(false);
await Task.Delay(200).ConfigureAwait(false); await Task.Delay(200).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)
@ -119,7 +119,7 @@ namespace NadekoBot.Modules.Administration
{ {
var created = (await guild.CreateTextChannelAsync(GetChannelName(afterVch.Name).ToLowerInvariant()).ConfigureAwait(false)); var created = (await guild.CreateTextChannelAsync(GetChannelName(afterVch.Name).ToLowerInvariant()).ConfigureAwait(false));
try { await guild.CurrentUser.AddRolesAsync(roleToAdd).ConfigureAwait(false); } catch {/*ignored*/} try { await guild.CurrentUser.AddRoleAsync(roleToAdd).ConfigureAwait(false); } catch {/*ignored*/}
await Task.Delay(50).ConfigureAwait(false); await Task.Delay(50).ConfigureAwait(false);
await created.AddPermissionOverwriteAsync(roleToAdd, new OverwritePermissions( await created.AddPermissionOverwriteAsync(roleToAdd, new OverwritePermissions(
readMessages: PermValue.Allow, readMessages: PermValue.Allow,
@ -133,7 +133,7 @@ namespace NadekoBot.Modules.Administration
await Task.Delay(50).ConfigureAwait(false); await Task.Delay(50).ConfigureAwait(false);
} }
_log.Warn("Adding role " + roleToAdd.Name + " to user " + user.Username); _log.Warn("Adding role " + roleToAdd.Name + " to user " + user.Username);
await user.AddRolesAsync(roleToAdd).ConfigureAwait(false); await user.AddRoleAsync(roleToAdd).ConfigureAwait(false);
} }
} }
finally finally

View File

@ -51,7 +51,7 @@ namespace NadekoBot.Modules.Gambling
} }
} }
public async Task SneakyGameStatusEvent(CommandContext context, int? arg) public async Task SneakyGameStatusEvent(ICommandContext context, int? arg)
{ {
int num; int num;
if (arg == null || arg < 5) if (arg == null || arg < 5)
@ -115,7 +115,7 @@ namespace NadekoBot.Modules.Gambling
return Task.Delay(0); return Task.Delay(0);
} }
public async Task FlowerReactionEvent(CommandContext context, int amount) public async Task FlowerReactionEvent(ICommandContext context, int amount)
{ {
if (amount <= 0) if (amount <= 0)
amount = 100; amount = 100;
@ -134,7 +134,7 @@ namespace NadekoBot.Modules.Gambling
public abstract class CurrencyEvent public abstract class CurrencyEvent
{ {
public abstract Task Start(IUserMessage msg, CommandContext channel, int amount); public abstract Task Start(IUserMessage msg, ICommandContext channel, int amount);
} }
public class FlowerReactionEvent : CurrencyEvent public class FlowerReactionEvent : CurrencyEvent
@ -142,31 +142,31 @@ namespace NadekoBot.Modules.Gambling
private readonly ConcurrentHashSet<ulong> _flowerReactionAwardedUsers = new ConcurrentHashSet<ulong>(); private readonly ConcurrentHashSet<ulong> _flowerReactionAwardedUsers = new ConcurrentHashSet<ulong>();
private readonly Logger _log; private readonly Logger _log;
private IUserMessage msg { get; set; } private IUserMessage StartingMessage { get; set; }
private CancellationTokenSource source { get; } private CancellationTokenSource Source { get; }
private CancellationToken cancelToken { get; } private CancellationToken CancelToken { get; }
public FlowerReactionEvent() public FlowerReactionEvent()
{ {
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
source = new CancellationTokenSource(); Source = new CancellationTokenSource();
cancelToken = source.Token; CancelToken = Source.Token;
} }
private async Task End() private async Task End()
{ {
if(msg != null) if(StartingMessage != null)
await msg.DeleteAsync().ConfigureAwait(false); await StartingMessage.DeleteAsync().ConfigureAwait(false);
if(!source.IsCancellationRequested) if(!Source.IsCancellationRequested)
source.Cancel(); Source.Cancel();
NadekoBot.Client.MessageDeleted -= MessageDeletedEventHandler; NadekoBot.Client.MessageDeleted -= MessageDeletedEventHandler;
} }
private Task MessageDeletedEventHandler(ulong id, Optional<SocketMessage> _) { private Task MessageDeletedEventHandler(Cacheable<IMessage, ulong> msg, ISocketMessageChannel channel) {
if (msg?.Id == id) if (StartingMessage?.Id == msg.Id)
{ {
_log.Warn("Stopping flower reaction event because message is deleted."); _log.Warn("Stopping flower reaction event because message is deleted.");
var __ = Task.Run(End); var __ = Task.Run(End);
@ -175,22 +175,22 @@ namespace NadekoBot.Modules.Gambling
return Task.CompletedTask; return Task.CompletedTask;
} }
public override async Task Start(IUserMessage umsg, CommandContext context, int amount) public override async Task Start(IUserMessage umsg, ICommandContext context, int amount)
{ {
msg = umsg; StartingMessage = umsg;
NadekoBot.Client.MessageDeleted += MessageDeletedEventHandler; NadekoBot.Client.MessageDeleted += MessageDeletedEventHandler;
try { await msg.AddReactionAsync("🌸").ConfigureAwait(false); } try { await StartingMessage.AddReactionAsync("🌸").ConfigureAwait(false); }
catch catch
{ {
try { await msg.AddReactionAsync("🌸").ConfigureAwait(false); } try { await StartingMessage.AddReactionAsync("🌸").ConfigureAwait(false); }
catch catch
{ {
try { await msg.DeleteAsync().ConfigureAwait(false); } try { await StartingMessage.DeleteAsync().ConfigureAwait(false); }
catch { return; } catch { return; }
} }
} }
using (msg.OnReaction(async (r) => using (StartingMessage.OnReaction(async (r) =>
{ {
try try
{ {
@ -208,13 +208,13 @@ namespace NadekoBot.Modules.Gambling
{ {
try try
{ {
await Task.Delay(TimeSpan.FromHours(24), cancelToken).ConfigureAwait(false); await Task.Delay(TimeSpan.FromHours(24), CancelToken).ConfigureAwait(false);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
} }
if (cancelToken.IsCancellationRequested) if (CancelToken.IsCancellationRequested)
return; return;
_log.Warn("Stopping flower reaction event because it expired."); _log.Warn("Stopping flower reaction event because it expired.");

View File

@ -102,7 +102,7 @@ namespace NadekoBot.Modules.Gambling
{ {
try try
{ {
await guser.AddRolesAsync(role).ConfigureAwait(false); await guser.AddRoleAsync(role).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -202,8 +202,10 @@ namespace NadekoBot.Modules.Gambling
{ {
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigs.For(Context.Guild.Id, var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigs.For(Context.Guild.Id,
set => set.Include(x => x.ShopEntries) set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items)).ShopEntries); .ThenInclude(x => x.Items)).ShopEntries)
entries.Add(entry); {
entry
};
uow.GuildConfigs.For(Context.Guild.Id, set => set).ShopEntries = entries; uow.GuildConfigs.For(Context.Guild.Id, set => set).ShopEntries = entries;
uow.Complete(); uow.Complete();
} }
@ -228,8 +230,10 @@ namespace NadekoBot.Modules.Gambling
{ {
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigs.For(Context.Guild.Id, var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigs.For(Context.Guild.Id,
set => set.Include(x => x.ShopEntries) set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items)).ShopEntries); .ThenInclude(x => x.Items)).ShopEntries)
entries.Add(entry); {
entry
};
uow.GuildConfigs.For(Context.Guild.Id, set => set).ShopEntries = entries; uow.GuildConfigs.For(Context.Guild.Id, set => set).ShopEntries = entries;
uow.Complete(); uow.Complete();
} }

View File

@ -39,7 +39,7 @@ namespace NadekoBot.Modules.Gambling
{ {
role = role ?? Context.Guild.EveryoneRole; role = role ?? Context.Guild.EveryoneRole;
var members = role.Members().Where(u => u.Status != UserStatus.Offline && u.Status != UserStatus.Unknown); var members = role.Members().Where(u => u.Status != UserStatus.Offline);
var membersArray = members as IUser[] ?? members.ToArray(); var membersArray = members as IUser[] ?? members.ToArray();
var usr = membersArray[new NadekoRandom().Next(0, membersArray.Length)]; var usr = membersArray[new NadekoRandom().Next(0, membersArray.Length)];
await Context.Channel.SendConfirmAsync("🎟 "+ GetText("raffled_user"), $"**{usr.Username}#{usr.Discriminator}**", footer: $"ID: {usr.Id}").ConfigureAwait(false); await Context.Channel.SendConfirmAsync("🎟 "+ GetText("raffled_user"), $"**{usr.Username}#{usr.Discriminator}**", footer: $"ID: {usr.Id}").ConfigureAwait(false);

View File

@ -30,7 +30,7 @@ namespace NadekoBot.Modules.Music.Classes
public class MusicPlayer public class MusicPlayer
{ {
private IAudioClient audioClient { get; set; } private IAudioClient AudioClient { get; set; }
/// <summary> /// <summary>
/// Player will prioritize different queuer name /// Player will prioritize different queuer name
@ -55,7 +55,7 @@ namespace NadekoBot.Modules.Music.Classes
/// <summary> /// <summary>
/// Users who recently got their music wish /// Users who recently got their music wish
/// </summary> /// </summary>
private ConcurrentHashSet<string> recentlyPlayedUsers { get; } = new ConcurrentHashSet<string>(); private ConcurrentHashSet<string> RecentlyPlayedUsers { get; } = new ConcurrentHashSet<string>();
private readonly List<Song> _playlist = new List<Song>(); private readonly List<Song> _playlist = new List<Song>();
private readonly Logger _log; private readonly Logger _log;
@ -64,7 +64,7 @@ namespace NadekoBot.Modules.Music.Classes
public Song CurrentSong { get; private set; } public Song CurrentSong { get; private set; }
public CancellationTokenSource SongCancelSource { get; private set; } public CancellationTokenSource SongCancelSource { get; private set; }
private CancellationToken cancelToken { get; set; } private CancellationToken CancelToken { get; set; }
public bool Paused { get; set; } public bool Paused { get; set; }
@ -77,13 +77,13 @@ namespace NadekoBot.Modules.Music.Classes
public IVoiceChannel PlaybackVoiceChannel { get; private set; } public IVoiceChannel PlaybackVoiceChannel { get; private set; }
public ITextChannel OutputTextChannel { get; set; } public ITextChannel OutputTextChannel { get; set; }
private bool destroyed { get; set; } private bool Destroyed { get; set; }
public bool RepeatSong { get; private set; } public bool RepeatSong { get; private set; }
public bool RepeatPlaylist { get; private set; } public bool RepeatPlaylist { get; private set; }
public bool Autoplay { get; set; } public bool Autoplay { get; set; }
public uint MaxQueueSize { get; set; } = 0; public uint MaxQueueSize { get; set; } = 0;
private ConcurrentQueue<Action> actionQueue { get; } = new ConcurrentQueue<Action>(); private ConcurrentQueue<Action> ActionQueue { get; } = new ConcurrentQueue<Action>();
public string PrettyVolume => $"🔉 {(int)(Volume * 100)}%"; public string PrettyVolume => $"🔉 {(int)(Volume * 100)}%";
@ -91,28 +91,24 @@ namespace NadekoBot.Modules.Music.Classes
public MusicPlayer(IVoiceChannel startingVoiceChannel, ITextChannel outputChannel, float? defaultVolume) public MusicPlayer(IVoiceChannel startingVoiceChannel, ITextChannel outputChannel, float? defaultVolume)
{ {
if (startingVoiceChannel == null)
throw new ArgumentNullException(nameof(startingVoiceChannel));
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
OutputTextChannel = outputChannel; OutputTextChannel = outputChannel;
Volume = defaultVolume ?? 1.0f; Volume = defaultVolume ?? 1.0f;
PlaybackVoiceChannel = startingVoiceChannel; PlaybackVoiceChannel = startingVoiceChannel ?? throw new ArgumentNullException(nameof(startingVoiceChannel));
SongCancelSource = new CancellationTokenSource(); SongCancelSource = new CancellationTokenSource();
cancelToken = SongCancelSource.Token; CancelToken = SongCancelSource.Token;
Task.Run(async () => Task.Run(async () =>
{ {
try try
{ {
while (!destroyed) while (!Destroyed)
{ {
try try
{ {
Action action; if (ActionQueue.TryDequeue(out Action action))
if (actionQueue.TryDequeue(out action))
{ {
action(); action();
} }
@ -132,7 +128,7 @@ namespace NadekoBot.Modules.Music.Classes
var t = new Thread(async () => var t = new Thread(async () =>
{ {
while (!destroyed) while (!Destroyed)
{ {
try try
{ {
@ -141,9 +137,9 @@ namespace NadekoBot.Modules.Music.Classes
if (CurrentSong == null) if (CurrentSong == null)
continue; continue;
if (audioClient != null) if (AudioClient != null)
try { await audioClient.DisconnectAsync().ConfigureAwait(false); } catch { } try { await AudioClient.StopAsync().ConfigureAwait(false); } catch { }
audioClient = await PlaybackVoiceChannel.ConnectAsync().ConfigureAwait(false); AudioClient = await PlaybackVoiceChannel.ConnectAsync().ConfigureAwait(false);
var index = _playlist.IndexOf(CurrentSong); var index = _playlist.IndexOf(CurrentSong);
if (index != -1) if (index != -1)
@ -152,7 +148,7 @@ namespace NadekoBot.Modules.Music.Classes
OnStarted(this, CurrentSong); OnStarted(this, CurrentSong);
try try
{ {
await CurrentSong.Play(audioClient, cancelToken); await CurrentSong.Play(AudioClient, CancelToken);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
@ -178,12 +174,12 @@ namespace NadekoBot.Modules.Music.Classes
} }
finally finally
{ {
if (!cancelToken.IsCancellationRequested) if (!CancelToken.IsCancellationRequested)
{ {
SongCancelSource.Cancel(); SongCancelSource.Cancel();
} }
SongCancelSource = new CancellationTokenSource(); SongCancelSource = new CancellationTokenSource();
cancelToken = SongCancelSource.Token; CancelToken = SongCancelSource.Token;
CurrentSong = null; CurrentSong = null;
await Task.Delay(300).ConfigureAwait(false); await Task.Delay(300).ConfigureAwait(false);
} }
@ -195,7 +191,7 @@ namespace NadekoBot.Modules.Music.Classes
public void Next() public void Next()
{ {
actionQueue.Enqueue(() => ActionQueue.Enqueue(() =>
{ {
Paused = false; Paused = false;
SongCancelSource.Cancel(); SongCancelSource.Cancel();
@ -204,7 +200,7 @@ namespace NadekoBot.Modules.Music.Classes
public void Stop() public void Stop()
{ {
actionQueue.Enqueue(() => ActionQueue.Enqueue(() =>
{ {
RepeatPlaylist = false; RepeatPlaylist = false;
RepeatSong = false; RepeatSong = false;
@ -234,24 +230,24 @@ namespace NadekoBot.Modules.Music.Classes
{ {
return _playlist.FirstOrDefault(); return _playlist.FirstOrDefault();
} }
var song = _playlist.FirstOrDefault(c => !recentlyPlayedUsers.Contains(c.QueuerName)) var song = _playlist.FirstOrDefault(c => !RecentlyPlayedUsers.Contains(c.QueuerName))
?? _playlist.FirstOrDefault(); ?? _playlist.FirstOrDefault();
if (song == null) if (song == null)
return null; return null;
if (recentlyPlayedUsers.Contains(song.QueuerName)) if (RecentlyPlayedUsers.Contains(song.QueuerName))
{ {
recentlyPlayedUsers.Clear(); RecentlyPlayedUsers.Clear();
} }
recentlyPlayedUsers.Add(song.QueuerName); RecentlyPlayedUsers.Add(song.QueuerName);
return song; return song;
} }
public void Shuffle() public void Shuffle()
{ {
actionQueue.Enqueue(() => ActionQueue.Enqueue(() =>
{ {
var oldPlaylist = _playlist.ToArray(); var oldPlaylist = _playlist.ToArray();
_playlist.Clear(); _playlist.Clear();
@ -264,7 +260,7 @@ namespace NadekoBot.Modules.Music.Classes
if (s == null) if (s == null)
throw new ArgumentNullException(nameof(s)); throw new ArgumentNullException(nameof(s));
ThrowIfQueueFull(); ThrowIfQueueFull();
actionQueue.Enqueue(() => ActionQueue.Enqueue(() =>
{ {
s.MusicPlayer = this; s.MusicPlayer = this;
s.QueuerName = username.TrimTo(10); s.QueuerName = username.TrimTo(10);
@ -276,7 +272,7 @@ namespace NadekoBot.Modules.Music.Classes
{ {
if (s == null) if (s == null)
throw new ArgumentNullException(nameof(s)); throw new ArgumentNullException(nameof(s));
actionQueue.Enqueue(() => ActionQueue.Enqueue(() =>
{ {
_playlist.Insert(index, s); _playlist.Insert(index, s);
}); });
@ -286,7 +282,7 @@ namespace NadekoBot.Modules.Music.Classes
{ {
if (s == null) if (s == null)
throw new ArgumentNullException(nameof(s)); throw new ArgumentNullException(nameof(s));
actionQueue.Enqueue(() => ActionQueue.Enqueue(() =>
{ {
_playlist.Remove(s); _playlist.Remove(s);
}); });
@ -294,7 +290,7 @@ namespace NadekoBot.Modules.Music.Classes
public void RemoveSongAt(int index, bool silent = false) public void RemoveSongAt(int index, bool silent = false)
{ {
actionQueue.Enqueue(() => ActionQueue.Enqueue(() =>
{ {
if (index < 0 || index >= _playlist.Count) if (index < 0 || index >= _playlist.Count)
return; return;
@ -309,7 +305,7 @@ namespace NadekoBot.Modules.Music.Classes
public void ClearQueue() public void ClearQueue()
{ {
actionQueue.Enqueue(() => ActionQueue.Enqueue(() =>
{ {
_playlist.Clear(); _playlist.Clear();
}); });
@ -347,15 +343,15 @@ namespace NadekoBot.Modules.Music.Classes
public void Destroy() public void Destroy()
{ {
actionQueue.Enqueue(async () => ActionQueue.Enqueue(async () =>
{ {
RepeatPlaylist = false; RepeatPlaylist = false;
RepeatSong = false; RepeatSong = false;
Autoplay = false; Autoplay = false;
destroyed = true; Destroyed = true;
_playlist.Clear(); _playlist.Clear();
try { await audioClient.DisconnectAsync(); } catch { } try { await AudioClient.StopAsync(); } catch { }
if (!SongCancelSource.IsCancellationRequested) if (!SongCancelSource.IsCancellationRequested)
SongCancelSource.Cancel(); SongCancelSource.Cancel();
}); });

View File

@ -29,13 +29,13 @@ namespace NadekoBot.Modules.Music.Classes
public string QueuerName { get; set; } public string QueuerName { get; set; }
public TimeSpan TotalTime { get; set; } = TimeSpan.Zero; public TimeSpan TotalTime { get; set; } = TimeSpan.Zero;
public TimeSpan CurrentTime => TimeSpan.FromSeconds(bytesSent / (float)_frameBytes / (1000 / (float)_milliseconds)); public TimeSpan CurrentTime => TimeSpan.FromSeconds(BytesSent / (float)_frameBytes / (1000 / (float)_milliseconds));
private const int _milliseconds = 20; private const int _milliseconds = 20;
private const int _samplesPerFrame = (48000 / 1000) * _milliseconds; private const int _samplesPerFrame = (48000 / 1000) * _milliseconds;
private const int _frameBytes = 3840; //16-bit, 2 channels private const int _frameBytes = 3840; //16-bit, 2 channels
private ulong bytesSent { get; set; } private ulong BytesSent { get; set; }
//pwetty //pwetty
@ -138,7 +138,7 @@ namespace NadekoBot.Modules.Music.Classes
public async Task Play(IAudioClient voiceClient, CancellationToken cancelToken) public async Task Play(IAudioClient voiceClient, CancellationToken cancelToken)
{ {
bytesSent = (ulong) SkipTo * 3840 * 50; BytesSent = (ulong) SkipTo * 3840 * 50;
var filename = Path.Combine(Music.MusicDataPath, DateTime.Now.UnixTimestamp().ToString()); var filename = Path.Combine(Music.MusicDataPath, DateTime.Now.UnixTimestamp().ToString());
var inStream = new SongBuffer(MusicPlayer, filename, SongInfo, SkipTo, _frameBytes * 100); var inStream = new SongBuffer(MusicPlayer, filename, SongInfo, SkipTo, _frameBytes * 100);
@ -188,7 +188,7 @@ namespace NadekoBot.Modules.Music.Classes
sw.Stop(); sw.Stop();
_log.Debug("Prebuffering successfully completed in " + sw.Elapsed); _log.Debug("Prebuffering successfully completed in " + sw.Elapsed);
var outStream = voiceClient.CreatePCMStream(960); var outStream = voiceClient.CreatePCMStream(AudioApplication.Music);
int nextTime = Environment.TickCount + _milliseconds; int nextTime = Environment.TickCount + _milliseconds;
@ -203,7 +203,7 @@ namespace NadekoBot.Modules.Music.Classes
_log.Debug("read {0}", read); _log.Debug("read {0}", read);
unchecked unchecked
{ {
bytesSent += (ulong)read; BytesSent += (ulong)read;
} }
if (read < _frameBytes) if (read < _frameBytes)
{ {

View File

@ -24,7 +24,7 @@ namespace NadekoBot.Modules.Utility
if (string.IsNullOrWhiteSpace(guildName)) if (string.IsNullOrWhiteSpace(guildName))
guild = channel.Guild; guild = channel.Guild;
else else
guild = NadekoBot.Client.GetGuilds().FirstOrDefault(g => g.Name.ToUpperInvariant() == guildName.ToUpperInvariant()); guild = NadekoBot.Client.Guilds.FirstOrDefault(g => g.Name.ToUpperInvariant() == guildName.ToUpperInvariant());
if (guild == null) if (guild == null)
return; return;
var ownername = await guild.GetUserAsync(guild.OwnerId); var ownername = await guild.GetUserAsync(guild.OwnerId);

View File

@ -384,9 +384,9 @@ namespace NadekoBot.Modules.Utility
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
public async Task ShardId(ulong guildid) public async Task ShardId(IGuild guild)
{ {
var shardId = NadekoBot.Client.GetShardIdFor(guildid); var shardId = NadekoBot.Client.GetShardIdFor(guild);
await Context.Channel.SendConfirmAsync(shardId.ToString()).ConfigureAwait(false); await Context.Channel.SendConfirmAsync(shardId.ToString()).ConfigureAwait(false);
} }
@ -397,7 +397,7 @@ namespace NadekoBot.Modules.Utility
var stats = NadekoBot.Stats; var stats = NadekoBot.Stats;
var shardId = Context.Guild != null var shardId = Context.Guild != null
? NadekoBot.Client.GetShardIdFor(Context.Guild.Id) ? NadekoBot.Client.GetShardIdFor(Context.Guild)
: 0; : 0;
await Context.Channel.EmbedAsync( await Context.Channel.EmbedAsync(
@ -415,7 +415,7 @@ namespace NadekoBot.Modules.Utility
.AddField(efb => efb.WithName(GetText("uptime")).WithValue(stats.GetUptimeString("\n")).WithIsInline(true)) .AddField(efb => efb.WithName(GetText("uptime")).WithValue(stats.GetUptimeString("\n")).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("presence")).WithValue( .AddField(efb => efb.WithName(GetText("presence")).WithValue(
GetText("presence_txt", GetText("presence_txt",
NadekoBot.Client.GetGuildCount(), stats.TextChannels, stats.VoiceChannels)).WithIsInline(true)) NadekoBot.Client.Guilds.Count, stats.TextChannels, stats.VoiceChannels)).WithIsInline(true))
#if !GLOBAL_NADEKO #if !GLOBAL_NADEKO
.WithFooter(efb => efb.WithText(GetText("stats_songs", .WithFooter(efb => efb.WithText(GetText("stats_songs",
Music.Music.MusicPlayers.Count(mp => mp.Value.CurrentSong != null), Music.Music.MusicPlayers.Count(mp => mp.Value.CurrentSong != null),
@ -446,7 +446,7 @@ namespace NadekoBot.Modules.Utility
if (page < 0) if (page < 0)
return; return;
var guilds = await Task.Run(() => NadekoBot.Client.GetGuilds().OrderBy(g => g.Name).Skip((page - 1) * 15).Take(15)).ConfigureAwait(false); var guilds = await Task.Run(() => NadekoBot.Client.Guilds.OrderBy(g => g.Name).Skip((page - 1) * 15).Take(15)).ConfigureAwait(false);
if (!guilds.Any()) if (!guilds.Any())
{ {

View File

@ -20,6 +20,7 @@ using NadekoBot.Modules.Music;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
using System.Resources; using System.Resources;
using NadekoBot.Resources; using NadekoBot.Resources;
using System.Threading;
namespace NadekoBot namespace NadekoBot
{ {
@ -74,7 +75,6 @@ namespace NadekoBot
//create client //create client
Client = new DiscordShardedClient(new DiscordSocketConfig Client = new DiscordShardedClient(new DiscordSocketConfig
{ {
AudioMode = Discord.Audio.AudioMode.Outgoing,
MessageCacheSize = 10, MessageCacheSize = 10,
LogLevel = LogSeverity.Warning, LogLevel = LogSeverity.Warning,
TotalShards = Credentials.TotalShards, TotalShards = Credentials.TotalShards,
@ -119,8 +119,17 @@ namespace NadekoBot
var sw = Stopwatch.StartNew(); var sw = Stopwatch.StartNew();
//connect //connect
await Client.LoginAsync(TokenType.Bot, Credentials.Token).ConfigureAwait(false); await Client.LoginAsync(TokenType.Bot, Credentials.Token).ConfigureAwait(false);
await Client.ConnectAsync().ConfigureAwait(false); await Client.StartAsync().ConfigureAwait(false);
//await Client.DownloadAllUsersAsync().ConfigureAwait(false); //await Client.DownloadAllUsersAsync().ConfigureAwait(false);
// wait for all shards to be ready
int readyCount = 0;
foreach (var s in Client.Shards)
s.Ready += () => Task.FromResult(Interlocked.Increment(ref readyCount));
while (readyCount < Client.Shards.Count)
await Task.Delay(100).ConfigureAwait(false);
Stats.Initialize(); Stats.Initialize();
sw.Stop(); sw.Stop();
@ -162,10 +171,10 @@ namespace NadekoBot
try try
{ {
var logConfig = new LoggingConfiguration(); var logConfig = new LoggingConfiguration();
var consoleTarget = new ColoredConsoleTarget(); var consoleTarget = new ColoredConsoleTarget()
{
consoleTarget.Layout = @"${date:format=HH\:mm\:ss} ${logger} | ${message}"; Layout = @"${date:format=HH\:mm\:ss} ${logger} | ${message}"
};
logConfig.AddTarget("Console", consoleTarget); logConfig.AddTarget("Console", consoleTarget);
logConfig.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, consoleTarget)); logConfig.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, consoleTarget));

View File

@ -4,7 +4,7 @@
<Description>General purpose Discord bot written in C#.</Description> <Description>General purpose Discord bot written in C#.</Description>
<Copyright>Kwoth</Copyright> <Copyright>Kwoth</Copyright>
<Authors>Kwoth</Authors> <Authors>Kwoth</Authors>
<TargetFramework>netcoreapp1.0</TargetFramework> <TargetFramework>netcoreapp1.1</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AssemblyName>NadekoBot</AssemblyName> <AssemblyName>NadekoBot</AssemblyName>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
@ -30,13 +30,9 @@
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Discord.Net\src\Discord.Net.Commands\Discord.Net.Commands.csproj" />
<ProjectReference Include="..\..\Discord.Net\src\Discord.Net.WebSocket\Discord.Net.WebSocket.csproj" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AngleSharp" Version="0.9.9" /> <PackageReference Include="AngleSharp" Version="0.9.9" />
<PackageReference Include="Discord.Net" Version="1.0.0-rc2-00014" />
<PackageReference Include="libvideo" Version="1.0.1" /> <PackageReference Include="libvideo" Version="1.0.1" />
<PackageReference Include="CoreCLR-NCalc" Version="2.1.2" /> <PackageReference Include="CoreCLR-NCalc" Version="2.1.2" />
<PackageReference Include="Google.Apis.Urlshortener.v1" Version="1.19.0.138" /> <PackageReference Include="Google.Apis.Urlshortener.v1" Version="1.19.0.138" />
@ -67,5 +63,4 @@
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" /> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" /> <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -34,7 +34,7 @@ namespace NadekoBot.Services
private readonly CommandService _commandService; private readonly CommandService _commandService;
private readonly Logger _log; private readonly Logger _log;
private List<IDMChannel> ownerChannels { get; set; } = new List<IDMChannel>(); private List<IDMChannel> OwnerChannels { get; set; } = new List<IDMChannel>();
public event Func<IUserMessage, CommandInfo, Task> CommandExecuted = delegate { return Task.CompletedTask; }; public event Func<IUserMessage, CommandInfo, Task> CommandExecuted = delegate { return Task.CompletedTask; };
@ -60,7 +60,7 @@ namespace NadekoBot.Services
var _ = Task.Run(async () => var _ = Task.Run(async () =>
{ {
await Task.Delay(5000).ConfigureAwait(false); await Task.Delay(5000).ConfigureAwait(false);
ownerChannels = (await Task.WhenAll(_client.GetGuilds().SelectMany(g => g.Users) OwnerChannels = (await Task.WhenAll(_client.Guilds.SelectMany(g => g.Users)
.Where(u => NadekoBot.Credentials.OwnerIds.Contains(u.Id)) .Where(u => NadekoBot.Credentials.OwnerIds.Contains(u.Id))
.Distinct(new GuildUserComparer()) .Distinct(new GuildUserComparer())
.Select(async u => .Select(async u =>
@ -78,14 +78,14 @@ namespace NadekoBot.Services
.OrderBy(x => NadekoBot.Credentials.OwnerIds.IndexOf(x.Id)) .OrderBy(x => NadekoBot.Credentials.OwnerIds.IndexOf(x.Id))
.ToList(); .ToList();
if (!ownerChannels.Any()) if (!OwnerChannels.Any())
_log.Warn("No owner channels created! Make sure you've specified correct OwnerId in the credentials.json file."); _log.Warn("No owner channels created! Make sure you've specified correct OwnerId in the credentials.json file.");
else else
_log.Info($"Created {ownerChannels.Count} out of {NadekoBot.Credentials.OwnerIds.Length} owner message channels."); _log.Info($"Created {OwnerChannels.Count} out of {NadekoBot.Credentials.OwnerIds.Length} owner message channels.");
}); });
_client.MessageReceived += MessageReceivedHandler; _client.MessageReceived += MessageReceivedHandler;
_client.MessageUpdated += (oldmsg, newMsg) => _client.MessageUpdated += (oldmsg, newMsg, channel) =>
{ {
var ignore = Task.Run(async () => var ignore = Task.Run(async () =>
{ {
@ -115,18 +115,15 @@ namespace NadekoBot.Services
return false; return false;
try try
{ {
Games.ChatterBotSession cbs; var message = Games.CleverBotCommands.PrepareMessage(usrMsg, out Games.ChatterBotSession cbs);
var message = Games.CleverBotCommands.PrepareMessage(usrMsg, out cbs);
if (message == null || cbs == null) if (message == null || cbs == null)
return false; return false;
PermissionCache pc = Permissions.GetCache(guild.Id); PermissionCache pc = Permissions.GetCache(guild.Id);
int index; if (!pc.Permissions.CheckPermissions(usrMsg,
if (
!pc.Permissions.CheckPermissions(usrMsg,
NadekoBot.ModulePrefixes[typeof(Games).Name] + "cleverbot", NadekoBot.ModulePrefixes[typeof(Games).Name] + "cleverbot",
typeof(Games).Name, typeof(Games).Name,
out index)) out int index))
{ {
//todo print in guild actually //todo print in guild actually
var returnMsg = var returnMsg =
@ -317,10 +314,8 @@ namespace NadekoBot.Services
{ {
PermissionCache pc = Permissions.GetCache(guild.Id); PermissionCache pc = Permissions.GetCache(guild.Id);
int index; if (!pc.Permissions.CheckPermissions(usrMsg, cr.Trigger, "ActualCustomReactions",
if ( out int index))
!pc.Permissions.CheckPermissions(usrMsg, cr.Trigger, "ActualCustomReactions",
out index))
{ {
//todo print in guild actually //todo print in guild actually
var returnMsg = var returnMsg =
@ -349,8 +344,7 @@ namespace NadekoBot.Services
string messageContent = usrMsg.Content; string messageContent = usrMsg.Content;
if (guild != null) if (guild != null)
{ {
ConcurrentDictionary<string, string> maps; if (Modules.Utility.Utility.CommandMapCommands.AliasMaps.TryGetValue(guild.Id, out ConcurrentDictionary<string, string> maps))
if (Modules.Utility.Utility.CommandMapCommands.AliasMaps.TryGetValue(guild.Id, out maps))
{ {
var keys = maps.Keys var keys = maps.Keys
@ -405,12 +399,11 @@ namespace NadekoBot.Services
{ {
// rofl, gotta do this to prevent dm help message being sent to // rofl, gotta do this to prevent dm help message being sent to
// users who are voting on private polls (sending a number in a DM) // users who are voting on private polls (sending a number in a DM)
int vote; if (int.TryParse(usrMsg.Content, out int vote)) return;
if (int.TryParse(usrMsg.Content, out vote)) return;
await usrMsg.Channel.SendMessageAsync(Help.DMHelpString).ConfigureAwait(false); await usrMsg.Channel.SendMessageAsync(Help.DMHelpString).ConfigureAwait(false);
await SelfCommands.HandleDmForwarding(usrMsg, ownerChannels).ConfigureAwait(false); await SelfCommands.HandleDmForwarding(usrMsg, OwnerChannels).ConfigureAwait(false);
} }
} }
} }
@ -470,8 +463,7 @@ namespace NadekoBot.Services
if (context.Guild != null) if (context.Guild != null)
{ {
PermissionCache pc = Permissions.GetCache(context.Guild.Id); PermissionCache pc = Permissions.GetCache(context.Guild.Id);
int index; if (!resetCommand && !pc.Permissions.CheckPermissions(context.Message, cmd.Aliases.First(), module.Name, out int index))
if (!resetCommand && !pc.Permissions.CheckPermissions(context.Message, cmd.Aliases.First(), module.Name, out index))
{ {
var returnMsg = $"Permission number #{index + 1} **{pc.Permissions[index].GetCommand((SocketGuild)context.Guild)}** is preventing this action."; var returnMsg = $"Permission number #{index + 1} **{pc.Permissions[index].GetCommand((SocketGuild)context.Guild)}** is preventing this action.";
return new ExecuteCommandResult(cmd, pc, SearchResult.FromError(CommandError.Exception, returnMsg)); return new ExecuteCommandResult(cmd, pc, SearchResult.FromError(CommandError.Exception, returnMsg));

View File

@ -17,20 +17,18 @@ namespace NadekoBot.Services.Discord
public ReactionEventWrapper(IUserMessage msg) public ReactionEventWrapper(IUserMessage msg)
{ {
if (msg == null) Message = msg ?? throw new ArgumentNullException(nameof(msg));
throw new ArgumentNullException(nameof(msg));
Message = msg;
NadekoBot.Client.ReactionAdded += Discord_ReactionAdded; NadekoBot.Client.ReactionAdded += Discord_ReactionAdded;
NadekoBot.Client.ReactionRemoved += Discord_ReactionRemoved; NadekoBot.Client.ReactionRemoved += Discord_ReactionRemoved;
NadekoBot.Client.ReactionsCleared += Discord_ReactionsCleared; NadekoBot.Client.ReactionsCleared += Discord_ReactionsCleared;
} }
private Task Discord_ReactionsCleared(ulong messageId, Optional<SocketUserMessage> reaction) private Task Discord_ReactionsCleared(Cacheable<IUserMessage, ulong> msg, ISocketMessageChannel channel)
{ {
try try
{ {
if (messageId == Message.Id) if (msg.Id == Message.Id)
OnReactionsCleared?.Invoke(); OnReactionsCleared?.Invoke();
} }
catch { } catch { }
@ -38,11 +36,11 @@ namespace NadekoBot.Services.Discord
return Task.CompletedTask; return Task.CompletedTask;
} }
private Task Discord_ReactionRemoved(ulong messageId, Optional<SocketUserMessage> arg2, SocketReaction reaction) private Task Discord_ReactionRemoved(Cacheable<IUserMessage, ulong> msg, ISocketMessageChannel channel, SocketReaction reaction)
{ {
try try
{ {
if (messageId == Message.Id) if (msg.Id == Message.Id)
OnReactionRemoved?.Invoke(reaction); OnReactionRemoved?.Invoke(reaction);
} }
catch { } catch { }
@ -50,11 +48,11 @@ namespace NadekoBot.Services.Discord
return Task.CompletedTask; return Task.CompletedTask;
} }
private Task Discord_ReactionAdded(ulong messageId, Optional<SocketUserMessage> message, SocketReaction reaction) private Task Discord_ReactionAdded(Cacheable<IUserMessage, ulong> msg, ISocketMessageChannel channel, SocketReaction reaction)
{ {
try try
{ {
if (messageId == Message.Id) if (msg.Id == Message.Id)
OnReactionAdded?.Invoke(reaction); OnReactionAdded?.Invoke(reaction);
} }
catch { } catch { }

View File

@ -16,7 +16,7 @@ namespace NadekoBot.Services.Impl
private readonly DiscordShardedClient _client; private readonly DiscordShardedClient _client;
private readonly DateTime _started; private readonly DateTime _started;
public const string BotVersion = "1.3a"; public const string BotVersion = "1.4-alpha";
public string Author => "Kwoth#2560"; public string Author => "Kwoth#2560";
public string Library => "Discord.Net"; public string Library => "Discord.Net";
@ -124,7 +124,7 @@ namespace NadekoBot.Services.Impl
{ {
using (var content = new FormUrlEncodedContent( using (var content = new FormUrlEncodedContent(
new Dictionary<string, string> { new Dictionary<string, string> {
{ "servercount", _client.GetGuildCount().ToString() }, { "servercount", _client.Guilds.Count.ToString() },
{ "key", NadekoBot.Credentials.CarbonKey }})) { "key", NadekoBot.Credentials.CarbonKey }}))
{ {
content.Headers.Clear(); content.Headers.Clear();
@ -143,21 +143,23 @@ namespace NadekoBot.Services.Impl
public void Initialize() public void Initialize()
{ {
var guilds = _client.GetGuilds().ToArray(); var guilds = _client.Guilds.ToArray();
_textChannels = guilds.Sum(g => g.Channels.Count(cx => cx is ITextChannel)); _textChannels = guilds.Sum(g => g.Channels.Count(cx => cx is ITextChannel));
_voiceChannels = guilds.Sum(g => g.Channels.Count) - _textChannels; _voiceChannels = guilds.Sum(g => g.Channels.Count) - _textChannels;
} }
public Task<string> Print() public Task<string> Print()
{ {
var curUser = _client.CurrentUser; SocketSelfUser curUser;
while ((curUser = _client.CurrentUser) == null) Task.Delay(1000).ConfigureAwait(false);
return Task.FromResult($@" return Task.FromResult($@"
Author: [{Author}] | Library: [{Library}] Author: [{Author}] | Library: [{Library}]
Bot Version: [{BotVersion}] Bot Version: [{BotVersion}]
Bot ID: {curUser.Id} Bot ID: {curUser.Id}
Owner ID(s): {string.Join(", ", NadekoBot.Credentials.OwnerIds)} Owner ID(s): {string.Join(", ", NadekoBot.Credentials.OwnerIds)}
Uptime: {GetUptimeString()} Uptime: {GetUptimeString()}
Servers: {_client.GetGuildCount()} | TextChannels: {TextChannels} | VoiceChannels: {VoiceChannels} Servers: {_client.Guilds.Count} | TextChannels: {TextChannels} | VoiceChannels: {VoiceChannels}
Commands Ran this session: {CommandsRan} Commands Ran this session: {CommandsRan}
Messages: {MessageCounter} [{MessagesPerSecond:F2}/sec] Heap: [{Heap} MB]"); Messages: {MessageCounter} [{MessagesPerSecond:F2}/sec] Heap: [{Heap} MB]");
} }

View File

@ -9,7 +9,7 @@ namespace NadekoBot.TypeReaders
public override Task<TypeReaderResult> Read(ICommandContext context, string input) public override Task<TypeReaderResult> Read(ICommandContext context, string input)
{ {
input = input.Trim().ToLowerInvariant(); input = input.Trim().ToLowerInvariant();
var guilds = NadekoBot.Client.GetGuilds(); var guilds = NadekoBot.Client.Guilds;
var guild = guilds.FirstOrDefault(g => g.Id.ToString().Trim().ToLowerInvariant() == input) ?? //by id var guild = guilds.FirstOrDefault(g => g.Id.ToString().Trim().ToLowerInvariant() == input) ?? //by id
guilds.FirstOrDefault(g => g.Name.Trim().ToLowerInvariant() == input); //by name guilds.FirstOrDefault(g => g.Name.Trim().ToLowerInvariant() == input); //by name

View File

@ -448,7 +448,7 @@ namespace NadekoBot.Extensions
{ {
return usr.AvatarId.StartsWith("a_") return usr.AvatarId.StartsWith("a_")
? $"{DiscordConfig.CDNUrl}avatars/{usr.Id}/{usr.AvatarId}.gif" ? $"{DiscordConfig.CDNUrl}avatars/{usr.Id}/{usr.AvatarId}.gif"
: usr.AvatarUrl; : usr.GetAvatarUrl(ImageFormat.Auto);
} }
} }
} }