added account creation to the Discord bot

This commit is contained in:
Sarah Faey 2022-11-10 21:47:44 +01:00
parent 69337e69ae
commit 56cb43a479
18 changed files with 198 additions and 152 deletions

View file

@ -2,9 +2,11 @@ using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Mvc;
using System.Text.RegularExpressions;
using Lieb.Data;
using Lieb.Models.GuildWars2.Raid;
using Lieb.Models.GuildWars2;
using Lieb.Models;
using SharedClasses.SharedModels;
namespace Lieb.Controllers
@ -15,11 +17,33 @@ namespace Lieb.Controllers
{
RaidService _raidService;
UserService _userService;
GuildWars2AccountService _gw2AccountService;
public DiscordBotController(RaidService raidService, UserService userService)
public DiscordBotController(RaidService raidService, UserService userService, GuildWars2AccountService gw2AccountService)
{
_raidService = raidService;
_userService = userService;
_gw2AccountService = gw2AccountService;
}
[HttpGet]
[Route("[action]/{userId}")]
public ActionResult<bool> DoesUserExist(ulong userId)
{
LiebUser user = _userService.GetLiebUserGW2AccountOnly(userId);
return user != null && user.GuildWars2Accounts.Count() > 0;
}
[HttpGet]
[Route("[action]/{raidId}/{userId}")]
public ActionResult IsSignUpAllowed(int raidId, ulong userId)
{
Raid raid = _raidService.GetRaid(raidId);
if(!_raidService.IsRaidSignUpAllowed(userId, raidId, out string errorMessage))
{
return Problem(errorMessage);
}
return Ok();
}
[HttpGet]
@ -27,10 +51,6 @@ namespace Lieb.Controllers
public ActionResult<List<ApiRole>> GetRoles(int raidId, ulong userId)
{
Raid raid = _raidService.GetRaid(raidId);
if(!_raidService.IsRaidSignUpAllowed(userId, raidId, out string errorMessage))
{
return Problem(errorMessage);
}
List<ApiRole> apiRoles = new List<ApiRole>();
foreach(RaidRole role in raid.Roles)
@ -84,5 +104,23 @@ namespace Lieb.Controllers
int accountId = _userService.GetLiebUserGW2AccountOnly(signUp.userId).GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()).GuildWars2AccountId;
await _raidService.SignOff(signUp.raidId, signUp.userId);
}
[HttpPost]
[Route("[action]")]
public async Task<ActionResult> CreateAccount(ApiRaid.Role.User user)
{
if(!Regex.IsMatch(user.AccountName, Constants.GW2_ACCOUNT_REGEX))
{
return Problem("Invalid Account Name");
}
GuildWars2Account gw2Account = new GuildWars2Account()
{
AccountName = user.AccountName
};
await _userService.CreateUser(user.UserId, user.UserName);
await _gw2AccountService.AddOrEditAccount(gw2Account, user.UserId);
return Ok();
}
}
}

View file

@ -4,6 +4,7 @@
{
public const string HttpClientName = "Discord";
public const string ClaimType = "Role";
public const string GW2_ACCOUNT_REGEX = "^[a-zA-z ]{3,27}\\.[0-9]{4}$";
public static readonly int RaidEditPowerLevel = Roles.Moderator.PowerLevel;
public static class Roles

View file

@ -40,8 +40,8 @@ namespace Lieb.Data
GuildWars2Account bloodseeker = new GuildWars2Account() { AccountName = "Bloodseeker.2043" };
var users = new LiebUser[]
{
//new LiebUser{Id=0, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith, sarah} },
new LiebUser{Id=194863625477816321, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith, sarah} },
new LiebUser{Id=0, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith, sarah} },
//new LiebUser{Id=194863625477816321, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith, sarah} },
#if DEBUG
new LiebUser{Id=194455125769715713, Name="Lisa", GuildWars2Accounts = new List<GuildWars2Account>(){ hierpiepts}},
new LiebUser{Id=2, Name="Simon", GuildWars2Accounts = new List<GuildWars2Account>(){ bloodseeker}}
@ -195,6 +195,16 @@ namespace Lieb.Data
context.Equipped.AddRange(equippedBuilds);
context.SaveChanges();
var discordMessage = new DiscordRaidMessage()
{
DiscordChannelId = 666954070388637697,
DiscordGuildId = 666953424734257182,
DiscordMessageId = 1040355092630614087,
RaidId = raid.RaidId
};
context.DiscordRaidMessages.Add(discordMessage);
context.SaveChanges();
#endif
}
}

View file

@ -204,11 +204,15 @@ namespace Lieb.Data
foreach(RaidSignUp signUp in raid.SignUps.Where(x => x.RaidRoleId == role.RaidRoleId))
{
apiRole.Users.Add(new ApiRaid.Role.User(){
AccountName = signUp.GuildWars2Account.AccountName,
Status = signUp.SignUpType.ToString(),
UserName = signUp.LiebUser.Name
});
if(signUp.SignUpType != SignUpType.SignedOff)
{
string status = signUp.SignUpType != SignUpType.SignedUp ? signUp.SignUpType.ToString() : string.Empty;
apiRole.Users.Add(new ApiRaid.Role.User(){
AccountName = signUp.GuildWars2Account.AccountName,
Status = status,
UserName = signUp.LiebUser.Name
});
}
}
apiRaid.Roles.Add(apiRole);
}

View file

@ -333,6 +333,7 @@ namespace Lieb.Data
if(raid.EndTimeUTC < DateTimeOffset.UtcNow)
{
errorMessage = $"The raid already ended.";
return false;
}

View file

@ -1,62 +0,0 @@
using Discord.Commands;
using Discord.WebSocket;
using System.Reflection;
namespace Lieb.Discord
{
public class CommandHandler
{
private readonly DiscordSocketClient _client;
private readonly CommandService _commands;
// Retrieve client and CommandService instance via ctor
public CommandHandler(DiscordSocketClient client, CommandService commands)
{
_commands = commands;
_client = client;
}
public async Task InstallCommandsAsync()
{
// Hook the MessageReceived event into our command handler
_client.MessageReceived += HandleCommandAsync;
// Here we discover all of the command modules in the entry
// assembly and load them. Starting from Discord.NET 2.0, a
// service provider is required to be passed into the
// module registration method to inject the
// required dependencies.
//
// If you do not use Dependency Injection, pass null.
// See Dependency Injection guide for more information.
await _commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(),
services: null);
}
private async Task HandleCommandAsync(SocketMessage messageParam)
{
// Don't process the command if it was a system message
var message = messageParam as SocketUserMessage;
if (message == null) return;
// Create a number to track where the prefix ends and the command begins
int argPos = 0;
// Determine if the message is a command based on the prefix and make sure no bots trigger commands
if (!(message.HasCharPrefix('!', ref argPos) ||
message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
message.Author.IsBot)
return;
// Create a WebSocket-based command context based on the message
var context = new SocketCommandContext(_client, message);
// Execute the command with the command context we just
// created, along with the service provider for precondition checks.
await _commands.ExecuteAsync(
context: context,
argPos: argPos,
services: null);
}
}
}

View file

@ -1,25 +0,0 @@
using Lieb.Models.GuildWars2.Raid;
namespace Lieb.Discord.Messages
{
public class RaidMessage
{
private Raid _raid;
public RaidMessage(Raid raid)
{
_raid = raid;
}
public void PostRaidMessage()
{
}
public void UpdateRaidMessage()
{
}
}
}

View file

@ -7,7 +7,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Discord.Net" Version="3.7.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.2" />

View file

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using Lieb.Data;
namespace Lieb.Models.GuildWars2
{
@ -9,7 +10,7 @@ namespace Lieb.Models.GuildWars2
public string ApiKey { get; set; } = string.Empty;
[Required]
[RegularExpression("^[a-zA-z ]{3,27}\\.[0-9]{4}$", ErrorMessage = "Invalid Account Name")]
[RegularExpression(Constants.GW2_ACCOUNT_REGEX, ErrorMessage = "Invalid Account Name")]
public string AccountName { get; set; } = string.Empty;
public ICollection<Equipped> EquippedBuilds { get; set; } = new List<Equipped>();

View file

@ -323,7 +323,7 @@
}
_raid.StartTimeUTC = await TimeZoneService.GetUTCDateTime(_raidDate.Date + _startTime.TimeOfDay);
if(_startTime.TimeOfDay > _endTime.TimeOfDay)
if(_startTime.TimeOfDay <= _endTime.TimeOfDay)
{
_raid.EndTimeUTC = await TimeZoneService.GetUTCDateTime(_raidDate.Date + _endTime.TimeOfDay);
}

View file

@ -1,9 +1,5 @@
using Discord;
using Discord.Commands;
using Discord.OAuth2;
using Discord.WebSocket;
using Discord.OAuth2;
using Lieb.Data;
using Lieb.Discord;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore;
using Microsoft.Net.Http.Headers;