Attempts to fix ytsearch, side effect age-restricted videos should be ok now

This commit is contained in:
Robert 2023-04-28 15:50:08 -07:00
parent e9055670b9
commit 55500fc439
4 changed files with 58 additions and 62 deletions

0
.data/.gitkeep Executable file
View File

View File

@ -1,3 +1,6 @@
Sounds Sounds
Markov Markov
.git
.gitignore
Dockerfile
.dockerignore

113
bot.js
View File

@ -1,19 +1,17 @@
// ZeSkypeBot 7.0.0_a
// *** Libraries to import *** // *** Libraries to import ***
const { Client, Events, GatewayIntentBits } = require('discord.js'); const { Client, Events, GatewayIntentBits } = require('discord.js');
const Voice = require('@discordjs/voice'); const Voice = require('@discordjs/voice');
const { createReadStream, existsSync } = require('fs'); const { createReadStream, existsSync } = require('fs');
const play = require('play-dl'); const play = require('play-dl');
// Local config files to import // Local config files to import
const { token } = require('./config.json'); const { token } = require('./.data/config.json');
// Declare a new Discord Client Instance // Declare a new Discord Client Instance
const client = new Client( { intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildVoiceStates] } ); const client = new Client( { intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildVoiceStates] } );
// Define Sigtrap Callback for clean shutdowns // Define Sigtrap Callback for clean shutdowns
function signalExit(signal) { function signalExit(signal) {
console.log("\tCaught ${signal}"); console.log(`\tCaught ${signal}`);
// Kills voice connections, and logs the client out // Kills voice connections, and logs the client out
client.destroy(); client.destroy();
// Exit with return code 0 // Exit with return code 0
@ -108,24 +106,66 @@ client.on( Events.MessageCreate, async message => {
const resource = Voice.createAudioResource(createReadStream(soundPath)); const resource = Voice.createAudioResource(createReadStream(soundPath));
const player = Voice.createAudioPlayer(); const player = Voice.createAudioPlayer();
// Play the resource through the player to the connection (i hate discord v16) // Play the resource through the player to the connection (i hate discord v16)
player.on('Playing', () => {
console.log(`Playing ${soundpath}`);
});
player.play(resource); player.play(resource);
// Connect the player to the voice connection (internal screaming intensifies)
connection.subscribe(player); connection.subscribe(player);
// Setup Callbacks for when the player completes
player.on('Idle', () => {
console.log(`Finished ${soundpath}`);
});
return; return;
} }
} }
// Otherwise process commands // Check for youtube play command
if (message.content.startsWith('!play') && message.member.voice.channel) {
console.log(`!play issued by ${message.author.tag} ${message.author.id}`);
// Determine if we have a URL or a search term
const ytmatch = message.content.match(/(?<=(https?:\/\/)(www\.)?youtu((be\.com\/watch\?v=)|(\.be\/)))[\w-]{11,12}/);
var yturl = null;
var timestamp = '0'
if (ytmatch) {
console.log(`ytmatch yields ${ytmatch[0]}`);
// Message matches the youtube video regex, see if it comes with a timestamp
const timematch = message.content.match(/(?<=t=)(\d{1,4})s?(?=$)/);
if (timematch) {
console.log(`timematch yields $timematch[1]`);
timestamp = timematch[1];
}
// Reconstruct a URL for the supplied video
yturl = `https://youtu.be/${ytmatch[0]}`;
} else {
// Message may be a search term
let args = message.content.split('play ')[1];
console.log(`ytsearch args: "${args}"`);
// Search youtube with the term
let yt_info = await play.search(args, { limit: 1 });
// and get the first result as a URL
yturl = yt_info[0].url;
// Link it to the chat
message.channel.send(`Found video ${yturl} hope its what you were looking for`);
}
console.log(`yturl: ${yturl}`);
// Create a stream reference to the provided URL
const stream = await play.stream(yturl, { seek: timestamp });
// Now that we have a stream reference we can join the channel and play
const connection = Voice.joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});
// Open the stream as a voice resource
const resource = Voice.createAudioResource(stream.stream, { inputType: stream.type });
// Create a player to play the resource
const player = Voice.createAudioPlayer();
// Play the resource through the player (i hate discord v16)
player.play(resource);
// Connect the player to the voice connection (internal screaming intensifies)
connection.subscribe(player);
// Setup Callbacks for when the player completes
return;
}
if (message.content === "!help" ) { if (message.content === "!help" ) {
console.log(`!help issued by ${message.author.tag} ${message.author.id}`); console.log(`!help issued by ${message.author.tag} ${message.author.id}`);
var response = 'ZeSkypeBot v7.1.0 \'Table Salt\'\n'; var response = 'ZeSkypeBot v7.2.0 \'Table Salt\'\n';
response += 'Original by moosecrap#5953 for Skype, Remade by Weegee#6402 for Discord\n'; response += 'Original by moosecrap#5953 for Skype, Remade by Weegee#6402 for Discord\n';
response += 'Changelog:\n'; response += 'Changelog:\n';
response += 'v7.2.0 Attempts to fix seemingly random searches, now should support age-restricted videos\n';
response += 'v7.1.0 Added support for searching in !play\n' response += 'v7.1.0 Added support for searching in !play\n'
response += 'v7.0.0 Update to Discord.js 14\n'; response += 'v7.0.0 Update to Discord.js 14\n';
response += 'v6.1.0 Update to Discord.js 13, replaced ytdl-core-discord with play-dl\n'; response += 'v6.1.0 Update to Discord.js 13, replaced ytdl-core-discord with play-dl\n';
@ -144,53 +184,6 @@ client.on( Events.MessageCreate, async message => {
message.channel.send(response); message.channel.send(response);
return; return;
} }
// Check for youtube play command
if (message.content.startsWith('!play') && message.member.voice.channel) {
// Determine if we have a URL or a search term
const ytmatch = message.content.match(/(?<=(https?:\/\/)(www\.)?youtu((be\.com\/watch\?v=)|(\.be\/)))[\w-]{11}/);
var yturl = null;
var timestamp = '0'
if (ytmatch) {
// Message matches the youtube video regex, see if it comes with a timestamp
const timematch = message.content.match(/(?<=t=)(\d{1,4})s?(?=$)/);
if (timematch) {
timestamp = timematch[1];
}
// Reconstruct a URL for the supplied video
yturl = `https://youtu.be/${ytmatch[0]}`;
} else {
// Message may be a search term
let args = message.content.split('play')[1];
// Search youtube with the term
let yt_info = await play.search(args, { limit: 1 });
// and get the first result as a URL
yturl = yt_info[0].url;
}
// Create a stream reference to the provided URL
const stream = await play.stream(yturl, { seek: timestamp });
// Now that we have a stream reference we can join the channel and play
const connection = Voice.joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});
// Open the stream as a voice resource
const resource = Voice.createAudioResource(stream.stream, { inputType: stream.type });
// Create a player to play the resource
const player = Voice.createAudioPlayer();
// Play the resource through the player (i hate discord v16)
player.on('Playing', () => {
console.log(`Playing ${yturl}`);
});
player.play(resource);
// Connect the player to the voice connection (internal screaming intensifies)
connection.subscribe(player);
// Setup Callbacks for when the player completes
player.on('Idle', () => {
console.log(`Finished ${yturl}`);
});
return;
}
// Generic message responses // Generic message responses
if (message.content.match(/https:\/\/ootbingo\.github\.io/)) { if (message.content.match(/https:\/\/ootbingo\.github\.io/)) {

View File

@ -1,6 +1,6 @@
{ {
"name": "zeskypebot", "name": "zeskypebot",
"version": "7.0.0_a", "version": "7.2.0",
"description": "Goldar Squad's Skype Bot, ported for Discord\"", "description": "Goldar Squad's Skype Bot, ported for Discord\"",
"main": "bot.js", "main": "bot.js",
"scripts": { "scripts": {