Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make stuff work #45

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
config.json
lyrics
downloads
Binary file added bun.lockb
Binary file not shown.
8 changes: 5 additions & 3 deletions buttonrow.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { MessageActionRow } from "discord.js"
import { ActionRowBuilder, ButtonBuilder } from "discord.js"

export default class ButtonRow {

constructor(options, ...buttons) {
this.options = options
this.row = new MessageActionRow()
this.row = new ActionRowBuilder()
.addComponents(buttons)
this.msg = null
this.disabled = false
Expand Down Expand Up @@ -40,7 +40,9 @@ export default class ButtonRow {
editRow(cb) {
if(!cb || this.disabled) return
this.editOptions(msg => {
let row = msg.components[0]
console.log(msg.components[0]);
let row = msg.components[0];
row.components = row.components.map(component => ButtonBuilder.from(component))
cb(row)
return { components: [row] }
})
Expand Down
4 changes: 3 additions & 1 deletion commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export function register(bot) {
await bot.commands[interaction.commandName].run(bot, interaction);
} catch (error) {
logger.error(error);
interaction.reply({ content: ":woozy_face: There was an error executing that command.", ephemeral: true });
if (interaction.isRepliable()) {
interaction.reply({ content: ":woozy_face: There was an error executing that command.", ephemeral: true });
}
}
} else {
logger.error("Unknown command received: " + interaction.commandName);
Expand Down
10 changes: 5 additions & 5 deletions commands/game.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessageEmbed } from "discord.js";
import { EmbedBuilder } from "discord.js";
import { games } from "../lyrics.js";
import Game from "../game.js"

Expand Down Expand Up @@ -53,10 +53,10 @@ export default {
run: async (bot, interaction) => {
const sub = interaction.options.getSubcommand();
if(sub === "list") {
const embed = new MessageEmbed();
const embed = new EmbedBuilder();
embed.setTitle("Songs (" + lyrics.length + ")");
embed.setFooter("SongGuesser");
embed.setColor([0, 230, 0]);
embed.setColor(0x00E600);
embed.addFields(lyrics.map(l => {
return {
name: `${l.title}`,
Expand Down Expand Up @@ -84,7 +84,7 @@ export default {
interaction.reply({ embeds: [{
title: "**No game running!**",
description: "There is no game running in this channel",
color: [230, 0, 0]
color: 0xE60000
}]})
return;
}
Expand All @@ -95,7 +95,7 @@ export default {
interaction.reply({ embeds: [{
title: "**No game running!**",
description: "There is no game running in this channel",
color: [230, 0, 0]
color: 0xE60000
}]})
return;
}
Expand Down
2 changes: 1 addition & 1 deletion commands/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
interaction.reply({ embeds: [{
title: "**I am responsive!**",
description: `:ping_pong: Pong! (${bot.ws.ping}ms)`,
color: [0, 230, 0]
color: 0x00E600
}], ping: bot.ws.ping });
}
}
6 changes: 3 additions & 3 deletions commands/points.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessageEmbed } from "discord.js";
import { EmbedBuilder } from "discord.js";
import points from "../points.js";

export default {
Expand Down Expand Up @@ -44,7 +44,7 @@ export default {
const user = interaction.options.getUser("user") || interaction.user;
const point = points.getPoints(user.id, interaction.guild.id);
console.log(point);
const embed = new MessageEmbed();
const embed = new EmbedBuilder();
embed.setTitle(`${user.username}'s points`);
embed.setFooter("SongGuesser");
embed.setColor("YELLOW");
Expand All @@ -58,7 +58,7 @@ export default {
return;
}
const top = global ? points.getTopList() : points.getTopList(interaction.guild.id);
const embed = new MessageEmbed();
const embed = new EmbedBuilder();
embed.setTitle("Top list for " + (global ? "all guilds" : interaction.guild.name));
embed.setFooter("SongGuesser");
embed.setColor("#3cbd9b");
Expand Down
6 changes: 3 additions & 3 deletions commands/round.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessageEmbed } from "discord.js";
import { EmbedBuilder } from "discord.js";
import { games } from "../lyrics.js";
import Game from "../game.js"

Expand Down Expand Up @@ -28,7 +28,7 @@ export default {
interaction.reply({ embeds: [{
title: "**No game running!**",
description: "There is no game running in this channel",
color: [230, 0, 0]
color: 0xE60000
}]})
return;
}
Expand All @@ -39,7 +39,7 @@ export default {
interaction.reply({ embeds: [{
title: "**No game running!**",
description: "There is no game running in this channel",
color: [230, 0, 0]
color: 0xE60000
}]})
return;
}
Expand Down
46 changes: 23 additions & 23 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import Logger from "./logger.js"
import { bot, getFooter } from "./main.js"
import Round from "./round.js"
import ButtonRow from "./buttonrow.js"
import config from "./config.json" assert {type: "json"};
import points from "./points.js"
import { ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
import { readFile } from "fs/promises"

export const config = await readFile("./config.json", { encoding: "utf-8" }).then(JSON.parse);

const logger = new Logger("Game")

Expand Down Expand Up @@ -60,27 +63,24 @@ export default class Game {
}]
}

this.buttonrow = new ButtonRow(msgopt, {
customId: `join_game`,
disabled: false,
emoji: { name: `🎶` },
label: "Tune in",
style: "PRIMARY",
type: "BUTTON"
}, {
customId: `quickstart_game`,
disabled: true,
emoji: { name: `⏩` },
label: "Quickstart",
style: "SUCCESS",
type: "BUTTON"
}, {
customId: `cancel_game`,
disabled: false,
label: "Cancel",
style: "DANGER",
type: "BUTTON"
})
this.buttonrow = new ButtonRow(msgopt, new ButtonBuilder()
.setCustomId("join_game")
.setDisabled(false)
.setEmoji("🎶")
.setLabel("Tune in")
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId("quickstart_game")
.setDisabled(true)
.setEmoji("⏩")
.setLabel("Quickstart")
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setCustomId("cancel_game")
.setDisabled(false)
.setLabel("Cancel")
.setStyle(ButtonStyle.Danger)
)

if(interaction) this.buttonrow.reply(interaction)
else this.buttonrow.send(this.channel)
Expand Down Expand Up @@ -136,7 +136,7 @@ export default class Game {

if(this.buttonrow && this.state == Game.STARTING) {
this.buttonrow.editOptions(msg => {
let embed = msg.embeds[0]
let embed = EmbedBuilder.from(msg.embeds[0])
embed.fields = [{name: `Participants (${this.participantCount}/${minParticipants}):`, value: this.participantlist.join(", ")}]
return {embeds: [embed]}
})
Expand Down
56 changes: 34 additions & 22 deletions lyrics.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import fs from "fs/promises"
import ytdl from "ytdl-core"
import { readFile } from "fs/promises"
import Logger from "./logger.js"
import { COLOR } from "./logger.js"
import Game from "./game.js"
import config from "./config.json" assert {type: "json"}
import { ytDlp } from "./main.js"

export const config = await readFile("./config.json", { encoding: "utf-8" }).then(JSON.parse);

const lyricslib = await import(config.lyrics + "main.mjs")

Expand All @@ -24,6 +25,34 @@ let bot

export default class LyricsMan {

static async init() {
// fetch song lengths
const a = []
const promises = []

for (let l of lyrics) {
let id = l.id
a.push(id)
if(l.options.length) continue

promises.push(ytDlp.retrieveMediaInfoList(l.url))
}

let mediaInfos
try {
mediaInfos = await Promise.all(promises)
} catch (e) {
logger.error(`fetch length error ${id}:`, e)
}

for (let i in mediaInfos) {
const info = mediaInfos[i][0]
lyrics[i].length = info.duration
}

logger.log(`Loaded lyrics: ${a.join(", ")}`)
}

constructor(_bot) {
bot = _bot

Expand All @@ -40,7 +69,6 @@ export default class LyricsMan {
})

bot.on("interactionCreate", i => {
if(i.type != "MESSAGE_COMPONENT") return
if(!i.isButton()) return
if(!["join_game","quickstart_game","cancel_game"].includes(i.customId)) return

Expand All @@ -49,7 +77,7 @@ export default class LyricsMan {
i.reply({ embeds: [{
title: "**No game running!**",
description: "There is no game running in this channel",
color: [230, 0, 0],
color: 0xE60000,
ephemeral: true
}]})
return
Expand Down Expand Up @@ -83,20 +111,4 @@ export default class LyricsMan {

}

}

// fetch song lengths
let a = []
for (let l of lyrics) {
let id = l.id
a.push(id)
if(l.options.length) continue
ytdl.getBasicInfo(l.url).then(info => {
l.length = parseInt(info.videoDetails.lengthSeconds)
}).catch(e => {
logger.error(`fetch length error ${id}:`, e)
})
}

logger.log(`Loaded lyrics: ${a.join(", ")}`)
//console.log(lyrics)
}
20 changes: 15 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Client, WebhookClient } from "discord.js";
import config from "./config.json" assert {type: "json"};
import { Client, GatewayIntentBits, Partials, WebhookClient } from "discord.js";
import { load, register } from "./commands.js"
import statusmgr from "./statusmgr.js"
import LyricsMan from "./lyrics.js"
import Logger from "./logger.js"
import { COLOR } from "./logger.js"
import points from "./points.js";
import { db, createTables } from "./sql.js";
import fs from "fs/promises"
import { readFile } from "fs/promises"
import { execSync } from "child_process"
import { YtDlp } from "@yemreak/yt-dlp"

export const config = await readFile("./config.json", { encoding: "utf-8" }).then(JSON.parse);

export let errorhook = null;
if(config.errorhook) {
Expand Down Expand Up @@ -42,11 +44,19 @@ if(config.errorhook) {
}

const logger = new Logger("Discord Bot", "38;2;255;0;255;3")
export const bot = new Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_VOICE_STATES", "DIRECT_MESSAGES"], partials: ["CHANNEL"] });
export const bot = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.DirectMessages, GatewayIntentBits.MessageContent], partials: [Partials.Channel] });

export const VERSION = await getVersion()
export const BRANCH = getBranch()

const ytdlpConfig = { workdir: "./downloads" }
export const ytDlp = new YtDlp(ytdlpConfig)

// install ytdlp from pip
await ytDlp.downloadLatestReleaseIfNotExists()

await LyricsMan.init()

bot.on("ready", async () => {
logger.log(`Logged in as ${bot.user.tag}!`);

Expand All @@ -70,7 +80,7 @@ bot.on("messageCreate", (msg) => {
})

async function getVersion() {
let data = await fs.readFile("./package.json", { encoding: "utf-8" })
let data = await readFile("./package.json", { encoding: "utf-8" })
try {
data = JSON.parse(data)
} catch (e) {
Expand Down
Loading