Update the code to work

This commit is contained in:
Oliver-Akins 2021-12-12 15:50:27 -06:00
parent 4dc25492e5
commit 1b18eabbc3
8 changed files with 332 additions and 2130 deletions

106
src/main.ts Normal file
View file

@ -0,0 +1,106 @@
import { Client } from "tmi.js";
import { readFileSync } from "fs";
import { Logger } from "tslog";
import * as tmi from "tmi.js";
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 = [
"A", "B", "D", "C"
];
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();
log.info(`${ctx.username} voted for answer: ${letter}`);
client.say(channel, `${ctx.username} voted for answer: ${answer[0]}`)
if (user_answers[ctx.username] == null) {
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]]) {
log.warn(`${ctx.username} tried to run an invalid command`);
}
else {
log.info(`${ctx.username} is running a command`);
let response = commands[cmd[0]](cmd.slice(1));
if (response.length > 0) {
client.say( channel, response );
};
};
};
})