diff --git a/PickupBot.Commands/Constants/CategoryNames.cs b/PickupBot.Commands/Constants/CategoryNames.cs new file mode 100644 index 0000000..1b5f087 --- /dev/null +++ b/PickupBot.Commands/Constants/CategoryNames.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PickupBot.Commands.Constants +{ + public class CategoryNames + { + public const string Pickups = "Pickups"; + public const string PickupVoiceChannels = "Pickup voice channels"; + } +} diff --git a/PickupBot.Commands/Constants/ChannelNames.cs b/PickupBot.Commands/Constants/ChannelNames.cs new file mode 100644 index 0000000..73ad879 --- /dev/null +++ b/PickupBot.Commands/Constants/ChannelNames.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PickupBot.Commands.Constants +{ + public class ChannelNames + { + public const string ActivePickups = "active-pickups"; + public const string Pickup = "pickup"; + + public const string Duel = "duel"; + } +} diff --git a/PickupBot.Commands/Constants/RoleNames.cs b/PickupBot.Commands/Constants/RoleNames.cs new file mode 100644 index 0000000..416e8e3 --- /dev/null +++ b/PickupBot.Commands/Constants/RoleNames.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PickupBot.Commands.Constants +{ + public class RoleNames + { + public const string PickupPromote = "pickup-promote"; + public const string Duellist = "duellist"; + } +} diff --git a/PickupBot.Commands/Infrastructure/Helpers/PickupHelpers.cs b/PickupBot.Commands/Infrastructure/Helpers/PickupHelpers.cs index 9f1fe67..c288cd2 100644 --- a/PickupBot.Commands/Infrastructure/Helpers/PickupHelpers.cs +++ b/PickupBot.Commands/Infrastructure/Helpers/PickupHelpers.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Discord; using Discord.WebSocket; +using PickupBot.Commands.Constants; using PickupBot.Data.Models; namespace PickupBot.Commands.Infrastructure.Helpers @@ -15,14 +16,14 @@ public static async Task GetOrCreateVoiceChannel(string name, ulo ?? await guild.CreateVoiceChannelAsync(name, properties => properties.CategoryId = categoryId); } - public static bool IsInPickupChannel(IChannel channel) => channel.Name.StartsWith("pickup", StringComparison.OrdinalIgnoreCase); - public static bool IsInDuelChannel(IChannel channel) => channel.Name.Equals("duel", StringComparison.OrdinalIgnoreCase); + public static bool IsInPickupChannel(IChannel channel) => channel.Name.StartsWith(ChannelNames.Pickup, StringComparison.OrdinalIgnoreCase); + public static bool IsInDuelChannel(IChannel channel) => channel.Name.Equals(ChannelNames.Duel, StringComparison.OrdinalIgnoreCase); public static async Task GetPickupQueuesChannel(SocketGuild guild) { var queuesChannel = (ITextChannel)guild.TextChannels.FirstOrDefault(c => - c.Name.Equals("active-pickups", StringComparison.OrdinalIgnoreCase)) ?? - await guild.CreateTextChannelAsync("active-pickups", + c.Name.Equals(ChannelNames.ActivePickups, StringComparison.OrdinalIgnoreCase)) ?? + await guild.CreateTextChannelAsync(ChannelNames.ActivePickups, properties => { properties.Topic = "Active pickups, use reactions to signup"; }); return queuesChannel; } diff --git a/PickupBot.Commands/Infrastructure/Services/ListCommandService.cs b/PickupBot.Commands/Infrastructure/Services/ListCommandService.cs index 7b45221..89d3c3d 100644 --- a/PickupBot.Commands/Infrastructure/Services/ListCommandService.cs +++ b/PickupBot.Commands/Infrastructure/Services/ListCommandService.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Discord; using Discord.WebSocket; +using PickupBot.Commands.Constants; using PickupBot.Commands.Extensions; using PickupBot.Commands.Infrastructure.Helpers; using PickupBot.Commands.Infrastructure.Utilities; @@ -147,7 +148,7 @@ public async Task Promote(PickupQueue queue, ITextChannel pickupChannel, IGuildU return; } - var role = guild.Roles.FirstOrDefault(w => w.Name == "pickup-promote"); + var role = guild.Roles.FirstOrDefault(w => w.Name == RoleNames.PickupPromote); if (role == null) return; //Failed to get role; var users = guild.Users.Where(w => w.Roles.Any(r => r.Id == role.Id)).ToList(); diff --git a/PickupBot.Commands/Infrastructure/Services/MiscCommandService.cs b/PickupBot.Commands/Infrastructure/Services/MiscCommandService.cs index e396d37..634d723 100644 --- a/PickupBot.Commands/Infrastructure/Services/MiscCommandService.cs +++ b/PickupBot.Commands/Infrastructure/Services/MiscCommandService.cs @@ -47,17 +47,17 @@ public MiscCommandService( public async Task VerifyQueueByName(string queueName, IGuildChannel channel) { - var queue = await _queueRepository.FindQueue(queueName, channel.Guild.Id.ToString()).ConfigureAwait(false); + var queue = await _queueRepository.FindQueue(queueName, channel.Guild.Id.ToString()); if (queue != null) return queue; - await ((ITextChannel)channel).SendMessageAsync($"`Queue with the name '{queueName}' doesn't exists!`").AutoRemoveMessage(10).ConfigureAwait(false); + await ((ITextChannel)channel).SendMessageAsync($"`Queue with the name '{queueName}' doesn't exists!`").AutoRemoveMessage(10); return null; } public async Task VerifyUserFlaggedStatus(IGuildUser user, ISocketMessageChannel channel) { - var flagged = await _flagRepository.IsFlagged(user).ConfigureAwait(false); + var flagged = await _flagRepository.IsFlagged(user); if (flagged == null) return true; var sb = new StringBuilder() @@ -73,8 +73,7 @@ public async Task VerifyUserFlaggedStatus(IGuildUser user, ISocketMessageC }.Build(); await channel.SendMessageAsync(embed: embed) - .AutoRemoveMessage(10) - .ConfigureAwait(false); + .AutoRemoveMessage(10); return false; } @@ -82,10 +81,10 @@ await channel.SendMessageAsync(embed: embed) public void TriggerDelayedRconNotification(PickupQueue queue) { // 2 minute delay message - AsyncUtilities.DelayAction(TimeSpan.FromMinutes(2), async t => { await TriggerRconNotification(queue).ConfigureAwait(false); }); + AsyncUtilities.DelayAction(TimeSpan.FromMinutes(2), async t => { await TriggerRconNotification(queue); }); // 4 minute delay message - AsyncUtilities.DelayAction(TimeSpan.FromMinutes(4), async t => { await TriggerRconNotification(queue).ConfigureAwait(false); }); + AsyncUtilities.DelayAction(TimeSpan.FromMinutes(4), async t => { await TriggerRconNotification(queue); }); } [SuppressMessage("ReSharper", "PossibleNullReferenceException")] @@ -121,7 +120,7 @@ public async Task TriggerRconNotification(PickupQueue queue) $"^1RED TEAM: ^5{string.Join(", ", redTeam.Subscribers.Select(w => w.Name))} ^7- " + $"^4BLUE TEAM: ^5{string.Join(", ", blueTeam.Subscribers.Select(w => w.Name))}\""; - await RCON.UDPSendCommand(command, rconHost, rconPassword, rconPort, true).ConfigureAwait(false); + await RCON.UDPSendCommand(command, rconHost, rconPassword, rconPort, true); } catch (Exception e) diff --git a/PickupBot.Commands/Infrastructure/Services/SubscriberCommandService.cs b/PickupBot.Commands/Infrastructure/Services/SubscriberCommandService.cs index 91f306a..2e15d54 100644 --- a/PickupBot.Commands/Infrastructure/Services/SubscriberCommandService.cs +++ b/PickupBot.Commands/Infrastructure/Services/SubscriberCommandService.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Discord; using Discord.WebSocket; +using PickupBot.Commands.Constants; using PickupBot.Commands.Extensions; using PickupBot.Commands.Infrastructure.Helpers; using PickupBot.Data.Models; @@ -65,24 +66,28 @@ public async Task Add(string queueName, ISocketMessageChannel channel, IGuildUse queue.Updated = DateTime.UtcNow; + bool updateStaticMessage = false; + if (queue.Subscribers.Count >= queue.MaxInQueue) { if (queue.WaitingList.All(w => w.Id != user.Id)) { queue.WaitingList.Add(new Subscriber { Id = user.Id, Name = PickupHelpers.GetNickname(user) }); - queue = await _listCommandService.SaveStaticQueueMessage(queue, guild); + updateStaticMessage = true; - await channel.SendMessageAsync($"`You have been added to the '{queue.Name}' waiting list`").AutoRemoveMessage(10); + if(channel.Name != ChannelNames.ActivePickups) + await channel.SendMessageAsync($"`You have been added to the '{queue.Name}' waiting list`").AutoRemoveMessage(10); } else { - await channel.SendMessageAsync($"`You are already on the '{queue.Name}' waiting list`").AutoRemoveMessage(10); + if(channel.Name != ChannelNames.ActivePickups) + await channel.SendMessageAsync($"`You are already on the '{queue.Name}' waiting list`").AutoRemoveMessage(10); } } else { + updateStaticMessage = true; queue.Subscribers.Add(new Subscriber { Id = user.Id, Name = PickupHelpers.GetNickname(user) }); - queue = await _listCommandService.SaveStaticQueueMessage(queue, guild); if (queue.Subscribers.Count == queue.MaxInQueue) { @@ -92,11 +97,18 @@ await PickupHelpers.NotifyUsers( user, queue.Subscribers.Select(s => guild.GetUser(s.Id)).Where(u => u != null).ToArray()); } + + if(channel.Name != ChannelNames.ActivePickups) + await channel.SendMessageAsync($"`{queue.Name} - {PickupHelpers.ParseSubscribers(queue)}`"); + } - await channel.SendMessageAsync($"`{queue.Name} - {PickupHelpers.ParseSubscribers(queue)}`"); + if (updateStaticMessage) + { + queue = await _listCommandService.SaveStaticQueueMessage(queue, guild); } - + await _queueRepository.UpdateQueue(queue); + await _activitiesRepository.Update(activity); } public async Task Leave(PickupQueue queue, ISocketMessageChannel channel, IGuildUser user, bool notify = true) diff --git a/PickupBot.Commands/Modules/DuelModule.cs b/PickupBot.Commands/Modules/DuelModule.cs index 6cb320e..d7394a4 100644 --- a/PickupBot.Commands/Modules/DuelModule.cs +++ b/PickupBot.Commands/Modules/DuelModule.cs @@ -6,6 +6,7 @@ using Discord; using Discord.Commands; using Microsoft.Extensions.Logging; +using PickupBot.Commands.Constants; using PickupBot.Commands.Extensions; using PickupBot.Commands.Infrastructure.Helpers; using PickupBot.Data.Models; @@ -444,8 +445,8 @@ private static SkillLevel Parse(string level) } private async Task GetDuellistRole() => - Context.Guild.Roles.FirstOrDefault(r => r.Name.Equals("duellist", StringComparison.OrdinalIgnoreCase)) ?? - (IRole)await Context.Guild.CreateRoleAsync("duellist", GuildPermissions.None, isHoisted: false, isMentionable: false); + Context.Guild.Roles.FirstOrDefault(r => r.Name.Equals(RoleNames.Duellist, StringComparison.OrdinalIgnoreCase)) ?? + (IRole)await Context.Guild.CreateRoleAsync(RoleNames.Duellist, GuildPermissions.None, isHoisted: false, isMentionable: false); } } diff --git a/PickupBot.Commands/Modules/Pickup/PickupListModule.cs b/PickupBot.Commands/Modules/Pickup/PickupListModule.cs index 9899dcf..7bc8576 100644 --- a/PickupBot.Commands/Modules/Pickup/PickupListModule.cs +++ b/PickupBot.Commands/Modules/Pickup/PickupListModule.cs @@ -7,6 +7,7 @@ using Discord; using Discord.Commands; using Discord.WebSocket; +using PickupBot.Commands.Constants; using PickupBot.Commands.Extensions; using PickupBot.Commands.Infrastructure.Helpers; using PickupBot.Commands.Infrastructure.Services; @@ -48,43 +49,45 @@ public PickupListModule( private async Task ReactionAdded(Cacheable message, ISocketMessageChannel channel, SocketReaction reaction) { - if (!(channel is IGuildChannel guildChannel) || guildChannel.Name != "active-pickups") return; + if (!(channel is IGuildChannel guildChannel) || guildChannel.Name != ChannelNames.ActivePickups) return; if (reaction.User.Value.IsBot) return; - var queue = await _queueRepository.FindQueueByMessageId(reaction.MessageId, guildChannel.GuildId.ToString()).ConfigureAwait(false); + var queue = await _queueRepository.FindQueueByMessageId(reaction.MessageId, guildChannel.GuildId.ToString()); if (queue == null) return; - var pickupChannel = ((SocketGuild)guildChannel.Guild).Channels.FirstOrDefault(c => c.Name.Equals("pickup")) as SocketTextChannel; + var pickupChannel = ((SocketGuild)guildChannel.Guild).Channels + .FirstOrDefault(c => c.Name.Equals(ChannelNames.Pickup)) as SocketTextChannel; + switch (reaction.Emote.Name) { case "\u2705": await _subscriberCommandService.Add(queue.Name, pickupChannel ?? (SocketTextChannel)guildChannel, - (SocketGuildUser)reaction.User).ConfigureAwait(false); + (SocketGuildUser)reaction.User); break; case "\uD83D\uDCE2": await _listCommandService.Promote( queue, pickupChannel ?? (SocketTextChannel)guildChannel, - (SocketGuildUser)reaction.User).ConfigureAwait(false); + (SocketGuildUser)reaction.User); break; } } private async Task ReactionRemoved(Cacheable message, ISocketMessageChannel channel, SocketReaction reaction) { - if (!(channel is IGuildChannel guildChannel) || guildChannel.Name != "active-pickups") return; + if (!(channel is IGuildChannel guildChannel) || guildChannel.Name != ChannelNames.ActivePickups) return; if (reaction.User.Value.IsBot) return; if (reaction.Emote.Name == "\u2705") { - var queue = await _queueRepository.FindQueueByMessageId(reaction.MessageId, guildChannel.GuildId.ToString()).ConfigureAwait(false); + var queue = await _queueRepository.FindQueueByMessageId(reaction.MessageId, guildChannel.GuildId.ToString()); if (queue != null) { - var pickupChannel = ((SocketGuild)guildChannel.Guild).Channels.FirstOrDefault(c => c.Name.Equals("pickup")) as SocketTextChannel; + var pickupChannel = ((SocketGuild)guildChannel.Guild).Channels.FirstOrDefault(c => c.Name.Equals(ChannelNames.Pickup)) as SocketTextChannel; await _subscriberCommandService.Leave(queue, pickupChannel ?? (SocketTextChannel)guildChannel, - (SocketGuildUser)reaction.User).ConfigureAwait(false); + (SocketGuildUser)reaction.User); } } } @@ -104,29 +107,28 @@ public async Task Create( if (teamSize > 16) teamSize = 16; - if (!await _miscCommandService.VerifyUserFlaggedStatus((IGuildUser)Context.User, Context.Channel).ConfigureAwait(false)) + if (!await _miscCommandService.VerifyUserFlaggedStatus((IGuildUser)Context.User, Context.Channel)) return; queueName = queueName.Trim(' ', '"').Trim(); //find queue with name {queueName} - var queue = await _queueRepository.FindQueue(queueName, Context.Guild.Id.ToString()).ConfigureAwait(false); + var queue = await _queueRepository.FindQueue(queueName, Context.Guild.Id.ToString()); if (queue != null) { - await Context.Channel.SendMessageAsync($"`Queue with the name '{queueName}' already exists!`").AutoRemoveMessage(10).ConfigureAwait(false); + await Context.Channel.SendMessageAsync($"`Queue with the name '{queueName}' already exists!`").AutoRemoveMessage(10); return; } - var activity = await _activitiesRepository.Find((IGuildUser)Context.User).ConfigureAwait(false); + var activity = await _activitiesRepository.Find((IGuildUser)Context.User); activity.PickupCreate += 1; activity.PickupAdd += 1; - await _activitiesRepository.Update(activity).ConfigureAwait(false); + await _activitiesRepository.Update(activity); - queue = await _listCommandService.Create(queueName, teamSize, operators, (SocketGuildUser)Context.User).ConfigureAwait(false); + queue = await _listCommandService.Create(queueName, teamSize, operators, (SocketGuildUser)Context.User); - await Context.Channel.SendMessageAsync($"`Queue '{queue.Name}' was added by {PickupHelpers.GetNickname(Context.User)}`") - .ConfigureAwait(false); + await Context.Channel.SendMessageAsync($"`Queue '{queue.Name}' was added by {PickupHelpers.GetNickname(Context.User)}`"); } @@ -139,16 +141,16 @@ public async Task Rename([Name("Queue name")] string queueName, [Name("New name" queueName = queueName.Trim(' ', '"').Trim(); newName = newName.Trim(' ', '"').Trim(); - var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel).ConfigureAwait(false); + var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel); if (queue == null) return; var isAdmin = (Context.User as IGuildUser)?.GuildPermissions.Has(GuildPermission.Administrator) ?? false; if (isAdmin || queue.OwnerId == Context.User.Id.ToString()) { - var newQueueCheck = await _queueRepository.FindQueue(newName, Context.Guild.Id.ToString()).ConfigureAwait(false); + var newQueueCheck = await _queueRepository.FindQueue(newName, Context.Guild.Id.ToString()); if (newQueueCheck != null) { - await ReplyAsync($"`A queue with the name '{newName}' already exists.`").AutoRemoveMessage(10).ConfigureAwait(false); + await ReplyAsync($"`A queue with the name '{newName}' already exists.`").AutoRemoveMessage(10); return; } @@ -159,23 +161,21 @@ public async Task Rename([Name("Queue name")] string queueName, [Name("New name" var result = await _queueRepository.AddQueue(newQueue); if (result) { - await _queueRepository.RemoveQueue(queue).ConfigureAwait(false); + await _queueRepository.RemoveQueue(queue); await ReplyAsync($"The queue '{queue.Name}' has been renamed to '{newQueue.Name}'"); await ReplyAsync($"`{newQueue.Name} - {PickupHelpers.ParseSubscribers(newQueue)}`"); if (!string.IsNullOrEmpty(queue.StaticMessageId)) - await _listCommandService.SaveStaticQueueMessage(newQueue, Context.Guild).ConfigureAwait(false); + await _listCommandService.SaveStaticQueueMessage(newQueue, Context.Guild); return; } await ReplyAsync("An error occured when trying to update the queue name, try again.") - .AutoRemoveMessage(10) - .ConfigureAwait(false); + .AutoRemoveMessage(10); } else { await ReplyAsync("`You do not have permission to rename this queue, you have to be either the owner or a server admin`") - .AutoRemoveMessage(10) - .ConfigureAwait(false); + .AutoRemoveMessage(10); } } @@ -190,17 +190,16 @@ public async Task Update( queueName = queueName.Trim(' ', '"').Trim(); - var queue = await _listCommandService.UpdateOperators(queueName, operators, (SocketGuildUser)Context.User).ConfigureAwait(false); + var queue = await _listCommandService.UpdateOperators(queueName, operators, (SocketGuildUser)Context.User); if (queue == null) { - await Context.Channel.SendMessageAsync($"`Queue with the name '{queueName}' doesn't exists!`").AutoRemoveMessage(10).ConfigureAwait(false); + await Context.Channel.SendMessageAsync($"`Queue with the name '{queueName}' doesn't exists!`").AutoRemoveMessage(10); return; } await Context.Channel - .SendMessageAsync($"`Queue '{queue.Name}' was updated by {PickupHelpers.GetNickname(Context.User)}`") - .ConfigureAwait(false); + .SendMessageAsync($"`Queue '{queue.Name}' was updated by {PickupHelpers.GetNickname(Context.User)}`"); } @@ -214,7 +213,7 @@ public async Task Delete([Name("Queue name"), Summary("Queue name"), Remainder] queueName = queueName.Trim(' ', '"').Trim(); - var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel).ConfigureAwait(false); + var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel); if (queue == null) { return; @@ -223,30 +222,29 @@ public async Task Delete([Name("Queue name"), Summary("Queue name"), Remainder] var isAdmin = (Context.User as IGuildUser)?.GuildPermissions.Has(GuildPermission.Administrator) ?? false; if (!isAdmin && queue.OwnerId != Context.User.Id.ToString()) { - await Context.Channel.SendMessageAsync("You do not have permission to remove the queue.").AutoRemoveMessage(10).ConfigureAwait(false); + await Context.Channel.SendMessageAsync("You do not have permission to remove the queue.").AutoRemoveMessage(10); return; } if (!queue.Started) { - var queuesChannel = await PickupHelpers.GetPickupQueuesChannel(Context.Guild).ConfigureAwait(false); + var queuesChannel = await PickupHelpers.GetPickupQueuesChannel(Context.Guild); var result = await _queueRepository.RemoveQueue(queueName, Context.Guild.Id.ToString()) - .ConfigureAwait(false); + ; var message = result ? $"`Queue '{queueName}' has been canceled`" : $"`Queue with the name '{queueName}' doesn't exists or you are not the owner of the queue!`"; - await Context.Channel.SendMessageAsync(message).AutoRemoveMessage(10).ConfigureAwait(false); + await Context.Channel.SendMessageAsync(message).AutoRemoveMessage(10); if (!string.IsNullOrEmpty(queue.StaticMessageId)) await queuesChannel.DeleteMessageAsync(Convert.ToUInt64(queue.StaticMessageId)) - .ConfigureAwait(false); + ; } else { await Context.Channel.SendMessageAsync("Queue is started, you have to run `!stop` to clean up efter yourself first") - .AutoRemoveMessage(15) - .ConfigureAwait(false); + .AutoRemoveMessage(15); } } @@ -259,7 +257,7 @@ public async Task List() return; //find all active queues - var queues = await _queueRepository.AllQueues(Context.Guild.Id.ToString()).ConfigureAwait(false); + var queues = await _queueRepository.AllQueues(Context.Guild.Id.ToString()); Embed embed; //if queues found var pickupQueues = queues as PickupQueue[] ?? queues.ToArray(); @@ -272,7 +270,7 @@ public async Task List() Color = Color.Orange }.Build(); - await Context.Channel.SendMessageAsync(embed: embed).AutoRemoveMessage(10).ConfigureAwait(false); + await Context.Channel.SendMessageAsync(embed: embed).AutoRemoveMessage(10); return; } @@ -285,7 +283,7 @@ public async Task List() Description = BuildListResponse(q).ToString(), Color = Color.Orange }.Build(); - await Context.Channel.SendMessageAsync(embed: embed).AutoRemoveMessage().ConfigureAwait(false); + await Context.Channel.SendMessageAsync(embed: embed).AutoRemoveMessage(); } } @@ -321,7 +319,7 @@ public async Task WaitList([Name("Queue name"), Summary("Queue name"), Remainder if (queue == null) { - await Context.Channel.SendMessageAsync($"`Queue with the name '{queueName}' doesn't exists!`").AutoRemoveMessage(10).ConfigureAwait(false); + await Context.Channel.SendMessageAsync($"`Queue with the name '{queueName}' doesn't exists!`").AutoRemoveMessage(10); return; } @@ -335,7 +333,7 @@ public async Task WaitList([Name("Queue name"), Summary("Queue name"), Remainder Description = waitlist, Color = Color.Orange }.Build(); - await Context.Channel.SendMessageAsync(embed: embed).AutoRemoveMessage(15).ConfigureAwait(false); + await Context.Channel.SendMessageAsync(embed: embed).AutoRemoveMessage(15); } [Command("promote")] @@ -353,7 +351,7 @@ public async Task Promote([Name("Queue name"), Summary("Queue name"), Remainder] queue = await _queueRepository.FindQueue(queueName, Context.Guild.Id.ToString()); if (queue == null) { - await Context.Channel.SendMessageAsync($"`Queue with the name '{queueName}' doesn't exists!`").AutoRemoveMessage(10).ConfigureAwait(false); + await Context.Channel.SendMessageAsync($"`Queue with the name '{queueName}' doesn't exists!`").AutoRemoveMessage(10); return; } @@ -364,7 +362,7 @@ public async Task Promote([Name("Queue name"), Summary("Queue name"), Remainder] } } - await _listCommandService.Promote(queue, (ITextChannel)Context.Channel, (IGuildUser)Context.User).ConfigureAwait(false); + await _listCommandService.Promote(queue, (ITextChannel)Context.Channel, (IGuildUser)Context.User); } [Command("start")] @@ -375,17 +373,17 @@ public async Task Start([Name("Queue name"), Summary("Queue name"), Remainder] s queueName = queueName.Trim(' ', '"').Trim(); - var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel).ConfigureAwait(false); + var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel); if (queue == null || queue.Started) return; var pickupCategory = (ICategoryChannel)Context.Guild.CategoryChannels.FirstOrDefault(c => - c.Name.Equals("Pickup voice channels", StringComparison.OrdinalIgnoreCase)) - ?? await Context.Guild.CreateCategoryChannelAsync("Pickup voice channels"); + c.Name.Equals(CategoryNames.PickupVoiceChannels, StringComparison.OrdinalIgnoreCase)) + ?? await Context.Guild.CreateCategoryChannelAsync(CategoryNames.PickupVoiceChannels); var vcRedTeamName = $"{queue.Name} \uD83D\uDD34"; var vcBlueTeamName = $"{queue.Name} \uD83D\uDD35"; - var vcRed = await PickupHelpers.GetOrCreateVoiceChannel(vcRedTeamName, pickupCategory.Id, Context.Guild).ConfigureAwait(false); + var vcRed = await PickupHelpers.GetOrCreateVoiceChannel(vcRedTeamName, pickupCategory.Id, Context.Guild); var vcBlue = queue.IsCoop ? null : await PickupHelpers.GetOrCreateVoiceChannel(vcBlueTeamName, pickupCategory.Id, Context.Guild); @@ -421,9 +419,9 @@ public async Task Start([Name("Queue name"), Summary("Queue name"), Remainder] s queue.Started = true; queue.Updated = DateTime.UtcNow; - queue = await _listCommandService.SaveStaticQueueMessage(queue, Context.Guild).ConfigureAwait(false); - await _queueRepository.UpdateQueue(queue).ConfigureAwait(false); - await _listCommandService.PrintTeams(queue, Context.Channel, Context.Guild).ConfigureAwait(false); + queue = await _listCommandService.SaveStaticQueueMessage(queue, Context.Guild); + await _queueRepository.UpdateQueue(queue); + await _listCommandService.PrintTeams(queue, Context.Channel, Context.Guild); _miscCommandService.TriggerDelayedRconNotification(queue); } @@ -437,15 +435,15 @@ public async Task Teams([Name("Queue name"), Remainder] string queueName) queueName = queueName.Trim(' ', '"').Trim(); - var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel).ConfigureAwait(false); + var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel); if (queue == null) return; using (Context.Channel.EnterTypingState()) { - await _listCommandService.PrintTeams(queue, Context.Channel, Context.Guild).ConfigureAwait(false); + await _listCommandService.PrintTeams(queue, Context.Channel, Context.Guild); - await _miscCommandService.TriggerRconNotification(queue).ConfigureAwait(false); + await _miscCommandService.TriggerRconNotification(queue); } } @@ -458,7 +456,7 @@ public async Task Stop([Name("Queue name"), Summary("Queue name")]string queueNa queueName = queueName.Trim(' ', '"').Trim(); - var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel).ConfigureAwait(false); + var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel); if (queue == null) return; var voiceIds = queue.Teams.Select(w => w.VoiceChannel.Value).ToList(); @@ -470,17 +468,17 @@ public async Task Stop([Name("Queue name"), Summary("Queue name")]string queueNa var vc = (IVoiceChannel)Context.Guild.GetVoiceChannel(voiceId.Value); if (vc == null) continue; - await vc.DeleteAsync().ConfigureAwait(false); + await vc.DeleteAsync(); } } queue.Started = false; queue.Updated = DateTime.UtcNow; queue.Teams.Clear(); - queue = await _listCommandService.SaveStaticQueueMessage(queue, Context.Guild).ConfigureAwait(false); - await _queueRepository.UpdateQueue(queue).ConfigureAwait(false); + queue = await _listCommandService.SaveStaticQueueMessage(queue, Context.Guild); + await _queueRepository.UpdateQueue(queue); - await Delete(queueName).ConfigureAwait(false); + await Delete(queueName); } public void Dispose() diff --git a/PickupBot.Commands/Modules/Pickup/PickupModuleSubscriberActions.cs b/PickupBot.Commands/Modules/Pickup/PickupModuleSubscriberActions.cs index bba8277..2baef96 100644 --- a/PickupBot.Commands/Modules/Pickup/PickupModuleSubscriberActions.cs +++ b/PickupBot.Commands/Modules/Pickup/PickupModuleSubscriberActions.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; +using PickupBot.Commands.Constants; using PickupBot.Commands.Extensions; using PickupBot.Commands.Infrastructure.Helpers; using PickupBot.Commands.Infrastructure.Services; @@ -43,7 +44,7 @@ public async Task Add([Name("Queue name"), Summary("Queue name"), Remainder]stri queueName = queueName.Trim(' ', '"').Trim(); //find queue with name {queueName} - await _subscriberCommandService.Add(queueName, Context.Channel, (IGuildUser)Context.User).ConfigureAwait(false); + await _subscriberCommandService.Add(queueName, Context.Channel, (IGuildUser)Context.User); } [Command("remove")] @@ -63,13 +64,13 @@ public async Task Add([Name("Queue name"), Summary("Queue name"), Remainder]stri } //find queue with name {queueName} - var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel).ConfigureAwait(false); + var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel); if (queue == null) { return; } - await _subscriberCommandService.Leave(queue, Context.Channel, (IGuildUser)Context.User).ConfigureAwait(false); + await _subscriberCommandService.Leave(queue, Context.Channel, (IGuildUser)Context.User); } [Command("clear")] @@ -80,7 +81,7 @@ public async Task Clear() if (!PickupHelpers.IsInPickupChannel((IGuildChannel)Context.Channel)) return; //find queues with user in it - var allQueues = await _queueRepository.AllQueues(Context.Guild.Id.ToString()).ConfigureAwait(false); + var allQueues = await _queueRepository.AllQueues(Context.Guild.Id.ToString()); var matchingQueues = allQueues.Where(q => q.Subscribers.Any(s => s.Id == Context.User.Id) || q.WaitingList.Any(w => w.Id == Context.User.Id)); @@ -92,26 +93,25 @@ public async Task Clear() queue.WaitingList.RemoveAll(w => w.Id == Context.User.Id); queue.Updated = DateTime.UtcNow; - var updatedQueue = await _subscriberCommandService.Leave(queue, Context.Channel, (IGuildUser)Context.User, false).ConfigureAwait(false); + var updatedQueue = await _subscriberCommandService.Leave(queue, Context.Channel, (IGuildUser)Context.User, false); updatedQueue ??= queue; if (!updatedQueue.Subscribers.Any() && !updatedQueue.WaitingList.Any()) { - await _queueRepository.RemoveQueue(updatedQueue.Name, updatedQueue.GuildId).ConfigureAwait(false); //Try to remove queue if its empty. + await _queueRepository.RemoveQueue(updatedQueue.Name, updatedQueue.GuildId); //Try to remove queue if its empty. if (string.IsNullOrEmpty(queue.StaticMessageId)) continue; - var queuesChannel = await PickupHelpers.GetPickupQueuesChannel(Context.Guild).ConfigureAwait(false); - await queuesChannel.DeleteMessageAsync(Convert.ToUInt64(queue.StaticMessageId)).ConfigureAwait(false); + var queuesChannel = await PickupHelpers.GetPickupQueuesChannel(Context.Guild); + await queuesChannel.DeleteMessageAsync(Convert.ToUInt64(queue.StaticMessageId)); } else - await _queueRepository.UpdateQueue(updatedQueue).ConfigureAwait(false); + await _queueRepository.UpdateQueue(updatedQueue); } //if queues found and user is in queue await Context.Channel.SendMessageAsync($"{PickupHelpers.GetMention(Context.User)} - You have been removed from all queues") - .AutoRemoveMessage(10) - .ConfigureAwait(false); + .AutoRemoveMessage(10); } } @@ -122,7 +122,7 @@ public async Task Subscribe() if (!PickupHelpers.IsInPickupChannel((IGuildChannel)Context.Channel)) return; - var role = Context.Guild.Roles.FirstOrDefault(w => w.Name == "pickup-promote"); + var role = Context.Guild.Roles.FirstOrDefault(w => w.Name == RoleNames.PickupPromote); if (role == null) return; //Failed to get or create role; @@ -130,17 +130,16 @@ public async Task Subscribe() if (user.RoleIds.Any(w => w == role.Id)) { - await user.RemoveRoleAsync(role).ConfigureAwait(false); + await user.RemoveRoleAsync(role); await ReplyAsync($"{PickupHelpers.GetMention(user)} - you are no longer subscribed to get notifications on `!promote`") - .AutoRemoveMessage(10) - .ConfigureAwait(false); + .AutoRemoveMessage(10); } else { - await user.AddRoleAsync(role).ConfigureAwait(false); - await ReplyAsync($"{PickupHelpers.GetMention(user)} - you are now subscribed to get notifications on `!promote`") - .AutoRemoveMessage(10) - .ConfigureAwait(false); + await user.AddRoleAsync(role); + await ReplyAsync( + $"{PickupHelpers.GetMention(user)} - you are now subscribed to get notifications on `!promote`") + .AutoRemoveMessage(10); } } } diff --git a/PickupBot/PickupBot.csproj b/PickupBot/PickupBot.csproj index 57cee07..d87d670 100644 --- a/PickupBot/PickupBot.csproj +++ b/PickupBot/PickupBot.csproj @@ -4,7 +4,7 @@ netcoreapp3.1 true a7bb364b-0d73-420c-a3b9-18270f20e20f - 1.18.0 + 1.18.1 Joakim Höglund MIT https://github.com/Floydan/discord-pickup-bot diff --git a/PickupBot/Program.cs b/PickupBot/Program.cs index b047822..ff58abc 100644 --- a/PickupBot/Program.cs +++ b/PickupBot/Program.cs @@ -17,6 +17,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using PickupBot.Commands; +using PickupBot.Commands.Constants; using PickupBot.Commands.Extensions; using PickupBot.Commands.Infrastructure; using PickupBot.Commands.Infrastructure.Services; @@ -103,7 +104,7 @@ private static void ConfigureServices(HostBuilderContext hostContext, IServiceCo services.Configure(option => { - option.ShutdownTimeout = TimeSpan.FromSeconds(5); + option.ShutdownTimeout = TimeSpan.FromSeconds(10); }); var assemblies = new[] @@ -160,36 +161,36 @@ private static async Task OnJoinedGuild(SocketGuild guild) { try { - var pickupsCategory = - (ICategoryChannel)guild.CategoryChannels.FirstOrDefault(c => c.Name.Equals("Pickups", StringComparison.OrdinalIgnoreCase)) ?? - await guild.CreateCategoryChannelAsync("Pickups"); + var pickupsCategory = (ICategoryChannel)guild.CategoryChannels + .FirstOrDefault(c => c.Name.Equals(CategoryNames.Pickups, StringComparison.OrdinalIgnoreCase)) ?? + await guild.CreateCategoryChannelAsync(CategoryNames.Pickups); await CreateChannel(guild, - "pickup", + ChannelNames.Pickup, "powered by pickup-bot | !help for instructions", pickupsCategory.Id); await CreateChannel(guild, - "duel", + ChannelNames.Duel, "powered by pickup-bot | !help for instructions", pickupsCategory.Id); await CreateChannel(guild, - "active-pickup", + ChannelNames.ActivePickups, "Active pickups | use reactions to signup | powered by pickup-bot", pickupsCategory.Id); // create applicable roles if missing - if (guild.Roles.All(w => w.Name != "pickup-promote")) - await guild.CreateRoleAsync("pickup-promote", GuildPermissions.None, Color.Orange, isHoisted: false, isMentionable: true); + if (guild.Roles.All(w => w.Name != RoleNames.PickupPromote)) + await guild.CreateRoleAsync(RoleNames.PickupPromote, GuildPermissions.None, Color.Orange, isHoisted: false, isMentionable: true); // create applicable roles if missing - if (guild.Roles.All(w => w.Name != "duellist")) - await guild.CreateRoleAsync("duellist", GuildPermissions.None, isHoisted: false, isMentionable: true); + if (guild.Roles.All(w => w.Name != RoleNames.Duellist)) + await guild.CreateRoleAsync(RoleNames.Duellist, GuildPermissions.None, isHoisted: false, isMentionable: true); // create voice channel category if missing - if (guild.CategoryChannels.FirstOrDefault(c => c.Name.Equals("Pickup voice channels", StringComparison.OrdinalIgnoreCase)) == null) - await guild.CreateCategoryChannelAsync("Pickup voice channels"); + if (guild.CategoryChannels.FirstOrDefault(c => c.Name.Equals(CategoryNames.PickupVoiceChannels, StringComparison.OrdinalIgnoreCase)) == null) + await guild.CreateCategoryChannelAsync(CategoryNames.PickupVoiceChannels); } catch (Exception e) {