103 lines
No EOL
2 KiB
TypeScript
103 lines
No EOL
2 KiB
TypeScript
import { Client } from "tmi.js";
|
|
import { readFileSync } from "fs";
|
|
import { Logger } from "tslog";
|
|
const toml = require("toml");
|
|
|
|
var log = new Logger({
|
|
name: null,
|
|
displayFunctionName: false,
|
|
displayFilePath: "hidden",
|
|
minLevel: `silly`,
|
|
})
|
|
|
|
var config: config;
|
|
try {
|
|
config = toml.parse(readFileSync("config.toml"));
|
|
} catch {
|
|
log.error("Something went wrong while loading the configuration.");
|
|
process.exit(1);
|
|
};
|
|
|
|
|
|
|
|
export function change_index(value: number): void {
|
|
question_index = value;
|
|
};
|
|
|
|
export function reset_users() {
|
|
user_answers = {};
|
|
};
|
|
|
|
export var question_index = -1;
|
|
export var user_answers = {};
|
|
export var user_right_count = {};
|
|
export const correct_answers = config.quiz.answers;
|
|
|
|
|
|
export var commands = {};
|
|
|
|
|
|
import "./commands/start";
|
|
import "./commands/next";
|
|
|
|
|
|
const client = Client({
|
|
channels: config.twitch.chat.channels,
|
|
connection: {
|
|
secure: true,
|
|
reconnect: true,
|
|
},
|
|
identity: {
|
|
username: config.twitch.auth.username,
|
|
password: config.twitch.auth.oauth_token,
|
|
},
|
|
});
|
|
|
|
client.connect();
|
|
|
|
client.on(`connected`, () => {
|
|
log.info(`Connected to Twitch`);
|
|
});
|
|
|
|
client.on(`chat`, (channel, ctx, msg, self) => {
|
|
|
|
if (self) return;
|
|
|
|
let answer = msg.match(/\([ABCDEF]\)/i);
|
|
if (answer) {
|
|
let letter = answer[0].slice(1,2).toUpperCase();
|
|
// client.say(channel, `${ctx.username} voted for answer: ${answer[0]}`)
|
|
|
|
if (user_answers[ctx.username] == null) {
|
|
log.info(`${ctx.username} voted for answer: ${letter}`);
|
|
user_answers[ctx.username] = letter;
|
|
};
|
|
};
|
|
|
|
if (msg.startsWith(config.twitch.chat.command_prefix)) {
|
|
let cmd = msg.split(` `);
|
|
|
|
var is_mod = (
|
|
ctx.mod
|
|
|| ctx.badges?.moderator === "1"
|
|
|| ctx.badges?.broadcaster === "1"
|
|
);
|
|
|
|
if (!is_mod) {
|
|
log.warn(`${ctx.username} tried to run a command, but isn't a mod`);
|
|
return;
|
|
};
|
|
|
|
if (!commands[cmd[0]]) {
|
|
// No-OP
|
|
}
|
|
else {
|
|
log.info(`${ctx.username} is running a command (${cmd[0]})`);
|
|
let response = commands[cmd[0]](cmd.slice(1));
|
|
|
|
if (response.length > 0) {
|
|
client.say( channel, response );
|
|
};
|
|
};
|
|
};
|
|
}) |