Skip to content

Commit

Permalink
Add osu profile command
Browse files Browse the repository at this point in the history
  • Loading branch information
Pasi4K5 committed Mar 25, 2024
1 parent 8ef913a commit 96d11fd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
49 changes: 49 additions & 0 deletions Numerous/Discord/Commands/OsuProfileCommandModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) Pasi4K5 <https://www.github.com/Pasi4K5>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

using Discord;
using Discord.Interactions;
using JetBrains.Annotations;

namespace Numerous.Discord.Commands;

public sealed class OsuProfileCommand(OsuVerifier verifier) : CommandModule
{
[UsedImplicitly]
[SlashCommand("profile", "View the osu! profile of a user.")]
public async Task Profile(
[Summary("user", "The user to view the profile of.")]
IUser user
)
{
await Execute(user, false);
}

[UsedImplicitly]
[UserCommand("osu! profile")]
public async Task OsuProfile(IUser user)
{
await Execute(user, true);
}

private async Task Execute(IUser user, bool ephemeral)
{
await DeferAsync(ephemeral);

var osuId = await verifier.GetOsuIdAsync(user);

if (osuId is null)
{
await FollowupWithEmbedAsync(
"This user is not verified.",
"They can use `/verify` to verify their osu! account."
);

return;
}

await FollowupAsync($"https://osu.ppy.sh/users/{osuId}");
}
}
32 changes: 21 additions & 11 deletions Numerous/Discord/Events/EventHandler.Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public partial class DiscordEventHandler
private void Logger_Init()
{
client.SlashCommandExecuted += LogSlashCommand;
client.InteractionCreated += LogInteraction;
client.MessageCommandExecuted += LogMessageCommand;
client.UserCommandExecuted += LogUserCommand;
}

private async Task LogSlashCommand(SocketSlashCommand cmd)
Expand All @@ -36,17 +37,26 @@ private async Task LogSlashCommand(SocketSlashCommand cmd)
cmd.GuildId);
}

private static Task LogInteraction(SocketInteraction interaction)
private static Task LogMessageCommand(SocketMessageCommand msgCmd)
{
if (interaction is SocketMessageCommand msgCmd)
{
Log.Information(
"""User "{User}" (ID: {Uid}) executed message command "{Cmd}" on message <{Msg}>.""",
msgCmd.User.Username,
msgCmd.User.Id,
msgCmd.CommandName,
msgCmd.Data.Message.GetLink());
}
Log.Information(
"""User "{User}" (ID: {Uid}) executed message command "{Cmd}" on message <{Msg}>.""",
msgCmd.User.Username,
msgCmd.User.Id,
msgCmd.CommandName,
msgCmd.Data.Message.GetLink());

return Task.CompletedTask;
}

private static Task LogUserCommand(SocketUserCommand userCmd)
{
Log.Information("""User "{User} (ID: {Uid}) executed user command "{Cmd}" on user "{Target} (ID: {Tid}).""",
userCmd.User.Username,
userCmd.User.Id,
userCmd.CommandName,
userCmd.Data.Member.Username,
userCmd.Data.Member.Id);

return Task.CompletedTask;
}
Expand Down
11 changes: 8 additions & 3 deletions Numerous/Discord/OsuVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,23 @@ await dbUsers.ForEachAsync(async dbUser =>
}
}

public async Task<bool> UserIsVerifiedAsync(IGuildUser guildUser)
public async Task<bool> UserIsVerifiedAsync(IUser user)
{
var user = await db.GetUserAsync(guildUser.Id);
var dbUser = await db.GetUserAsync(user.Id);

return user.OsuId is not null;
return dbUser.OsuId is not null;
}

public async Task<bool> OsuUserIsVerifiedAsync(OsuUser osuUser)
{
return await db.Users.Find(x => x.OsuId == osuUser.Id).AnyAsync();
}

public async Task<ulong?> GetOsuIdAsync(IUser discordUser)
{
return (await db.GetUserAsync(discordUser.Id)).OsuId;
}

public async Task VerifyUserAsync(IGuildUser guildUser, OsuUser osuUser)
{
await db.EnsureUserExistsAsync(guildUser.Id);
Expand Down

0 comments on commit 96d11fd

Please sign in to comment.