75 lines
3.2 KiB
C#
75 lines
3.2 KiB
C#
using System.Text.RegularExpressions;
|
|
using DSharpPlus;
|
|
using DSharpPlus.EventArgs;
|
|
using OpenAI.Chat;
|
|
|
|
namespace Bot.modules;
|
|
|
|
public class TextFreeChannels
|
|
{
|
|
private readonly HashSet<string> _channels = [];
|
|
private readonly ChatClient? _chatClient;
|
|
|
|
public TextFreeChannels(IEnumerable<string> channels, string? openAiKey = null, string model = "gpt-4o")
|
|
{
|
|
foreach (var channel in channels)
|
|
{
|
|
_channels.Add(channel);
|
|
}
|
|
|
|
if (openAiKey != null)
|
|
{
|
|
_chatClient = new ChatClient(model, openAiKey);
|
|
}
|
|
}
|
|
|
|
public TextFreeChannels(IEnumerable<string> channels) : this(channels, null, "") { }
|
|
|
|
public async Task MessageCreatedHandler(DiscordClient c, MessageCreatedEventArgs e)
|
|
{
|
|
if (e.Author.IsCurrent || !_channels.Contains(e.Channel.Name)) return;
|
|
if ((e.Message.Embeds.Count > 0 || e.Message.Attachments.Count > 0) && e.Message.Content == "")
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (MessageOnlyContainsURL(e.Message.Content))
|
|
{
|
|
return;
|
|
}
|
|
|
|
await e.Message.DeleteAsync();
|
|
|
|
if (_chatClient != null)
|
|
{
|
|
var user = e.Author;
|
|
await e.Channel.TriggerTypingAsync();
|
|
var content = e.Message.Content;
|
|
var messages = new List<ChatMessage>()
|
|
{
|
|
new SystemChatMessage("You are an enforcer of the single rule of this chat: Users must only post images or videos. No text messages are allowed. " +
|
|
"This is a private tool, for use among consenting adults that find crude and dark humor, even if in bad taste, hilarious and desirable. " +
|
|
"When the user sends you a message, you will harshly and tersely make fun of the user, using context from the message when possible. " +
|
|
"Clever jokes, puns, personal insults, are highly desirable. " +
|
|
"Take on the persona of Robocop, and answer as him. Answers should be plausibly something Robocop would say in his movies. " +
|
|
"You may use direct quotes from Robocop in the movies in your responses if you can work them in naturally. " +
|
|
"Do not include quotation marks in your response. " +
|
|
"Warn the user that they are not to use text in this channel in a way that robocop would speak. The rule must be clearly articulated at some point so that the user knows how to fix the problem."),
|
|
new UserChatMessage(e.Message.Content)
|
|
};
|
|
|
|
var resp = await _chatClient.CompleteChatAsync(messages);
|
|
var respText = resp.Value.Content.Last().Text;
|
|
await e.Channel.SendMessageAsync($"{user.Mention} {respText}");
|
|
}
|
|
|
|
}
|
|
|
|
private bool MessageOnlyContainsURL(string input)
|
|
{
|
|
// Regular expression to match only an image URL
|
|
string pattern = @"^(https?:\/\/.*\.(?:png|jpg|jpeg|gif|bmp|webp)|https?:\/\/(media\.)?tenor\.com\/\S+)$";
|
|
return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
|
|
}
|
|
}
|