From c0562026bcbbd7642e3f30d18f76a9253af6a4e2 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sat, 8 Oct 2016 20:24:34 +0200 Subject: [PATCH] Fixed some stuff --- .../CustomReactions/CustomReactions.cs | 37 ++++++++++++++++--- src/NadekoBot/Modules/Gambling/Gambling.cs | 36 ++++++++++++++---- .../Modules/Permissions/Permissions.cs | 11 ++++++ .../Services/Database/Models/GuildConfig.cs | 2 +- 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs index 0ecccb16..42199af1 100644 --- a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs +++ b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs @@ -18,18 +18,43 @@ namespace NadekoBot.Modules.CustomReactions public class CustomReactions : DiscordModule { public static HashSet GlobalReactions { get; } = new HashSet(); - public static ConcurrentDictionary> AllReactions { get; } = new ConcurrentDictionary>(); + public static ConcurrentDictionary> GuildReactions { get; } = new ConcurrentDictionary>(); static CustomReactions() { using (var uow = DbHandler.UnitOfWork()) { var items = uow.CustomReactions.GetAll(); - AllReactions = new ConcurrentDictionary>(items.Where(g => g.GuildId != null).GroupBy(k => k.GuildId.Value).ToDictionary(g => g.Key, g => new HashSet(g))); + GuildReactions = new ConcurrentDictionary>(items.Where(g => g.GuildId != null).GroupBy(k => k.GuildId.Value).ToDictionary(g => g.Key, g => new HashSet(g))); GlobalReactions = new HashSet(items.Where(g => g.GuildId == null)); } } public CustomReactions(ILocalization loc, CommandService cmds, ShardedDiscordClient client) : base(loc, cmds, client) { + client.MessageReceived += (imsg) => + { + var umsg = imsg as IUserMessage; + if (umsg == null) + return Task.CompletedTask; + + var channel = umsg.Channel as ITextChannel; + if (channel == null) + return Task.CompletedTask; + + var t = Task.Run(async () => + { + HashSet reactions; + GuildReactions.TryGetValue(channel.Guild.Id, out reactions); + if (reactions != null && reactions.Any()) + { + var reaction = reactions.Where(cr => cr.Trigger == umsg.Content).Shuffle().FirstOrDefault(); + if (reaction != null) + { + await channel.SendMessageAsync(reaction.Response).ConfigureAwait(false); + } + } + }); + return Task.CompletedTask; + }; } [NadekoCommand, Usage, Description, Aliases] @@ -40,7 +65,7 @@ namespace NadekoBot.Modules.CustomReactions if (string.IsNullOrWhiteSpace(message) || string.IsNullOrWhiteSpace(key)) return; - if ((channel == null && NadekoBot.Credentials.IsOwner(imsg.Author)) || (channel != null && ((IGuildUser)imsg.Author).GuildPermissions.Administrator)) + if ((channel == null && !NadekoBot.Credentials.IsOwner(imsg.Author)) || (channel != null && !((IGuildUser)imsg.Author).GuildPermissions.Administrator)) { try { await channel.SendMessageAsync("Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for guild custom reactions."); } catch { } return; @@ -67,7 +92,7 @@ namespace NadekoBot.Modules.CustomReactions } else { - var reactions = AllReactions.GetOrAdd(channel.Guild.Id, new HashSet()); + var reactions = GuildReactions.GetOrAdd(channel.Guild.Id, new HashSet()); reactions.Add(cr); } @@ -85,7 +110,7 @@ namespace NadekoBot.Modules.CustomReactions if (channel == null) customReactions = GlobalReactions; else - customReactions = AllReactions.GetOrAdd(channel.Guild.Id, new HashSet()); + customReactions = GuildReactions.GetOrAdd(channel.Guild.Id, new HashSet()); if (customReactions == null || !customReactions.Any()) await channel.SendMessageAsync("`No custom reactions found`").ConfigureAwait(false); @@ -99,7 +124,7 @@ namespace NadekoBot.Modules.CustomReactions { var channel = imsg.Channel as ITextChannel; - if ((channel == null && NadekoBot.Credentials.IsOwner(imsg.Author)) || (channel != null && ((IGuildUser)imsg.Author).GuildPermissions.Administrator)) + if ((channel == null && !NadekoBot.Credentials.IsOwner(imsg.Author)) || (channel != null && !((IGuildUser)imsg.Author).GuildPermissions.Administrator)) { try { await channel.SendMessageAsync("Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for guild custom reactions."); } catch { } return; diff --git a/src/NadekoBot/Modules/Gambling/Gambling.cs b/src/NadekoBot/Modules/Gambling/Gambling.cs index b47c45ce..6335295f 100644 --- a/src/NadekoBot/Modules/Gambling/Gambling.cs +++ b/src/NadekoBot/Modules/Gambling/Gambling.cs @@ -46,8 +46,9 @@ namespace NadekoBot.Modules.Gambling var usr = membersArray[new NadekoRandom().Next(0, membersArray.Length)]; await channel.SendMessageAsync($"**Raffled user:** {usr.Username} (id: {usr.Id})").ConfigureAwait(false); } - + [NadekoCommand, Usage, Description, Aliases] + [Priority(1)] public async Task Cash(IUserMessage umsg, [Remainder] IUser user = null) { var channel = umsg.Channel; @@ -63,7 +64,24 @@ namespace NadekoBot.Modules.Gambling await channel.SendMessageAsync($"{user.Username} has {amount} {config.CurrencySign}").ConfigureAwait(false); } - + + [NadekoCommand, Usage, Description, Aliases] + [Priority(0)] + public async Task Cash(IUserMessage umsg, ulong userId) + { + var channel = umsg.Channel; + + long amount; + BotConfig config; + using (var uow = DbHandler.UnitOfWork()) + { + amount = uow.Currency.GetUserCurrency(userId); + config = uow.BotConfig.GetOrCreate(); + } + + await channel.SendMessageAsync($"`{userId}` has {amount} {config.CurrencySign}").ConfigureAwait(false); + } + [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] public async Task Give(IUserMessage umsg, long amount, [Remainder] IGuildUser receiver) @@ -111,9 +129,10 @@ namespace NadekoBot.Modules.Gambling if (amount <= 0) return; - await CurrencyHandler.RemoveCurrencyAsync(user, $"Taken by bot owner.({umsg.Author.Username}/{umsg.Author.Id})", amount, true).ConfigureAwait(false); - - await channel.SendMessageAsync($"{umsg.Author.Mention} successfully took {amount} {Gambling.CurrencyName}s from {user}!").ConfigureAwait(false); + if(await CurrencyHandler.RemoveCurrencyAsync(user, $"Taken by bot owner.({umsg.Author.Username}/{umsg.Author.Id})", amount, true).ConfigureAwait(false)) + await channel.SendMessageAsync($"{umsg.Author.Mention} successfully took {amount} {Gambling.CurrencyPluralName} from {user}!").ConfigureAwait(false); + else + await channel.SendMessageAsync($"{umsg.Author.Mention} was unable to take {amount} {Gambling.CurrencyPluralName} from {user} because the user doesn't have that much {Gambling.CurrencyPluralName}!").ConfigureAwait(false); } @@ -126,9 +145,10 @@ namespace NadekoBot.Modules.Gambling if (amount <= 0) return; - await CurrencyHandler.RemoveCurrencyAsync(usrId, $"Taken by bot owner.({umsg.Author.Username}/{umsg.Author.Id})", amount).ConfigureAwait(false); - - await channel.SendMessageAsync($"{umsg.Author.Mention} successfully took {amount} {Gambling.CurrencyName}s from <@{usrId}>!").ConfigureAwait(false); + if(await CurrencyHandler.RemoveCurrencyAsync(usrId, $"Taken by bot owner.({umsg.Author.Username}/{umsg.Author.Id})", amount).ConfigureAwait(false)) + await channel.SendMessageAsync($"{umsg.Author.Mention} successfully took {amount} {Gambling.CurrencyName}s from <@{usrId}>!").ConfigureAwait(false); + else + await channel.SendMessageAsync($"{umsg.Author.Mention} was unable to take {amount} {Gambling.CurrencyPluralName} from `{usrId}` because the user doesn't have that much {Gambling.CurrencyPluralName}!").ConfigureAwait(false); } [NadekoCommand, Usage, Description, Aliases] diff --git a/src/NadekoBot/Modules/Permissions/Permissions.cs b/src/NadekoBot/Modules/Permissions/Permissions.cs index aa0ab412..94ba7ebb 100644 --- a/src/NadekoBot/Modules/Permissions/Permissions.cs +++ b/src/NadekoBot/Modules/Permissions/Permissions.cs @@ -490,6 +490,17 @@ namespace NadekoBot.Modules.Permissions State = action.Value, }; uow.GuildConfigs.SetNewRootPermission(channel.Guild.Id, newPerm); + + var allowUser = new Permission + { + PrimaryTarget = PrimaryPermissionType.User, + PrimaryTargetId = imsg.Author.Id, + SecondaryTarget = SecondaryPermissionType.AllModules, + SecondaryTargetName = "*", + State = true, + }; + + uow.GuildConfigs.SetNewRootPermission(channel.Guild.Id, allowUser); await uow.CompleteAsync().ConfigureAwait(false); } await channel.SendMessageAsync($"{(action.Value ? "Allowed" : "Denied")} usage of `ALL MODULES` on this server.").ConfigureAwait(false); diff --git a/src/NadekoBot/Services/Database/Models/GuildConfig.cs b/src/NadekoBot/Services/Database/Models/GuildConfig.cs index 9dc7a57d..7084671d 100644 --- a/src/NadekoBot/Services/Database/Models/GuildConfig.cs +++ b/src/NadekoBot/Services/Database/Models/GuildConfig.cs @@ -44,7 +44,7 @@ namespace NadekoBot.Services.Database.Models //permissions public Permission RootPermission { get; set; } - public bool VerbosePermissions { get; set; } + public bool VerbosePermissions { get; set; } = true; public string PermissionRole { get; set; } = "Nadeko"; public HashSet CommandCooldowns { get; set; } = new HashSet();