Skip to content

Commit

Permalink
Merge pull request #134 from bealsbe/jsh/bugfix
Browse files Browse the repository at this point in the history
Fix user update logging
  • Loading branch information
jkchen2 authored Dec 9, 2020
2 parents fa7e7d8 + ba64362 commit fd3dc40
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Floofbot/Floofbot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<ProjectReference Include="../Discord.Addons.Interactive/Discord.Addons.Interactive.csproj" />
<PackageReference Include="Discord.Net" Version="2.2.0" />
<PackageReference Include="Discord.Net" Version="2.3.0-dev-20201109.3" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3">
Expand Down
128 changes: 84 additions & 44 deletions Floofbot/Services/EventLoggerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public EventLoggerService(DiscordSocketClient client)
_client.UserJoined += UserJoined;
_client.UserLeft += UserLeft;
_client.GuildMemberUpdated += GuildMemberUpdated;
_client.UserUpdated += UserUpdated;
_client.MessageReceived += OnMessage;
_client.ReactionAdded += _nicknameAlertService.OnReactionAdded;
}
Expand Down Expand Up @@ -466,87 +467,126 @@ public Task UserLeft(IGuildUser user)
return Task.CompletedTask;

}
public Task GuildMemberUpdated(SocketGuildUser before, SocketGuildUser after)
public Task UserUpdated(SocketUser before, SocketUser userAfter)
{

var _ = Task.Run(async () =>
{
try
{
if (!(userAfter is SocketGuildUser after))
return;
if (before == null || after == null) // empty user params
return;
if (after.IsBot)
return;
var user = after as SocketGuildUser;
if ((IsToggled(user.Guild) == false)) // turned off
if (IsToggled(after.Guild) == false) // turned off
return;
Discord.ITextChannel channel = await GetChannel(user.Guild, "MemberUpdatesChannel");
Discord.ITextChannel channel = await GetChannel(after.Guild, "MemberUpdatesChannel");
if (channel == null) // no log channel set
return;
var embed = new EmbedBuilder();
if (before.Username != after.Username)
{
embed.WithTitle($"👥 Username Changed | {user.Username}#{user.Discriminator}")
embed.WithTitle($"👥 Username Changed | {after.Username}#{after.Discriminator}")
.WithColor(Color.Purple)
.WithDescription($"{user.Mention} | ``{user.Id}``")
.AddField("Old Username", user.Username, true)
.AddField("New Name", user.Username, true)
.WithFooter($"user_username_change user_namelog {user.Id}")
.WithDescription($"{after.Mention} | ``{after.Id}``")
.AddField("Old Username", after.Username, true)
.AddField("New Name", after.Username, true)
.WithFooter($"user_username_change user_namelog {after.Id}")
.WithCurrentTimestamp();
if (Uri.IsWellFormedUriString(user.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(user.GetAvatarUrl());
if (Uri.IsWellFormedUriString(after.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(after.GetAvatarUrl());
bool hasBadWord = _wordFilterService.hasFilteredWord(new FloofDataContext(), after.Username, channel.Guild.Id);
if (hasBadWord)
await _nicknameAlertService.HandleBadNickname(user, user.Guild);
await _nicknameAlertService.HandleBadNickname(after, after.Guild);
}
else if (before.Nickname != after.Nickname)
else if (before.AvatarId != after.AvatarId)
{
embed.WithTitle($"👥 Nickname Changed | {user.Username}#{user.Discriminator}")
embed.WithTitle($"🖼️ Avatar Changed | {after.Username}#{after.Discriminator}")
.WithColor(Color.Purple)
.WithDescription($"{user.Mention} | ``{user.Id}``")
.WithFooter($"user_nickname_change user_namelog {user.Id}")
.WithDescription($"{after.Mention} | ``{after.Id}``")
.WithFooter($"user_avatar_change {after.Id}")
.WithCurrentTimestamp();
if (Uri.IsWellFormedUriString(before.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(before.GetAvatarUrl());
if (Uri.IsWellFormedUriString(after.GetAvatarUrl(), UriKind.Absolute))
embed.WithImageUrl(after.GetAvatarUrl());
}
else
{
return;
}
await channel.SendMessageAsync("", false, embed.Build());
}
catch (Exception ex)
{
Log.Error("Error with the user updated event handler: " + ex);
return;
}
});
return Task.CompletedTask;

}

public Task GuildMemberUpdated(SocketGuildUser before, SocketGuildUser after)
{
var _ = Task.Run(async () =>
{
try
{
if (before == null || after == null) // empty user params
return;
if (after.IsBot)
return;
if (IsToggled(after.Guild) == false) // turned off
return;
Discord.ITextChannel channel = await GetChannel(after.Guild, "MemberUpdatesChannel");
if (channel == null) // no log channel set
return;
var embed = new EmbedBuilder();
if (before.Nickname != after.Nickname)
{
embed.WithTitle($"👥 Nickname Changed | {after.Username}#{after.Discriminator}")
.WithColor(Color.Purple)
.WithDescription($"{after.Mention} | ``{after.Id}``")
.WithFooter($"user_nickname_change user_namelog {after.Id}")
.WithCurrentTimestamp();
if (before.Nickname != null && after.Nickname != null) // changing nickname
{
embed.AddField("Old Nickname", before.Nickname, true);
embed.AddField("New Nickname", user.Nickname, true);
embed.AddField("New Nickname", after.Nickname, true);
}
else if (after.Nickname == null) // removed their nickname
embed.AddField("Old Nickname", before.Nickname, true);
else // new nickname, didnt have one before
embed.AddField("New Nickname", user.Nickname, true);
embed.AddField("New Nickname", after.Nickname, true);
if (Uri.IsWellFormedUriString(user.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(user.GetAvatarUrl());
if (Uri.IsWellFormedUriString(after.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(after.GetAvatarUrl());
if (after.Nickname != null)
{
bool hasBadWord = _wordFilterService.hasFilteredWord(new FloofDataContext(), after.Nickname, channel.Guild.Id);
if (hasBadWord)
await _nicknameAlertService.HandleBadNickname(user, user.Guild);
await _nicknameAlertService.HandleBadNickname(after, after.Guild);
}
}
else if (before.AvatarId != after.AvatarId)
{
embed.WithTitle($"🖼️ Avatar Changed | {user.Username}#{user.Discriminator}")
.WithColor(Color.Purple)
.WithDescription($"{user.Mention} | ``{user.Id}``")
.WithFooter($"user_avatar_change {user.Id}")
.WithCurrentTimestamp();
if (Uri.IsWellFormedUriString(before.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(before.GetAvatarUrl());
if (Uri.IsWellFormedUriString(after.GetAvatarUrl(), UriKind.Absolute))
embed.WithImageUrl(after.GetAvatarUrl());
}
else if (before.Roles.Count != after.Roles.Count)
{
List<SocketRole> beforeRoles = new List<SocketRole>(before.Roles);
Expand All @@ -556,34 +596,34 @@ public Task GuildMemberUpdated(SocketGuildUser before, SocketGuildUser after)
if (before.Roles.Count > after.Roles.Count) // roles removed
{
roleDifference = beforeRoles.Except(afterRoles).ToList();
embed.WithTitle($"❗ Roles Removed | {user.Username}#{user.Discriminator}")
embed.WithTitle($"❗ Roles Removed | {after.Username}#{after.Discriminator}")
.WithColor(Color.Orange)
.WithDescription($"{user.Mention} | ``{user.Id}``")
.WithFooter($"user_roles_removed user_rolelog {user.Id}")
.WithDescription($"{after.Mention} | ``{after.Id}``")
.WithFooter($"user_roles_removed user_rolelog {after.Id}")
.WithCurrentTimestamp();
foreach (SocketRole role in roleDifference)
{
embed.AddField("Role Removed", role);
}
if (Uri.IsWellFormedUriString(user.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(user.GetAvatarUrl());
if (Uri.IsWellFormedUriString(after.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(after.GetAvatarUrl());
}
else if (before.Roles.Count < after.Roles.Count) // roles added
{
roleDifference = afterRoles.Except(beforeRoles).ToList();
embed.WithTitle($"❗ Roles Added | {user.Username}#{user.Discriminator}")
embed.WithTitle($"❗ Roles Added | {after.Username}#{after.Discriminator}")
.WithColor(Color.Orange)
.WithDescription($"{user.Mention} | ``{user.Id}``")
.WithFooter($"user_roles_added user_rolelog {user.Id}")
.WithDescription($"{after.Mention} | ``{after.Id}``")
.WithFooter($"user_roles_added user_rolelog {after.Id}")
.WithCurrentTimestamp();
foreach (SocketRole role in roleDifference)
{
embed.AddField("Role Added", role);
}
if (Uri.IsWellFormedUriString(user.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(user.GetAvatarUrl());
if (Uri.IsWellFormedUriString(after.GetAvatarUrl(), UriKind.Absolute))
embed.WithThumbnailUrl(after.GetAvatarUrl());
}
}
else
Expand Down
7 changes: 7 additions & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="Discord.Net development builds" value="https://www.myget.org/F/discord-net/api/v3/index.json" />
</packageSources>
</configuration>

0 comments on commit fd3dc40

Please sign in to comment.