Revert "Splitting up, doesn't work"

This reverts commit 56fae83f03.
This commit is contained in:
2025-12-13 19:16:20 +01:00
parent 56fae83f03
commit 3bf9ab8096
11 changed files with 197 additions and 225 deletions

View File

@@ -1,7 +0,0 @@
namespace FluxPose.DiscordBot
{
public abstract class ActionsBase<TContext>(TContext context) where TContext : IInteractionContext
{
public ulong GuildId => context.Interaction.Guild?.Id ?? throw new InvalidOperationException("Action cannot be performed outside of a server.");
}
}

View File

@@ -1,32 +0,0 @@
namespace FluxPose.DiscordBot.Experimenting;
public class Actions<TContext>(Users.Actions<TContext> usersActions, GatewayClient client, TContext context) : ActionsBase<TContext>(context) where TContext : IInteractionContext
{
public async Task<GuildUser?> GetRandomUserFromGuild()
{
var users = await usersActions.GetAllUsersFromGuild();
var index = new Random().Next(users.Count);
return users[index];
}
public async Task RemoveRoleFromAllUsers(ulong roleId)
{
var users = await usersActions.GetAllUsersFromGuild();
foreach (var user in users)
{
if (user.RoleIds.Contains(roleId))
{
continue;
}
await client.Rest.RemoveGuildUserRoleAsync(GuildId, user.Id, roleId);
}
}
public async Task<IReadOnlyList<GuildEmoji>> GetAllGuildEmoji()
{
return await client.Rest.GetGuildEmojisAsync(GuildId);
}
}

View File

@@ -1,16 +0,0 @@
using NetCord.Services.ComponentInteractions;
namespace FluxPose.DiscordBot.Experimenting;
public class ButtonCommands : ComponentInteractionModule<ButtonInteractionContext>
{
[ComponentInteraction("testButton")]
public async Task RespondToTestButton()
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
await Context.Message.ModifyAsync(message => message.WithContent($"{Context.Message.Content}a"));
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Clicked!"));
}
}

View File

@@ -1,107 +0,0 @@
namespace FluxPose.DiscordBot.Experimenting;
public class SlashCommands(Actions<ApplicationCommandContext> experimentingActions, Users.Actions<ApplicationCommandContext> usersActions, Roles.Actions<ApplicationCommandContext> rolesActions, GatewayClient client) : ApplicationCommandModule<ApplicationCommandContext>
{
[SlashCommand("randomuser", "Get a random user from the server")]
public async Task<string> GetRandomUserCommand()
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
var randomUser = await experimentingActions.GetRandomUserFromGuild();
if (randomUser == null)
{
return $"Found no users :(";
}
return $"Here's a random user: {randomUser.Username}";
}
[SlashCommand("getroles", "Get all roles on the server")]
public async Task<string> GetRolesCommand()
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
var roles = await rolesActions.GetRolesFromGuild();
roles = [.. roles.Where(role => !role.Name.StartsWith('@'))];
if (roles.Count == 0)
{
return $"Found no roles :(";
}
return $"Roles in the server:\n{String.Join("\n", roles.Select(role => $"- {role.Id}: {role.Name}"))}";
}
[SlashCommand("giveroletouser", "Give a role")]
public async Task<string> GiveRoleToAllUsersCommand(string userId, string roleId)
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
var parsed = ulong.TryParse(userId, out ulong userIdParsed);
if (!parsed)
{
return "Invalid roleId";
}
parsed = ulong.TryParse(roleId, out ulong roleIdParsed);
if (!parsed)
{
return "Invalid roleId";
}
await rolesActions.GiveRoleToUser(userIdParsed, roleIdParsed);
return "Done.";
}
[SlashCommand("removerolefromallusers", "Remove role from all users on the server")]
public async Task<string> RemoveRoleFromAllUsersCommand(string roleId)
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
var parsed = ulong.TryParse(roleId, out ulong roleIdParsed);
if (!parsed)
{
return "Invalid roleId";
}
await experimentingActions.RemoveRoleFromAllUsers(roleIdParsed);
return "Done.";
}
[SlashCommand("defer", "Defer")]
public async Task DeferCommand()
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
Thread.Sleep(2000);
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Sup"));
Thread.Sleep(1000);
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Sup bitches"));
}
[SlashCommand("button", "Gives a button")]
public async Task ButtonCommand()
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
var emojis = await experimentingActions.GetAllGuildEmoji();
EmojiProperties? emoji = null;
if (emojis.Count > 0)
{
emoji = EmojiProperties.Custom(emojis[new Random().Next(emojis.Count)].Id);
}
var button = new ButtonProperties("testButton", "Click Me!", emoji!, ButtonStyle.Primary);
var component = new ActionRowProperties([button]);
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Here's a button").WithComponents([component]));
}
}

View File

@@ -5,7 +5,6 @@
<TargetFramework>net9.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<RootNamespace>FluxPose.DiscordBot</RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@@ -21,4 +20,5 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,10 +0,0 @@
global using NetCord;
global using NetCord.Gateway;
global using NetCord.Rest;
global using NetCord.Services.ApplicationCommands;
global using NetCord.Services;
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Text;
global using System.Threading.Tasks;

View File

@@ -1,15 +1,20 @@
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.VisualBasic;
using NetCord;
using NetCord.Gateway;
using NetCord.Hosting.Gateway; using NetCord.Hosting.Gateway;
using NetCord.Hosting.Services; using NetCord.Hosting.Services;
using NetCord.Hosting.Services.ApplicationCommands; using NetCord.Hosting.Services.ApplicationCommands;
using NetCord.Hosting.Services.ComponentInteractions; using NetCord.Hosting.Services.ComponentInteractions;
using NetCord.Rest;
using NetCord.Services.ApplicationCommands;
using NetCord.Services.ComponentInteractions; using NetCord.Services.ComponentInteractions;
using System.ComponentModel;
using System.Linq;
namespace FluxPose.DiscordBot; public static class Program
internal static class Program
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
@@ -23,30 +28,204 @@ internal static class Program
.AddApplicationCommands() .AddApplicationCommands()
.AddComponentInteractions<ButtonInteraction, ButtonInteractionContext>(); .AddComponentInteractions<ButtonInteraction, ButtonInteractionContext>();
AddActions(builder.Services);
var host = builder.Build(); var host = builder.Build();
var configuration = host.Services.GetRequiredService<IConfiguration>(); var configuration = host.Services.GetRequiredService<IConfiguration>();
var token = Environment.GetEnvironmentVariable("BOT_TOKEN"); //configuration["Discord:Token"] = Environment.GetEnvironmentVariable("BOT_TOKEN");
if (token != null)
{
configuration["Discord:Token"] = token;
}
host.AddModules(typeof(Program).Assembly); host.AddModules(typeof(Program).Assembly);
host.RunAsync().GetAwaiter().GetResult(); host.RunAsync().GetAwaiter().GetResult();
} }
}
private static void AddActions(IServiceCollection services) public class SlashCommands(GatewayClient client) : ApplicationCommandModule<ApplicationCommandContext>
{
private ulong GuildId => GetGuildId() ?? throw new InvalidOperationException("Command cannot be used outside of a server.");
[SlashCommand("randomuser", "Get a random user from the server")]
public async Task<string> GetRandomUserCommand()
{ {
services.AddScoped<IInteractionContext, ButtonInteractionContext>(); await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
services.AddScoped<IInteractionContext, ApplicationCommandContext>(); var randomUser = await GetRandomUserFromGuild();
services.AddScoped<Experimenting.Actions>(); if (randomUser == null)
services.AddScoped<Roles.Actions>(); {
services.AddScoped<Users.Actions>(); return $"Found no users :(";
}
return $"Here's a random user: {randomUser.Username}";
}
[SlashCommand("getroles", "Get all roles on the server")]
public async Task<string> GetRolesCommand()
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
var roles = await GetRolesFromGuild();
roles = [.. roles.Where(role => !role.Name.StartsWith('@'))];
if (roles.Count == 0)
{
return $"Found no roles :(";
}
return $"Roles in the server:\n{String.Join("\n", roles.Select(role => $"- {role.Id}: {role.Name}"))}";
}
[SlashCommand("giveroletoallusers", "Give role to all users on the server")]
public async Task<string> GiveRoleToAllUsersCommand(string roleId)
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
var parsed = ulong.TryParse(roleId, out ulong roleIdParsed);
if (!parsed)
{
return "Invalid roleId";
}
await GiveRoleToAllUsers(roleIdParsed);
return "Done.";
}
[SlashCommand("removerolefromallusers", "Remove role from all users on the server")]
public async Task<string> RemoveRoleFromAllUsersCommand(string roleId)
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
var parsed = ulong.TryParse(roleId, out ulong roleIdParsed);
if (!parsed)
{
return "Invalid roleId";
}
await RemoveRoleFromAllUsers(roleIdParsed);
return "Done.";
}
[SlashCommand("defer", "Defer")]
public async Task DeferCommand()
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
Thread.Sleep(2000);
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Sup"));
Thread.Sleep(1000);
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Sup bitches"));
}
//[SlashCommand("button", "Gives a button")]
//public async Task ButtonCommand()
//{
// await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
// var emojis = await client.Rest.GetGuildEmojisAsync(GuildId);
// EmojiProperties? emoji = null;
// if (emojis.Count > 0)
// {
// emoji = EmojiProperties.Custom(emojis[new Random().Next(emojis.Count)].Id);
// }
// var button = new ButtonProperties(TestButtonId, "Click Me!", emoji!, ButtonStyle.Primary);
// var component = new ActionRowProperties([button]);
// await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Here's a button").WithComponents([component]));
//}
[SlashCommand("button", "Gives a button")]
public async Task ButtonCommand()
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
var emojis = await client.Rest.GetGuildEmojisAsync(GuildId);
EmojiProperties? emoji = null;
if (emojis.Count > 0)
{
emoji = EmojiProperties.Custom(emojis[new Random().Next(emojis.Count)].Id);
}
var button = new ButtonProperties("testButton", "Click Me!", emoji!, ButtonStyle.Primary);
var component = new ActionRowProperties([button]);
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Here's a button").WithComponents([component]));
}
private ulong? GetGuildId()
{
return Context.Guild?.Id;
}
private async Task<GuildUser?> GetRandomUserFromGuild()
{
var users = await GetAllUsersFromGuild();
var index = new Random().Next(users.Count);
return users[index];
}
private async Task<List<GuildUser>> GetAllUsersFromGuild()
{
var users = await client.Rest.GetGuildUsersAsync(GuildId).ToListAsync();
if (users == null)
{
return [];
}
return users;
}
private async Task<List<Role>> GetRolesFromGuild()
{
var roles = (await client.Rest.GetGuildRolesAsync(GuildId)).ToList();
return roles;
}
private async Task GiveRoleToAllUsers(ulong roleId)
{
var users = await GetAllUsersFromGuild();
foreach (var user in users)
{
await client.Rest.AddGuildUserRoleAsync(GuildId, user.Id, roleId);
}
}
private async Task RemoveRoleFromAllUsers(ulong roleId)
{
var users = await GetAllUsersFromGuild();
foreach (var user in users)
{
if (user.RoleIds.Contains(roleId))
{
continue;
}
await client.Rest.RemoveGuildUserRoleAsync(GuildId, user.Id, roleId);
}
} }
} }
public class ButtonCommands : ComponentInteractionModule<ButtonInteractionContext>
{
[ComponentInteraction("testButton")]
public async Task RespondToTestButton()
{
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
await Context.Message.ModifyAsync(message => message.WithContent($"{Context.Message.Content}a"));
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Clicked!"));
}
}

View File

@@ -1,15 +0,0 @@
namespace FluxPose.DiscordBot.Roles;
public class Actions<TContext>(Users.Actions usersActions, GatewayClient client, TContext context) : ActionsBase<TContext>(context) where TContext : IInteractionContext
{
public async Task<List<Role>> GetRolesFromGuild()
{
var roles = (await client.Rest.GetGuildRolesAsync(GuildId)).ToList();
return roles;
}
public async Task GiveRoleToUser(ulong userId, ulong roleId)
{
await client.Rest.AddGuildUserRoleAsync(GuildId, userId, roleId);
}
}

View File

@@ -1,5 +0,0 @@
namespace FluxPose.DiscordBot.Roles;
public class SlashCommands
{
}

View File

@@ -1,15 +0,0 @@
namespace FluxPose.DiscordBot.Users;
public class Actions<TContext>(GatewayClient client, TContext context) : ActionsBase<TContext>(context) where TContext : IInteractionContext
{
public async Task<List<GuildUser>> GetAllUsersFromGuild()
{
var users = await client.Rest.GetGuildUsersAsync(GuildId).ToListAsync();
if (users == null)
{
return [];
}
return users;
}
}

View File

@@ -1,5 +1,5 @@
{ {
"Discord": { "Discord": {
"Token": "MTQzOTk1MjIzMzAxMjY1ODI2Ng.GePLJF.ljh--yyvzz3Os5t8kZlhNnHnJuT-gQxpjGAgyA" "Token": "Token from Discord Developer Portal"
} }
} }