Update the code to work
This commit is contained in:
parent
4dc25492e5
commit
1b18eabbc3
8 changed files with 332 additions and 2130 deletions
|
|
@ -1,40 +0,0 @@
|
||||||
{
|
|
||||||
"DEV": false,
|
|
||||||
"twitch": {
|
|
||||||
"OAUTH_TOKEN": null,
|
|
||||||
"USERNAME": null,
|
|
||||||
"CLIENT_ID": null,
|
|
||||||
"CHANNELS": [],
|
|
||||||
"ADMIN": [],
|
|
||||||
"REPLY_TO_AUTO_ADD": false,
|
|
||||||
"ALERT_MISSED_BITS": false,
|
|
||||||
"AUTO_ADD": false
|
|
||||||
},
|
|
||||||
"discord": {
|
|
||||||
"OAUTH_TOKEN": null,
|
|
||||||
"DEV_TOKEN": null,
|
|
||||||
"SECRET": null,
|
|
||||||
"PERMISSIONS_VALUE": 3072,
|
|
||||||
"CLIENT_ID": null,
|
|
||||||
"MOD_ROLES": [],
|
|
||||||
"ADMIN": []
|
|
||||||
},
|
|
||||||
"bot": {
|
|
||||||
"PREFIX": "!",
|
|
||||||
"COOLDOWN_TIME": 5,
|
|
||||||
"COOLDOWN_TYPE": "SERVICE",
|
|
||||||
"INVALID_PERM_ERROR": true
|
|
||||||
},
|
|
||||||
"webhooks": {
|
|
||||||
"ENABLED": false,
|
|
||||||
"LOGGING": null,
|
|
||||||
"ERROR": null,
|
|
||||||
"TWITCH_MISSED_BITS": null
|
|
||||||
},
|
|
||||||
"web": {
|
|
||||||
"ADDRESS": null,
|
|
||||||
"ROOT": "./docs",
|
|
||||||
"PORT": 4000
|
|
||||||
},
|
|
||||||
"extra": {}
|
|
||||||
}
|
|
||||||
15
config.template.toml
Normal file
15
config.template.toml
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
[twitch]
|
||||||
|
|
||||||
|
[twitch.auth]
|
||||||
|
oauth_token = ""
|
||||||
|
username = ""
|
||||||
|
|
||||||
|
[twitch.chat]
|
||||||
|
command_prefix = "!"
|
||||||
|
channels = []
|
||||||
|
|
||||||
|
[discord]
|
||||||
|
|
||||||
|
[discord.webhook]
|
||||||
|
enabled = true
|
||||||
|
url = ""
|
||||||
2188
package-lock.json
generated
2188
package-lock.json
generated
File diff suppressed because it is too large
Load diff
11
package.json
11
package.json
|
|
@ -8,10 +8,13 @@
|
||||||
},
|
},
|
||||||
"author": "Oliver Akins",
|
"author": "Oliver Akins",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/express": "^4.17.2",
|
|
||||||
"@types/node": "^12.7.5",
|
|
||||||
"@types/tmi.js": "^1.4.0",
|
|
||||||
"axios": "^0.24.0",
|
"axios": "^0.24.0",
|
||||||
"tmi.js": "^1.5.0"
|
"tmi.js": "^1.8.5",
|
||||||
|
"toml": "^3.0.0",
|
||||||
|
"tslog": "^3.3.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^16.11.12",
|
||||||
|
"@types/tmi.js": "^1.8.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
69
src/commands/next.ts
Normal file
69
src/commands/next.ts
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
import axios from "axios";
|
||||||
|
import {
|
||||||
|
change_index,
|
||||||
|
commands,
|
||||||
|
question_index,
|
||||||
|
user_answers,
|
||||||
|
correct_answers,
|
||||||
|
user_right_count,
|
||||||
|
reset_users
|
||||||
|
} from "../main";
|
||||||
|
|
||||||
|
|
||||||
|
function next_command(_: string[]) {
|
||||||
|
|
||||||
|
// Ensure the quiz has been started.
|
||||||
|
if (question_index < 0) {
|
||||||
|
return `Cannot go the next question before "!start"ing the quiz.`;
|
||||||
|
};
|
||||||
|
|
||||||
|
// count users who got it right
|
||||||
|
let users_correct = Object.values(user_answers)
|
||||||
|
.filter(x => x == correct_answers[question_index])
|
||||||
|
.length;
|
||||||
|
|
||||||
|
let correct_users: string[] = [];
|
||||||
|
for (var user in user_answers) {
|
||||||
|
if (user_answers[user] == correct_answers[question_index]) {
|
||||||
|
correct_users.push(user);
|
||||||
|
if (user_right_count[user] == null) {
|
||||||
|
user_right_count[user] = 1;
|
||||||
|
} else {
|
||||||
|
user_right_count[user]++;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
reset_users();
|
||||||
|
|
||||||
|
axios.post(
|
||||||
|
`https://ptb.discord.com/api/webhooks/916068083863539782/9k2b66SUIeY8hl5id7gdnZF6Abc8Fb1f5-RVlgbUJqirNmALkrrgrCWdsufcZ5Bn2a4i`,
|
||||||
|
{
|
||||||
|
content: correct_users.length > 0 ? `Users who got question ${question_index + 1} right:
|
||||||
|
\`\`\`
|
||||||
|
${correct_users.join(", ")}
|
||||||
|
\`\`\`` : `No one got question ${question_index + 1} right.`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Check if the quiz has hit the end
|
||||||
|
if (question_index == correct_answers.length - 1) {
|
||||||
|
|
||||||
|
// Send message in Discord for record-keeping
|
||||||
|
axios.post(
|
||||||
|
`https://ptb.discord.com/api/webhooks/916068083863539782/9k2b66SUIeY8hl5id7gdnZF6Abc8Fb1f5-RVlgbUJqirNmALkrrgrCWdsufcZ5Bn2a4i`,
|
||||||
|
{
|
||||||
|
content: `\`\`\`json\n${JSON.stringify(user_right_count, null, "\t")}\`\`\``
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
change_index(-1);
|
||||||
|
return `${users_correct} players got the last question right. Quiz has ended. Kthxbai.`;
|
||||||
|
};
|
||||||
|
|
||||||
|
change_index(question_index + 1);
|
||||||
|
return `${users_correct} players got that question right. Changed to the next question!`;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
commands["!next"] = next_command;
|
||||||
15
src/commands/start.ts
Normal file
15
src/commands/start.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
import { change_index, commands, question_index } from "../main";
|
||||||
|
|
||||||
|
|
||||||
|
function start_command(_: string[]) {
|
||||||
|
|
||||||
|
if (question_index >= 0) {
|
||||||
|
return `The quiz has already been started`;
|
||||||
|
};
|
||||||
|
|
||||||
|
change_index(0);
|
||||||
|
return `Quiz has been started!`;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
commands["!start"] = start_command;
|
||||||
106
src/main.ts
Normal file
106
src/main.ts
Normal 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 );
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
})
|
||||||
18
src/types/config.d.ts
vendored
Normal file
18
src/types/config.d.ts
vendored
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
interface config {
|
||||||
|
twitch: {
|
||||||
|
auth: {
|
||||||
|
oauth_token: string;
|
||||||
|
username: string;
|
||||||
|
};
|
||||||
|
chat: {
|
||||||
|
command_prefix: string;
|
||||||
|
channels: string[];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
discord: {
|
||||||
|
webhook: {
|
||||||
|
enabled: boolean;
|
||||||
|
url: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue