mirror of
https://github.com/DerTyp7/discord-twitch-bot.git
synced 2025-10-29 12:52:12 +01:00
init
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
node_modules/
|
||||
4
bot/.prettierrc
Normal file
4
bot/.prettierrc
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"tabWidth": 2,
|
||||
"useTabs": false
|
||||
}
|
||||
25
bot/app.js
Normal file
25
bot/app.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const { Client, GatewayIntentBits } = require("discord.js");
|
||||
const { registerCommands } = require("./commands");
|
||||
const config = require("./config");
|
||||
const { db, insertServer } = require("./database_handler");
|
||||
|
||||
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
|
||||
|
||||
registerCommands();
|
||||
|
||||
client.on("ready", () => {
|
||||
console.log(`Logged in as ${client.user.tag}!`);
|
||||
});
|
||||
|
||||
client.on("interactionCreate", async (interaction) => {
|
||||
console.log();
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
|
||||
if (interaction.commandName === "setup") {
|
||||
insertServer(interaction.guildId, interaction.guild.ownerId);
|
||||
|
||||
await interaction.reply("Pong!");
|
||||
}
|
||||
});
|
||||
|
||||
client.login(config.token);
|
||||
27
bot/commands.js
Normal file
27
bot/commands.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const { REST, Routes } = require("discord.js");
|
||||
const config = require("./config");
|
||||
|
||||
const commands = [
|
||||
{
|
||||
name: "setup",
|
||||
description: "Setup Twitch bot!",
|
||||
},
|
||||
];
|
||||
|
||||
const rest = new REST({ version: "10" }).setToken(config.token);
|
||||
|
||||
async function registerCommands() {
|
||||
try {
|
||||
console.log("Started refreshing application (/) commands.");
|
||||
|
||||
await rest.put(Routes.applicationCommands(config.clientId), {
|
||||
body: commands,
|
||||
});
|
||||
|
||||
console.log("Successfully reloaded application (/) commands.");
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { registerCommands };
|
||||
7
bot/config.js
Normal file
7
bot/config.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const config = {
|
||||
clientId: "1037396501732794489",
|
||||
token:
|
||||
"MTAzNzM5NjUwMTczMjc5NDQ4OQ.G-vEdY.yH-xVPVzErIPSLwCJ0AdNfcH-vPHX_dvNHLm3o",
|
||||
};
|
||||
|
||||
module.exports = config;
|
||||
24
bot/database_handler.js
Normal file
24
bot/database_handler.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const sqlite3 = require("sqlite3").verbose();
|
||||
const db = new sqlite3.Database("../database.sqlite");
|
||||
|
||||
function createDatabaseStructure() {
|
||||
db.serialize(() => {
|
||||
db.run(
|
||||
"CREATE TABLE server (id INTEGER, ownerId INTEGER, twitchOAuth TEXT)"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function insertServer(id, ownerId, twitchOAuth) {
|
||||
db.serialize(() => {
|
||||
db.run(
|
||||
`INSERT INTO server(id, ownerId, twitchOAuth) VALUES(${id}, ${ownerId}, '${
|
||||
twitchOAuth ? twitchOAuth : "null"
|
||||
}')`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// createDatabaseStructure();
|
||||
|
||||
module.exports = { db, insertServer, createDatabaseStructure };
|
||||
2739
bot/package-lock.json
generated
Normal file
2739
bot/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
16
bot/package.json
Normal file
16
bot/package.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "discord-twitch-bot",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"discord": "^0.8.2",
|
||||
"discord.js": "^14.6.0",
|
||||
"sqlite3": "^5.1.2"
|
||||
}
|
||||
}
|
||||
BIN
database.sqlite
Normal file
BIN
database.sqlite
Normal file
Binary file not shown.
1442
setup_server/package-lock.json
generated
Normal file
1442
setup_server/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
20
setup_server/package.json
Normal file
20
setup_server/package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "setup_server",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node server.js"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.20.1",
|
||||
"cookie-parser": "^1.4.6",
|
||||
"ejs": "^3.1.8",
|
||||
"express": "^4.18.2",
|
||||
"express-session": "^1.17.3",
|
||||
"uuid": "^9.0.0"
|
||||
}
|
||||
}
|
||||
63
setup_server/server.js
Normal file
63
setup_server/server.js
Normal file
@@ -0,0 +1,63 @@
|
||||
const express = require("express");
|
||||
const app = express();
|
||||
const path = require("path");
|
||||
const ejs = require("ejs");
|
||||
const { v4 } = require("uuid");
|
||||
const session = require("express-session");
|
||||
const cookieParser = require("cookie-parser");
|
||||
const bodyParser = require("body-parser");
|
||||
const port = 3000;
|
||||
const oneDay = 1000 * 60 * 60 * 24;
|
||||
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.use(cookieParser());
|
||||
|
||||
app.set("view engine", "ejs");
|
||||
app.use(
|
||||
session({
|
||||
secret: "thisismysecrctekeyfhrgfgrfrty84fwir767",
|
||||
saveUninitialized: true,
|
||||
cookie: { maxAge: oneDay },
|
||||
resave: false,
|
||||
})
|
||||
);
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
let discordAuthenticated = false;
|
||||
let twitchAuthenticated = false;
|
||||
|
||||
if (req.session.discordCode && req.session.discordGuildId) {
|
||||
discordAuthenticated = true;
|
||||
}
|
||||
|
||||
if (req.session.twtichCode) {
|
||||
twitchAuthenticated = true;
|
||||
}
|
||||
|
||||
res.render("./index.ejs", {
|
||||
discordAuthenticated: discordAuthenticated,
|
||||
twitchAuthenticated: twitchAuthenticated,
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/logout", (req, res) => {
|
||||
req.session.destroy();
|
||||
res.redirect("/");
|
||||
});
|
||||
|
||||
app.get("/authorize/twitch", (req, res) => {
|
||||
req.session.twtichCode = req.query.code;
|
||||
res.redirect("/");
|
||||
});
|
||||
|
||||
app.get("/authorize/discord", (req, res) => {
|
||||
req.session.discordCode = req.query.code;
|
||||
req.session.discordGuildId = req.query.guild_id;
|
||||
res.redirect("/");
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`);
|
||||
});
|
||||
12
setup_server/views/authorize.html
Normal file
12
setup_server/views/authorize.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Authorize</title>
|
||||
</head>
|
||||
<body>
|
||||
auth
|
||||
</body>
|
||||
</html>
|
||||
23
setup_server/views/index.ejs
Normal file
23
setup_server/views/index.ejs
Normal file
@@ -0,0 +1,23 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Document</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
discord <%= discordAuthenticated %> twitch <%= twitchAuthenticated%>
|
||||
<a
|
||||
id="discordLink"
|
||||
href="https://discord.com/api/oauth2/authorize?client_id=1037396501732794489&permissions=8&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauthorize%2Fdiscord&response_type=code&scope=identify%20bot"
|
||||
>Connect with Discord</a
|
||||
>
|
||||
<a
|
||||
id="twitchLink"
|
||||
href="https://id.twitch.tv/oauth2/authorize?client_id=evozwlc7xv0aggczc6ya92vnsrsxp2&redirect_uri=http://localhost:3000/authorize/twitch&response_type=code&scope=channel%3Aread%3Asubscriptions"
|
||||
>Connect with Twitch</a
|
||||
>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user