258 lines
8.4 KiB
C#
258 lines
8.4 KiB
C#
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.Diagnostics;
|
|
using System.Linq;
|
|
|
|
public static class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var builder = Host.CreateApplicationBuilder(args);
|
|
|
|
builder.Services
|
|
.AddDiscordGateway(options =>
|
|
{
|
|
options.Intents = GatewayIntents.Guilds | GatewayIntents.GuildUsers | GatewayIntents.GuildEmojisAndStickers;
|
|
})
|
|
.AddApplicationCommands()
|
|
.AddComponentInteractions<ButtonInteraction, ButtonInteractionContext>();
|
|
|
|
var host = builder.Build();
|
|
|
|
var configuration = host.Services.GetRequiredService<IConfiguration>();
|
|
|
|
//configuration["Discord:Token"] = Environment.GetEnvironmentVariable("BOT_TOKEN");
|
|
|
|
host.AddModules(typeof(Program).Assembly);
|
|
|
|
host.RunAsync().GetAwaiter().GetResult();
|
|
}
|
|
}
|
|
|
|
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()
|
|
{
|
|
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
|
var randomUser = await 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 GetRolesCommand()
|
|
{
|
|
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
|
|
|
var roles = await GetRolesFromGuild();
|
|
|
|
roles = [.. roles.Where(role => !role.Name.StartsWith('@'))];
|
|
|
|
if (roles.Count == 0)
|
|
{
|
|
await Context.Interaction.ModifyResponseAsync(message => message.WithContent($"Found no roles :("));
|
|
}
|
|
|
|
await Context.Interaction.ModifyResponseAsync(message => message.WithContent($"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 GiveRoleToAllUsersCommand(string roleId)
|
|
{
|
|
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
|
|
|
var parsed = ulong.TryParse(roleId, out ulong roleIdParsed);
|
|
if (!parsed)
|
|
{
|
|
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Invalid roleId"));
|
|
}
|
|
|
|
_ = GiveRoleToAllUsers(roleIdParsed);
|
|
}
|
|
|
|
[SlashCommand("removerolefromallusers", "Remove role from all users on the server")]
|
|
public async Task RemoveRoleFromAllUsersCommand(string roleId)
|
|
{
|
|
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
|
|
|
var parsed = ulong.TryParse(roleId, out ulong roleIdParsed);
|
|
if (!parsed)
|
|
{
|
|
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Invalid roleId"));
|
|
return;
|
|
}
|
|
|
|
await RemoveRoleFromAllUsers(roleIdParsed);
|
|
|
|
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("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();
|
|
|
|
int total = users.Count;
|
|
await Context.Interaction.ModifyResponseAsync(message => message.WithContent($"Applying role for {users.Count} users."));
|
|
|
|
int count = 0;
|
|
foreach (var user in users)
|
|
{
|
|
count++;
|
|
if (user.RoleIds.Contains(roleId))
|
|
{
|
|
Debug.WriteLine($"{count}/{total} User already has role, skipping. {user.Username} ({user.Id})");
|
|
continue;
|
|
}
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
try
|
|
{
|
|
await client.Rest.AddGuildUserRoleAsync(GuildId, user.Id, roleId);
|
|
Console.WriteLine($"{count}/{total} Applied role to user {user.Username} ({user.Id})");
|
|
break;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"{count}/{total} Failed to apply role to user {user.Username} ({user.Id}). Try {i}/3. Error:");
|
|
Console.WriteLine(ex);
|
|
await Task.Delay(800);
|
|
}
|
|
}
|
|
|
|
await Task.Delay(800);
|
|
}
|
|
}
|
|
|
|
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!"));
|
|
}
|
|
} |