From e5682ce2cd4b4ddee2fbb30ea78c96dfbd94e9d8 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 4 Aug 2016 04:30:54 +0200 Subject: [PATCH] Just making sure nothing is running before bot is ready. This is hopefuly addressing shared nadeko issues --- .../Administration/Commands/AutoAssignRole.cs | 2 +- .../Commands/CrossServerTextChannel.cs | 61 ++++++++++--------- .../Administration/Commands/LogCommand.cs | 57 +++++++++-------- .../Administration/Commands/PlayingRotate.cs | 14 ++--- .../Commands/RatelimitCommand.cs | 2 +- .../Commands/ServerGreetCommand.cs | 12 ++-- .../Commands/VoicePlusTextCommand.cs | 2 +- NadekoBot/Modules/Music/MusicModule.cs | 2 +- .../Commands/FilterInvitesCommand.cs | 2 +- .../Commands/FilterWordsCommand.cs | 2 +- .../Searches/Commands/StreamNotifications.cs | 3 +- NadekoBot/Modules/Utility/Commands/Remind.cs | 2 +- 12 files changed, 86 insertions(+), 75 deletions(-) diff --git a/NadekoBot/Modules/Administration/Commands/AutoAssignRole.cs b/NadekoBot/Modules/Administration/Commands/AutoAssignRole.cs index 38084dfa..373840cb 100644 --- a/NadekoBot/Modules/Administration/Commands/AutoAssignRole.cs +++ b/NadekoBot/Modules/Administration/Commands/AutoAssignRole.cs @@ -10,7 +10,7 @@ namespace NadekoBot.Modules.Administration.Commands { public AutoAssignRole(DiscordModule module) : base(module) { - NadekoBot.Client.UserJoined += (s, e) => + NadekoBot.OnReady += () => NadekoBot.Client.UserJoined += (s, e) => { try { diff --git a/NadekoBot/Modules/Administration/Commands/CrossServerTextChannel.cs b/NadekoBot/Modules/Administration/Commands/CrossServerTextChannel.cs index c08c34ef..88f6cd40 100644 --- a/NadekoBot/Modules/Administration/Commands/CrossServerTextChannel.cs +++ b/NadekoBot/Modules/Administration/Commands/CrossServerTextChannel.cs @@ -13,46 +13,49 @@ namespace NadekoBot.Modules.Administration.Commands { public CrossServerTextChannel(DiscordModule module) : base(module) { - NadekoBot.Client.MessageReceived += async (s, e) => + NadekoBot.OnReady += () => { - try + NadekoBot.Client.MessageReceived += async (s, e) => { - if (e.User.Id == NadekoBot.Client.CurrentUser.Id) return; - foreach (var subscriber in Subscribers) + try { - var set = subscriber.Value; - if (!set.Contains(e.Channel)) - continue; - foreach (var chan in set.Except(new[] { e.Channel })) + if (e.User.Id == NadekoBot.Client.CurrentUser.Id) return; + foreach (var subscriber in Subscribers) { - await chan.SendMessage(GetText(e.Server, e.Channel, e.User, e.Message)).ConfigureAwait(false); + var set = subscriber.Value; + if (!set.Contains(e.Channel)) + continue; + foreach (var chan in set.Except(new[] { e.Channel })) + { + await chan.SendMessage(GetText(e.Server, e.Channel, e.User, e.Message)).ConfigureAwait(false); + } } } - } - catch { } - }; - NadekoBot.Client.MessageUpdated += async (s, e) => - { - try + catch { } + }; + NadekoBot.Client.MessageUpdated += async (s, e) => { - if (e.After?.User?.Id == null || e.After.User.Id == NadekoBot.Client.CurrentUser.Id) return; - foreach (var subscriber in Subscribers) + try { - var set = subscriber.Value; - if (!set.Contains(e.Channel)) - continue; - foreach (var chan in set.Except(new[] { e.Channel })) + if (e.After?.User?.Id == null || e.After.User.Id == NadekoBot.Client.CurrentUser.Id) return; + foreach (var subscriber in Subscribers) { - var msg = chan.Messages - .FirstOrDefault(m => - m.RawText == GetText(e.Server, e.Channel, e.User, e.Before)); - if (msg != default(Message)) - await msg.Edit(GetText(e.Server, e.Channel, e.User, e.After)).ConfigureAwait(false); + var set = subscriber.Value; + if (!set.Contains(e.Channel)) + continue; + foreach (var chan in set.Except(new[] { e.Channel })) + { + var msg = chan.Messages + .FirstOrDefault(m => + m.RawText == GetText(e.Server, e.Channel, e.User, e.Before)); + if (msg != default(Message)) + await msg.Edit(GetText(e.Server, e.Channel, e.User, e.After)).ConfigureAwait(false); + } } - } - } - catch { } + } + catch { } + }; }; } diff --git a/NadekoBot/Modules/Administration/Commands/LogCommand.cs b/NadekoBot/Modules/Administration/Commands/LogCommand.cs index b5f19eb1..5956840c 100644 --- a/NadekoBot/Modules/Administration/Commands/LogCommand.cs +++ b/NadekoBot/Modules/Administration/Commands/LogCommand.cs @@ -19,36 +19,39 @@ namespace NadekoBot.Modules.Administration.Commands public LogCommand(DiscordModule module) : base(module) { - NadekoBot.Client.MessageReceived += MsgRecivd; - NadekoBot.Client.MessageDeleted += MsgDltd; - NadekoBot.Client.MessageUpdated += MsgUpdtd; - NadekoBot.Client.UserUpdated += UsrUpdtd; - NadekoBot.Client.UserBanned += UsrBanned; - NadekoBot.Client.UserLeft += UsrLeft; - NadekoBot.Client.UserJoined += UsrJoined; - NadekoBot.Client.UserUnbanned += UsrUnbanned; - NadekoBot.Client.ChannelCreated += ChannelCreated; - NadekoBot.Client.ChannelDestroyed += ChannelDestroyed; - NadekoBot.Client.ChannelUpdated += ChannelUpdated; - - - NadekoBot.Client.MessageReceived += async (s, e) => + NadekoBot.OnReady += () => { - if (e.Channel.IsPrivate || e.User.Id == NadekoBot.Client.CurrentUser.Id) - return; - if (!SpecificConfigurations.Default.Of(e.Server.Id).SendPrivateMessageOnMention) return; - try + NadekoBot.Client.MessageReceived += MsgRecivd; + NadekoBot.Client.MessageDeleted += MsgDltd; + NadekoBot.Client.MessageUpdated += MsgUpdtd; + NadekoBot.Client.UserUpdated += UsrUpdtd; + NadekoBot.Client.UserBanned += UsrBanned; + NadekoBot.Client.UserLeft += UsrLeft; + NadekoBot.Client.UserJoined += UsrJoined; + NadekoBot.Client.UserUnbanned += UsrUnbanned; + NadekoBot.Client.ChannelCreated += ChannelCreated; + NadekoBot.Client.ChannelDestroyed += ChannelDestroyed; + NadekoBot.Client.ChannelUpdated += ChannelUpdated; + + + NadekoBot.Client.MessageReceived += async (s, e) => { - var usr = e.Message.MentionedUsers.FirstOrDefault(u => u != e.User); - if (usr?.Status != UserStatus.Offline) + if (e.Channel.IsPrivate || e.User.Id == NadekoBot.Client.CurrentUser.Id) return; - await e.Channel.SendMessage($"User `{usr.Name}` is offline. PM sent.").ConfigureAwait(false); - await usr.SendMessage( - $"User `{e.User.Name}` mentioned you on " + - $"`{e.Server.Name}` server while you were offline.\n" + - $"`Message:` {e.Message.Text}").ConfigureAwait(false); - } - catch { } + if (!SpecificConfigurations.Default.Of(e.Server.Id).SendPrivateMessageOnMention) return; + try + { + var usr = e.Message.MentionedUsers.FirstOrDefault(u => u != e.User); + if (usr?.Status != UserStatus.Offline) + return; + await e.Channel.SendMessage($"User `{usr.Name}` is offline. PM sent.").ConfigureAwait(false); + await usr.SendMessage( + $"User `{e.User.Name}` mentioned you on " + + $"`{e.Server.Name}` server while you were offline.\n" + + $"`Message:` {e.Message.Text}").ConfigureAwait(false); + } + catch { } + }; }; // start the userpresence queue diff --git a/NadekoBot/Modules/Administration/Commands/PlayingRotate.cs b/NadekoBot/Modules/Administration/Commands/PlayingRotate.cs index c1caa0bc..3eff2ff2 100644 --- a/NadekoBot/Modules/Administration/Commands/PlayingRotate.cs +++ b/NadekoBot/Modules/Administration/Commands/PlayingRotate.cs @@ -9,7 +9,6 @@ using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; -using System.Timers; using Timer = System.Timers.Timer; namespace NadekoBot.Modules.Administration.Commands @@ -38,7 +37,7 @@ namespace NadekoBot.Modules.Administration.Commands {"%trivia%", () => Games.Commands.TriviaCommands.RunningTrivias.Count.ToString()} }; - private readonly SemaphoreSlim playingPlaceholderLock = new SemaphoreSlim(1,1); + private readonly SemaphoreSlim playingPlaceholderLock = new SemaphoreSlim(1, 1); public PlayingRotate(DiscordModule module) : base(module) { @@ -70,8 +69,7 @@ namespace NadekoBot.Modules.Administration.Commands } catch { } }; - - timer.Enabled = NadekoBot.Config.IsRotatingStatus; + NadekoBot.OnReady += () => timer.Enabled = NadekoBot.Config.IsRotatingStatus; } public Func DoFunc() => async e => @@ -86,7 +84,8 @@ namespace NadekoBot.Modules.Administration.Commands NadekoBot.Config.IsRotatingStatus = timer.Enabled; await ConfigHandler.SaveConfig().ConfigureAwait(false); } - finally { + finally + { playingPlaceholderLock.Release(); } await e.Channel.SendMessage($"❗`Rotating playing status has been {(timer.Enabled ? "enabled" : "disabled")}.`").ConfigureAwait(false); @@ -103,7 +102,7 @@ namespace NadekoBot.Modules.Administration.Commands cgb.CreateCommand(Module.Prefix + "addplaying") .Alias(Module.Prefix + "adpl") .Description("Adds a specified string to the list of playing strings to rotate. " + - "Supported placeholders: " + string.Join(", ", PlayingPlaceholders.Keys)+ $" **Bot Owner Only!**| `{Prefix}adpl`") + "Supported placeholders: " + string.Join(", ", PlayingPlaceholders.Keys) + $" **Bot Owner Only!**| `{Prefix}adpl`") .Parameter("text", ParameterType.Unparsed) .AddCheck(SimpleCheckers.OwnerOnly()) .Do(async e => @@ -152,7 +151,8 @@ namespace NadekoBot.Modules.Administration.Commands int num; string str; await playingPlaceholderLock.WaitAsync().ConfigureAwait(false); - try { + try + { if (!int.TryParse(arg.Trim(), out num) || num <= 0 || num > NadekoBot.Config.RotatingStatuses.Count) return; str = NadekoBot.Config.RotatingStatuses[num - 1]; diff --git a/NadekoBot/Modules/Administration/Commands/RatelimitCommand.cs b/NadekoBot/Modules/Administration/Commands/RatelimitCommand.cs index d1f62bd9..38c30d07 100644 --- a/NadekoBot/Modules/Administration/Commands/RatelimitCommand.cs +++ b/NadekoBot/Modules/Administration/Commands/RatelimitCommand.cs @@ -15,7 +15,7 @@ namespace NadekoBot.Modules.Administration.Commands public RatelimitCommand(DiscordModule module) : base(module) { - NadekoBot.Client.MessageReceived += async (s, e) => + NadekoBot.OnReady += () => NadekoBot.Client.MessageReceived += async (s, e) => { if (e.Channel.IsPrivate || e.User.Id == NadekoBot.Client.CurrentUser.Id) return; diff --git a/NadekoBot/Modules/Administration/Commands/ServerGreetCommand.cs b/NadekoBot/Modules/Administration/Commands/ServerGreetCommand.cs index b2cdf3b4..c4ebf22c 100644 --- a/NadekoBot/Modules/Administration/Commands/ServerGreetCommand.cs +++ b/NadekoBot/Modules/Administration/Commands/ServerGreetCommand.cs @@ -31,8 +31,12 @@ namespace NadekoBot.Modules.Administration.Commands { AnnouncementsDictionary = new ConcurrentDictionary(); - NadekoBot.Client.UserJoined += UserJoined; - NadekoBot.Client.UserLeft += UserLeft; + //gotta subscribe after ready, to prevent trying to send these before all guilds are initialized + NadekoBot.OnReady += () => + { + NadekoBot.Client.UserJoined += UserJoined; + NadekoBot.Client.UserLeft += UserLeft; + }; var data = Classes.DbHandler.Instance.GetAllRows(); @@ -245,7 +249,7 @@ namespace NadekoBot.Modules.Administration.Commands }); cgb.CreateCommand(Module.Prefix + "greetmsg") - .Description($"Sets a new join announcement message. Type %user% if you want to mention the new member. Using it with no message will show the current greet message. **Needs Manage Server Permissions.**| `{Prefix}greetmsg Welcome to the server, %user%.`") + .Description($"Sets a new join announcement message. Type %user% if you want to mention the new member. Using it with no message will show the current greet message. **Needs Manage Server Permissions.**| `{Prefix}greetmsg Welcome, %user%.`") .Parameter("msg", ParameterType.Unparsed) .Do(async e => { @@ -278,7 +282,7 @@ namespace NadekoBot.Modules.Administration.Commands }); cgb.CreateCommand(Module.Prefix + "byemsg") - .Description($"Sets a new leave announcement message. Type %user% if you want to mention the new member. Using it with no message will show the current bye message. **Needs Manage Server Permissions.**| `{Prefix}byemsg %user% has left the server.`") + .Description($"Sets a new leave announcement message. Type %user% if you want to mention the new member. Using it with no message will show the current bye message. **Needs Manage Server Permissions.**| `{Prefix}byemsg %user% has left.`") .Parameter("msg", ParameterType.Unparsed) .Do(async e => { diff --git a/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommand.cs b/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommand.cs index fb9a64b3..af220059 100644 --- a/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommand.cs +++ b/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommand.cs @@ -17,7 +17,7 @@ namespace NadekoBot.Modules.Administration.Commands public VoicePlusTextCommand(DiscordModule module) : base(module) { // changing servers may cause bugs - NadekoBot.Client.UserUpdated += async (sender, e) => + NadekoBot.OnReady += () => NadekoBot.Client.UserUpdated += async (sender, e) => { try { diff --git a/NadekoBot/Modules/Music/MusicModule.cs b/NadekoBot/Modules/Music/MusicModule.cs index 3c4aa351..9bf173b7 100644 --- a/NadekoBot/Modules/Music/MusicModule.cs +++ b/NadekoBot/Modules/Music/MusicModule.cs @@ -324,7 +324,7 @@ namespace NadekoBot.Modules.Music cgb.CreateCommand(Prefix + "soundcloudpl") .Alias(Prefix + "scpl") - .Description($"Queue a soundcloud playlist using a link. | `{Prefix}scpl https://soundcloud.com/saratology/sets/symphony`") + .Description($"Queue a soundcloud playlist using a link. | `{Prefix}scpl soundcloudseturl`") .Parameter("pl", ParameterType.Unparsed) .Do(async e => { diff --git a/NadekoBot/Modules/Permissions/Commands/FilterInvitesCommand.cs b/NadekoBot/Modules/Permissions/Commands/FilterInvitesCommand.cs index 445558c3..29458c7a 100644 --- a/NadekoBot/Modules/Permissions/Commands/FilterInvitesCommand.cs +++ b/NadekoBot/Modules/Permissions/Commands/FilterInvitesCommand.cs @@ -14,7 +14,7 @@ namespace NadekoBot.Modules.Permissions.Commands public FilterInvitesCommand(DiscordModule module) : base(module) { - NadekoBot.Client.MessageReceived += async (sender, args) => + NadekoBot.OnReady += () => NadekoBot.Client.MessageReceived += async (sender, args) => { if (args.Channel.IsPrivate || args.User.Id == NadekoBot.Client.CurrentUser.Id) return; try diff --git a/NadekoBot/Modules/Permissions/Commands/FilterWordsCommand.cs b/NadekoBot/Modules/Permissions/Commands/FilterWordsCommand.cs index b574ef23..5b3c77e3 100644 --- a/NadekoBot/Modules/Permissions/Commands/FilterWordsCommand.cs +++ b/NadekoBot/Modules/Permissions/Commands/FilterWordsCommand.cs @@ -11,7 +11,7 @@ namespace NadekoBot.Modules.Permissions.Commands { public FilterWords(DiscordModule module) : base(module) { - NadekoBot.Client.MessageReceived += async (sender, args) => + NadekoBot.OnReady += () => NadekoBot.Client.MessageReceived += async (sender, args) => { if (args.Channel.IsPrivate || args.User.Id == NadekoBot.Client.CurrentUser.Id) return; try diff --git a/NadekoBot/Modules/Searches/Commands/StreamNotifications.cs b/NadekoBot/Modules/Searches/Commands/StreamNotifications.cs index f8720324..11609db2 100644 --- a/NadekoBot/Modules/Searches/Commands/StreamNotifications.cs +++ b/NadekoBot/Modules/Searches/Commands/StreamNotifications.cs @@ -70,7 +70,8 @@ namespace NadekoBot.Modules.Searches.Commands catch { } await ConfigHandler.SaveConfig().ConfigureAwait(false); }; - checkTimer.Start(); + //start checking only after ready, because we need all servers to be initialized + NadekoBot.OnReady += checkTimer.Start; } private async Task> GetStreamStatus(StreamNotificationConfig stream, bool checkCache = true) diff --git a/NadekoBot/Modules/Utility/Commands/Remind.cs b/NadekoBot/Modules/Utility/Commands/Remind.cs index 808f4b11..07054394 100644 --- a/NadekoBot/Modules/Utility/Commands/Remind.cs +++ b/NadekoBot/Modules/Utility/Commands/Remind.cs @@ -30,7 +30,7 @@ namespace NadekoBot.Modules.Utility.Commands { var remList = DbHandler.Instance.GetAllRows(); - reminders = remList.Select(StartNewReminder).ToList(); + NadekoBot.OnReady += () => reminders = remList.Select(StartNewReminder).ToList(); } private Timer StartNewReminder(Reminder r)