104 lines
2.7 KiB
C#
104 lines
2.7 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Discord;
|
|
using Discord.WebSocket;
|
|
using DotNetEnv;
|
|
|
|
class Program
|
|
{
|
|
private DiscordSocketClient _client;
|
|
|
|
static async Task Main(string[] args)
|
|
{
|
|
Env.Load();
|
|
await new Program().MainAsync();
|
|
}
|
|
|
|
public async Task MainAsync()
|
|
{
|
|
var config = new DiscordSocketConfig()
|
|
{
|
|
GatewayIntents = GatewayIntents.AllUnprivileged
|
|
| GatewayIntents.MessageContent
|
|
| GatewayIntents.GuildVoiceStates,
|
|
|
|
EnableVoiceDaveEncryption = true
|
|
};
|
|
|
|
_client = new DiscordSocketClient(config);
|
|
|
|
_client.Log += LogAsync;
|
|
_client.Ready += ReadyAsync;
|
|
|
|
var token = Environment.GetEnvironmentVariable("DISCORD_TOKEN");
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
Console.WriteLine("[Error] DISCORD_TOKEN is missing.");
|
|
return;
|
|
}
|
|
|
|
await _client.LoginAsync(TokenType.Bot, token);
|
|
await _client.StartAsync();
|
|
|
|
await Task.Delay(-1);
|
|
}
|
|
|
|
private Task LogAsync(LogMessage message)
|
|
{
|
|
Console.WriteLine($"[Discord] {message}");
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private Task ReadyAsync()
|
|
{
|
|
Console.WriteLine($"[Client] Connected successfully as {_client.CurrentUser}.");
|
|
|
|
var status = Environment.GetEnvironmentVariable("BOT_STATUS") ?? "Among Us";
|
|
var streamUrl = Environment.GetEnvironmentVariable("BOT_STREAM_URL");
|
|
|
|
if (!string.IsNullOrEmpty(streamUrl))
|
|
{
|
|
_ = _client.SetGameAsync(
|
|
status,
|
|
streamUrl,
|
|
ActivityType.Streaming
|
|
);
|
|
}
|
|
else
|
|
{
|
|
_ = _client.SetGameAsync(status);
|
|
}
|
|
|
|
_ = Task.Run(async () =>
|
|
{
|
|
try
|
|
{
|
|
await Task.Delay(2000);
|
|
|
|
var guildId = ulong.Parse(
|
|
Environment.GetEnvironmentVariable("GUILD_ID")
|
|
?? throw new InvalidOperationException("GUILD_ID is missing.")
|
|
);
|
|
|
|
var voiceId = ulong.Parse(
|
|
Environment.GetEnvironmentVariable("VOICE_CHANNEL_ID")
|
|
?? throw new InvalidOperationException("VOICE_CHANNEL_ID is missing.")
|
|
);
|
|
|
|
await VocalManager.ConnectToVoiceChannelAsync(
|
|
_client,
|
|
guildId,
|
|
voiceId
|
|
);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[Error] Background voice connection failed: {ex.Message}");
|
|
}
|
|
});
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|