Skip to content

Commit

Permalink
Merge pull request #7 from Transcom-DT/develop
Browse files Browse the repository at this point in the history
Hotfix 2.1.2
  • Loading branch information
SakuraIsayeki authored Apr 22, 2021
2 parents 43f510c + fd575f6 commit 39aee76
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 31 deletions.
2 changes: 1 addition & 1 deletion GuildTrafficHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task OnMemberJoinedAsync(DiscordClient _, GuildMemberAddEventArgs e
{
TrustlistUser entry = await apiService.LookupUserAsync(e.Member.Id);
DiscordChannel joinLog = e.Guild.GetChannel(config.JoinLogChannel);
DiscordEmbed entryEmbed = Utilities.BuildUserRecordEmbed(entry, e.Member, e.Member.Id);
DiscordEmbed entryEmbed = Utilities.BuildUserRecordEmbed(entry, e.Member);


await joinLog.SendMessageAsync($"User **{e.Member}** ({e.Member.Mention}) has joined the server.", entryEmbed);
Expand Down
4 changes: 2 additions & 2 deletions Modules/GuildConfigModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public async Task ConfigureAccessKeyAsync(CommandContext context, string usernam
GuildConfig config = await repository.FindOrCreateConfigAsync(context.Guild.Id);
config.ApiLogin = new(username, encryption.Encrypt(password));
await repository.ReplaceOneAsync(config);
await context.RespondAsync($"API credentials has been set.");
await context.Channel.SendMessageAsync($"API credentials has been set.");
}

[Command("set-autoban"), Aliases("autoban"), RequireUserPermissions(Permissions.ManageGuild), RequireBotPermissions(Permissions.BanMembers)]
Expand Down Expand Up @@ -105,7 +105,7 @@ public async Task RegisterAsync(CommandContext context, string username, [EmailA
AuthRegisterCredentials credentials = new(username, email, password);
AuthResponse<IAuthComponent> result = await auth.RegisterNewUserAsync(credentials);

await context.RespondAsync($"{context.User.Mention} {result.Status} : {result.Message}\n");
await context.Channel.SendMessageAsync($"{context.User.Mention} {result.Status} : {result.Message}\n");
}
}
}
Expand Down
43 changes: 24 additions & 19 deletions Modules/UserLookupModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,44 @@ public UserLookupModule(TrustlistUserApiService trustlist, AuthApiService auth,
}

[Command("lookup"), Aliases("get")]
public async Task LookupAsync(CommandContext context, DiscordUser user) => await LookupAsync(context, user, user.Id);
public async Task LookupAsync(CommandContext context, DiscordUser user) => await RespondLookupAsync(context, user);

[Command("insert"), Aliases("add"), RequireGuild, RequireUserPermissions(Permissions.BanMembers)]
public async Task InsertUserAsync(CommandContext context, DiscordUser user, [Range(0, 3)] byte level, [RemainingText] string reason)
public async Task InsertUserAsync(CommandContext context, DiscordUser user, byte level, [RemainingText] string reason)
{
await InsertUserAsync(context, user, user.Id, level, reason);
await InsertUserAsync(context, user, level, reason, false);
}

[Command("ban"), RequireGuild, RequireUserPermissions(Permissions.BanMembers), RequireBotPermissions(Permissions.BanMembers)]
public async Task BanUserAsync(CommandContext context, DiscordUser user, [Range(0, 3)] byte level, [RemainingText] string reason)
{
await InsertUserAsync(context, user, user.Id, level, reason, true);
await InsertUserAsync(context, user, level, reason, true);
}

private async Task InsertUserAsync(CommandContext context, DiscordUser user, ulong userId, byte level, string reason, bool banUser = false)
private async Task InsertUserAsync(CommandContext context, DiscordUser user, byte level, string reason, bool banUser = false)
{
if (user is not null)
{
if (user?.Id == context.User.Id)
{
await context.RespondAsync($"{context.User.Mention} You cannot insert yourself in the Trustlist.");
await context.RespondAsync("You cannot insert yourself in the Trustlist.");
return;
}
else if (user.IsBot)
{
await context.RespondAsync($"{context.User.Mention} You cannot insert a Bot in the Trustlist.");
await context.RespondAsync("You cannot insert a Bot in the Trustlist.");
return;
}
else if ((user as DiscordMember).Roles.Any(r => r.Permissions == (r.Permissions & Permissions.ManageGuild)))
else if ((user as DiscordMember)?.Roles.Any(r => r.Permissions == (r.Permissions & Permissions.ManageGuild)) ?? false)
{
await context.RespondAsync($"You cannot insert a server operator in the Trustlist. Demote them first.");
await context.RespondAsync("You cannot insert a server operator in the Trustlist. Demote them first.");
return;
}
}

if (reason.Length < 5)
{
await context.RespondAsync($"{context.User.Mention} Reason is too short");
await context.RespondAsync("Reason is too short");
}
else
{
Expand All @@ -78,23 +78,23 @@ private async Task InsertUserAsync(CommandContext context, DiscordUser user, ulo
{
await trustlist.InsertOrEscalateUserAsync(new()
{
Id = userId,
Id = user.Id,
EscalationLevel = level,
EscalationNote = reason
}, await auth.GetOrUpdateAuthTokenAsync(context.Guild.Id));

await context.RespondAsync($"User '{user?.Mention ?? userId.ToString()}' successfully inserted into Trustlist.");
await LookupAsync(context, user, userId);
string userMention = (user as DiscordMember)?.Mention ?? user.Id.ToString();
await context.RespondAsync($"User '{userMention}' successfully inserted into Trustlist.", await LookupAsync(user));

if (banUser || (config.AutoBanBlacklisted && level >= 3))
{
await context.Guild.BanMemberAsync(userId, 0, $"[SocialGuard] {reason}");
await context.Guild.GetChannel(config.BanLogChannel).SendMessageAsync($"Banned user '{user}'.");
await context.Guild.BanMemberAsync(user.Id, 0, $"[SocialGuard] {reason}");
await context.Guild.GetChannel(config.BanLogChannel).SendMessageAsync($"Banned user '{userMention}'.");
}
}
else
{
await context.RespondAsync("No API Credentials set. Use ``sg config accesskey <key>`` to set an Access Key.");
await context.RespondAsync($"No API Credentials set. Use ``{context.Prefix}sg config accesskey <key>`` to set an Access Key.");
}
}
catch (ApplicationException e)
Expand All @@ -107,15 +107,20 @@ await trustlist.InsertOrEscalateUserAsync(new()
}
}

public async Task LookupAsync(CommandContext context, DiscordUser user, ulong userId, bool silenceOnClear = false)
public async Task RespondLookupAsync(CommandContext context, DiscordUser user, bool silenceOnClear = false)
{
TrustlistUser entry = await trustlist.LookupUserAsync(userId);
TrustlistUser entry = await trustlist.LookupUserAsync(user.Id);

if (!silenceOnClear || entry.EscalationLevel is not 0)
{
await context.RespondAsync(embed: Utilities.BuildUserRecordEmbed(entry, user, userId));
await context.RespondAsync(Utilities.BuildUserRecordEmbed(entry, user));
}
}

public async Task<DiscordEmbed> LookupAsync(DiscordUser user)
{
return Utilities.BuildUserRecordEmbed(await trustlist.LookupUserAsync(user.Id), user);
}
}
}
}
2 changes: 1 addition & 1 deletion SocialGuard.YC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>2.1.1-dev</Version>
<Version>2.1.2</Version>
<Authors>Sakura Akeno Isayeki</Authors>
<Company>Nodsoft Systems</Company>
<Product>NSYS SocialGuard (YC)</Product>
Expand Down
20 changes: 12 additions & 8 deletions Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Security.Cryptography;
using System;
using DSharpPlus.Entities;
using DSharpPlus;

namespace SocialGuard.YC
{
Expand All @@ -29,7 +30,7 @@ public static IApiConfig PopulateApiConfig(this IApiConfig config)
return config;
}

public static DiscordEmbed BuildUserRecordEmbed(TrustlistUser trustlistUser, DiscordUser discordUser, ulong userId)
public static DiscordEmbed BuildUserRecordEmbed(TrustlistUser trustlistUser, DiscordUser discordUser)
{
(DiscordColor color, string name, string desc) = trustlistUser?.EscalationLevel switch
{
Expand All @@ -40,12 +41,8 @@ public static DiscordEmbed BuildUserRecordEmbed(TrustlistUser trustlistUser, Dis
};

DiscordEmbedBuilder builder = new();
builder.WithTitle($"Trustlist User : {discordUser?.Username ?? userId.ToString()}");

if (discordUser is not null)
{
builder.AddField("ID", $"``{discordUser?.Id}``", true);
}
builder.WithTitle($"Trustlist User : {discordUser?.Username}");
builder.AddField("ID", $"`{discordUser?.Id}`", true);

builder.Color = color;
builder.Description = desc;
Expand All @@ -54,7 +51,7 @@ public static DiscordEmbed BuildUserRecordEmbed(TrustlistUser trustlistUser, Dis
if (trustlistUser is not null)
{
builder.AddField("Escalation Level", $"{trustlistUser.EscalationLevel} - {name}", true)
.AddField("Emitter", $"{trustlistUser.Emitter.DisplayName} (``{trustlistUser.Emitter.Login}``)")
.AddField("Emitter", $"{trustlistUser.Emitter.DisplayName} (`{trustlistUser.Emitter.Login}`)")
.AddField("First Entered", trustlistUser.EntryAt.ToString(), true)
.AddField("Last Escalation", trustlistUser.LastEscalated.ToString(), true)
.AddField("Reason", trustlistUser.EscalationNote);
Expand Down Expand Up @@ -85,5 +82,12 @@ internal static string GenerateLocalMasterKey()
string localMasterKeyBase64 = Convert.ToBase64String(bytes);
return localMasterKeyBase64;
}

public static DiscordEmbedBuilder WithAuthor(this DiscordEmbedBuilder embed, DiscordUser user)
{
return embed.WithAuthor(user.GetFullUsername(), null, user.GetAvatarUrl(ImageFormat.Auto, 128));
}

public static string GetFullUsername(this DiscordUser user) => $"{user.Username}#{user.Discriminator}";
}
}

0 comments on commit 39aee76

Please sign in to comment.