From f680dd869b581b00a5feb9958808d7db9d8d760c Mon Sep 17 00:00:00 2001 From: balos Date: Sat, 26 Apr 2025 20:05:22 +0200 Subject: [PATCH] first commit --- .gitignore | 5 ++++ bot.csproj | 18 ++++++++++++ commands/admin.cs | 18 ++++++++++++ commands/hello.cs | 19 +++++++++++++ events/reaction-roles.cs | 59 ++++++++++++++++++++++++++++++++++++++++ program.cs | 47 ++++++++++++++++++++++++++++++++ 6 files changed, 166 insertions(+) create mode 100644 .gitignore create mode 100644 bot.csproj create mode 100644 commands/admin.cs create mode 100644 commands/hello.cs create mode 100644 events/reaction-roles.cs create mode 100644 program.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..984dcc3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +.env + +databases/ diff --git a/bot.csproj b/bot.csproj new file mode 100644 index 0000000..8c94810 --- /dev/null +++ b/bot.csproj @@ -0,0 +1,18 @@ + + + + Exe + net9.0 + false + enable + + + + + + + + + + + diff --git a/commands/admin.cs b/commands/admin.cs new file mode 100644 index 0000000..4a4c819 --- /dev/null +++ b/commands/admin.cs @@ -0,0 +1,18 @@ +using DSharpPlus.CommandsNext; +using DSharpPlus.CommandsNext.Attributes; +using System.Threading.Tasks; +using System.Linq; + +public class HasAdminRole : CheckBaseAttribute +{ + public override async Task ExecuteCheckAsync(CommandContext ctx, bool help) + { + bool hasAdminRole = ctx.Member?.Roles.Any(role => role.Name == "Admin") ?? false; + if (!hasAdminRole) + { + await ctx.RespondAsync("Csicskáknak kuss van !"); + } + return (hasAdminRole); + } +} + diff --git a/commands/hello.cs b/commands/hello.cs new file mode 100644 index 0000000..6acbcf3 --- /dev/null +++ b/commands/hello.cs @@ -0,0 +1,19 @@ +using DSharpPlus.CommandsNext; +using DSharpPlus.CommandsNext.Attributes; +using System.Threading.Tasks; + +namespace Commands +{ + public class Hello : BaseCommandModule + { + [Command("hello")] + [Description("Beköszönés. Hello World")] + [HasAdminRole] + public async Task HelloCommand(CommandContext ctx) + { + await ctx.RespondAsync($"Szia! T3kerg0 vagyok!"); + } + + } +} + diff --git a/events/reaction-roles.cs b/events/reaction-roles.cs new file mode 100644 index 0000000..440c4f2 --- /dev/null +++ b/events/reaction-roles.cs @@ -0,0 +1,59 @@ +using DSharpPlus; +using DSharpPlus.EventArgs; +using Microsoft.Data.Sqlite; +using System.Threading.Tasks; +using System; + +namespace T3k3rg0 +{ + public static class ReactionRoleHandler + { + public static void Register(DiscordClient discord) + { + discord.MessageReactionAdded += OnReactionAdded; + discord.MessageReactionRemoved += OnReactionRemoved; + } + + private static async Task OnReactionAdded(DiscordClient client, MessageReactionAddEventArgs e) + { + if (e.User.IsBot) return; + + var dbPath = Environment.GetEnvironmentVariable("DB_ROLES_LOC"); + using var connection = new SqliteConnection($"Data Source={dbPath}"); + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = @"SELECT role_id FROM reaction_roles WHERE message_id = $message_id AND emoji = $emoji"; + command.Parameters.AddWithValue("$message_id", e.Message.Id); + command.Parameters.AddWithValue("$emoji", e.Emoji.ToString()); + + var roleId = (long?)command.ExecuteScalar(); + if (roleId == null) return; + + var member = await e.Guild.GetMemberAsync(e.User.Id); + var role = e.Guild.GetRole((ulong)roleId); + await member.GrantRoleAsync(role); + } + + private static async Task OnReactionRemoved(DiscordClient client, MessageReactionRemoveEventArgs e) + { + if (e.User.IsBot) return; + + var dbPath = Environment.GetEnvironmentVariable("DB_ROLES_LOC"); + using var connection = new SqliteConnection($"Data Source={dbPath}"); + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = @"SELECT role_id FROM reaction_roles WHERE message_id = $message_id AND emoji = $emoji"; + command.Parameters.AddWithValue("$message_id", e.Message.Id); + command.Parameters.AddWithValue("$emoji", e.Emoji.ToString()); + + var roleId = (long?)command.ExecuteScalar(); + if (roleId == null) return; + + var member = await e.Guild.GetMemberAsync(e.User.Id); + var role = e.Guild.GetRole((ulong)roleId); + await member.RevokeRoleAsync(role); + } + } +} diff --git a/program.cs b/program.cs new file mode 100644 index 0000000..ad09b03 --- /dev/null +++ b/program.cs @@ -0,0 +1,47 @@ +using DSharpPlus; +using DSharpPlus.CommandsNext; +using System.Threading.Tasks; +using System; +using DotNetEnv; +using Microsoft.Extensions.Logging; + +namespace T3k3rg0 +{ + public class Program + { + static async Task Main(string[] args) + { + Env.Load(); // .env fájl betöltése + + var discord = new DiscordClient(new DiscordConfiguration() + { + Token = Environment.GetEnvironmentVariable("DISCORD_BOT_TOKEN"), + TokenType = TokenType.Bot, + Intents = DiscordIntents.All, + MinimumLogLevel = LogLevel.Debug, + LoggerFactory = LoggerFactory.Create(builder => + { + builder.AddConsole(); + builder.SetMinimumLevel(LogLevel.Debug); + }) + }); + var commands = discord.UseCommandsNext(new CommandsNextConfiguration() + { + StringPrefixes = new[] { "!" }, + EnableDms = false, + EnableMentionPrefix = true + }); + + commands.RegisterCommands(); + ReactionRoleHandler.Register(discord); + + discord.Ready += async (s, e) => + { + Console.WriteLine("T3k3rg0 elindult !"); + }; + + await discord.ConnectAsync(); + await Task.Delay(-1); + } + } +}