Added Discord Bot

This commit is contained in:
Sarah Faey 2022-11-06 19:10:58 +01:00
parent e7a0c9ae68
commit e445b2a181
48 changed files with 1255 additions and 157 deletions

3
.gitignore vendored
View file

@ -16,6 +16,9 @@
# Mono auto generated files # Mono auto generated files
mono_crash.* mono_crash.*
# Sqlite Database
Database/
# Build results # Build results
[Dd]ebug/ [Dd]ebug/
[Dd]ebugPublic/ [Dd]ebugPublic/

60
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,60 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": "Lieb Website",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Lieb/bin/Debug/net6.0/Lieb.dll",
"args": [],
"cwd": "${workspaceFolder}/Lieb",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": "DiscordBot",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/DiscordBot/bin/Debug/net6.0/DiscordBot.dll",
"args": [],
"cwd": "${workspaceFolder}/DiscordBot",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}

41
.vscode/tasks.json vendored Normal file
View file

@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Lieb/Lieb.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Lieb/Lieb.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/Lieb/Lieb.csproj"
],
"problemMatcher": "$msCompile"
}
]
}

View file

@ -0,0 +1,82 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using System.Reflection;
namespace DiscordBot
{
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()
{
_client.SlashCommandExecuted += SlashCommandHandler;
_client.ButtonExecuted += MyButtonHandler;
}
private async Task SlashCommandHandler(SocketSlashCommand command)
{
switch (command.Data.Name)
{
case Constants.SlashCommands.FIRST_COMMAND:
await HandleFirstCommand(command);
break;
}
await command.RespondAsync($"You executed {command.Data.Name}");
}
private async Task HandleFirstCommand(SocketSlashCommand command)
{
// We need to extract the user parameter from the command. since we only have one option and it's required, we can just use the first option.
var guildUser = (SocketGuildUser)command.Data.Options.First().Value;
// We remove the everyone role and select the mention of each role.
var roleList = string.Join(",\n", guildUser.Roles.Where(x => !x.IsEveryone).Select(x => x.Mention));
var embedBuiler = new EmbedBuilder()
.WithAuthor(guildUser.ToString(), guildUser.GetAvatarUrl() ?? guildUser.GetDefaultAvatarUrl())
.WithTitle("Roles")
.WithDescription(roleList)
.WithColor(Color.Green)
.WithCurrentTimestamp();
// Now, Let's respond with the embed.
await command.RespondAsync(embed: embedBuiler.Build());
}
public async Task MyButtonHandler(SocketMessageComponent component)
{
string[] ids = component.Data.CustomId.Split('-');
switch(ids[0])
{
case Constants.ComponentIds.SIGN_UP:
//await component.RespondAsync($"{component.User.Mention} has clicked the SignUp button!");
var mb = new ModalBuilder()
.WithTitle("Fav Food")
.WithCustomId("food_menu")
.AddTextInput("What??", "food_name", placeholder:"Pizza")
.AddTextInput("Why??", "food_reason", TextInputStyle.Paragraph,
"Kus it's so tasty");
//await component.RespondWithModalAsync(mb.Build());
await component.RespondAsync("hi", ephemeral: true);
break;
case Constants.ComponentIds.SIGN_OFF:
//await component.RespondAsync($"{component.User.Mention} has clicked the SignOff button!");
break;
}
}
}
}

16
DiscordBot/Constants.cs Normal file
View file

@ -0,0 +1,16 @@
namespace DiscordBot
{
public class Constants
{
public class ComponentIds
{
public const string SIGN_UP = "su";
public const string SIGN_OFF = "so";
}
public class SlashCommands
{
public const string FIRST_COMMAND = "first-command";
}
}
}

View file

@ -0,0 +1,88 @@
using Microsoft.AspNetCore.Mvc;
using SharedClasses.SharedModels;
using DiscordBot.Messages;
using Discord;
using Discord.WebSocket;
using System.Linq;
namespace DiscordBot.Controllers
{
[ApiController]
[Route("[controller]")]
public class RaidController : ControllerBase
{
DiscordSocketClient _client;
public RaidController(DiscordSocketClient client)
{
_client = client;
}
/*{"Title":"Testraid","Description":"This is a test raidnwith multiple lines?","Organizer":"Sarah","Guild":"LIEB","VoiceChat":"ts.lieb.games","RaidId":4,"StartTimeUTC":"2022-11-13T10:24:58.3955622+00:00","EndTimeUTC":"2022-11-14T12:24:58.3955622+00:00","Roles":[{"Name":"Random","Description":"RandomWithBoons","Spots":10,"Users":[]}],"DisocrdMessages":[{"GuildId":666953424734257182,"ChannelId":666954070388637697,"MessageId":0,"WebsiteDatabaseId":2}]}*/
[HttpPost]
[Route("[action]")]
public async Task<ApiRaid> PostRaidMessage(ApiRaid raid)
{
RaidMessage message = new RaidMessage(_client);
return await message.PostRaidMessage(raid);
}
[HttpPost]
[Route("[action]")]
public async Task DeleteRaidMessage(IEnumerable<ApiRaid.DiscordMessage> messages)
{
RaidMessage message = new RaidMessage(_client);
await message.DeleteRaidMessage(messages);
}
[HttpGet]
[Route("[action]")]
public List<DiscordServer> GetServers()
{
List<DiscordServer> servers = new List<DiscordServer>();
foreach(var guild in _client.Guilds)
{
DiscordServer server = new DiscordServer(){
Name = guild.Name,
Id = guild.Id
};
foreach(var channel in guild.Channels)
{
if( channel is SocketTextChannel && channel.Users.Where(u => u.Id == _client.CurrentUser.Id).Any())
{
server.Channels.Add(new DiscordChannel(){
Name = channel.Name,
Id = channel.Id
});
}
}
servers.Add(server);
}
return servers;
}
[HttpPost]
[Route("[action]")]
public async Task SendUserReminder(ApiUserReminder reminder)
{
foreach(ulong userId in reminder.UserIds)
{
await _client.GetUser(userId).SendMessageAsync(reminder.Message);
}
}
[HttpPost]
[Route("[action]")]
public async Task SendChannelReminder(ApiChannelReminder reminder)
{
var channel = _client.GetGuild(reminder.DiscordServerId).GetChannel(reminder.DiscordChannelId);
if (channel != null && channel is IMessageChannel)
{
IMessageChannel messageChannel = channel as IMessageChannel;
await messageChannel.SendMessageAsync(reminder.Message);
}
}
}
}

View file

@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Mvc;
namespace DiscordBot.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

View file

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Discord.Net" Version="3.7.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SharedClasses\SharedClasses.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,131 @@
using Discord;
using Discord.WebSocket;
using System;
using System.ComponentModel.DataAnnotations;
using SharedClasses.SharedModels;
namespace DiscordBot.Messages
{
public class RaidMessage
{
DiscordSocketClient _client;
public RaidMessage(DiscordSocketClient client)
{
_client = client;
}
public async Task DeleteRaidMessage(IEnumerable<ApiRaid.DiscordMessage> messages)
{
foreach (ApiRaid.DiscordMessage message in messages)
{
var channel = _client.GetGuild(message.GuildId).GetChannel(message.ChannelId);
if (channel != null && channel is IMessageChannel)
{
IMessageChannel messageChannel = channel as IMessageChannel;
if (message.MessageId != 0)
{
await messageChannel.DeleteMessageAsync(message.MessageId);
}
}
}
}
public async Task<ApiRaid> PostRaidMessage(ApiRaid raid)
{
var menuBuilder = new SelectMenuBuilder()
.WithPlaceholder("Select an option")
.WithCustomId("menu-1")
.WithMinValues(1)
.WithMaxValues(1)
.AddOption("Option A", "opt-a", "Option B is lying!")
.AddOption("Option B", "opt-b", "Option A is telling the truth!");
var builder = new ComponentBuilder()
.WithButton("SignUp", $"{Constants.ComponentIds.SIGN_UP}-{raid.RaidId.ToString()}", ButtonStyle.Secondary)
.WithButton("SignOff", $"{Constants.ComponentIds.SIGN_OFF}-{raid.RaidId.ToString()}", ButtonStyle.Secondary);
//.WithSelectMenu(menuBuilder);
MessageComponent components = builder.Build();
Embed raidMessage = CreateRaidMessage(raid);
foreach (ApiRaid.DiscordMessage message in raid.DisocrdMessages)
{
var channel = _client.GetGuild(message.GuildId).GetChannel(message.ChannelId);
if (channel != null && channel is IMessageChannel)
{
IMessageChannel messageChannel = channel as IMessageChannel;
if (message.MessageId != 0)
{
MessageProperties properties = new MessageProperties()
{
Embed = raidMessage,
Components = components
};
await messageChannel.ModifyMessageAsync(message.MessageId, new Action<MessageProperties>(x => x = properties));
}
else
{
IUserMessage sentMessage = await messageChannel.SendMessageAsync(embed: raidMessage, components: components);
message.MessageId = sentMessage.Id;
}
}
}
return raid;
}
public Embed CreateRaidMessage(ApiRaid raid)
{
var embed = new EmbedBuilder()
{
Title = raid.Title,
Description = raid.Description,
Footer = new EmbedFooterBuilder()
{
Text = $"RaidId: {raid.RaidId}"
}
};
AddMessageDetails(raid, ref embed);
AddMessageRoles(raid, ref embed);
return embed.Build();
}
private void AddMessageDetails(ApiRaid raid, ref EmbedBuilder embed)
{
embed.AddField("Date", $"{raid.StartTimeUTC.ToLocalTime().DateTime.ToLongDateString()}");
embed.AddField("Time", $"from: {raid.StartTimeUTC.ToLocalTime().DateTime.ToShortTimeString()} to: {raid.EndTimeUTC.ToLocalTime().DateTime.ToShortTimeString()}");
embed.AddField("Organisator", raid.Organizer, true);
embed.AddField("Guild", raid.Guild, true);
embed.AddField("Voice chat", raid.VoiceChat, true);
}
private void AddMessageRoles(ApiRaid raid, ref EmbedBuilder embed)
{
Dictionary<string, string> fieldList = new Dictionary<string, string>();
embed.AddField("Signed up", $"({raid.Roles.Sum(r => r.Users.Count)}/{raid.Roles.Sum(r => r.Spots)}):");
foreach (ApiRaid.Role role in raid.Roles)
{
//print signed up users
string signedUpUsers = PrintUsers(role);
if (string.IsNullOrEmpty(signedUpUsers)) signedUpUsers = "-";
string fieldName = $"{role.Name}: {role.Description} ({role.Users.Where(u => string.IsNullOrWhiteSpace(u.Status)).Count()}/{role.Spots})";
embed.AddField(fieldName, signedUpUsers);
}
}
private string PrintUsers(ApiRaid.Role role)
{
string rolesString = string.Empty;
foreach (ApiRaid.Role.User user in role.Users.OrderBy(u => u.Status))
{
string status = !string.IsNullOrWhiteSpace(user.Status) ? $" - {user.Status}" : string.Empty;
rolesString += $"\t{user.UserName} ({user.AccountName}) {status}\n";
}
return rolesString;
}
}
}

123
DiscordBot/Program.cs Normal file
View file

@ -0,0 +1,123 @@
using Discord;
using Discord.Commands;
using Discord.Net;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System.Reflection;
namespace DiscordBot
{
internal class Program
{
DiscordSocketClient _client;
public static Task Main(string[] args) => new Program().MainAsync(args);
public async Task MainAsync(string[] args)
{
var dicordConfig = new DiscordSocketConfig()
{
};
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddSingleton(dicordConfig);
builder.Services.AddSingleton<DiscordSocketClient>();
builder.Services.AddSingleton<CommandService>();
builder.Services.AddSingleton<CommandHandler>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
_client = app.Services.GetRequiredService<DiscordSocketClient>();
var commandHandler = app.Services.GetRequiredService<CommandHandler>();
await commandHandler.InstallCommandsAsync();
_client.Log += Log;
//_client.Ready += Client_Ready;
// You can assign your bot token to a string, and pass that in to connect.
// This is, however, insecure, particularly if you plan to have your code hosted in a public repository.
var token = "MTAxODE3MDczMDU0Mzg1Nzc3NA.GA2Qzu.__kLICXm-8VLTQD6KzL-uGaPx60Q6fn8K6lE3A";
//var token = "Njc2NzQ4NjM2NjI5MTA2NzE4.Xtu2Ww._2oF7lQtxLLOUhfAIMxocN52dYo";
// Some alternative options would be to keep your token in an Environment Variable or a standalone file.
// var token = Environment.GetEnvironmentVariable("NameOfYourEnvironmentVariable");
// var token = File.ReadAllText("token.txt");
// var token = JsonConvert.DeserializeObject<AConfigurationClass>(File.ReadAllText("config.json")).Token;
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
app.Run();
// Block this task until the program is closed.
await Task.Delay(-1);
}
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
public async Task Client_Ready()
{
// Let's build a guild command! We're going to need a guild so lets just put that in a variable.
var guild = _client.GetGuild(666953424734257182);
// Next, lets create our slash command builder. This is like the embed builder but for slash commands.
var guildCommand = new SlashCommandBuilder();
// Note: Names have to be all lowercase and match the regular expression ^[\w-]{3,32}$
guildCommand.WithName("first-command");
// Descriptions can have a max length of 100.
guildCommand.WithDescription("This is my first guild slash command!");
try
{
// Now that we have our builder, we can call the CreateApplicationCommandAsync method to make our slash command.
await guild.CreateApplicationCommandAsync(guildCommand.Build());
// Using the ready event is a simple implementation for the sake of the example. Suitable for testing and development.
// For a production bot, it is recommended to only run the CreateGlobalApplicationCommandAsync() once for each command.
}
catch (ApplicationCommandException exception)
{
// If our command was invalid, we should catch an ApplicationCommandException. This exception contains the path of the error as well as the error message. You can serialize the Error field in the exception to get a visual of where your error is.
//var json = JsonConvert.SerializeObject(exception.Error, Formatting.Indented);
// You can send this error somewhere or just print it to the console, for this example we're just going to print it.
//Console.WriteLine(json);
}
}
}
}

View file

@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:52668",
"sslPort": 44379
}
},
"profiles": {
"DiscordBot": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7240;http://localhost:5240",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View file

@ -0,0 +1,13 @@
namespace DiscordBot
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}

View file

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View file

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View file

@ -5,7 +5,7 @@ VisualStudioVersion = 17.0.32126.317
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lieb", "Lieb\Lieb.csproj", "{48554958-F16E-466A-B9B7-F17511FDA415}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lieb", "Lieb\Lieb.csproj", "{48554958-F16E-466A-B9B7-F17511FDA415}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{8F30A150-F8B2-4173-B2AA-5A9C1BE1EF51}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBot", "DiscordBot\DiscordBot.csproj", "{73A8E9D9-4997-4D91-8319-F2E51F18F66C}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -17,10 +17,10 @@ Global
{48554958-F16E-466A-B9B7-F17511FDA415}.Debug|Any CPU.Build.0 = Debug|Any CPU {48554958-F16E-466A-B9B7-F17511FDA415}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48554958-F16E-466A-B9B7-F17511FDA415}.Release|Any CPU.ActiveCfg = Release|Any CPU {48554958-F16E-466A-B9B7-F17511FDA415}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48554958-F16E-466A-B9B7-F17511FDA415}.Release|Any CPU.Build.0 = Release|Any CPU {48554958-F16E-466A-B9B7-F17511FDA415}.Release|Any CPU.Build.0 = Release|Any CPU
{8F30A150-F8B2-4173-B2AA-5A9C1BE1EF51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {73A8E9D9-4997-4D91-8319-F2E51F18F66C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8F30A150-F8B2-4173-B2AA-5A9C1BE1EF51}.Debug|Any CPU.Build.0 = Debug|Any CPU {73A8E9D9-4997-4D91-8319-F2E51F18F66C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8F30A150-F8B2-4173-B2AA-5A9C1BE1EF51}.Release|Any CPU.ActiveCfg = Release|Any CPU {73A8E9D9-4997-4D91-8319-F2E51F18F66C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8F30A150-F8B2-4173-B2AA-5A9C1BE1EF51}.Release|Any CPU.Build.0 = Release|Any CPU {73A8E9D9-4997-4D91-8319-F2E51F18F66C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View file

@ -1,130 +0,0 @@
using Lieb.Data;
using Lieb.Models.GuildWars2;
using Lieb.Models.GuildWars2.Raid;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Net;
namespace Lieb.Controllers
{
public class GuildWars2RaidController : ControllerBase
{
private readonly RaidService _raidService;
private readonly UserService _userService;
private readonly IDbContextFactory<LiebContext> _contextFactory;
public GuildWars2RaidController(RaidService raidService, UserService userService, IDbContextFactory<LiebContext> contextFactory)
{
_raidService = raidService;
_userService = userService;
_contextFactory = contextFactory;
}
[HttpGet]
public int GetRaidIdByDiscordMessageId(int discordMessageId)
{
using var context = _contextFactory.CreateDbContext();
DiscordRaidMessage discordMessage = context.DiscordRaidMessages.FirstOrDefault(m => m.DiscordRaidMessageId == discordMessageId);
//TODO retrun 404
return discordMessage != null ? discordMessage.RaidId : -1;
}
[HttpGet]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<IActionResult> GetRaidIdByDiscordMessageId([FromRoute] ulong discordMessageId)
{
using var context = _contextFactory.CreateDbContext();
DiscordRaidMessage? discordMessage = await context.DiscordRaidMessages.FirstOrDefaultAsync(m => m.DiscordMessageId == discordMessageId);
if (discordMessage == null)
{
return Problem(statusCode: (int)HttpStatusCode.NotFound);
}
return Ok(discordMessage.RaidId);
}
[HttpGet]
[ProducesResponseType(typeof(List<RaidRole>), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<IActionResult> GetFreeRoles([FromRoute] int raidId)
{
Raid raid = _raidService.GetRaid(raidId);
Dictionary<int, int> roleMap = new Dictionary<int, int>();
foreach(RaidSignUp signUp in raid.SignUps)
{
if (signUp.SignUpType == SignUpType.SignedUp)
{
roleMap[signUp.RaidRoleId] += 1;
}
}
return Ok(raid.Roles.Where(r => roleMap.ContainsKey(r.RaidRoleId) && roleMap[r.RaidRoleId] < r.Spots).ToList());
}
[HttpGet]
[ProducesResponseType(typeof(List<GuildWars2Account>), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<IActionResult> GetGuildwars2Accounts([FromRoute] ulong userId)
{
using var context = _contextFactory.CreateDbContext();
List<GuildWars2Account>? gw2Accounts = (await context.LiebUsers.FirstOrDefaultAsync(u => u.Id == userId))?.GuildWars2Accounts.ToList();
if (gw2Accounts == null)
{
return Problem(statusCode: (int)HttpStatusCode.NotFound);
}
return Ok(gw2Accounts);
}
[HttpPost]
public string SignUp(int raidId, ulong discordUserId, int guildWars2AccountId, int roleId, SignUpType signUpType)
{
int userId = _userService.GetLiebUserId(discordUserId).Result;
_raidService.SignUp(raidId, userId, guildWars2AccountId, roleId, signUpType).Wait();
return String.Empty;
}
[HttpGet("metadata/{fileId}")]
[Consumes("application/json")]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<IActionResult> GetFileMetadata([FromRoute] Guid fileId)
{
using var context = _contextFactory.CreateDbContext();
DiscordRaidMessage discordMessage = context.DiscordRaidMessages.FirstOrDefault(m => m.DiscordRaidMessageId == discordMessageId);
if (discordMessage == null)
{
return Problem(statusCode: (int)HttpStatusCode.NotFound);
}
return Ok(discordMessage.RaidId);
}
[HttpGet("metadata/{fileId}")]
[Authorize(Roles = Roles.TimeBoard)]
[Consumes("application/json")]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<IActionResult> GetFileMetadata([FromRoute] Guid fileId)
{
using var context = _contextFactory.CreateDbContext();
DiscordRaidMessage discordMessage = context.DiscordRaidMessages.FirstOrDefault(m => m.DiscordRaidMessageId == discordMessageId);
if(discordMessage == null)
{
return Problem(statusCode: (int)HttpStatusCode.NotFound);
}
return Ok(discordMessage.RaidId);
}
}
}

View file

@ -2,6 +2,7 @@
{ {
public static class Constants public static class Constants
{ {
public const string HttpClientName = "Discord";
public const string ClaimType = "Role"; public const string ClaimType = "Role";
public static readonly int RaidEditPowerLevel = Roles.Moderator.PowerLevel; public static readonly int RaidEditPowerLevel = Roles.Moderator.PowerLevel;

View file

@ -40,8 +40,8 @@ namespace Lieb.Data
GuildWars2Account bloodseeker = new GuildWars2Account() { AccountName = "Bloodseeker.2043" }; GuildWars2Account bloodseeker = new GuildWars2Account() { AccountName = "Bloodseeker.2043" };
var users = new LiebUser[] 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=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=194863625477816321, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith, sarah} },
#if DEBUG #if DEBUG
new LiebUser{Id=1, Name="Lisa", GuildWars2Accounts = new List<GuildWars2Account>(){ hierpiepts}}, new LiebUser{Id=1, Name="Lisa", GuildWars2Accounts = new List<GuildWars2Account>(){ hierpiepts}},
new LiebUser{Id=2, Name="Simon", GuildWars2Accounts = new List<GuildWars2Account>(){ bloodseeker}} new LiebUser{Id=2, Name="Simon", GuildWars2Accounts = new List<GuildWars2Account>(){ bloodseeker}}

253
Lieb/Data/DiscordService.cs Normal file
View 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
};
}
}
}

View file

@ -7,10 +7,12 @@ namespace Lieb.Data
public class RaidService public class RaidService
{ {
private readonly IDbContextFactory<LiebContext> _contextFactory; private readonly IDbContextFactory<LiebContext> _contextFactory;
private readonly DiscordService _discordService;
public RaidService(IDbContextFactory<LiebContext> contextFactory) public RaidService(IDbContextFactory<LiebContext> contextFactory, DiscordService discordService)
{ {
_contextFactory = contextFactory; _contextFactory = contextFactory;
_discordService = discordService;
} }
public List<Raid> GetRaids() public List<Raid> GetRaids()
@ -26,6 +28,7 @@ namespace Lieb.Data
.ThenInclude(s => s.GuildWars2Account) .ThenInclude(s => s.GuildWars2Account)
.Include(r => r.SignUps) .Include(r => r.SignUps)
.ThenInclude(s => s.RaidRole) .ThenInclude(s => s.RaidRole)
.Include(r => r.DiscordRaidMessages)
.ToList(); .ToList();
} }
@ -42,10 +45,11 @@ namespace Lieb.Data
.ThenInclude(s => s.GuildWars2Account) .ThenInclude(s => s.GuildWars2Account)
.Include(r => r.SignUps) .Include(r => r.SignUps)
.ThenInclude(s => s.RaidRole) .ThenInclude(s => s.RaidRole)
.Include(r => r.DiscordRaidMessages)
.FirstOrDefault(r => r.RaidId == raidId); .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) if (raid != null)
{ {
@ -60,6 +64,7 @@ namespace Lieb.Data
context.Update(raid); context.Update(raid);
context.RaidRoles.RemoveRange(rolesToDelete); context.RaidRoles.RemoveRange(rolesToDelete);
context.RaidReminders.RemoveRange(remindersToDelete); context.RaidReminders.RemoveRange(remindersToDelete);
context.DiscordRaidMessages.RemoveRange(messagesToDelete);
//move users back to "Random" role //move users back to "Random" role
if (raid.RaidType != RaidType.Planned) if (raid.RaidType != RaidType.Planned)
@ -74,6 +79,7 @@ namespace Lieb.Data
await context.SaveChangesAsync(); await context.SaveChangesAsync();
} }
_discordService.PostRaidMessage(raid.RaidId);
} }
} }
@ -88,6 +94,7 @@ namespace Lieb.Data
await context.SaveChangesAsync(); await context.SaveChangesAsync();
context.Raids.Remove(raid); context.Raids.Remove(raid);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
_discordService.DeleteRaidMessages(raid);
} }
public async Task SignUp(int raidId, ulong liebUserId, int guildWars2AccountId, int plannedRoleId, SignUpType signUpType) public async Task SignUp(int raidId, ulong liebUserId, int guildWars2AccountId, int plannedRoleId, SignUpType signUpType)
@ -115,6 +122,7 @@ namespace Lieb.Data
}); });
await context.SaveChangesAsync(); await context.SaveChangesAsync();
} }
_discordService.PostRaidMessage(raidId);
} }
public async Task SignOff(int raidId, ulong liebUserId) public async Task SignOff(int raidId, ulong liebUserId)
@ -138,8 +146,8 @@ namespace Lieb.Data
signUp.RaidRole = raid.Roles.FirstOrDefault(r => r.IsRandomSignUpRole); signUp.RaidRole = raid.Roles.FirstOrDefault(r => r.IsRandomSignUpRole);
} }
} }
await context.SaveChangesAsync(); await context.SaveChangesAsync();
_discordService.PostRaidMessage(raidId);
} }
public async Task ChangeAccount(int raidId, ulong liebUserId, int guildWars2AccountId) public async Task ChangeAccount(int raidId, ulong liebUserId, int guildWars2AccountId)
@ -151,6 +159,7 @@ namespace Lieb.Data
signUp.GuildWars2AccountId = guildWars2AccountId; signUp.GuildWars2AccountId = guildWars2AccountId;
} }
await context.SaveChangesAsync(); await context.SaveChangesAsync();
_discordService.PostRaidMessage(raidId);
} }
public void ChangeSignUpType(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType) public void ChangeSignUpType(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType)
@ -180,6 +189,7 @@ namespace Lieb.Data
signUp.SignUpType = signUpType; signUp.SignUpType = signUpType;
} }
context.SaveChanges(); context.SaveChanges();
_discordService.PostRaidMessage(raidId);
} }
public bool IsRoleSignUpAllowed(ulong liebUserId, int plannedRoleId, SignUpType signUpType) public bool IsRoleSignUpAllowed(ulong liebUserId, int plannedRoleId, SignUpType signUpType)
@ -334,9 +344,8 @@ namespace Lieb.Data
using var context = _contextFactory.CreateDbContext(); using var context = _contextFactory.CreateDbContext();
List<Raid> raids = context.Raids List<Raid> raids = context.Raids
.Include(r => r.Reminders) .Include(r => r.Reminders)
.Where(raid => raid.Reminders.Where(reminder => !reminder.Sent && raid.StartTimeUTC.AddHours(-reminder.HoursBeforeRaid) < DateTime.UtcNow).Any())
.ToList(); .ToList();
foreach(Raid raid in raids) foreach(Raid raid in raids)
{ {
foreach(RaidReminder reminder in raid.Reminders.Where(reminder => !reminder.Sent && raid.StartTimeUTC.AddHours(-reminder.HoursBeforeRaid) < DateTime.UtcNow)) foreach(RaidReminder reminder in raid.Reminders.Where(reminder => !reminder.Sent && raid.StartTimeUTC.AddHours(-reminder.HoursBeforeRaid) < DateTime.UtcNow))

View file

@ -0,0 +1,62 @@
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

@ -0,0 +1,25 @@
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,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Discord.Net" Version="3.7.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.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" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.2"> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.2">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
@ -18,7 +20,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" /> <ProjectReference Include="..\SharedClasses\SharedClasses.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -67,7 +67,8 @@ namespace Lieb.Models.GuildWars2.Raid
{ {
this.Reminders.Add(new RaidReminder() this.Reminders.Add(new RaidReminder()
{ {
ChannelId = reminder.ChannelId, DiscordServerId = reminder.DiscordServerId,
DiscordChannelId = reminder.DiscordChannelId,
HoursBeforeRaid = reminder.HoursBeforeRaid, HoursBeforeRaid = reminder.HoursBeforeRaid,
Message = reminder.Message, Message = reminder.Message,
Sent = reminder.Sent, Sent = reminder.Sent,

View file

@ -23,8 +23,14 @@ namespace Lieb.Models.GuildWars2.Raid
[Required] [Required]
public double HoursBeforeRaid { get; set; } public double HoursBeforeRaid { get; set; }
public ulong ChannelId { get; set; } public ulong DiscordServerId { get; set; }
public ulong DiscordChannelId { get; set; }
public bool Sent { get; set; } = false; public bool Sent { get; set; } = false;
public int RaidId { get; set; }
public Raid Raid { get; set; }
} }
} }

View file

@ -5,8 +5,10 @@
@using Lieb.Models.GuildWars2.Raid @using Lieb.Models.GuildWars2.Raid
@using System.ComponentModel.DataAnnotations @using System.ComponentModel.DataAnnotations
@using System.Security.Claims @using System.Security.Claims
@using SharedClasses.SharedModels
@inject RaidService RaidService @inject RaidService RaidService
@inject UserService UserService @inject UserService UserService
@inject DiscordService DiscordService
@inject TimeZoneService TimeZoneService @inject TimeZoneService TimeZoneService
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider @inject AuthenticationStateProvider AuthenticationStateProvider
@ -143,6 +145,46 @@
</p> </p>
} }
<p>
<label>
Discord Messages:
</label>
<button type=button @onclick="() => AddDiscordMessageClicked()">Add message</button>
<table>
<tr>
<th>Server</th>
<th>Channel</th>
</tr>
@foreach( DiscordRaidMessage message in _raid.DiscordRaidMessages)
{
bool disableEdit = message.DiscordRaidMessageId != 0;
<tr>
<td>
<InputSelect @bind-Value="message.DiscordGuildId" disabled="@disableEdit">
@foreach(DiscordServer item in _discordServers)
{
<option value="@item.Id">@item.Name</option>
}
</InputSelect>
</td>
<td>
<InputSelect @bind-Value="message.DiscordChannelId" disabled="@disableEdit">
@if(message.DiscordGuildId > 0)
{
List<DiscordChannel> channels = _discordServers.Where(s => s.Id == message.DiscordGuildId).FirstOrDefault(new DiscordServer()).Channels;
@foreach(DiscordChannel item in channels)
{
<option value="@item.Id">@item.Name</option>
}
}
</InputSelect>
</td>
<td><button type=button @onclick="() => DeleteMessageClicked(message)">Delete</button></td>
</tr>
}
</table>
</p>
<ValidationSummary /> <ValidationSummary />
<label class="validation-message" >@_errorMessage</label> <label class="validation-message" >@_errorMessage</label>
<button type="submit">Submit</button> <button type="submit">Submit</button>
@ -168,6 +210,10 @@
private DateTimeOffset _freeForAllTime; private DateTimeOffset _freeForAllTime;
private List<RaidRole> _rolesToDelete = new List<RaidRole>(); private List<RaidRole> _rolesToDelete = new List<RaidRole>();
private List<RaidReminder> _remindersToDelete = new List<RaidReminder>();
private List<DiscordRaidMessage> _messagesToDelete = new List<DiscordRaidMessage>();
private List<DiscordServer> _discordServers = new List<DiscordServer>();
@ -206,6 +252,8 @@
{ {
_raid = new Raid(); _raid = new Raid();
} }
_discordServers = await DiscordService.GetServers();
} }
async Task AddRoleClicked() async Task AddRoleClicked()
@ -222,6 +270,20 @@
} }
_raid.Roles.Remove(role); _raid.Roles.Remove(role);
} }
async Task AddDiscordMessageClicked()
{
_raid.DiscordRaidMessages.Add(new DiscordRaidMessage());
}
async Task DeleteMessageClicked(DiscordRaidMessage message)
{
if(message.DiscordRaidMessageId != 0)
{
_messagesToDelete.Add(message);
}
_raid.DiscordRaidMessages.Remove(message);
}
async Task DeleteRaidClicked() async Task DeleteRaidClicked()
{ {
@ -276,7 +338,7 @@
_raid.RaidOwnerId = _user.Id; _raid.RaidOwnerId = _user.Id;
} }
await RaidService.AddOrEditRaid(_raid, _rolesToDelete, new List<RaidReminder>()); await RaidService.AddOrEditRaid(_raid, _rolesToDelete, _remindersToDelete, _messagesToDelete);
NavigationManager.NavigateTo("raidoverview"); NavigationManager.NavigateTo("raidoverview");
} }
} }

View file

@ -1,5 +1,5 @@
@page "/useredit" @page "/useredit"
@page "/useredit/{userId}" @page "/useredit/{_userId}"
@using Lieb.Data @using Lieb.Data
@using Lieb.Models @using Lieb.Models
@using Lieb.Models.GuildWars2 @using Lieb.Models.GuildWars2
@ -59,6 +59,7 @@
</tr> </tr>
} }
</table> </table>
hi
<br /> <br />
@code { @code {

View file

@ -1,7 +1,12 @@
using Discord.OAuth2; using Discord;
using Discord.Commands;
using Discord.OAuth2;
using Discord.WebSocket;
using Lieb.Data; using Lieb.Data;
using Lieb.Discord;
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Net.Http.Headers;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -10,7 +15,8 @@ builder.Services.AddRazorPages();
#if DEBUG #if DEBUG
builder.Services.AddDbContextFactory<LiebContext>(opt => builder.Services.AddDbContextFactory<LiebContext>(opt =>
opt.UseSqlServer(builder.Configuration.GetConnectionString("LiebContext")).EnableSensitiveDataLogging(), ServiceLifetime.Transient); //opt.UseSqlServer(builder.Configuration.GetConnectionString("LiebContext")).EnableSensitiveDataLogging(), ServiceLifetime.Transient);
opt.UseSqlite(builder.Configuration.GetConnectionString("LiebContext")));
#else #else
builder.Services.AddDbContextFactory<LiebContext>(opt => builder.Services.AddDbContextFactory<LiebContext>(opt =>
opt.UseMySql(builder.Configuration.GetConnectionString("LiebContext"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("LiebContext"))), ServiceLifetime.Transient); opt.UseMySql(builder.Configuration.GetConnectionString("LiebContext"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("LiebContext"))), ServiceLifetime.Transient);
@ -27,8 +33,21 @@ builder.Services.AddScoped<GuildWars2AccountService>();
builder.Services.AddScoped<GuildWars2BuildService>(); builder.Services.AddScoped<GuildWars2BuildService>();
builder.Services.AddScoped<RaidRandomizerService>(); builder.Services.AddScoped<RaidRandomizerService>();
builder.Services.AddScoped<TimeZoneService>(); builder.Services.AddScoped<TimeZoneService>();
builder.Services.AddScoped<DiscordService>();
builder.Services.AddHostedService<TimerService>(); builder.Services.AddHostedService<TimerService>();
/*
#region discord
//DiscordSocketClient client = new DiscordSocketClient();
//client.Log += Log;
var token = File.ReadAllText("token.txt");
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
builder.Services.AddSingleton(client);
builder.Services.AddSingleton<CommandService>();
builder.Services.AddSingleton<CommandHandler>();
#endregion discord*/
builder.Services.AddAuthentication(opt => builder.Services.AddAuthentication(opt =>
{ {
@ -60,6 +79,24 @@ builder.Services.AddAuthorization(options =>
Constants.Roles.Admin.Name })); Constants.Roles.Admin.Name }));
}); });
builder.Services.AddHttpClient(Constants.HttpClientName , httpClient =>
{
httpClient.BaseAddress = new Uri("http://localhost:5240/");
// using Microsoft.Net.Http.Headers;
// The GitHub API requires two headers.
httpClient.DefaultRequestHeaders.Add(
HeaderNames.Accept, "application/vnd.github.v3+json");
httpClient.DefaultRequestHeaders.Add(
HeaderNames.UserAgent, "HttpRequestsSample");
}).ConfigurePrimaryHttpMessageHandler(() => {
var handler = new HttpClientHandler();
if (builder.Environment.IsDevelopment())
{
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
}
return handler;
});
var app = builder.Build(); var app = builder.Build();

View file

@ -8,6 +8,6 @@
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"ConnectionStrings": { "ConnectionStrings": {
"LiebContext": "Server=(localdb)\\mssqllocaldb;Database=LiebContext-f9dae0eb-8d5c-495d-ab99-da7951e0638b;Trusted_Connection=True;MultipleActiveResultSets=true" "LiebContext": "Data Source=./Database/mydb.sqlite;"
} }
} }

View file

@ -1,7 +0,0 @@
namespace Shared
{
public class Class1
{
}
}

View file

@ -6,4 +6,8 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
</ItemGroup>
</Project> </Project>

View file

@ -0,0 +1,58 @@
namespace SharedClasses.SharedModels
{
public class ApiRaid
{
public string Title { get; set; } = String.Empty;
public string Description { get; set; } = String.Empty;
public string Organizer { get; set; } = String.Empty;
public string Guild { get; set; } = String.Empty;
public string VoiceChat { get; set; } = String.Empty;
public int RaidId { get; set; }
public DateTimeOffset StartTimeUTC { get; set; } = DateTime.Now;
public DateTimeOffset EndTimeUTC { get; set; }
public List<Role> Roles { get; set; } = new List<Role>();
public List<DiscordMessage> DisocrdMessages { get; set; } = new List<DiscordMessage>();
public class DiscordMessage
{
public ulong GuildId { get; set; } = 0;
public ulong ChannelId { get; set; } = 0;
public ulong MessageId { get; set; } = 0;
public int WebsiteDatabaseId {get; set; } = 0;
}
public class Role
{
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public int Spots { get; set; } = 1;
public List<User> Users { get; set; }
public class User
{
public string UserName { get; set; } = string.Empty;
public string AccountName { get; set; } = string.Empty;
public string Status { get; set; } = string.Empty;
}
}
}
}

View file

@ -0,0 +1,19 @@
namespace SharedClasses.SharedModels
{
public class ApiUserReminder
{
public string Message { get; set; }
public List<ulong> UserIds {get; set;} = new List<ulong>();
}
public class ApiChannelReminder
{
public string Message { get; set; }
public ulong DiscordServerId { get; set; }
public ulong DiscordChannelId { get; set; }
}
}

View file

@ -0,0 +1,12 @@
namespace SharedClasses.SharedModels
{
public class ApiRole
{
public string Name { get; set; } = String.Empty;
public string Description { get; set; } = String.Empty;
public bool IsSignUpAllowed{ get; set; } = false;
}
}

View file

@ -0,0 +1,11 @@
namespace SharedClasses.SharedModels
{
public class ApiSignUp
{
public int raidId {get; set;}
public ulong userId {get; set;}
public int gw2AccountId {get; set;}
public int roleId {get; set;}
}
}

View file

@ -0,0 +1,16 @@
namespace SharedClasses.SharedModels
{
public class DiscordServer
{
public string Name {get; set;}
public ulong Id {get; set;}
public List<DiscordChannel> Channels {get; set;} = new List<DiscordChannel>();
}
public class DiscordChannel
{
public string Name {get; set;}
public ulong Id {get; set;}
}
}