@@ -1,15 +1,20 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.VisualBasic;
|
||||
using NetCord;
|
||||
using NetCord.Gateway;
|
||||
using NetCord.Hosting.Gateway;
|
||||
using NetCord.Hosting.Services;
|
||||
using NetCord.Hosting.Services.ApplicationCommands;
|
||||
using NetCord.Hosting.Services.ComponentInteractions;
|
||||
using NetCord.Rest;
|
||||
using NetCord.Services.ApplicationCommands;
|
||||
using NetCord.Services.ComponentInteractions;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
|
||||
namespace FluxPose.DiscordBot;
|
||||
|
||||
internal static class Program
|
||||
public static class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
@@ -23,30 +28,204 @@ internal static class Program
|
||||
.AddApplicationCommands()
|
||||
.AddComponentInteractions<ButtonInteraction, ButtonInteractionContext>();
|
||||
|
||||
AddActions(builder.Services);
|
||||
|
||||
var host = builder.Build();
|
||||
|
||||
var configuration = host.Services.GetRequiredService<IConfiguration>();
|
||||
|
||||
var token = Environment.GetEnvironmentVariable("BOT_TOKEN");
|
||||
if (token != null)
|
||||
{
|
||||
configuration["Discord:Token"] = token;
|
||||
}
|
||||
//configuration["Discord:Token"] = Environment.GetEnvironmentVariable("BOT_TOKEN");
|
||||
|
||||
host.AddModules(typeof(Program).Assembly);
|
||||
|
||||
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>();
|
||||
services.AddScoped<IInteractionContext, ApplicationCommandContext>();
|
||||
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
||||
var randomUser = await GetRandomUserFromGuild();
|
||||
|
||||
services.AddScoped<Experimenting.Actions>();
|
||||
services.AddScoped<Roles.Actions>();
|
||||
services.AddScoped<Users.Actions>();
|
||||
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 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!"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user