Immortal
Immortal
CC#
Created by Immortal on 5/22/2024 in #help
Is this the correct way to replace to replace a discord bot background service logger with Serilog?
https://paste.mod.gg/dblzdkrlozuv/0 I did see that there is an example of doing this on github however the example uses a console app instead of a worker service. And while this method seems to work from the messages that are in the console, it is doesn't allow me to use the Serilog methods so I have: _logger.LogInformation("Bot starting..."); Instead of using the Serilog methods like so: _logger.Information("Bot starting..."); Which makes me think this isn't the correct way to do this.
6 replies
CC#
Created by Immortal on 5/20/2024 in #help
✅ Difference between CreateApplicationBuilder and CreateDefaultBuilder?
Only difference I can see is that CreateApplicationBuilder returns a HostApplicationBuilder and CreateDefaultBuilder returns a HostBuilder?
7 replies
CC#
Created by Immortal on 5/2/2024 in #help
✅ Best practices regarding non-nullable property warnings?
Hi, In my Discord Bot project I get some warnings for example: Non-nullable property 'Config' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. I understand what it means and why I get it but not sure if it's ok for me to ignore it or if it indicates bad practices so I should learn to write it better. Here is my Program.cs file:
namespace self_bot
{
internal class Program
{
private static void Main(string[] args)
{
var bot = new Bot();
bot.RunAsync().GetAwaiter().GetResult();
}
}
}
namespace self_bot
{
internal class Program
{
private static void Main(string[] args)
{
var bot = new Bot();
bot.RunAsync().GetAwaiter().GetResult();
}
}
}
And I've attached the Bot.cs file contents because it's too long to be put in this message. The warning also shows with the Client and Slash properties. From looking online the best way would either be to use lazy initialization or a constructor but I have no clue which is best for my project so would like advice from more experienced people. Thanks
45 replies
CC#
Created by Immortal on 6/19/2023 in #help
❔ 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.
133 replies