no real commit messages
This commit is contained in:
parent
66b307276f
commit
493c290e9b
10
IRCBot.cs
10
IRCBot.cs
@ -11,7 +11,7 @@ public abstract class IRCBot
|
|||||||
public int Port;
|
public int Port;
|
||||||
public string Nick;
|
public string Nick;
|
||||||
public string RealName;
|
public string RealName;
|
||||||
public List<string>? Channels;
|
public List<string> Channels = new();
|
||||||
public List<Action<PRIVMSG>> Listeners = new();
|
public List<Action<PRIVMSG>> Listeners = new();
|
||||||
private List<CommandRunner> CommandRunners = new();
|
private List<CommandRunner> CommandRunners = new();
|
||||||
private List<PollRunner> PollRunners = new();
|
private List<PollRunner> PollRunners = new();
|
||||||
@ -60,15 +60,16 @@ public abstract class IRCBot
|
|||||||
Stream.Write(bytes, 0, bytes.Length);
|
Stream.Write(bytes, 0, bytes.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendPrivmsg(string channel, string message)
|
public void SendPrivmsg(string destination, string message)
|
||||||
{
|
{
|
||||||
if (message != string.Empty)
|
if (message != string.Empty)
|
||||||
SendLine($"PRIVMSG {channel} :{message}");
|
SendLine($"PRIVMSG {destination} :{message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void JoinChannel(string channel)
|
public void JoinChannel(string channel)
|
||||||
{
|
{
|
||||||
SendLine($"JOIN {channel}");
|
SendLine($"JOIN {channel}");
|
||||||
|
Channels.Add(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void JoinChannels(string[] channels)
|
public void JoinChannels(string[] channels)
|
||||||
@ -80,14 +81,13 @@ public abstract class IRCBot
|
|||||||
public void PartChannel(string channel)
|
public void PartChannel(string channel)
|
||||||
{
|
{
|
||||||
SendLine($"PART {channel}");
|
SendLine($"PART {channel}");
|
||||||
|
Channels.Remove(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MainLoop()
|
public void MainLoop()
|
||||||
{
|
{
|
||||||
foreach (PollRunner runner in PollRunners)
|
foreach (PollRunner runner in PollRunners)
|
||||||
{
|
|
||||||
runner.Run();
|
runner.Run();
|
||||||
}
|
|
||||||
using var reader = new StreamReader(Stream, Encoding.UTF8);
|
using var reader = new StreamReader(Stream, Encoding.UTF8);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
@ -4,7 +4,7 @@ class Program
|
|||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var cube = new Cube("localhost", 6667, "testbot", "~nebula");
|
var cube = new Cube("localhost", 6667, "mysterious_cube", "~nebula https://git.tilde.town/nebula/riff");
|
||||||
new Thread(new ThreadStart(cube.MainLoop)).Start();
|
new Thread(new ThreadStart(cube.MainLoop)).Start();
|
||||||
}
|
}
|
||||||
}
|
}
|
6
Types.cs
6
Types.cs
@ -45,9 +45,11 @@ public class PRIVMSG
|
|||||||
{
|
{
|
||||||
if (message.Source == null || message.Arguments == null)
|
if (message.Source == null || message.Arguments == null)
|
||||||
throw new NoNullAllowedException();
|
throw new NoNullAllowedException();
|
||||||
Nick = message.Source.Substring(0, message.Source.IndexOf("!") - 1);
|
Nick = message.Source.Substring(0, message.Source.IndexOf("!"));
|
||||||
Sender = message.Arguments[0];
|
|
||||||
Body = message.Arguments[1].Trim();
|
Body = message.Arguments[1].Trim();
|
||||||
|
Sender = message.Arguments[0];
|
||||||
|
if (Sender[0] != '#')
|
||||||
|
Sender = Nick;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
76
bots/Cube.cs
76
bots/Cube.cs
@ -1,16 +1,28 @@
|
|||||||
|
using System.Data;
|
||||||
|
using System.Text.Json;
|
||||||
using riff;
|
using riff;
|
||||||
|
|
||||||
public class Cube : IRCBot
|
public class Cube : IRCBot
|
||||||
{
|
{
|
||||||
|
private HttpClient RequestClient = new();
|
||||||
|
private const string AllTriviaQuestionsPath = "data/TriviaQuestions.json";
|
||||||
|
private const string UnaskedTriviaQuestionsPath = "data/UnaskedTriviaQuestions.json";
|
||||||
|
private TriviaQuestion[] AllTriviaQuestions;
|
||||||
|
private List<TriviaQuestion> UnaskedTriviaQuestions;
|
||||||
|
private Random Rand = new();
|
||||||
|
|
||||||
public Cube(string host, int port, string nick, string realname) : base(host, port, nick, realname)
|
public Cube(string host, int port, string nick, string realname) : base(host, port, nick, realname)
|
||||||
{
|
{
|
||||||
|
AllTriviaQuestions = LoadAllQuestions();
|
||||||
|
UnaskedTriviaQuestions = LoadUnaskedQuestions();
|
||||||
|
|
||||||
string[] channels = [
|
string[] channels = [
|
||||||
"#bots",
|
"#testbot",
|
||||||
];
|
];
|
||||||
|
|
||||||
(string, Action<PRIVMSG>)[] commands = [
|
(string, Action<PRIVMSG>)[] commands = [
|
||||||
("echo", Echo)
|
("echo", Echo),
|
||||||
|
("trivia", PostQuestion)
|
||||||
];
|
];
|
||||||
|
|
||||||
HookCommands(commands);
|
HookCommands(commands);
|
||||||
@ -21,4 +33,64 @@ public class Cube : IRCBot
|
|||||||
{
|
{
|
||||||
SendPrivmsg(privmsg.Sender, privmsg.Body);
|
SendPrivmsg(privmsg.Sender, privmsg.Body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void PostQuestion(PRIVMSG privmsg)
|
||||||
|
{
|
||||||
|
var question = GetQuestion();
|
||||||
|
SendPrivmsg(privmsg.Sender, question.Question);
|
||||||
|
}
|
||||||
|
|
||||||
|
private TriviaQuestion GetQuestion()
|
||||||
|
{
|
||||||
|
if (UnaskedTriviaQuestions.Count == 0)
|
||||||
|
UnaskedTriviaQuestions = AllTriviaQuestions.ToList();
|
||||||
|
TriviaQuestion question = UnaskedTriviaQuestions[Rand.Next(UnaskedTriviaQuestions.Count)];
|
||||||
|
UnaskedTriviaQuestions.Remove(question);
|
||||||
|
File.WriteAllText(UnaskedTriviaQuestionsPath, JsonSerializer.Serialize(UnaskedTriviaQuestions));
|
||||||
|
return question;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TriviaQuestion[] LoadAllQuestions()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (FileStream stream = File.OpenRead(AllTriviaQuestionsPath))
|
||||||
|
{
|
||||||
|
TriviaQuestion[] questions = JsonSerializer.Deserialize<TriviaQuestion[]>(stream)!;
|
||||||
|
if (questions == null)
|
||||||
|
throw new NoNullAllowedException();
|
||||||
|
return questions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (FileNotFoundException)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Trivia questions not downloaded. Use download_trivia.py script.");
|
||||||
|
Environment.Exit(1);
|
||||||
|
return Array.Empty<TriviaQuestion>(); // Unreachable, but required for compiler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<TriviaQuestion> LoadUnaskedQuestions()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (FileStream stream = File.OpenRead(UnaskedTriviaQuestionsPath))
|
||||||
|
{
|
||||||
|
List<TriviaQuestion> questions = JsonSerializer.Deserialize<List<TriviaQuestion>>(stream)!;
|
||||||
|
if (questions == null)
|
||||||
|
throw new NoNullAllowedException();
|
||||||
|
return questions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (FileNotFoundException)
|
||||||
|
{
|
||||||
|
return AllTriviaQuestions.ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TriviaQuestion
|
||||||
|
{
|
||||||
|
public required string Question { get; set; }
|
||||||
|
public required bool Answer { get; set; }
|
||||||
}
|
}
|
1
data/TriviaQuestions.json
Normal file
1
data/TriviaQuestions.json
Normal file
File diff suppressed because one or more lines are too long
32
download_trivia.py
Normal file
32
download_trivia.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from json import dump
|
||||||
|
from time import sleep
|
||||||
|
from base64 import b64decode
|
||||||
|
import requests
|
||||||
|
|
||||||
|
api_url = "https://opentdb.com/api.php"
|
||||||
|
token_url = "https://opentdb.com/api_token.php?command=request"
|
||||||
|
|
||||||
|
file_path = "TriviaQuestions.json"
|
||||||
|
|
||||||
|
questions = []
|
||||||
|
|
||||||
|
token = requests.get(token_url).json()["token"]
|
||||||
|
params = {
|
||||||
|
"amount": 50,
|
||||||
|
"type": "boolean",
|
||||||
|
"encode": "base64",
|
||||||
|
"token": token
|
||||||
|
}
|
||||||
|
while True:
|
||||||
|
r = requests.get(api_url, params=params).json()
|
||||||
|
if r["response_code"] != 0:
|
||||||
|
print(r)
|
||||||
|
break
|
||||||
|
for question in r["results"]:
|
||||||
|
question_text = b64decode(question["question"]).decode("utf-8")
|
||||||
|
question_answer = b64decode(question["correct_answer"]).decode("utf-8") == "True"
|
||||||
|
questions.append({"Question": question_text, "Answer": question_answer})
|
||||||
|
print(len(questions))
|
||||||
|
sleep(6)
|
||||||
|
with open(file_path, "w") as f:
|
||||||
|
dump(questions, f)
|
@ -7,4 +7,8 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="System.Net.Http.Json" Version="9.0.6" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user