added napiszar, using MsEntitiFramework, addreactionrole command working just with link
This commit is contained in:
parent
1695770f9f
commit
d508bffac1
9 changed files with 318 additions and 94 deletions
99
helpers/reactionroleservice.cs
Normal file
99
helpers/reactionroleservice.cs
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using T3k3rg0.Helpers.Data;
|
||||
using T3k3rg0.Helpers.Models;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace T3k3rg0.Helpers
|
||||
{
|
||||
public static class ReactionRoleService
|
||||
{
|
||||
public static ulong? GetRoleId(ulong messageId, string emoji)
|
||||
{
|
||||
using var db = new RolesDbContext();
|
||||
var entry = db.ReactionRoles
|
||||
.FirstOrDefault(r => r.MessageId == messageId && r.Emoji == emoji);
|
||||
|
||||
return entry?.RoleId;
|
||||
}
|
||||
|
||||
public static List<(string Emoji, ulong RoleId)> GetAllRolesForMessage(ulong messageId)
|
||||
{
|
||||
using var db = new RolesDbContext();
|
||||
return db.ReactionRoles
|
||||
.Where(r => r.MessageId == messageId)
|
||||
.ToList()
|
||||
.Select(r => (r.Emoji, r.RoleId))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public static void AddReactionRole(ulong messageId, string emoji, ulong roleId)
|
||||
{
|
||||
using var db = new RolesDbContext();
|
||||
db.ReactionRoles.Add(new ReactionRole
|
||||
{
|
||||
MessageId = messageId,
|
||||
Emoji = emoji,
|
||||
RoleId = roleId
|
||||
});
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
public static void RemoveReactionRole(ulong messageId, string emoji)
|
||||
{
|
||||
using var db = new RolesDbContext();
|
||||
var entry = db.ReactionRoles
|
||||
.FirstOrDefault(r => r.MessageId == messageId && r.Emoji == emoji);
|
||||
|
||||
if (entry != null)
|
||||
{
|
||||
db.ReactionRoles.Remove(entry);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<ReactionRole> GetAllReactionRoles()
|
||||
{
|
||||
using var db = new RolesDbContext();
|
||||
return db.ReactionRoles.ToList(); // Minden rekord lekérése az adatbázisból
|
||||
}
|
||||
public static async Task<List<ReactionRole>> GetAllReactionRolesAsync()
|
||||
{
|
||||
await using var db = new RolesDbContext();
|
||||
return await db.ReactionRoles
|
||||
.OrderBy(r => r.MessageId)
|
||||
.ThenBy(r => r.Emoji)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public static async Task<(bool Success, string? Message, string? Emoji, ulong? RoleId)> RemoveReactionRoleByIdAsync(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var db = new RolesDbContext();
|
||||
|
||||
var allRoles = (await db.ReactionRoles.ToListAsync())
|
||||
.OrderBy(r => r.MessageId)
|
||||
.ThenBy(r => r.Emoji)
|
||||
.ToList();
|
||||
|
||||
if (id < 1 || id > allRoles.Count)
|
||||
return (false, "❌ Érvénytelen ID.", null, null);
|
||||
|
||||
var toRemove = allRoles[id - 1];
|
||||
db.ReactionRoles.Remove(toRemove);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return (true, null, toRemove.Emoji, toRemove.RoleId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
return (false, "❌ Hiba történt az eltávolítás során.", null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue