public async Task LMGTFY([Remainder] string query) { using var Database = new SkuldDbContextFactory().CreateDbContext(); var prefix = (await Database.InsertOrGetGuildAsync(Context.Guild).ConfigureAwait(false)).Prefix ?? Configuration.Prefix; string url = "https://lmgtfy.com/"; var firstPart = query.Split(" ")[0]; url = (firstPart.ToLowerInvariant()) switch { "b" or "bing" => url + "?s=b&q=" + query.ReplaceFirst(firstPart, "").Replace(" ", "%20"), "y" or "yahoo" => url + "?s=y&q=" + query.ReplaceFirst(firstPart, "").Replace(" ", "%20"), "a" or "aol" => url + "?a=b&q=" + query.ReplaceFirst(firstPart, "").Replace(" ", "%20"), "k" or "ask" => url + "?k=b&q=" + query.ReplaceFirst(firstPart, "").Replace(" ", "%20"), "d" or "duckduckgo" or "ddg" => url + "?s=d&q=" + query.ReplaceFirst(firstPart, "").Replace(" ", "%20"), "g" or "google" => url + "?s=g&q=" + query.ReplaceFirst(firstPart, "").Replace(" ", "%20"), _ => url + "?q=" + query.Replace(" ", "%20"), }; if (url != "https://lmgtfy.com/") { await url.QueueMessageAsync(Context).ConfigureAwait(false); } else { await EmbedExtensions.FromError($"Ensure your parameters are correct, example: `{prefix}lmgtfy g How to use lmgtfy`", Context).QueueMessageAsync(Context).ConfigureAwait(false); StatsdClient.DogStatsd.Increment("commands.errors", 1, 1, new[] { "generic" }); } }
private static async Task Bot_GuildUpdated( SocketGuild arg1, SocketGuild arg2 ) { try { using SkuldDbContext Database = new SkuldDbContextFactory() .CreateDbContext(); var sguild = await Database.InsertOrGetGuildAsync(arg2) .ConfigureAwait(false); if (sguild.Name == null || !sguild.Name.Equals(arg2.Name)) { sguild.Name = arg2.Name; } if (sguild.IconUrl == null || !sguild.IconUrl.Equals(arg2.IconUrl)) { sguild.IconUrl = arg2.IconUrl; } await Database.SaveChangesAsync().ConfigureAwait(false); } catch (Exception ex) { Log.Error("UsrJoin", ex.Message, null, ex); } }
private static async Task Bot_JoinedGuild( SocketGuild arg ) { DogStatsd.Increment("guilds.joined"); Log.Verbose(Key, $"Just left {arg}", null); try { using var database = new SkuldDbContextFactory().CreateDbContext(); await SkuldApp.DiscordClient.SendDataAsync( SkuldApp.Configuration.IsDevelopmentBuild, SkuldApp.Configuration.DiscordGGKey, SkuldApp.Configuration.DBotsOrgKey, SkuldApp.Configuration.B4DToken ) .ConfigureAwait(false); await database.InsertOrGetGuildAsync( arg, SkuldApp.Configuration.Prefix, SkuldApp.MessageServiceConfig.MoneyName, SkuldApp.MessageServiceConfig.MoneyIcon ) .ConfigureAwait(false); //MessageQueue.CheckForEmptyGuilds = true; } catch (Exception ex) { Log.Error("UsrJoin", ex.Message, null, ex); } }
public static async Task HandleSideTasksAsync(ICommandContext context) { using var Database = new SkuldDbContextFactory().CreateDbContext(); Guild sguild = null; if (context.Guild != null) { sguild = await Database.InsertOrGetGuildAsync(context.Guild) .ConfigureAwait(false); } if (sguild != null) { if (!Database.Features.Any(x => x.Id == sguild.Id)) { Database.Features.Add(new GuildFeatures { Id = sguild.Id }); await Database.SaveChangesAsync().ConfigureAwait(false); } GuildFeatures features = Database.Features.Find(sguild.Id); if (features.Experience) { _ = Task.Run(() => ExperienceService.HandleExperienceAsync(context)); } if (!Database.Modules.Any(x => x.Id == sguild.Id)) { Database.Modules.Add(new GuildModules { Id = sguild.Id }); await Database.SaveChangesAsync().ConfigureAwait(false); } GuildModules modules; if (!Database.Modules.Any(x => x.Id == sguild.Id)) { Database.Modules.Add(new GuildModules { Id = sguild.Id }); await Database.SaveChangesAsync().ConfigureAwait(false); } modules = Database.Modules.Find(sguild.Id); if (modules.Custom) { _ = Task.Run(() => CustomCommandService.HandleCustomCommandAsync(context, SkuldApp.Configuration)); } } }
private static async Task Shard_MessagesBulkDeleted( IReadOnlyCollection <Cacheable <IMessage, ulong> > arg1, ISocketMessageChannel arg2 ) { using var Database = new SkuldDbContextFactory().CreateDbContext(); if (arg2 is IGuildChannel guildChannel) { var gld = await Database .InsertOrGetGuildAsync(guildChannel.Guild) .ConfigureAwait(false); var feats = Database.Features .Find(guildChannel.GuildId); if (feats.Starboard && gld.StarDeleteIfSourceDelete) { foreach (var msg in arg1) { var message = await msg.GetOrDownloadAsync() .ConfigureAwait(false); if (Database.StarboardVotes .Any(x => x.MessageId == message.Id)) { var vote = Database.StarboardVotes .FirstOrDefault(x => x.MessageId == message.Id ); Database.StarboardVotes .RemoveRange(Database.StarboardVotes.ToList() .Where(x => x.MessageId == message.Id)); var chan = await guildChannel.Guild .GetTextChannelAsync(gld.StarboardChannel) .ConfigureAwait(false); var starMessage = await chan .GetMessageAsync(vote.StarboardMessageId) .ConfigureAwait(false); await starMessage.DeleteAsync() .ConfigureAwait(false); await Database.SaveChangesAsync() .ConfigureAwait(false); } } } } }
public async Task GetServer() { using var Database = new SkuldDbContextFactory().CreateDbContext(); var dbguild = await Database.InsertOrGetGuildAsync(Context.Guild) .ConfigureAwait(false); Embed embed = await Context.Guild.GetSummaryAsync(Context.Client, Context, dbguild) .ConfigureAwait(false); await embed.QueueMessageAsync(Context).ConfigureAwait(false); }
public async Task GiveMoney(IGuildUser user, long amount) { using var Database = new SkuldDbContextFactory().CreateDbContext(); var usr = await Database.InsertOrGetUserAsync(user) .ConfigureAwait(false); if (amount < 0) { TransactionService.DoTransaction(new TransactionStruct { Amount = (ulong)Math.Abs(amount), Receiver = usr }); } else { TransactionService.DoTransaction(new TransactionStruct { Amount = (ulong)amount, Receiver = usr }); } await Database.SaveChangesAsync().ConfigureAwait(false); var icon = ( await Database.InsertOrGetGuildAsync(Context.Guild) .ConfigureAwait(false) ).MoneyIcon; StringBuilder message = new StringBuilder(); message .Append("User ") .Append(user.Username) .Append(" now has: ") .Append(icon) .Append(usr.Money.ToFormattedString()); await message.QueueMessageAsync(Context).ConfigureAwait(false); }
private static async Task Bot_UserLeft( SocketGuildUser arg ) { DogStatsd.Increment("guild.users.left"); try { using var db = new SkuldDbContextFactory().CreateDbContext(); var gld = await db .InsertOrGetGuildAsync(arg.Guild) .ConfigureAwait(false); if (gld != null) { if (gld.LeaveChannel != 0 && !string.IsNullOrEmpty(gld.LeaveMessage) ) { var channel = arg.Guild.GetTextChannel(gld.JoinChannel); var message = gld.LeaveMessage .ReplaceGuildEventMessage( arg, arg.Guild ); await channel .SendMessageAsync(message) .ConfigureAwait(false); } } } catch (Exception ex) { Log.Error("UsrJoin", ex.Message, null, ex); } Log.Verbose(Key, $"{arg} left {arg.Guild}", null); }
public async Task Money([Remainder] IGuildUser user = null) { using var Database = new SkuldDbContextFactory().CreateDbContext(); if (user is not null && (user.IsBot || user.IsWebhook)) { await EmbedExtensions.FromError("SkuldBank - Account Information", DiscordUtilities.NoBotsString, Context).QueueMessageAsync(Context).ConfigureAwait(false); return; } if (user is null) { user = (IGuildUser)Context.User; } var gld = await Database.InsertOrGetGuildAsync(Context.Guild).ConfigureAwait(false); var dbusr = await Database.InsertOrGetUserAsync(user).ConfigureAwait(false); await EmbedExtensions.FromMessage("SkuldBank - Account Information", $"{user.Mention} has {gld.MoneyIcon}{dbusr.Money.ToFormattedString()} {gld.MoneyName}", Context) .QueueMessageAsync(Context).ConfigureAwait(false); }
private static async Task Bot_UserJoined( SocketGuildUser arg ) { DogStatsd.Increment("guild.users.joined"); //Insert into Database try { using SkuldDbContext database = new SkuldDbContextFactory() .CreateDbContext(); await database.InsertOrGetUserAsync(arg as IUser) .ConfigureAwait(false); } catch (Exception ex) { Log.Error("UsrJoin", ex.Message, null, ex); } //Persistent Roles try { using SkuldDbContext database = new SkuldDbContextFactory() .CreateDbContext(); if (database.PersistentRoles.ToList() .Any(x => x.UserId == arg.Id && x.GuildId == arg.Guild.Id)) { foreach (var persistentRole in database.PersistentRoles .ToList().Where(x => x.UserId == arg.Id && x.GuildId == arg.Guild.Id) ) { try { await arg.AddRoleAsync( arg.Guild.GetRole(persistentRole.RoleId) ).ConfigureAwait(false); } catch (Exception ex) { Log.Error("UsrJoin", ex.Message, null, ex); } } } } catch (Exception ex) { Log.Error("UsrJoin", ex.Message, null, ex); } //Join Message try { using SkuldDbContext database = new SkuldDbContextFactory() .CreateDbContext(); var gld = await database.InsertOrGetGuildAsync(arg.Guild) .ConfigureAwait(false); if (gld != null) { if (gld.JoinRole != 0) { var joinrole = arg.Guild.GetRole(gld.JoinRole); await arg .AddRoleAsync(joinrole) .ConfigureAwait(false); } if (gld.JoinChannel != 0 && !string.IsNullOrEmpty(gld.JoinMessage) ) { var channel = arg.Guild .GetTextChannel(gld.JoinChannel); var message = gld.JoinMessage .ReplaceGuildEventMessage( arg, arg.Guild ); await channel .SendMessageAsync(message) .ConfigureAwait(false); } } } catch (Exception ex) { Log.Error("UsrJoin", ex.Message, null, ex); } //Experience Roles try { using SkuldDbContext database = new SkuldDbContextFactory() .CreateDbContext(); var feats = database.Features .Find(arg.Guild.Id); if (feats.Experience) { var rewards = database.LevelRewards .ToList().Where(x => x.GuildId == arg.Guild.Id); var usrLvl = database.UserXp .FirstOrDefault(x => x.GuildId == arg.Guild.Id && x.UserId == arg.Id ); var lvl = DatabaseUtilities.GetLevelFromTotalXP( usrLvl.TotalXP, DiscordUtilities.LevelModifier ); var rolesToGive = rewards .Where(x => x.LevelRequired <= lvl) .Select(z => z.RoleId); if (feats.StackingRoles) { var roles = (arg.Guild as IGuild).Roles .Where(z => rolesToGive.Contains(z.Id)); if (roles.Any()) { try { await arg.AddRolesAsync(roles) .ConfigureAwait(false); } catch (Exception ex) { Log.Error("UsrJoin", ex.Message, null, ex); } } } else { var r = rewards .Where(x => x.LevelRequired <= lvl) .OrderByDescending(x => x.LevelRequired) .FirstOrDefault(); if (r != null) { var role = arg.Guild.Roles.FirstOrDefault(z => rolesToGive.Contains(r.Id) ); if (role != null) { try { await arg.AddRoleAsync(role) .ConfigureAwait(false); } catch (Exception ex) { Log.Error("UsrJoin", ex.Message, null, ex); } } } } } } catch (Exception ex) { Log.Error("UsrJoin", ex.Message, null, ex); } Log.Verbose(Key, $"{arg} joined {arg.Guild}", null); }
public async Task Slots(ulong bet) { if (bet <= 0) { await EmbedExtensions.FromError("Slots", $"Can't bet 0", Context).QueueMessageAsync(Context).ConfigureAwait(false); return; } using var Database = new SkuldDbContextFactory().CreateDbContext(); var user = await Database.InsertOrGetUserAsync(Context.User).ConfigureAwait(false); string MoneyPrefix = SkuldApp.MessageServiceConfig.MoneyIcon; if (!Context.IsPrivate) { var guild = await Database.InsertOrGetGuildAsync(Context.Guild).ConfigureAwait(false); MoneyPrefix = guild.MoneyIcon; } { if (!IsValidBet(bet)) { await EmbedExtensions.FromError("Slots", $"You have not specified a valid bet, minimum is {MoneyPrefix}{MinimumBet.ToFormattedString()}", Context).QueueMessageAsync(Context).ConfigureAwait(false); return; } if (user.Money < bet) { await EmbedExtensions.FromError("Slots", $"You don't have enough money available to make that bet, you have {MoneyPrefix}{user.Money.ToFormattedString()} available", Context).QueueMessageAsync(Context).ConfigureAwait(false); return; } TransactionService.DoTransaction(new TransactionStruct { Amount = bet, Sender = user }).Then(async _ => { await Database.SaveChangesAsync().ConfigureAwait(false); }); } var rows = GetSlotsRows(); var middleRow = rows[1]; var stringRow = GetStringRows(rows); var message = await EmbedExtensions.FromInfo("Slots", "Please Wait, Calculating Wheels", Context).QueueMessageAsync(Context).ConfigureAwait(false); await Task.Delay(500).ConfigureAwait(false); double percentageMod = 0.0d; percentageMod = GetPercentageModifier(percentageMod, middleRow, SlotIcon.Cherry, .5d, 1d); percentageMod = GetPercentageModifier(percentageMod, middleRow, SlotIcon.Lemon, .8d, 1.5d); percentageMod = GetPercentageModifier(percentageMod, middleRow, SlotIcon.Melon, 1d, 2d); percentageMod = GetPercentageModifier(percentageMod, middleRow, SlotIcon.Bell, 1d, 4d); percentageMod = GetPercentageModifier(percentageMod, middleRow, SlotIcon.Crown, 1.2d, 6d); percentageMod = GetPercentageModifier(percentageMod, middleRow, SlotIcon.Diamond, 1.5d, 10d); percentageMod = GetPercentageModifier(percentageMod, middleRow, SlotIcon.Star, 2d, 12d); await Task.Delay(SkuldRandom.Next(50, 300)).ConfigureAwait(false); if (percentageMod == 0.0d) { await message.ModifyAsync(x => { x.Embed = EmbedExtensions.FromMessage( "Slots", $"{stringRow}\n\n" + $"You lost {bet.ToFormattedString()}! " + $"You now have {MoneyPrefix}`{user.Money}`", Color.Red, Context ).Build(); }).ConfigureAwait(false); } else { var amount = (ulong)Math.Round(bet * percentageMod); TransactionService.DoTransaction(new TransactionStruct { Amount = amount, Receiver = user }) .Then(async _ => { await Database.SaveChangesAsync().ConfigureAwait(false); await message.ModifyAsync(x => x.Embed = EmbedExtensions.FromMessage("Slots", $"{stringRow}\n\nYou won {amount.ToFormattedString()}! You now have {MoneyPrefix}`{user.Money}`", Color.Green, Context).Build()).ConfigureAwait(false); }); } }
public async Task RPS(string shoot, ulong bet) { if (bet <= 0) { await EmbedExtensions.FromError("Rock Paper Scissors", $"Can't bet 0", Context).QueueMessageAsync(Context).ConfigureAwait(false); return; } using var Database = new SkuldDbContextFactory().CreateDbContext(); var user = await Database.InsertOrGetUserAsync(Context.User).ConfigureAwait(false); var skuldThrow = rpsWeights.GetRandomWeightedValue().Value; var playerThrow = RockPaperScissorsHelper.FromString(shoot); if (playerThrow != RPSThrow.Invalid) { var result = (WinResult)((playerThrow - skuldThrow + 2) % 3); var throwName = Locale.GetLocale(user.Language).GetString(rps.FirstOrDefault(x => x.Key == skuldThrow).Value); string MoneyPrefix = SkuldApp.MessageServiceConfig.MoneyIcon; if (!Context.IsPrivate) { var guild = await Database.InsertOrGetGuildAsync(Context.Guild).ConfigureAwait(false); MoneyPrefix = guild.MoneyIcon; } { if (!IsValidBet(bet)) { await EmbedExtensions.FromError("Rock Paper Scissors", $"You have not specified a valid bet, minimum is {MoneyPrefix}{MinimumBet.ToFormattedString()}", Context).QueueMessageAsync(Context).ConfigureAwait(false); return; } if (user.Money < bet) { await EmbedExtensions.FromError("Rock Paper Scissors", $"You don't have enough money available to make that bet, you have {MoneyPrefix}{user.Money.ToFormattedString()} available", Context).QueueMessageAsync(Context).ConfigureAwait(false); return; } TransactionService.DoTransaction(new TransactionStruct { Amount = bet, Sender = user }); await Database.SaveChangesAsync().ConfigureAwait(false); } switch (result) { case WinResult.BotWin: { await EmbedExtensions.FromError("Rock Paper Scissors", $"I draw {throwName} and... You lost, you now have {MoneyPrefix}`{user.Money}`", Context).QueueMessageAsync(Context).ConfigureAwait(false); } break; case WinResult.PlayerWin: { TransactionService.DoTransaction(new TransactionStruct { Amount = bet * 2, Receiver = user }); await Database.SaveChangesAsync().ConfigureAwait(false); await EmbedExtensions.FromSuccess("Rock Paper Scissors", $"I draw {throwName} and... You won, you now have {MoneyPrefix}`{user.Money}`", Context).QueueMessageAsync(Context).ConfigureAwait(false); } break; case WinResult.Draw: { TransactionService.DoTransaction(new TransactionStruct { Amount = bet, Receiver = user }); await Database.SaveChangesAsync().ConfigureAwait(false); await EmbedExtensions.FromInfo("Rock Paper Scissors", $"I draw {throwName} and... It's a draw, your money has not been affected", Context).QueueMessageAsync(Context).ConfigureAwait(false); } break; } } else { await EmbedExtensions.FromError("Rock Paper Scissors", $"`{shoot}` is not a valid option", Context) .QueueMessageAsync(Context) .ConfigureAwait(false); } }
public async Task HeadsOrTails(string guess, ulong bet) { using var Database = new SkuldDbContextFactory().CreateDbContext(); var user = await Database.InsertOrGetUserAsync(Context.User).ConfigureAwait(false); string MoneyPrefix = SkuldApp.MessageServiceConfig.MoneyIcon; string Prefix = SkuldApp.MessageServiceConfig.Prefix; if (!Context.IsPrivate) { var guild = await Database.InsertOrGetGuildAsync(Context.Guild).ConfigureAwait(false); MoneyPrefix = guild.MoneyIcon; Prefix = guild.Prefix; } if (!IsValidBet(bet)) { await EmbedExtensions.FromError("Heads Or Tails", $"You have not specified a valid bet, minimum is {MoneyPrefix}{MinimumBet.ToFormattedString()}", Context).QueueMessageAsync(Context).ConfigureAwait(false); return; } if (user.Money < bet) { await EmbedExtensions.FromError("Heads Or Tails", $"You don't have enough money available to make that bet, you have {MoneyPrefix}{user.Money.ToFormattedString()} available", Context).QueueMessageAsync(Context).ConfigureAwait(false); return; } TransactionService.DoTransaction(new TransactionStruct { Amount = bet, Sender = user }); await Database.SaveChangesAsync().ConfigureAwait(false); var result = SkuldRandom.Next(0, coinflip.Count); var loweredGuess = guess.ToLowerInvariant(); switch (loweredGuess) { case "heads": case "head": case "h": case "tails": case "tail": case "t": { bool playerguess = loweredGuess == "heads" || loweredGuess == "head"; var res = (coinflip.Keys.ElementAt(result), coinflip.Values.ElementAt(result)); bool didWin = false; if (result == 0 && playerguess) { didWin = true; } else if (result == 1 && !playerguess) { didWin = true; } string suffix; if (didWin) { TransactionService.DoTransaction(new TransactionStruct { Amount = bet * 2, Receiver = user }); } if (didWin) { suffix = $"You Won! <:blobsquish:350681075296501760> Your money is now {MoneyPrefix}`{user.Money.ToFormattedString()}`"; } else { suffix = $"You Lost! <:blobcrying:662304318531305492> Your money is now {MoneyPrefix}`{user.Money.ToFormattedString()}`"; } await Database.SaveChangesAsync().ConfigureAwait(false); await EmbedExtensions .FromImage(res.Item2, didWin?Color.Green : Color.Red, Context) .WithTitle("Heads Or Tails") .WithDescription($"Result are: {Locale.GetLocale(user.Language).GetString(res.Item1)} {suffix}") .QueueMessageAsync(Context) .ConfigureAwait(false); } break; default: await EmbedExtensions.FromError("Heads Or Tails", $"Incorrect guess value. Try; `{Prefix}flip heads`", Context).QueueMessageAsync(Context).ConfigureAwait(false); return; } }
internal static async Task CommandService_CommandExecuted( Optional <CommandInfo> arg1, ICommandContext arg2, IResult arg3 ) { CommandInfo cmd = null; string name = ""; if (arg1.IsSpecified) { cmd = arg1.Value; name = cmd.Module.GetModulePath(); if (cmd.Name != null) { name += "." + cmd.Name; } name = name .ToLowerInvariant() .Replace(" ", "-") .Replace("/", "."); } if (arg3.IsSuccess) { using var Database = new SkuldDbContextFactory().CreateDbContext(); if (arg1.IsSpecified) { var cont = arg2 as ShardedCommandContext; DogStatsd.Increment("commands.total.threads", 1, 1, new[] { $"module:{cmd.Module.Name.ToLowerInvariant()}", $"cmd:{name}" }); DogStatsd.Histogram("commands.latency", watch.ElapsedMilliseconds, 0.5, new[] { $"module:{cmd.Module.Name.ToLowerInvariant()}", $"cmd:{name}" }); var usr = await Database.InsertOrGetUserAsync(cont.User).ConfigureAwait(false); await InsertCommandAsync(cmd, usr).ConfigureAwait(false); DogStatsd.Increment("commands.processed", 1, 1, new[] { $"module:{cmd.Module.Name.ToLowerInvariant()}", $"cmd:{name}" }); } } else { bool displayerror = true; if (arg3.ErrorReason.Contains("few parameters")) { var prefix = MessageTools.GetPrefixFromCommand(arg2.Message.Content, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix); string cmdName = ""; if (arg2.Guild != null) { using var Database = new SkuldDbContextFactory().CreateDbContext(); prefix = MessageTools.GetPrefixFromCommand(arg2.Message.Content, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix, (await Database.InsertOrGetGuildAsync(arg2.Guild).ConfigureAwait(false)).Prefix ); } if (cmd != null && cmd.Module.Group != null) { string pfx = ""; ModuleInfo mod = cmd.Module; while (mod.Group != null) { pfx += $"{mod.Group} "; if (mod.IsSubmodule) { mod = cmd.Module.Parent; } } cmdName = $"{pfx}{cmd.Name}"; var cmdembed = await SkuldApp.CommandService.GetCommandHelpAsync(arg2, cmdName, prefix).ConfigureAwait(false); await arg2.Channel.SendMessageAsync( "You seem to be missing a parameter or 2, here's the help", embed : cmdembed.Build() ) .ConfigureAwait(false); displayerror = false; } } if (arg3.ErrorReason.Contains("Timeout")) { var hourglass = new Emoji("⏳"); if (!arg2.Message.Reactions.Any(x => x.Key == hourglass && x.Value.IsMe)) { await arg2.Message.AddReactionAsync(hourglass).ConfigureAwait(false); } displayerror = false; } if (arg3.Error != CommandError.UnknownCommand && displayerror) { Log.Error(Key, "Error with command, Error is: " + arg3, arg2 as ShardedCommandContext); await EmbedExtensions.FromError(arg3.ErrorReason, arg2) .QueueMessageAsync(arg2) .ConfigureAwait(false); } switch (arg3.Error) { case CommandError.UnmetPrecondition: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:unm-precon", $"mod:{cmd.Module.Name}", $"cmd:{name}" } ); break; case CommandError.Unsuccessful: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:generic", $"mod:{cmd.Module.Name}", $"cmd:{name}" } ); break; case CommandError.MultipleMatches: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:multiple", $"mod:{cmd.Module.Name}", $"cmd:{name}" } ); break; case CommandError.BadArgCount: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:incorr-args", $"mod:{cmd.Module.Name}", $"cmd:{name}" } ); break; case CommandError.ParseFailed: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:parse-fail", $"mod:{cmd.Module.Name}", $"cmd:{name}" } ); break; case CommandError.Exception: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:exception", $"mod:{cmd.Module.Name}", $"cmd:{name}" } ); break; case CommandError.UnknownCommand: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:unk-cmd" } ); break; } } watch = new Stopwatch(); try { var message = arg2.Message as SocketUserMessage; using var Database = new SkuldDbContextFactory().CreateDbContext(); User suser = await Database.InsertOrGetUserAsync(message.Author).ConfigureAwait(false); { var keys = Database.DonatorKeys.AsAsyncEnumerable().Where(x => x.Redeemer == suser.Id); if (await keys.AnyAsync().ConfigureAwait(false)) { bool hasChanged = false; var current = DateTime.Now; await keys.ForEachAsync(x => { if (current > x.RedeemedWhen.FromEpoch().AddDays(365)) { Database.DonatorKeys.Remove(x); hasChanged = true; } }).ConfigureAwait(false); if (hasChanged) { if (!await Database.DonatorKeys.AsAsyncEnumerable().Where(x => x.Redeemer == suser.Id).AnyAsync().ConfigureAwait(false)) { suser.Flags -= DiscordUtilities.BotDonator; } await Database.SaveChangesAsync().ConfigureAwait(false); } } } if (!suser.IsUpToDate(message.Author as SocketUser)) { suser.AvatarUrl = message.Author.GetAvatarUrl() ?? message.Author.GetDefaultAvatarUrl(); suser.Username = message.Author.Username; await Database.SaveChangesAsync().ConfigureAwait(false); } } catch (Exception ex) { Log.Error(Key, ex.Message, arg2, ex); } }
public async Task PlaceImage(int x, int y, [Remainder] string image) { if (x <= 0 || y <= 0) { await EmbedExtensions.FromError("I can't process coordinates below 0", Context).QueueMessageAsync(Context); return; } if (x > SkuldAppContext.PLACEIMAGESIZE || y > SkuldAppContext.PLACEIMAGESIZE) { await EmbedExtensions.FromError("I can't process coordinates above " + SkuldAppContext.PLACEIMAGESIZE, Context).QueueMessageAsync(Context); return; } if (!image.IsWebsite() && !image.IsImageExtension()) { await EmbedExtensions.FromError("You haven't provided an image link", Context).QueueMessageAsync(Context); return; } Bitmap bitmapImage; try { bitmapImage = new(await HttpWebClient.GetStreamAsync(new Uri(image))); } catch (Exception ex) { await EmbedExtensions.FromError("Couldn't process image", Context).QueueMessageAsync(Context); Log.Error("ThePlace", ex.Message, Context, ex); return; } if (bitmapImage is null) { await EmbedExtensions.FromError("Couldn't process image", Context).QueueMessageAsync(Context); Log.Error("ThePlace", "Couldn't load image", Context); return; } double aspectRatio = (double)bitmapImage.Width / bitmapImage.Height; if (bitmapImage.Width > SkuldAppContext.PLACEIMAGESIZE - x) { double otherAspect = (double)bitmapImage.Height / bitmapImage.Width; int newHeight = (int)Math.Min(Math.Round(SkuldAppContext.PLACEIMAGESIZE - y * otherAspect), SkuldAppContext.PLACEIMAGESIZE - y); bitmapImage = bitmapImage.ResizeBitmap(SkuldAppContext.PLACEIMAGESIZE - x, newHeight); } if (bitmapImage.Height > SkuldAppContext.PLACEIMAGESIZE - y) { int newWidth = (int)Math.Min(Math.Round(SkuldAppContext.PLACEIMAGESIZE - x * aspectRatio), SkuldAppContext.PLACEIMAGESIZE - x); bitmapImage = bitmapImage.ResizeBitmap(newWidth, SkuldAppContext.PLACEIMAGESIZE - y); } ulong pixelCost = 0; for (int bx = 0; bx < bitmapImage.Width; bx++) { for (int by = 0; by < bitmapImage.Height; by++) { pixelCost += GetPixelCost(x + bx, y + by); } } using var Database = new SkuldDbContextFactory().CreateDbContext(); string prefix = (await Database.InsertOrGetConfigAsync(SkuldAppContext.ConfigurationId)).Prefix; if (!Context.IsPrivate) { prefix = (await Database.InsertOrGetGuildAsync(Context.Guild)).Prefix; } var dbUser = await Database.InsertOrGetUserAsync(Context.User).ConfigureAwait(false); TransactionService.DoTransaction(new TransactionStruct { Sender = dbUser, Amount = pixelCost }) .IsSuccessAsync(async _ => { using var PixelDb = new SkuldDbContextFactory().CreateDbContext(); using var historyDb = new SkuldDbContextFactory().CreateDbContext(); for (int bx = 0; bx < bitmapImage.Width; bx++) { for (int by = 0; by < bitmapImage.Height; by++) { var pixel = PixelDb.PlacePixelData.FirstOrDefault(p => p.XPos == x + bx && p.YPos == y + by); var colour = bitmapImage.GetPixel(bx, by); pixel.R = colour.R; pixel.G = colour.G; pixel.B = colour.B; historyDb.PlacePixelHistory.Add(new PixelHistory { PixelId = pixel.Id, ChangedTimestamp = DateTime.UtcNow.ToEpoch(), CostToChange = pixelCost, ModifierId = Context.User.Id }); } } await PixelDb.SaveChangesAsync().ConfigureAwait(false); await historyDb.SaveChangesAsync().ConfigureAwait(false); await $"Set it, use `{prefix}theplace view` to view it".QueueMessageAsync(Context).ConfigureAwait(false); }) .IsErrorAsync(async _ => { await "You don't have enough currency".QueueMessageAsync(Context).ConfigureAwait(false); }); await Database.SaveChangesAsync().ConfigureAwait(false); }
private static async Task HandleMessageAsync(SocketMessage arg) { DogStatsd.Increment("messages.recieved"); if (arg.Author.IsBot || arg.Author.IsWebhook || arg.Author.DiscriminatorValue == 0 || arg is not SocketUserMessage message) { return; } ShardedCommandContext context = new ShardedCommandContext( SkuldApp.DiscordClient, message ); Guild sguild = null; if (context.Guild != null) { if (!await CheckPermissionToSendMessageAsync(context.Channel as ITextChannel).ConfigureAwait(false)) { return; } if (SkuldApp.DiscordClient.GetShardFor(context.Guild).ConnectionState != ConnectionState.Connected) { return; } _ = Task.Run(() => HandleSideTasksAsync(context)); var guser = await (context.Guild as IGuild).GetCurrentUserAsync() .ConfigureAwait(false); var guildMem = await (context.Guild as IGuild).GetUserAsync(message.Author.Id) .ConfigureAwait(false); if (!guser.GetPermissions(context.Channel as IGuildChannel).SendMessages) { return; } if (!MessageTools.IsEnabledChannel(guildMem, context.Channel as ITextChannel)) { return; } using var Database = new SkuldDbContextFactory().CreateDbContext(); sguild = await Database.InsertOrGetGuildAsync(context.Guild) .ConfigureAwait(false); if (sguild.Name != context.Guild.Name) { sguild.Name = context.Guild.Name; await Database.SaveChangesAsync().ConfigureAwait(false); } if (sguild.IconUrl != context.Guild.IconUrl) { sguild.IconUrl = context.Guild.IconUrl; await Database.SaveChangesAsync().ConfigureAwait(false); } } if (!MessageTools.HasPrefix(message, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix, sguild?.Prefix)) { return; } try { using var Database = new SkuldDbContextFactory().CreateDbContext(); User suser = await Database.InsertOrGetUserAsync(message.Author).ConfigureAwait(false); if (suser != null && suser.Flags.IsBitSet(DiscordUtilities.Banned) && (!suser.Flags.IsBitSet(DiscordUtilities.BotCreator) || !suser.Flags.IsBitSet(DiscordUtilities.BotAdmin)) ) { return; } var prefix = MessageTools.GetPrefixFromCommand(context.Message.Content, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix, sguild?.Prefix); if (prefix != null) { await DispatchCommandAsync(context, prefix).ConfigureAwait(false); } } catch (Exception ex) { Log.Critical(Key, ex.Message, context, ex); } }
public async Task Daily(IGuildUser user = null) { if (user is not null && (user.IsBot || user.IsWebhook)) { await EmbedExtensions.FromError(DiscordUtilities.NoBotsString, Context).QueueMessageAsync(Context).ConfigureAwait(false); return; } using var Database = new SkuldDbContextFactory().CreateDbContext(); var self = await Database.InsertOrGetUserAsync(Context.User).ConfigureAwait(false); var gld = await Database.InsertOrGetGuildAsync(Context.Guild).ConfigureAwait(false); User target = null; if (user is not null) { target = await Database.InsertOrGetUserAsync(user).ConfigureAwait(false); } var previousAmount = user is null ? self.Money : target.Money; if (self.IsStreakReset(Configuration)) { self.Streak = 0; } ulong MoneyAmount = self.GetDailyAmount(Configuration); if ((user is null ? self : target).ProcessDaily(MoneyAmount, self)) { string desc = $"You just got your daily of {gld.MoneyIcon}{MoneyAmount}"; if (target is not null) { desc = $"You just gave your daily of {gld.MoneyIcon}{MoneyAmount.ToFormattedString()} to {user.Mention}"; } var newAmount = user is null ? self.Money : target.Money; var embed = EmbedExtensions .FromMessage("SkuldBank - Daily", desc, Context ) .AddInlineField( "Previous Amount", $"{gld.MoneyIcon}{previousAmount.ToFormattedString()}" ) .AddInlineField( "New Amount", $"{gld.MoneyIcon}{newAmount.ToFormattedString()}" ); if (self.Streak > 0) { embed.AddField( "Streak", $"You're on a streak of {self.Streak} days!!" ); } self.Streak = self.Streak.Add(1); if (self.MaxStreak < self.Streak) { self.MaxStreak = self.Streak; } await embed.QueueMessageAsync(Context).ConfigureAwait(false); }
public async Task PlacePixel(int x, int y, [Remainder] Color colour) { if (x <= 0 || y <= 0) { await EmbedExtensions.FromError("I can't process coordinates below 0", Context).QueueMessageAsync(Context); return; } if (x > SkuldAppContext.PLACEIMAGESIZE || y > SkuldAppContext.PLACEIMAGESIZE) { await EmbedExtensions.FromError("I can't process coordinates above " + SkuldAppContext.PLACEIMAGESIZE, Context).QueueMessageAsync(Context); return; } ulong pixelCost = GetPixelCost(x, y); using var Database = new SkuldDbContextFactory().CreateDbContext(); string prefix = (await Database.InsertOrGetConfigAsync(SkuldAppContext.ConfigurationId)).Prefix; if (!Context.IsPrivate) { prefix = (await Database.InsertOrGetGuildAsync(Context.Guild)).Prefix; } var dbUser = await Database.InsertOrGetUserAsync(Context.User).ConfigureAwait(false); TransactionService.DoTransaction(new TransactionStruct { Sender = dbUser, Amount = pixelCost }) .IsSuccessAsync(async _ => { using var db = new SkuldDbContextFactory().CreateDbContext(); var pixel = db.PlacePixelData.FirstOrDefault(p => p.XPos == x && p.YPos == y); pixel.R = colour.R; pixel.G = colour.G; pixel.B = colour.B; await db.SaveChangesAsync().ConfigureAwait(false); db.PlacePixelHistory.Add(new PixelHistory { PixelId = pixel.Id, ChangedTimestamp = DateTime.UtcNow.ToEpoch(), CostToChange = pixelCost, ModifierId = Context.User.Id }); await db.SaveChangesAsync().ConfigureAwait(false); await $"Set it, use `{prefix}theplace view` to view it".QueueMessageAsync(Context).ConfigureAwait(false); }) .IsErrorAsync(async _ => { await "You don't have enough currency".QueueMessageAsync(Context).ConfigureAwait(false); }); await Database.SaveChangesAsync().ConfigureAwait(false); }
private static async Task HandleMessageAsync(SocketMessage arg) { DogStatsd.Increment("messages.recieved"); if (arg.Author.IsBot || arg.Author.IsWebhook || arg.Author.DiscriminatorValue == 0 || arg is not SocketUserMessage message) { return; } using var Database = new SkuldDbContextFactory().CreateDbContext(); User suser = await Database.InsertOrGetUserAsync(arg.Author).ConfigureAwait(false); if (suser is not null && suser.Flags.IsBitSet(DiscordUtilities.Banned) && (!suser.Flags.IsBitSet(DiscordUtilities.BotCreator) || !suser.Flags.IsBitSet(DiscordUtilities.BotAdmin)) ) { return; } ShardedCommandContext context = new(SkuldApp.DiscordClient, message); Guild sguild = null; if (context.Guild is not null) { if (SkuldApp.DiscordClient.GetShardFor(context.Guild).ConnectionState != ConnectionState.Connected) { return; } if (!await CheckPermissionToSendMessageAsync(context.Channel as ITextChannel).ConfigureAwait(false)) { return; } var guser = await (context.Guild as IGuild).GetCurrentUserAsync() .ConfigureAwait(false); if (!guser.GetPermissions(context.Channel as IGuildChannel).SendMessages) { return; } var guildMem = await (context.Guild as IGuild).GetUserAsync(message.Author.Id) .ConfigureAwait(false); if (!MessageTools.IsEnabledChannel(guildMem, context.Channel as ITextChannel)) { return; } sguild = await Database.InsertOrGetGuildAsync(context.Guild) .ConfigureAwait(false); if (sguild.Name != context.Guild.Name) { sguild.Name = context.Guild.Name; await Database.SaveChangesAsync().ConfigureAwait(false); } if (sguild.IconUrl != context.Guild.IconUrl) { sguild.IconUrl = context.Guild.IconUrl; await Database.SaveChangesAsync().ConfigureAwait(false); } } if (!MessageTools.HasPrefix(message, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix, sguild?.Prefix)) { return; } await Task.WhenAll(ProcessCommandAsync(context, sguild), ProcessExperienceAsync(context, sguild), ProcessCustomCommand(context, sguild)); }
public async Task Help([Remainder] string command = null) { using var Database = new SkuldDbContextFactory().CreateDbContext(); if (command.IsNullOrWhiteSpace()) { string title = $"Commands of: {Context.Client.CurrentUser} that can be invoked in: "; title += Context.IsPrivate ? $"DMS/{Context.User.Username}#{Context.User.Discriminator}" : $"{Context.Guild.Name}/{Context.Channel.Name}"; var embed = new EmbedBuilder() .AddAuthor(Context.Client) .WithRandomColor() .WithDescription($"The prefix of **{(Context.Guild is null ? Context.User.FullName() : Context.Guild.Name)}** is: `{(Context.Guild is null ? Configuration.Prefix : (await Database.InsertOrGetGuildAsync(Context.Guild).ConfigureAwait(false)).Prefix)}`"); Dictionary <ModuleInfo, string> helpBuilder = new(); foreach (var module in SkuldApp.CommandService.Modules) { if (helpBuilder.ContainsKey(module)) { continue; } if (module.IsSubmodule) { continue; } string desc = module.Remarks ?? "ERR"; if (module.Submodules.Count != 0) { string suffix = "submodule"; if (module.Submodules.Count >= 2) { suffix += "s"; } embed.AddInlineField(module.GetModulePath(), $"{desc} - {module.Submodules.Count} {suffix}"); } else { embed.AddInlineField(module.GetModulePath(), desc); } } if (Context.Guild is not null) { if (await Database.InsertOrGetGuildAsync(Context.Guild).ConfigureAwait(false) is not null) { var commands = Database.CustomCommands.ToList().Where(x => x.GuildId == Context.Guild.Id); if (commands.Any()) { string commandsText = ""; foreach (var cmd in commands) { commandsText += $"{cmd.Name}, "; } commandsText = commandsText[0..^ 2];
public async Task Help([Remainder] string command = null) { using var Database = new SkuldDbContextFactory().CreateDbContext(); try { if (command == null) { string title = $"Commands of: {Context.Client.CurrentUser} that can be invoked in: "; if (Context.IsPrivate) { title += $"DMS/{Context.User.Username}#{Context.User.Discriminator}"; } else { title += $"{Context.Guild.Name}/{Context.Channel.Name}"; } var embed = new EmbedBuilder() .AddAuthor(Context.Client) .WithRandomColor() .WithDescription($"The prefix of **{(Context.Guild == null ? Context.User.FullName() : Context.Guild.Name)}** is: `{(Context.Guild == null ? Configuration.Prefix : (await Database.InsertOrGetGuildAsync(Context.Guild).ConfigureAwait(false)).Prefix)}`"); Dictionary <ModuleInfo, string> helpBuilder = new Dictionary <ModuleInfo, string>(); foreach (var module in SkuldApp.CommandService.Modules) { var commands = await module.GetExecutableCommandsAsync(Context, SkuldApp.Services).ConfigureAwait(false); if (commands.Any()) { if (module.IsSubmodule) { var topParent = module.GetTopLevelParent(); if (helpBuilder.ContainsKey(topParent)) { var value = helpBuilder.GetValueOrDefault(topParent); List <string> desc = new List <string>(); foreach (var cmd in commands) { if (cmd.Module == module) { desc.Add(cmd.Aliases[0]); } } string description = $"\n\nSubmodule: **{module.Name}**\n`"; foreach (var str in desc.Distinct()) { description += str + ", "; } description += "`"; value += description.ReplaceLast(", ", ""); helpBuilder.Remove(topParent); helpBuilder.Add(topParent, value); } } else { string desc = ""; foreach (var cmd in commands) { if (cmd.Module == module) { desc += $"{cmd.Aliases[0]}, "; } } string description = "`"; foreach (var str in desc.Split(' ').Distinct()) { description += str + " "; } description += "`"; description = description.ReplaceLast(", ", ""); helpBuilder.Add(module, description); } } } foreach (var helpSection in helpBuilder) { if (!string.IsNullOrWhiteSpace(helpSection.Value)) { var section = helpSection.Value.ReplaceLast(", ", ""); embed.AddField(helpSection.Key.Name, section); } } if (Context.Guild != null) { if (await Database.InsertOrGetGuildAsync(Context.Guild).ConfigureAwait(false) != null) { var commands = Database.CustomCommands.ToList().Where(x => x.GuildId == Context.Guild.Id); if (commands.Any()) { string commandsText = ""; foreach (var cmd in commands) { commandsText += $"{cmd.Name}, "; } commandsText = commandsText[0..^ 2];