Skip to content

Commit

Permalink
Merge pull request #1 from outerwinnie/test
Browse files Browse the repository at this point in the history
Added file attachment option
  • Loading branch information
outerwinnie authored Sep 2, 2024
2 parents 22b008e + 5a4506b commit a43e4c3
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Discord.WebSocket;
using Discord.Interactions;
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace DiscordBot
Expand Down Expand Up @@ -50,7 +52,8 @@ private async Task RegisterCommandsAsync()
var confesarCommand = new SlashCommandBuilder()
.WithName("confesar")
.WithDescription("Confess something anonymously")
.AddOption("text", ApplicationCommandOptionType.String, "The text to confess", isRequired: true);
.AddOption("text", ApplicationCommandOptionType.String, "The text to confess", isRequired: true)
.AddOption("file", ApplicationCommandOptionType.Attachment, "Optional file to attach", isRequired: false);

// Replace 'your_guild_id_here' with your actual guild ID
var guildId = ulong.Parse(Environment.GetEnvironmentVariable("GUILD_ID")); // Example: 123456789012345678
Expand All @@ -69,14 +72,28 @@ private async Task HandleInteraction(SocketInteraction interaction)
{
if (command.CommandName == "confesar")
{
var text = command.Data.Options.First().Value.ToString();
var text = command.Data.Options.FirstOrDefault(x => x.Name == "text")?.Value?.ToString();
var attachmentOption = command.Data.Options.FirstOrDefault(x => x.Name == "file")?.Value as Attachment;

var user = command.User; // Get the user who triggered the command

// Log the user and the message text
Console.WriteLine($"[{DateTime.Now}] {user.Username} ({user.Id}) sent: {text}");

await command.RespondAsync(text, ephemeral: true); // Responds privately
await command.Channel.SendMessageAsync(text); // Sends the message in the channel
await command.RespondAsync("Your confession has been sent!", ephemeral: true); // Responds privately

if (attachmentOption != null)
{
using (var httpClient = new HttpClient())
using (var stream = await httpClient.GetStreamAsync(attachmentOption.Url))
{
await command.Channel.SendFileAsync(stream, attachmentOption.Filename, text); // Sends the message with the file in the channel
}
}
else
{
await command.Channel.SendMessageAsync(text); // Sends the message in the channel
}
}
}
}
Expand Down

0 comments on commit a43e4c3

Please sign in to comment.