public static void UserMessages(Discord.WebSocket.SocketUserMessage message) { //Writes to file StreamWriter sW = File.AppendText(usermessagesPath); sW.WriteLine($"{message.Content}, {message.Author}, {message.CreatedAt.DateTime}"); sW.Close(); }
/// <summary> /// Gets called whenever the client receives a message from discord /// </summary> /// <param name="msg"></param> /// <returns></returns> private async Task on_message_received(Discord.WebSocket.SocketMessage msg) { string potential_command; bool is_command_by_mentioned = false; //TODO: make this work Console.WriteLine("=============== New Message ==============="); string content = msg.Content; Discord.WebSocket.SocketUserMessage user_msg = msg as Discord.WebSocket.SocketUserMessage; Console.WriteLine("Content: " + content); Console.WriteLine("Is user message: " + (user_msg != null ? "true" : "false")); if (msg.Author.IsBot) { Console.WriteLine("Message is generated by a bot"); } if (same_user(msg.Author, client.CurrentUser)) { Console.WriteLine("Message generated by myself, aborting processing"); return; } if (msg_mentioned_user(client.CurrentUser, user_msg, out potential_command)) { #if DEBUG await msg.Channel.SendMessageAsync("You mentioned me?"); #endif is_command_by_mentioned = true; } else { Console.WriteLine("Too bad, not mentioned :(."); } if (msg.Content == "#_THIS_CHANNEL_ID(\"-reply\")") { await msg.Channel.SendMessageAsync("This channel's ID is: " + msg.Channel.Id); } #if DEBUG System.DateTime benchmark_start = DateTime.Now; #endif if (is_command_by_mentioned || bot_conf.is_command_raw(content, out potential_command)) { string output = "Detected input command of " + potential_command + "."; #if DEBUG System.DateTime benchmark_end = DateTime.Now; TimeSpan delta = benchmark_end - benchmark_start; output += " " + delta.TotalMilliseconds.ToString() + "ms took to complete command detection algorithm."; #endif await msg.Channel.SendMessageAsync(output); await ExecuteCommand(potential_command, user_msg); } }
private async Task ExecuteCommand(string potential_command, Discord.WebSocket.SocketUserMessage user_msg) { #if DEBUG DateTime start = DateTime.Now; #endif //command Discord.Commands.CommandContext context = new Discord.Commands.CommandContext(client, user_msg); var result = await command_serv.ExecuteAsync(context, potential_command, service); if (!result.IsSuccess) { await context.Channel.SendMessageAsync(result.ErrorReason); } #if DEBUG DateTime end = DateTime.Now; TimeSpan delta_time = end - start; await user_msg.Channel.SendMessageAsync("It took additionally " + delta_time.TotalMilliseconds.ToString() + " ms to execute command"); #endif }
public static bool is_command(Discord.WebSocket.SocketMessage msg) { //If msg is null, ignore it if (nullify_system_message) { Discord.WebSocket.SocketUserMessage user_msg = msg as Discord.WebSocket.SocketUserMessage; if (user_msg == null) { return(false); } } else { if (msg == null) { return(false); } } //set up processor (sorry if it's getting less and less readable) #region processor Dictionary <lookup_type, char[]> char_lookup = new Dictionary <lookup_type, char[]>(); if (check_prefix_determine_command) { char_lookup[lookup_type.prefix] = new char[1] { char_command_prefix }; } if (check_suffix_determine_command) { char_lookup[lookup_type.suffix] = new char[1] { char_command_suffix }; } string content = msg.Content; custom_string_processor processor = new custom_string_processor(char_lookup, null, true); #endregion return(processor.string_satisfied(content)); }
public void CreateReminder(Discord.WebSocket.SocketUserMessage message, string input) { var dtNow = message.Timestamp.UtcDateTime; var results = DateTimeRecognizer.RecognizeDateTime(input, Culture.EnglishOthers); if (results.Count == 0) { throw new FormatException("Couldn't recognize DateTime from input"); } var result = results[0]; if (!result.TypeName.StartsWith(TypePrefix)) { throw new NotSupportedException($"'{result.TypeName}' not supported"); } var values = ((IList <Dictionary <string, string> >)result.Resolution["values"])[0]; DateTime dt; switch (result.TypeName.Substring(TypePrefix.Length)) { case Microsoft.Recognizers.Text.DateTime.Constants.SYS_DATETIME_DATE: // today, 21.1.2020 dt = DateTime.Parse(values["value"]) + dtNow.TimeOfDay; break; case Microsoft.Recognizers.Text.DateTime.Constants.SYS_DATETIME_TIME: // 10:00 dt = dtNow.Date + TimeSpan.Parse(values["value"]); break; case Microsoft.Recognizers.Text.DateTime.Constants.SYS_DATETIME_DATETIME: // now, in 2 hours, 21.1.2020 10:00 dt = DateTime.Parse(values["value"]); break; case Microsoft.Recognizers.Text.DateTime.Constants.SYS_DATETIME_TIMEPERIOD: // evening dt = dtNow.Date + TimeSpan.Parse(values["start"]); break; case Microsoft.Recognizers.Text.DateTime.Constants.SYS_DATETIME_DURATION: // 2 hours dt = dtNow.Date + dtNow.TimeOfDay + TimeSpan.FromSeconds(Convert.ToDouble(values["value"])); break; case Microsoft.Recognizers.Text.DateTime.Constants.SYS_DATETIME_DATETIMEPERIOD: // between 21.1.2020 1:00 and 22.2.2020 0:00 case Microsoft.Recognizers.Text.DateTime.Constants.SYS_DATETIME_DATEPERIOD: // from 21.1.2020 to 22.2.2020, between 21.1.2020 and 22.2.2020, next week, before tomorrow throw new NotSupportedException($"'{result.TypeName}' not supported"); case Microsoft.Recognizers.Text.DateTime.Constants.SYS_DATETIME_TIMEZONE: case Microsoft.Recognizers.Text.DateTime.Constants.SYS_DATETIME_DATETIMEPOINT: case Microsoft.Recognizers.Text.DateTime.Constants.SYS_DATETIME_DATETIMEALT: case Microsoft.Recognizers.Text.DateTime.Constants.SYS_DATETIME_SET: default: throw new NotImplementedException($"'{result.TypeName}' not implemented"); } if (dtNow >= dt) { throw new InvalidOperationException($"'{dt}' is not in the future"); } var text = (input.Substring(0, result.Start).TrimEnd() + input.Substring(result.End + 1)).Trim(); using var db = LennyServiceProvider.OpenDB(); var reminders = db.GetCollection <ReminderModel>(); var reminder = new ReminderModel(dtNow, dt, text, message.Author.Mention, message.GetJumpUrl()); reminders.Insert(reminder); }