C
C#12mo ago
StilauGamer

Discord.Net + MagicOnion, heheh

Hey! So I am working on a discord bot that I want to intergrate into a server using websockets, I found MagicOnion and wanted to test it out, but I can only find documentation to how to use it with Asp.Net. Does anyone know how I would implement it with Discord.Net? I have a test service that uses the ServiceBase<> and IService<> This is my current code for my bot:
public static Task Main()
=> new Program().MainAsync();

private async Task MainAsync()
{
try
{
var client = new DiscordSocketClient();

using var host = Host.CreateDefaultBuilder()
.ConfigureServices((_, services) =>
{
services
.AddSingleton(client)
.AddSingleton(i => new InteractionService(i.GetRequiredService<DiscordSocketClient>()))
.AddSingleton<InteractionHandler>()
.AddTransient<ITestService, TestService>();

services.AddGrpc();
services.AddMagicOnion();
}
).Build();

await RunAsync(host);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}

private async Task RunAsync(IHost host)
{
using var serviceScope = host.Services.CreateScope();
var serviceProvider = serviceScope.ServiceProvider;


var commands = serviceProvider
.GetRequiredService<InteractionService>();
var client = serviceProvider
.GetRequiredService<DiscordSocketClient>();

await serviceProvider
.GetRequiredService<InteractionHandler>()
.InitializeAsync();


await client.LoginAsync(TokenType.Bot, Token);
await client.StartAsync();

client.Log += Log;
client.Ready += async () =>
{
await commands.RegisterCommandsToGuildAsync(DiscordGuildId);
};

await Task.Delay(-1);
}
public static Task Main()
=> new Program().MainAsync();

private async Task MainAsync()
{
try
{
var client = new DiscordSocketClient();

using var host = Host.CreateDefaultBuilder()
.ConfigureServices((_, services) =>
{
services
.AddSingleton(client)
.AddSingleton(i => new InteractionService(i.GetRequiredService<DiscordSocketClient>()))
.AddSingleton<InteractionHandler>()
.AddTransient<ITestService, TestService>();

services.AddGrpc();
services.AddMagicOnion();
}
).Build();

await RunAsync(host);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}

private async Task RunAsync(IHost host)
{
using var serviceScope = host.Services.CreateScope();
var serviceProvider = serviceScope.ServiceProvider;


var commands = serviceProvider
.GetRequiredService<InteractionService>();
var client = serviceProvider
.GetRequiredService<DiscordSocketClient>();

await serviceProvider
.GetRequiredService<InteractionHandler>()
.InitializeAsync();


await client.LoginAsync(TokenType.Bot, Token);
await client.StartAsync();

client.Log += Log;
client.Ready += async () =>
{
await commands.RegisterCommandsToGuildAsync(DiscordGuildId);
};

await Task.Delay(-1);
}
63 Replies
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX12mo ago
$mains
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX12mo ago
The possible signatures for Main are
public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
public is not required (can be any accessibility). Top-level statements are compiled into a Main method and will use an appropriate signature depending on the body. https://docs.microsoft.com/en-US/dotnet/csharp/fundamentals/program-structure/main-command-line
Main() and command-line arguments - C#
Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program.
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
But idk if this solved my issue tho, having the bot be a websocket server aswell?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
I really appreciate the tips on how to simplify the code, don't get me wrong. My original question was how I could implement MagicOnion, a websocket library into the bot. I wanted the bot to act like a websocket server for other clients to connect into if that makes sense.
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
Ahhh okay, I'll give it a try. So just try running most of it from the Main method?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
So like this instead?
public class Program
{
public static Task Main()
=> new Program().MainAsync();

private async Task MainAsync()
{
try
{
var client = new DiscordSocketClient();

using var host = Host.CreateDefaultBuilder()
.ConfigureServices((_, services) =>
{

services.AddSingleton(client);
services.AddSingleton(i => new InteractionService(i.GetRequiredService<DiscordSocketClient>()));
services.AddHostedService<DiscordSocketClientHostedService>();
services.AddTransient<ITestService, TestService>();
services.AddSingleton<InteractionHandler>();
services.AddMagicOnion();
services.AddGrpc();
}
).Build();

await host.Services
.GetRequiredService<InteractionHandler>()
.InitializeAsync();

await host.RunAsync();
await Task.Delay(-1);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}

public class DiscordSocketClientHostedService : BackgroundService
{
private readonly DiscordSocketClient _client;
private readonly IServiceProvider _serviceProvider;

private const ulong DiscordGuildId = 0;
private const string Token = "NotGonnaGetTHECHANCE";

public DiscordSocketClientHostedService(
DiscordSocketClient client,
IServiceProvider serviceProvider
)
{
_client = client;
_serviceProvider = serviceProvider;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var commands = _serviceProvider
.GetRequiredService<InteractionService>();

await _client.LoginAsync(TokenType.Bot, Token);
await _client.StartAsync();

_client.Log += Log;
_client.Ready += async () =>
{
await commands.RegisterCommandsToGuildAsync(DiscordGuildId);
};
}

private Task Log(LogMessage log)
{
Console.WriteLine(log.Message);
return Task.CompletedTask;
}
}
public class Program
{
public static Task Main()
=> new Program().MainAsync();

private async Task MainAsync()
{
try
{
var client = new DiscordSocketClient();

using var host = Host.CreateDefaultBuilder()
.ConfigureServices((_, services) =>
{

services.AddSingleton(client);
services.AddSingleton(i => new InteractionService(i.GetRequiredService<DiscordSocketClient>()));
services.AddHostedService<DiscordSocketClientHostedService>();
services.AddTransient<ITestService, TestService>();
services.AddSingleton<InteractionHandler>();
services.AddMagicOnion();
services.AddGrpc();
}
).Build();

await host.Services
.GetRequiredService<InteractionHandler>()
.InitializeAsync();

await host.RunAsync();
await Task.Delay(-1);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}

public class DiscordSocketClientHostedService : BackgroundService
{
private readonly DiscordSocketClient _client;
private readonly IServiceProvider _serviceProvider;

private const ulong DiscordGuildId = 0;
private const string Token = "NotGonnaGetTHECHANCE";

public DiscordSocketClientHostedService(
DiscordSocketClient client,
IServiceProvider serviceProvider
)
{
_client = client;
_serviceProvider = serviceProvider;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var commands = _serviceProvider
.GetRequiredService<InteractionService>();

await _client.LoginAsync(TokenType.Bot, Token);
await _client.StartAsync();

_client.Log += Log;
_client.Ready += async () =>
{
await commands.RegisterCommandsToGuildAsync(DiscordGuildId);
};
}

private Task Log(LogMessage log)
{
Console.WriteLine(log.Message);
return Task.CompletedTask;
}
}
Hey! I still need some help with this. I've done some changes, but I can still not get it to work. This is my current code: https://pastecord.com/olofinitob All help is deeply appreciated. My goal is to send data from and to a game server, but I want the websocket server to be on the discord bot. So I don't need to do: - Game Server -> Magic Onion Server -> Discord Bot - Wise Versa How can I do so? It looks like Magic Onion is mostly made for Asp.Net apps, but this is a console app trollq
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
Am I able to run the bot on a ASP app?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
I've worked with like ASP api's, etc before. But never something like this lmao
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
aahh
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
So I was basically trying to make an ASP app in a console app ish
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
without the web server part
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
Does this look more correct then? https://pastecord.com/duhebusywo.cs Now its running on an asp app instead
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
But background service are basically just for starting something to run in the background right? Its just a way for it to auto load when the program starts ish?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
Why? Whats up with using the logger?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
Okay, I've never actually heard abt that lmao
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
Using the $ is not recommended?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
ahhhh
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
Ah okay, and I shouldn't be the one filling in the template {}, basically
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
Ahhhhhhh Bro is filling my mind, I feel my brain is expanding 😂
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
And there you talk about how to do it correctly when it comes to logging?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
Ahh okay, I'll have a read on it. I've fallen really for C# and just trying to learn as much as I can lol But its not always the easiest 😅
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
Okay wtf
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
😂
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
So basically. I make a log method. Like this:
[LoggerMessage(
EventId = 20,
Level = LogLevel.Critical,
Message = "{Username} has hit an error!")]
public static partial void LogUserError(
ILogger logger, string Username);
[LoggerMessage(
EventId = 20,
Level = LogLevel.Critical,
Message = "{Username} has hit an error!")]
public static partial void LogUserError(
ILogger logger, string Username);
and when I want to use it I can do smth like: LogUserError(logger, _client.CurrentUser.UserName);
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
Ahh, and this is the most official and recommended way of doing logging ig?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
CustomEventName 🤔 So instead of doing _logger.LogInformation("{Username} has hit an error", _client.CurrentUser.UserName), I should do this.
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
🥹 So basically like this: Log.BotStarted(_logger, _client.CurrentUser.Username);
public partial class Log
{
[LoggerMessage(
Level = LogLevel.Information,
Message = "{Username} has been started!")]
public static partial void BotStarted(ILogger logger, string username);
}
public partial class Log
{
[LoggerMessage(
Level = LogLevel.Information,
Message = "{Username} has been started!")]
public static partial void BotStarted(ILogger logger, string username);
}
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
StilauGamer
StilauGamer12mo ago
ahhh, went training so didn't see the last messsages. Basically waht it does is that the source generator detects this [LoggerMessage(.........)] and then creates the code inside the method automatically. Right?
MODiX
MODiX12mo ago
TeBeCo
and [LoggerMessage(.........] is what's detected by the source generator
Quoted by
React with ❌ to remove this embed.
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX12mo ago
TeBeCo
then it's using all the properties and generates sub optimized code to properly call the logger
Quoted by
React with ❌ to remove this embed.
StilauGamer
StilauGamer12mo ago
So I should make [LoggerMessage] for each logging I do?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server