Fixes giving of altruistic role

This commit is contained in:
Ben 2024-12-09 10:04:33 +01:00
parent e19311cef9
commit 37f037c7b0
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
4 changed files with 34 additions and 18 deletions

View File

@ -3,6 +3,8 @@ using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using BiblioTech.Rewards;
using System.Data;
namespace BiblioTech.Commands
{
@ -68,25 +70,17 @@ namespace BiblioTech.Commands
private async Task<bool> GiveAltruisticRole(CommandContext context, IUser user, string responseMessage)
{
var guildUser = context.Command.User as IGuildUser;
if (guildUser != null)
try
{
try
{
var role = context.Command.Guild.GetRole(Program.Config.AltruisticRoleId);
if (role != null)
{
await guildUser.AddRoleAsync(role);
await context.Followup($"{responseMessage}\n\nCongratulations! You've been granted the Altruistic Mode role for checking a valid CID!");
return true;
}
}
catch (Exception ex)
{
await Program.AdminChecker.SendInAdminChannel($"Failed to grant Altruistic Mode role to user {Mention(user)}: {ex.Message}");
}
await Program.RoleDriver.GiveAltruisticRole(user);
await context.Followup($"{responseMessage}\n\nCongratulations! You've been granted the Altruistic Mode role for checking a valid CID!");
return true;
}
catch (Exception ex)
{
await Program.AdminChecker.SendInAdminChannel($"Failed to grant Altruistic Mode role to user {Mention(user)}: {ex.Message}");
return false;
}
return false;
}
}

View File

@ -1,4 +1,5 @@
using BiblioTech.Rewards;
using Discord;
using DiscordRewards;
using Logging;
using Newtonsoft.Json;
@ -14,6 +15,13 @@ namespace BiblioTech
this.log = log;
}
public async Task GiveAltruisticRole(IUser user)
{
await Task.CompletedTask;
log.Log($"Give altruistic role to {user.Id}");
}
public async Task GiveRewards(GiveRewardsCommand rewards)
{
await Task.CompletedTask;

View File

@ -1,4 +1,5 @@
using DiscordRewards;
using Discord;
using DiscordRewards;
using Microsoft.AspNetCore.Mvc;
namespace BiblioTech.Rewards
@ -6,6 +7,7 @@ namespace BiblioTech.Rewards
public interface IDiscordRoleDriver
{
Task GiveRewards(GiveRewardsCommand rewards);
Task GiveAltruisticRole(IUser user);
}
[Route("api/[controller]")]

View File

@ -34,6 +34,18 @@ namespace BiblioTech.Rewards
await eventsSender.ProcessChainEvents(rewards.EventsOverview, rewards.Errors);
}
public async Task GiveAltruisticRole(IUser user)
{
var guild = GetGuild();
var role = guild.Roles.SingleOrDefault(r => r.Id == Program.Config.AltruisticRoleId);
if (role == null) return;
var guildUser = guild.Users.SingleOrDefault(u => u.Id == user.Id);
if (guildUser == null) return;
await guildUser.AddRoleAsync(role);
}
private async Task ProcessRewards(GiveRewardsCommand rewards)
{
try