public static LocalEmbedBuilder CreateEmbed(EmbedType type, string title, string content = null) { LocalEmbedBuilder embed = new LocalEmbedBuilder(); switch (type) { case EmbedType.Info: embed.WithAuthor(title); embed.WithColor(new Color(67, 181, 129)); break; case EmbedType.Success: embed.WithAuthor(title, "https://i.imgur.com/XnVa7ta.png"); embed.WithColor(new Color(67, 181, 129)); break; case EmbedType.Failure: embed.WithAuthor(title, "https://i.imgur.com/Sg4663k.png"); embed.WithColor(new Color(67, 181, 129)); break; } embed.WithDescription(content); return(embed); }
public static bool TryParseEmbed(string json, out LocalEmbedBuilder embed) { embed = new LocalEmbedBuilder(); try { var embedDeserialized = JsonConvert.DeserializeObject <JsonEmbed>(json); var author = embedDeserialized.Author; var title = embedDeserialized.Title; var description = embedDeserialized.Description; var colorString = embedDeserialized.Color; var thumbnail = embedDeserialized.Thumbnail; var image = embedDeserialized.Image; var fields = embedDeserialized.Fields; var footer = embedDeserialized.Footer; var timestamp = embedDeserialized.Timestamp; if (author != null) { embed.WithAuthor(author); } if (!string.IsNullOrEmpty(title)) { embed.WithTitle(title); } if (!string.IsNullOrEmpty(description)) { embed.WithDescription(description); } if (!string.IsNullOrEmpty(colorString)) { embed.WithColor(HexToInt(colorString) ?? 0xFFFFFF); } if (!string.IsNullOrEmpty(thumbnail)) { embed.WithThumbnailUrl(thumbnail); } if (!string.IsNullOrEmpty(image)) { embed.WithImageUrl(image); } if (fields != null) { foreach (var field in fields) { var fieldName = field.Name; var fieldValue = field.Value; var fieldInline = field.IsInline; if (!string.IsNullOrEmpty(fieldName) && !string.IsNullOrEmpty(fieldValue)) { embed.AddField(fieldName, fieldValue, fieldInline); } } } if (footer != null) { embed.WithFooter(footer); } if (timestamp.HasValue) { embed.WithTimestamp(timestamp.Value); } else if (embedDeserialized.WithCurrentTimestamp) { embed.WithCurrentTimestamp(); } return(true); } catch { return(false); } }
private async Task HandleReceivedMessageAsync(CachedUserMessage message) { if (message.Author.IsBot) { return; } if (!(message.Channel is CachedTextChannel textChannel) || !textChannel.Guild.CurrentMember.GetPermissionsFor(textChannel).Has(Permission.SendMessages)) { return; } if (CommandUtilities.HasAnyPrefix(message.Content, Prefixes, StringComparison.CurrentCulture, out var prefix, out var output) || CommandUtilities.HasPrefix(message.Content, $"<@{Client.CurrentUser.Id}>", StringComparison.Ordinal, out output) || CommandUtilities.HasPrefix(message.Content, $"<@!{Client.CurrentUser.Id}>", StringComparison.Ordinal, out output)) { if (string.IsNullOrWhiteSpace(output)) { return; } try { if (prefix is null) { prefix = Client.CurrentUser.Mention; } var ctx = MummyContext.Create(Client, message, Services, prefix); ActiveTimings[ctx.UserId] = Stopwatch.StartNew(); var r = await Commands.ExecuteAsync(output, ctx); ActiveTimings[ctx.UserId].Stop(); ActiveTimings.Remove(ctx.UserId, out var st); if (r.IsSuccessful) { LogService.LogInformation($"command: {ctx.Command.Name} has successful finished execution in {st.ElapsedMilliseconds}ms.", LogSource.MessagesService, ctx.GuildId); } else { switch (r) { case ExecutionFailedResult executionfailed: { LogService.LogError(executionfailed.ToString(), LogSource.MessagesService, ctx.GuildId, executionfailed.Exception); break; } case ArgumentParseFailedResult parsefailed: { var eb = new LocalEmbedBuilder(); eb.WithAuthor(ctx.User); eb.WithTitle("ArmgumentParseFailure"); eb.AddField(parsefailed.Reason, parsefailed.RawArguments); await ctx.Channel.SendMessageAsync(embed : eb.Build()); break; } case ArgumentParserResult parseresult: { LogService.LogCritical("dunno argumentparse", LogSource.MessagesService, ctx.GuildId); await ctx.Channel.SendMessageAsync("a error has occoured and not been handled this error will be resolved shortly. (hopefully)"); break; } case ChecksFailedResult checksfailed: { var eb = new LocalEmbedBuilder(); eb.WithAuthor(ctx.User); eb.WithTitle($"{checksfailed.FailedChecks.Count} checks have failed"); foreach (var(Check, Result) in checksfailed.FailedChecks) { eb.AddField((Check as MummyCheckBase).Name, Result.Reason, true); } await ctx.Channel.SendMessageAsync(embed : eb.Build()); break; } case CommandDisabledResult disabled: { var eb = new LocalEmbedBuilder(); eb.WithAuthor(ctx.User); await ctx.Channel.SendMessageAsync(); eb.WithTitle($"{disabled.Command} is currently diabled"); await ctx.Channel.SendMessageAsync(embed : eb.Build()); break; } case CommandNotFoundResult notfound: { var eb = new LocalEmbedBuilder(); eb.WithAuthor(ctx.User); eb.WithTitle($"command with name {notfound.Reason}"); await ctx.Channel.SendMessageAsync(embed : eb.Build()); break; } case CommandOnCooldownResult oncooldown: { var eb = new LocalEmbedBuilder(); eb.WithAuthor(ctx.User); eb.WithTitle($"{oncooldown.Command.Name} is currently on cooldown"); foreach (var(cooldown, retryafter) in oncooldown.Cooldowns) { int index = cooldown.ToString().LastIndexOf('.'); var bucketype = (CooldownBucketType)cooldown.BucketType; eb.AddField(cooldown.ToString().Substring(index + 1), $"is only allowed to be run {cooldown.Amount} per {cooldown.Per.Humanize()} and it forced per {bucketype}, currently locked for {retryafter.TotalMinutes} Minutes", true); } await ctx.Channel.SendMessageAsync(embed : eb.Build()); break; } case OverloadsFailedResult overloadsFailed: { var eb = new LocalEmbedBuilder(); eb.WithAuthor(ctx.User); eb.WithDescription(overloadsFailed.Reason); foreach (var(command, overloadResult) in overloadsFailed.FailedOverloads) { eb.AddField($"{command.Name} {string.Join(' ', command.Parameters.Select(x => x.Name))}", overloadResult.Reason); } await ctx.Channel.SendMessageAsync(embed : eb.Build()); break; } case ParameterChecksFailedResult paramcheckfailed: { var eb = new LocalEmbedBuilder(); eb.WithAuthor(ctx.User); eb.WithTitle($"checks on {paramcheckfailed.Parameter} have failed with provided argument: {paramcheckfailed.Argument}"); foreach (var(Check, Result) in paramcheckfailed.FailedChecks) { var index = Check.ToString().LastIndexOf('.'); var name = Check.ToString().Substring(index + 1); eb.AddField(name, Result.Reason); } break; } case TypeParseFailedResult typeParseFailed: { var eb = new LocalEmbedBuilder(); eb.WithAuthor(ctx.User); eb.AddField(typeParseFailed.Parameter.Name, typeParseFailed.Reason); await ctx.Channel.SendMessageAsync(embed : eb.Build()); break; } } } } catch (Exception ex) { LogService.LogError("Issue with message service", LogSource.MessagesService, exception: ex); } } }
public LocalizedEmbedBuilder WithAuthor(LocalizedAuthorBuilder builder) { _builder.WithAuthor(builder); return(this); }
public async Task HelpAsync() { var options = PaginatedAppearanceOptions.Default; var msg = new PaginatedMessage { Options = options }; var infoemb = new LocalEmbedBuilder(); infoemb.AddField("optional", $"*value*", true); infoemb.AddField("remainder", "__value__", true); infoemb.AddField("required", "**value**", true); infoemb.WithDescription("for example: !command __*cookies this can contain spaces*__ this means the command take a optional remainder"); msg.Pages.Add(infoemb); foreach (var module in Commands.GetAllModules()) { try { var modulecheck = await module.RunChecksAsync(Context); if (modulecheck.IsSuccessful) { if (module.Commands.Count == 0) { continue; //skip module if commands are 0 } if (module.Parent != null) { continue; } var emb = new LocalEmbedBuilder(); emb.WithTitle(module.Name); emb.WithAuthor(Context.User.DisplayName, Context.User.GetAvatarUrl()); var sb = new StringBuilder(); var commands = CommandUtilities.EnumerateAllCommands(module); foreach (var command in commands) { var checks = await command.RunChecksAsync(Context); if (checks.IsSuccessful) { sb.Append(Context.PrefixUsed).Append(command.Name).Append(" "); foreach (var parameter in command.Parameters) { if (parameter.IsOptional && parameter.IsRemainder)//optional remiander { sb.Append($"__*{parameter.Name}*__ "); } else if (parameter.IsOptional && !parameter.IsRemainder)//optional { sb.Append($"*{parameter.Name}* "); } else if (!parameter.IsOptional && parameter.IsRemainder) //required remainder { sb.Append($"__**{parameter.Name}**__ "); } else if (!parameter.IsOptional && !parameter.IsRemainder)//required { sb.Append($"**{parameter.Name}** "); } } sb.AppendLine(); } } emb.WithDescription(sb.ToString()); msg.Pages.Add(emb); } } catch (Exception ex) { Console.WriteLine(ex); } } await new PaginatedMessageCallback(Iservice, Context, msg, new EnsureReactionFromUser(Context.User)).DisplayAsync(); }
public async Task StatsAsync() { var embed = new LocalEmbedBuilder(); embed.WithAuthor( x => { x.IconUrl = Context.Bot.CurrentUser.GetAvatarUrl(); x.Name = Context.Bot.CurrentUser.Name; }); int bots = Context.Bot.Guilds .Sum(x => x.Value.Members.Count(z => z.Value?.IsBot == true)); int humans = Context.Bot.Guilds .Sum(x => x.Value.Members.Count(z => z.Value?.IsBot == false)); int presentUsers = Context.Bot.Guilds .Sum(x => x.Value.Members.Count(u => u.Value?.Presence?.Status != UserStatus.Offline)); embed.AddField( "Members", $"Bot: {bots}\n" + $"Human: {humans}\n" + $"Present: {presentUsers}", true); int online = Context.Bot.Guilds .Sum(x => x.Value.Members.Count(z => z.Value?.Presence?.Status == UserStatus.Online)); int afk = Context.Bot.Guilds .Sum(x => x.Value.Members.Count(z => z.Value?.Presence?.Status == UserStatus.Idle)); int dnd = Context.Bot.Guilds .Sum(x => x.Value.Members.Count(z => z.Value?.Presence?.Status == UserStatus.DoNotDisturb)); embed.AddField( "Members", $"Online: {online}\n" + $"AFK: {afk}\n" + $"DND: {dnd}", true); embed.AddField( "Channels", $"Text: {Context.Bot.Guilds.Sum(x => x.Value.TextChannels.Count)}\n" + $"Voice: {Context.Bot.Guilds.Sum(x => x.Value.VoiceChannels.Count)}\n" + $"Total: {Context.Bot.Guilds.Sum(x => x.Value.Channels.Count)}", true); embed.AddField( "Guilds", $"Count: {Context.Bot.Guilds.Count}\n" + $"Total Users: {Context.Bot.Guilds.Sum(x => x.Value.MemberCount)}\n" + $"Total Cached: {Context.Bot.Guilds.Sum(x => x.Value.Members.Count)}\n", true); embed.AddField( "Commands", $"Commands: {CmdService.GetAllCommands().Count()}\n" + $"Aliases: {CmdService.GetAllCommands().Sum(x => x.Aliases.Count)}\n" + $"Modules: {CmdService.GetAllModules().Count()}", true); embed.AddField( ":hammer_pick:", $"Heap: {Math.Round(GC.GetTotalMemory(true) / (1024.0 * 1024.0), 2)} MB\n" + $"Up: {(DateTime.Now - Process.GetCurrentProcess().StartTime).ToString(@"dd\D\ hh\H\ mm\M\ ss\S")}", true); embed.AddField(":beginner:", $"Written by: [PassiveModding](https://github.com/PassiveModding)", true); await ReplyAsync("", false, embed.Build()); }