Added Discord Bot
This commit is contained in:
parent
e7a0c9ae68
commit
e445b2a181
48 changed files with 1255 additions and 157 deletions
|
@ -2,6 +2,7 @@
|
|||
{
|
||||
public static class Constants
|
||||
{
|
||||
public const string HttpClientName = "Discord";
|
||||
public const string ClaimType = "Role";
|
||||
public static readonly int RaidEditPowerLevel = Roles.Moderator.PowerLevel;
|
||||
|
||||
|
|
|
@ -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=1, Name="Lisa", GuildWars2Accounts = new List<GuildWars2Account>(){ hierpiepts}},
|
||||
new LiebUser{Id=2, Name="Simon", GuildWars2Accounts = new List<GuildWars2Account>(){ bloodseeker}}
|
||||
|
|
253
Lieb/Data/DiscordService.cs
Normal file
253
Lieb/Data/DiscordService.cs
Normal file
|
@ -0,0 +1,253 @@
|
|||
using SharedClasses.SharedModels;
|
||||
using System.Net.Http;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
using System.Text.Json;
|
||||
using System.Text;
|
||||
using Lieb.Models.GuildWars2.Raid;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Lieb.Data
|
||||
{
|
||||
public class DiscordService
|
||||
{
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IDbContextFactory<LiebContext> _contextFactory;
|
||||
private readonly JsonSerializerOptions _serializerOptions;
|
||||
|
||||
public DiscordService(IHttpClientFactory httpClientFactory, IDbContextFactory<LiebContext> contextFactory)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_contextFactory = contextFactory;
|
||||
_serializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
}
|
||||
|
||||
public async Task PostRaidMessage(int raidId)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
Raid raid = context.Raids
|
||||
.Include(r => r.Roles)
|
||||
.Include(r => r.SignUps)
|
||||
.ThenInclude(s => s.LiebUser)
|
||||
.Include(r => r.SignUps)
|
||||
.ThenInclude(s => s.GuildWars2Account)
|
||||
.Include(r => r.SignUps)
|
||||
.ThenInclude(s => s.RaidRole)
|
||||
.Include(r => r.DiscordRaidMessages)
|
||||
.FirstOrDefault(r => r.RaidId == raidId);
|
||||
await PostRaidMessage(raid);
|
||||
}
|
||||
|
||||
public async Task PostRaidMessage(Raid raid)
|
||||
{
|
||||
var httpClient = _httpClientFactory.CreateClient(Constants.HttpClientName);
|
||||
|
||||
ApiRaid apiRaid = ConvertRaid(raid);
|
||||
|
||||
var raidItemJson = new StringContent(
|
||||
JsonSerializer.Serialize(apiRaid),
|
||||
Encoding.UTF8,
|
||||
Application.Json);
|
||||
|
||||
string json = JsonSerializer.Serialize(apiRaid);
|
||||
|
||||
var httpResponseMessage = await httpClient.PostAsync("raid/PostRaidMessage", raidItemJson);
|
||||
|
||||
if (httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
using var contentStream =
|
||||
await httpResponseMessage.Content.ReadAsStreamAsync();
|
||||
|
||||
ApiRaid returnedRaid = await JsonSerializer.DeserializeAsync<ApiRaid>(contentStream, _serializerOptions);
|
||||
await UpdateDiscordMessages(returnedRaid.DisocrdMessages, raid);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteRaidMessages(Raid raid)
|
||||
{
|
||||
await DeleteRaidMessages(raid.DiscordRaidMessages);
|
||||
}
|
||||
|
||||
public async Task DeleteRaidMessages(IEnumerable<DiscordRaidMessage> messages)
|
||||
{
|
||||
var httpClient = _httpClientFactory.CreateClient(Constants.HttpClientName);
|
||||
|
||||
IEnumerable<ApiRaid.DiscordMessage> apiMessages = ConvertMessages(messages);
|
||||
|
||||
var messageItemJson = new StringContent(
|
||||
JsonSerializer.Serialize(apiMessages),
|
||||
Encoding.UTF8,
|
||||
Application.Json);
|
||||
|
||||
var httpResponseMessage = await httpClient.PostAsync("raid/DeleteRaidMessage", messageItemJson);
|
||||
|
||||
httpResponseMessage.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
public async Task<List<DiscordServer>> GetServers()
|
||||
{
|
||||
try
|
||||
{
|
||||
var httpClient = _httpClientFactory.CreateClient(Constants.HttpClientName);
|
||||
|
||||
var httpResponseMessage = await httpClient.GetAsync("raid/GetServers");
|
||||
|
||||
if (httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
using var contentStream =
|
||||
await httpResponseMessage.Content.ReadAsStreamAsync();
|
||||
|
||||
return await JsonSerializer.DeserializeAsync<List<DiscordServer>>(contentStream, _serializerOptions);
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
|
||||
}
|
||||
return new List<DiscordServer>();
|
||||
}
|
||||
|
||||
public async Task SendUserReminder(RaidReminder reminder)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
var httpClient = _httpClientFactory.CreateClient(Constants.HttpClientName);
|
||||
|
||||
Raid raid = context.Raids
|
||||
.Include(r => r.SignUps)
|
||||
.ThenInclude(s => s.LiebUser)
|
||||
.FirstOrDefault(r => r.RaidId == reminder.RaidId);
|
||||
|
||||
if(raid == null) return;
|
||||
|
||||
ApiUserReminder apiReminder = ConvertUserReminder(reminder, raid);
|
||||
|
||||
var raidItemJson = new StringContent(
|
||||
JsonSerializer.Serialize(apiReminder),
|
||||
Encoding.UTF8,
|
||||
Application.Json);
|
||||
|
||||
var httpResponseMessage = await httpClient.PostAsync("raid/SendUserReminder", raidItemJson);
|
||||
|
||||
if (httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
reminder.Sent = true;
|
||||
context.Update(reminder);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendChannelReminder(RaidReminder reminder)
|
||||
{
|
||||
var httpClient = _httpClientFactory.CreateClient(Constants.HttpClientName);
|
||||
|
||||
ApiChannelReminder apiReminder = ConvertChannelReminder(reminder);
|
||||
|
||||
var raidItemJson = new StringContent(
|
||||
JsonSerializer.Serialize(apiReminder),
|
||||
Encoding.UTF8,
|
||||
Application.Json);
|
||||
|
||||
var httpResponseMessage = await httpClient.PostAsync("raid/SendChannelReminder", raidItemJson);
|
||||
|
||||
if (httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
reminder.Sent = true;
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
context.Update(reminder);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UpdateDiscordMessages(IEnumerable<ApiRaid.DiscordMessage> messages, Raid raid)
|
||||
{
|
||||
foreach(ApiRaid.DiscordMessage message in messages)
|
||||
{
|
||||
if(raid.DiscordRaidMessages.Where(m => m.DiscordRaidMessageId == message.WebsiteDatabaseId).Any())
|
||||
{
|
||||
raid.DiscordRaidMessages.FirstOrDefault(m => m.DiscordRaidMessageId == message.WebsiteDatabaseId).DiscordMessageId = message.MessageId;
|
||||
}
|
||||
}
|
||||
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
context.Update(raid.DiscordRaidMessages);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private ApiRaid ConvertRaid(Raid raid)
|
||||
{
|
||||
ApiRaid apiRaid = new ApiRaid(){
|
||||
Title = raid.Title,
|
||||
Description = raid.Description,
|
||||
Guild = raid.Guild,
|
||||
Organizer = raid.Organizer,
|
||||
VoiceChat = raid.VoiceChat,
|
||||
StartTimeUTC = raid.StartTimeUTC,
|
||||
EndTimeUTC = raid.EndTimeUTC,
|
||||
RaidId = raid.RaidId
|
||||
};
|
||||
apiRaid.DisocrdMessages = ConvertMessages(raid.DiscordRaidMessages);
|
||||
apiRaid.Roles = new List<ApiRaid.Role>();
|
||||
foreach(RaidRole role in raid.Roles)
|
||||
{
|
||||
ApiRaid.Role apiRole = new ApiRaid.Role(){
|
||||
Description = role.Description,
|
||||
Name = role.Name,
|
||||
Spots = role.Spots
|
||||
};
|
||||
apiRole.Users = new List<ApiRaid.Role.User>();
|
||||
|
||||
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
|
||||
});
|
||||
}
|
||||
apiRaid.Roles.Add(apiRole);
|
||||
}
|
||||
return apiRaid;
|
||||
}
|
||||
|
||||
private List<ApiRaid.DiscordMessage> ConvertMessages(IEnumerable<DiscordRaidMessage> messages)
|
||||
{
|
||||
List<ApiRaid.DiscordMessage> apiMessages = new List<ApiRaid.DiscordMessage>();
|
||||
foreach(DiscordRaidMessage message in messages)
|
||||
{
|
||||
apiMessages.Add(new ApiRaid.DiscordMessage(){
|
||||
ChannelId = message.DiscordChannelId,
|
||||
GuildId = message.DiscordGuildId,
|
||||
MessageId = message.DiscordMessageId,
|
||||
WebsiteDatabaseId = message.DiscordRaidMessageId
|
||||
});
|
||||
}
|
||||
return apiMessages;
|
||||
}
|
||||
|
||||
private ApiUserReminder ConvertUserReminder(RaidReminder reminder, Raid raid)
|
||||
{
|
||||
ApiUserReminder apiReminder = new ApiUserReminder()
|
||||
{
|
||||
Message = reminder.Message
|
||||
};
|
||||
apiReminder.UserIds = new List<ulong>();
|
||||
foreach(RaidSignUp signUp in raid.SignUps)
|
||||
{
|
||||
apiReminder.UserIds.Add(signUp.LiebUserId);
|
||||
}
|
||||
return apiReminder;
|
||||
}
|
||||
|
||||
private ApiChannelReminder ConvertChannelReminder(RaidReminder reminder)
|
||||
{
|
||||
return new ApiChannelReminder()
|
||||
{
|
||||
DiscordServerId = reminder.DiscordServerId,
|
||||
DiscordChannelId = reminder.DiscordChannelId,
|
||||
Message = reminder.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,10 +7,12 @@ namespace Lieb.Data
|
|||
public class RaidService
|
||||
{
|
||||
private readonly IDbContextFactory<LiebContext> _contextFactory;
|
||||
private readonly DiscordService _discordService;
|
||||
|
||||
public RaidService(IDbContextFactory<LiebContext> contextFactory)
|
||||
public RaidService(IDbContextFactory<LiebContext> contextFactory, DiscordService discordService)
|
||||
{
|
||||
_contextFactory = contextFactory;
|
||||
_discordService = discordService;
|
||||
}
|
||||
|
||||
public List<Raid> GetRaids()
|
||||
|
@ -26,6 +28,7 @@ namespace Lieb.Data
|
|||
.ThenInclude(s => s.GuildWars2Account)
|
||||
.Include(r => r.SignUps)
|
||||
.ThenInclude(s => s.RaidRole)
|
||||
.Include(r => r.DiscordRaidMessages)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
|
@ -42,10 +45,11 @@ namespace Lieb.Data
|
|||
.ThenInclude(s => s.GuildWars2Account)
|
||||
.Include(r => r.SignUps)
|
||||
.ThenInclude(s => s.RaidRole)
|
||||
.Include(r => r.DiscordRaidMessages)
|
||||
.FirstOrDefault(r => r.RaidId == raidId);
|
||||
}
|
||||
|
||||
public async Task AddOrEditRaid(Raid raid, List<RaidRole> rolesToDelete, List<RaidReminder> remindersToDelete)
|
||||
public async Task AddOrEditRaid(Raid raid, List<RaidRole> rolesToDelete, List<RaidReminder> remindersToDelete, List<DiscordRaidMessage> messagesToDelete)
|
||||
{
|
||||
if (raid != null)
|
||||
{
|
||||
|
@ -60,6 +64,7 @@ namespace Lieb.Data
|
|||
context.Update(raid);
|
||||
context.RaidRoles.RemoveRange(rolesToDelete);
|
||||
context.RaidReminders.RemoveRange(remindersToDelete);
|
||||
context.DiscordRaidMessages.RemoveRange(messagesToDelete);
|
||||
|
||||
//move users back to "Random" role
|
||||
if (raid.RaidType != RaidType.Planned)
|
||||
|
@ -74,6 +79,7 @@ namespace Lieb.Data
|
|||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
_discordService.PostRaidMessage(raid.RaidId);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -88,6 +94,7 @@ namespace Lieb.Data
|
|||
await context.SaveChangesAsync();
|
||||
context.Raids.Remove(raid);
|
||||
await context.SaveChangesAsync();
|
||||
_discordService.DeleteRaidMessages(raid);
|
||||
}
|
||||
|
||||
public async Task SignUp(int raidId, ulong liebUserId, int guildWars2AccountId, int plannedRoleId, SignUpType signUpType)
|
||||
|
@ -115,6 +122,7 @@ namespace Lieb.Data
|
|||
});
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
_discordService.PostRaidMessage(raidId);
|
||||
}
|
||||
|
||||
public async Task SignOff(int raidId, ulong liebUserId)
|
||||
|
@ -138,8 +146,8 @@ namespace Lieb.Data
|
|||
signUp.RaidRole = raid.Roles.FirstOrDefault(r => r.IsRandomSignUpRole);
|
||||
}
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
_discordService.PostRaidMessage(raidId);
|
||||
}
|
||||
|
||||
public async Task ChangeAccount(int raidId, ulong liebUserId, int guildWars2AccountId)
|
||||
|
@ -151,6 +159,7 @@ namespace Lieb.Data
|
|||
signUp.GuildWars2AccountId = guildWars2AccountId;
|
||||
}
|
||||
await context.SaveChangesAsync();
|
||||
_discordService.PostRaidMessage(raidId);
|
||||
}
|
||||
|
||||
public void ChangeSignUpType(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType)
|
||||
|
@ -180,6 +189,7 @@ namespace Lieb.Data
|
|||
signUp.SignUpType = signUpType;
|
||||
}
|
||||
context.SaveChanges();
|
||||
_discordService.PostRaidMessage(raidId);
|
||||
}
|
||||
|
||||
public bool IsRoleSignUpAllowed(ulong liebUserId, int plannedRoleId, SignUpType signUpType)
|
||||
|
@ -334,9 +344,8 @@ namespace Lieb.Data
|
|||
using var context = _contextFactory.CreateDbContext();
|
||||
List<Raid> raids = context.Raids
|
||||
.Include(r => r.Reminders)
|
||||
.Where(raid => raid.Reminders.Where(reminder => !reminder.Sent && raid.StartTimeUTC.AddHours(-reminder.HoursBeforeRaid) < DateTime.UtcNow).Any())
|
||||
.ToList();
|
||||
|
||||
|
||||
foreach(Raid raid in raids)
|
||||
{
|
||||
foreach(RaidReminder reminder in raid.Reminders.Where(reminder => !reminder.Sent && raid.StartTimeUTC.AddHours(-reminder.HoursBeforeRaid) < DateTime.UtcNow))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue