public async Task View([Summary("id", "the id of the case")] long caseId, [Summary("guildid", "the id of the guild")] string guildId = "") { ulong parsedGuildId = 0; if (Context.Guild == null) { if (!ulong.TryParse(guildId, out parsedGuildId)) { await Context.Interaction.RespondAsync(Translator.T().CmdViewInvalidGuildId()); return; } } else if (string.IsNullOrEmpty(guildId)) { parsedGuildId = Context.Guild.Id; } else { try { parsedGuildId = ulong.Parse(guildId); } catch (Exception) { await Context.Interaction.RespondAsync(Translator.T().CmdViewInvalidGuildId()); return; } } await Context.Interaction.RespondAsync("Getting modcases..."); ModCase modCase; try { modCase = await ModCaseRepository.CreateDefault(ServiceProvider, CurrentIdentity).GetModCase(parsedGuildId, (int)caseId); } catch (ResourceNotFoundException) { await Context.Interaction.ModifyOriginalResponseAsync(message => message.Content = Translator.T().NotFound()); return; } if (!await CurrentIdentity.IsAllowedTo(APIActionPermission.View, modCase)) { await Context.Interaction.ModifyOriginalResponseAsync(message => message.Content = Translator.T().CmdViewNotAllowedToView()); return; } EmbedBuilder embed = new(); embed.WithUrl($"{Config.GetBaseUrl()}/guilds/{modCase.GuildId}/cases/{modCase.CaseId}"); embed.WithTimestamp(modCase.CreatedAt); embed.WithColor(Color.Blue); IUser suspect = await DiscordAPI.FetchUserInfo(modCase.UserId, CacheBehavior.Default); if (suspect != null) { embed.WithThumbnailUrl(suspect.GetAvatarOrDefaultUrl()); } embed.WithTitle($"#{modCase.CaseId} {modCase.Title.Truncate(200)}"); embed.WithDescription(modCase.Description.Truncate(2000)); embed.AddField($"{SCALES_EMOTE} - {Translator.T().Punishment()}", Translator.T().Enum(modCase.PunishmentType), true); if (modCase.PunishedUntil != null) { embed.AddField($"{ALARM_CLOCK} - {Translator.T().PunishmentUntil()}", modCase.PunishedUntil.Value.ToDiscordTS(), true); } if (modCase.Labels.Length > 0) { StringBuilder labels = new(); foreach (string label in modCase.Labels) { if (labels.ToString().Length + label.Length + 2 > 2000) { break; } labels.Append($"`{label}` "); } embed.AddField($"{SCROLL_EMOTE} - {Translator.T().Labels()}", labels.ToString(), false); } await Context.Interaction.ModifyOriginalResponseAsync(message => { message.Content = ""; message.Embed = embed.Build(); }); }
public async Task Track([Summary("invite", "Either enter the invite code or the url")] string inviteCode) { await Context.Interaction.RespondAsync("Tracking invite code..."); if (!inviteCode.ToLower().Contains("https://discord.gg/")) { inviteCode = $"https://discord.gg/{inviteCode}"; } List <UserInvite> invites = await InviteRepository.CreateDefault(ServiceProvider).GetInvitesByCode(inviteCode); invites = invites.Where(x => x.GuildId == Context.Guild.Id).OrderByDescending(x => x.JoinedAt).ToList(); DateTime?createdAt = null; IUser creator = null; int? usages = invites.Count; Dictionary <ulong, IUser> invitees = new(); if (invites.Count > 0) { createdAt = invites[0].InviteCreatedAt; if (invites[0].InviteIssuerId != 0) { creator = await DiscordAPI.FetchUserInfo(invites[0].InviteIssuerId, CacheBehavior.Default); } int count = 0; foreach (UserInvite invite in invites) { if (count > 20) { break; } if (!invitees.ContainsKey(invite.JoinedUserId)) { invitees.Add(invite.JoinedUserId, await DiscordAPI.FetchUserInfo(invite.JoinedUserId, CacheBehavior.Default)); } } } else { string code = inviteCode.Split("/").Last(); try { var fetchedInvite = await Context.Client.GetInviteAsync(code); if (fetchedInvite.GuildId != Context.Guild.Id) { await Context.Interaction.ModifyOriginalResponseAsync(message => message.Content = Translator.T().CmdTrackInviteNotFromThisGuild()); return; } try { usages = fetchedInvite.Uses; creator = await DiscordAPI.FetchUserInfo(fetchedInvite.Inviter.Id, CacheBehavior.Default); } catch (NullReferenceException) { } } catch (HttpException e) { if (e.HttpCode == HttpStatusCode.NotFound) { await Context.Interaction.ModifyOriginalResponseAsync(message => message.Content = Translator.T().CmdTrackCannotFindInvite()); } else { await Context.Interaction.ModifyOriginalResponseAsync(message => message.Content = Translator.T().CmdTrackFailedToFetchInvite()); } return; } } EmbedBuilder embed = new(); embed.WithDescription(inviteCode); if (creator != null) { embed.WithAuthor(creator); if (createdAt.HasValue && createdAt.Value != default) { embed.WithDescription(Translator.T().CmdTrackCreatedByAt(inviteCode, creator, createdAt.Value)); } else { embed.WithDescription(Translator.T().CmdTrackCreatedBy(inviteCode, creator)); } } else if (createdAt.HasValue && createdAt.Value != default) { embed.WithDescription(Translator.T().CmdTrackCreatedAt(inviteCode, createdAt.Value)); } StringBuilder usedBy = new(); foreach (UserInvite invite in invites) { if (usedBy.Length > 900) { break; } usedBy.Append("- "); if (invitees.ContainsKey(invite.JoinedUserId)) { IUser user = invitees[invite.JoinedUserId]; usedBy.Append($"`{user.Username}#{user.Discriminator}` "); } usedBy.AppendLine($"`{invite.JoinedUserId}` - {invite.JoinedAt.ToDiscordTS()}"); } if (invites.Count == 0) { usedBy.Clear(); usedBy.Append(Translator.T().CmdTrackNotTrackedYet()); } embed.AddField(Translator.T().CmdTrackUsedBy(usages.GetValueOrDefault()), usedBy.ToString(), false); embed.WithFooter($"Invite: {inviteCode}"); embed.WithTimestamp(DateTime.UtcNow); embed.WithColor(Color.Gold); await Context.Interaction.ModifyOriginalResponseAsync(message => { message.Content = ""; message.Embed = embed.Build(); }); }