❔ Need help with discord command to toggle a task
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.SlashCommands;
namespace self_bot.commands
{
internal class TestCommand : ApplicationCommandModule
{
private DiscordClient _clientInstance;
public TestCommand(DiscordClient client)
{
_clientInstance = client;
}
private static bool IsOn = false;
[SlashCommand("ToggleTest", "Toggle test messages to be sent at user after every message")]
public async Task Toggle(InteractionContext ctx, [Option("user", "Specified user")] DiscordUser user)
{
IsOn=!IsOn;
if (IsOn==true)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent("Test toggle online"));
_clientInstance.MessageCreated += TestMessage(user, e);
}
if (IsOn==false)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent("Test toggle shutting down"));
_clientInstance.MessageCreated -= TestMessage(user, e);
}
}
public async Task TestMessage(DiscordUser user, MessageCreateEventArgs e)
{
if (e.Message.Author.Id == user.Id)
{
await e.Message.RespondAsync("Test success");
}
}
}
}using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.SlashCommands;
namespace self_bot.commands
{
internal class TestCommand : ApplicationCommandModule
{
private DiscordClient _clientInstance;
public TestCommand(DiscordClient client)
{
_clientInstance = client;
}
private static bool IsOn = false;
[SlashCommand("ToggleTest", "Toggle test messages to be sent at user after every message")]
public async Task Toggle(InteractionContext ctx, [Option("user", "Specified user")] DiscordUser user)
{
IsOn=!IsOn;
if (IsOn==true)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent("Test toggle online"));
_clientInstance.MessageCreated += TestMessage(user, e);
}
if (IsOn==false)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent("Test toggle shutting down"));
_clientInstance.MessageCreated -= TestMessage(user, e);
}
}
public async Task TestMessage(DiscordUser user, MessageCreateEventArgs e)
{
if (e.Message.Author.Id == user.Id)
{
await e.Message.RespondAsync("Test success");
}
}
}
}So I'm trying to make a command that takes a user as an input and toggles a task that uses that user as an input parameter for the toggled task. So you do something like:
/ToggleTest user1
Then anytime user1 sends a message the bot will respond with "Test success"
Then you can do /ToggleTest user1 to toggle it off.