-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
114 lines (94 loc) · 4.55 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Discord;
using Discord.WebSocket;
using Discord.Interactions;
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace DiscordBot
{
class Program
{
private DiscordSocketClient _client;
private InteractionService _interactionService;
private IServiceProvider _services;
static void Main(string[] args) => new Program().RunBotAsync().GetAwaiter().GetResult();
public async Task RunBotAsync()
{
var config = new DiscordSocketConfig
{
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent
};
_client = new DiscordSocketClient(config);
_interactionService = new InteractionService(_client.Rest);
_client.Log += Log;
_client.Ready += RegisterCommandsAsync;
// Fetch the bot token from the environment variable
var token = Environment.GetEnvironmentVariable("DISCORD_TOKEN");
if (string.IsNullOrWhiteSpace(token))
{
Console.WriteLine("Error: DISCORD_TOKEN environment variable is not set.");
return;
}
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
_client.InteractionCreated += HandleInteraction;
// Block this task until the program is closed.
await Task.Delay(-1);
}
private async Task RegisterCommandsAsync()
{
var postCommand = new SlashCommandBuilder()
.WithName("post")
.WithDescription("Post something anonymously")
.AddOption("text", ApplicationCommandOptionType.String, "The text to post", isRequired: false)
.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
var guild = _client.GetGuild(guildId);
await guild.DeleteApplicationCommandsAsync(); // Clear any existing commands in the guild
await _client.Rest.DeleteAllGlobalCommandsAsync();
await guild.CreateApplicationCommandAsync(postCommand.Build());
Console.WriteLine("Slash command /post registered for guild");
}
private async Task HandleInteraction(SocketInteraction interaction)
{
if (interaction is SocketSlashCommand command)
{
if (command.CommandName == "post")
{
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 or file information
if (!string.IsNullOrWhiteSpace(text))
{
Console.WriteLine($"[{DateTime.Now}] {user.Username} ({user.Id}) sent: {text}");
}
await command.RespondAsync("Your post 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 if (!string.IsNullOrWhiteSpace(text))
{
await command.Channel.SendMessageAsync(text); // Sends the message in the channel if text is provided
}
else
{
await command.RespondAsync("You need to provide either text or a file.", ephemeral: true);
}
}
}
}
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
}
}