Untitled
unknown
plain_text
a year ago
4.0 kB
2
Indexable
Never
using System; using System.Text.RegularExpressions; using System.Threading.Channels; using System.Threading.Tasks; using Discord; using Discord.WebSocket; using static System.Collections.Specialized.BitVector32; namespace DiscordBotExample { class Program { public class RecapMessage { public ulong MessageId { get; set; } public ulong UserId { get; set; } public string Username { get; set; } public ulong ChannelId { get; set; } public string ChannelName { get; set; } public DateTimeOffset Timestamp { get; set; } public string Content { get; set; } public List<string> Urls { get; set; } public List<RecapReply> Replies { get; set; } } public class RecapReply { public ulong ReplyId { get; set; } public ulong UserId { get; set; } public string Username { get; set; } public DateTime Timestamp { get; set; } public string Content { get; set; } } public static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult(); private DiscordSocketClient _client; public async Task MainAsync() { var testConfig = new DiscordSocketConfig(); testConfig.GatewayIntents = GatewayIntents.MessageContent | GatewayIntents.GuildMessages | GatewayIntents.Guilds; // works testConfig.LogLevel = LogSeverity.Info; _client = new DiscordSocketClient(testConfig); _client.Log += Log; _client.MessageReceived += ClientOnMessageReceived; _client.ReactionAdded += HandleReactionAddedAsync; var token = Environment.GetEnvironmentVariable("DCT"); await _client.LoginAsync(TokenType.Bot, token); await _client.StartAsync(); // Block this task until the program is closed. await Task.Delay(-1); } private static Task HandleReactionAddedAsync(Cacheable<IUserMessage, ulong> arg1, Cacheable<IMessageChannel, ulong> arg2, SocketReaction arg3) { return Task.CompletedTask; } private static Task Log(LogMessage msg) { Console.WriteLine(msg.ToString()); return Task.CompletedTask; } private static Task ClientOnMessageReceived(SocketMessage arg) { // if content has a url in it, lets save it. if (HasUrl(arg.Content)) { //arg.Channel.SendMessageAsync($"User '{arg.Author.Username}' posted a url!"); // Create the Recap Message var recapMessage = new RecapMessage(); recapMessage.MessageId = arg.Id; recapMessage.UserId = arg.Author.Id; recapMessage.Username = arg.Author.Username; recapMessage.ChannelId = arg.Channel.Id; recapMessage.ChannelName = arg.Channel.Name; recapMessage.Timestamp = arg.Timestamp; recapMessage.Content = arg.Content; recapMessage.Urls = new List<string>(); //recapMessage.Replies = new List<RecapReply>(); } return Task.CompletedTask; } private static bool HasUrl(string input) { bool result = false; //string input = "This is a sample text with a URL: https://www.example.com."; string pattern = @"(http|https):\/\/([\w\-_]+(\.[\w\-_]+)+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"; Match match = Regex.Match(input, pattern); if (match.Success) result = true; return result; } } }