Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Austin Oaks committed Jan 7, 2022
0 parents commit b83d9ae
Show file tree
Hide file tree
Showing 8 changed files with 2,947 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/*
.vscode/*
*.code-workspace
9 changes: 9 additions & 0 deletions bot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/sh
while true
do
echo "STARTING...."
#./bot.py
cd ~/tiktok/nodebot
node index.js
sleep 5
done
55 changes: 55 additions & 0 deletions commands/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { SlashCommandBuilder, inlineCode } = require('@discordjs/builders');
const { database } = require('../config.json');
var mysql = require('mysql');



module.exports = {
data: new SlashCommandBuilder()
.setName('request')
.setDescription('Adds a request to the list')
.addStringOption(option =>
option.setName('name')
.setDescription('The name of the request')
.setRequired(true)),
async execute(interaction) {
var connection = mysql.createConnection(database);

var request = {
request_name: interaction.options.getString('name'),
discord_id: interaction.member.id,
filled: 0
}

var request_id = 0;
connection.connect();
var query = connection.query('INSERT INTO requests SET ?', request, function (error, results, fields) {
if (error) throw error;
request_id = results.insertId;
});
const name = inlineCode(request.request_name);
const message = await interaction.reply({content :'You have requested ' + name + '.', fetchReply: true});
connection.end();

/*
const undoEmoji = '↩️';
message.react(undoEmoji);
const filter = (reaction) => {
return reaction.emoji.name === '↩️';
};
message.awaitReactions({ filter, max: 1, time: 10000, errors: ['time']})
.then(collected => {
const reaction = collected.first();
console.log(reaction);
if (reaction.emoji.name === '↩️') {
connection.query('DELETE FROM requests WHERE id = ?', request_id, function (error, results, fields) {
if (error) throw error;
});
interaction.deleteReply();
}
})
.catch();
*/

},
};
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"clientId": "798061576988917801",
"guildId": "293866901464023041",
"token": "Nzk4MDYxNTc2OTg4OTE3ODAx.X_viww.BPzUyUFySU53gpunHxTXt1dASw4",
"database": {
"socketPath": "/run/mysqld/mysqld.sock",
"user" : "laravel",
"password" : "Md188RRlvSRcOZJyMOGVCMuw",
"database" : "sac"
}
}
18 changes: 18 additions & 0 deletions deploy-commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { clientId, guildId, token } = require('./config.json');

const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
}

const rest = new REST({ version: '9' }).setToken(token);

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error);
163 changes: 163 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const { token, database, guildId } = require('./config.json');
const { createAudioPlayer, getVoiceConnection, joinVoiceChannel, createAudioResource, VoiceConnectionStatus } = require('@discordjs/voice');
const { inlineCode } = require('@discordjs/builders');
var mysql = require('mysql');
const { connect } = require('http2');
const { scheduleJob } = require('node-schedule');
const youtubedl = require('youtube-dl-exec');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_MESSAGES] });

client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}

client.once('ready', () => {
console.log('Ready!');
});


const pool = mysql.createPool(database);
const player = createAudioPlayer();


var guild_config = [];
const query = pool.query('SELECT guild, announce_channel FROM guild_config', function (error, results, fields) {
results.forEach(function(v) {
guild_config.push({guild: v.guild, announce_channel: v.announce_channel});
});
});


client.on('messageCreate', async (message) => {
if(message.content == '!stop') {
const voiceConnection = getVoiceConnection(message.guild.id);
if(voiceConnection != null) {
voiceConnection.disconnect();
}

}
/*if(message.content.startsWith('https://vm.tiktok.com')) {
var link = youtubedl(message.content, {
output: '/home/oaks/tiktok/toks/%(id).%(ext)'
});
console.log(link);
}*/

const query = pool.query('SELECT command_name FROM sounds', function (error, results, fields) {
results.forEach(function (res) {
if ('!' + res.command_name == message.content) {
const bigQuery = pool.query('SELECT * FROM sounds WHERE command_name = ?', res.command_name, function (error, results, fields) {
if (results.length != 0){
const voiceConnection = joinVoiceChannel({
channelId: message.member.voice.channelId,
guildId: message.guildId,
adapterCreator: message.guild.voiceAdapterCreator,
});
voiceConnection.subscribe(player);
voiceConnection.on(VoiceConnectionStatus.Disconnected, async (oldState, newState) => {
try {
await Promise.race([
entersState(voiceConnection, VoiceConnectionStatus.Signalling, 5_000),
entersState(voiceConnection, VoiceConnectionStatus.Connecting, 5_000),
]);
// Seems to be reconnecting to a new channel - ignore disconnect
} catch (error) {
// Seems to be a real disconnect which SHOULDN'T be recovered from
voiceConnection.destroy();
}
});
const resource = createAudioResource('/srv/http/storage/app/public/' + results[0].location);
player.play(resource);
}
});
}
});
});
});



client.on('voiceStateUpdate', async (oldState, newState) => {
if (oldState.channelId == null && newState.channelId != null && newState.member.id != client.user.id) {
// Someone joined a channel
// Do we have a sound for them?
var connection = mysql.createConnection(database);
var query = connection.query('SELECT * FROM join_sounds WHERE discord_id = ? AND checked = 1', newState.id, function (error, results, fields) {
if (results.length != 0){
const randNum = Math.floor(Math.random() * results.length);
const result = results[randNum];
const voiceConnection = joinVoiceChannel({
channelId: newState.channelId,
guildId: newState.guild.id,
adapterCreator: newState.guild.voiceAdapterCreator,
});
voiceConnection.subscribe(player);
voiceConnection.on(VoiceConnectionStatus.Disconnected, async (oldState, newState) => {
try {
await Promise.race([
entersState(voiceConnection, VoiceConnectionStatus.Signalling, 5_000),
entersState(voiceConnection, VoiceConnectionStatus.Connecting, 5_000),
]);
// Seems to be reconnecting to a new channel - ignore disconnect
} catch (error) {
// Seems to be a real disconnect which SHOULDN'T be recovered from
try{
voiceConnection.destroy();
} catch(err) {

}
}
});
const resource = createAudioResource('/srv/http/storage/app/public/' + result.location);
player.play(resource);
}
});
}
});

client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;

const command = client.commands.get(interaction.commandName);

if (!command) return;

try {
await command.execute(interaction);
} catch (error) {
console.error(error);
return interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});

scheduleJob('*/5 * * * *', function() {
const query = pool.query('SELECT id, request_name, filled, announced FROM requests', function (error, results, fields) {
const channel = client.channels.cache.get(guild_config[0].announce_channel);
results.forEach(function(v) {
if(v.filled == 1 && v.announced == 0) {
// announce
channel.send(inlineCode(v.request_name) + ' is on the server.');
const insertQuery = pool.query('UPDATE requests SET announced = 1 WHERE id = ?', v.id);
}
});
});
//guild_config.forEach(function(v) {
//const channel = client.channels.cache.get(v.announce_channel); // god i'm an idiot this doesn't actually work need a guild column in requests some day
//channel.send('content');
//});
});

process.on('beforeExit', () => {
pool.end(function (err) {
console.log(err);
});
});

client.login(token);
Loading

0 comments on commit b83d9ae

Please sign in to comment.