Skip to content

Commit

Permalink
rewrite with sqlite3 and sequelize
Browse files Browse the repository at this point in the history
  • Loading branch information
NoobJsPerson committed Jul 29, 2022
1 parent 62045f1 commit 534592e
Show file tree
Hide file tree
Showing 14 changed files with 457 additions and 374 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.env
node_modules
database.sqlite
package-lock.json
82 changes: 47 additions & 35 deletions commands/addgame.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,57 @@
const fs = require ('fs');
const fs = require('fs');
const fetch = require('node-fetch');
module.exports = {
name:'addgame',
aliases:['ag'],
usage:'<website-name|id>',
description:'adds the game you want to see its runs to the gamelist',
async execute(message,args){
if(message.guild&&!message.member.permissions.has("MANAGE_MESSAGES")) return message.reply('only staff can change game');
name: 'addgame',
aliases: ['ag'],
usage: '<website-name|id>',
description: 'adds the game you want to see its runs to the gamelist',
async execute(message, args, Guild, Game) {
if (message.guild && !message.member.permissions.has("MANAGE_MESSAGES")) return message.reply('only staff can change game');
const input = args.join('%20')
const errormsg = "please input a valid name, abbreviation or id";
const res = await fetch(`https://www.speedrun.com/api/v1/games/${input}`);
const res = await fetch(`https://www.speedrun.com/api/v1/games/${input}`);
let json = await res.json();
if(!json.data){

if (!json.data) {
const ares = await fetch(`https://www.speedrun.com/api/v1/games?name=${input}`);
let ajson = await ares.json();
if(!ajson.data) return message.reply(errormsg);
if(ajson.data[0]){
json.data = ajson.data.find(x => x.names.international == args.join(" "))
if(!json.data) return message.reply(errormsg);
} else {
json = ajson;
let ajson = await ares.json();
if (!ajson.data) return message.reply(errormsg);
if (ajson.data[0]) {
json.data = ajson.data.find(x => x.names.international == args.join(" "))
if (!json.data) return message.reply(errormsg);
} else {
json = ajson;

if(!json.data.id) return message.reply(errormsg);
if (!json.data.id) return message.reply(errormsg);
}
}
}
const content = await fs.promises.readFile('./storage.json');
const storageObject = JSON.parse(content);
let objtype = message.guild?message.guild.id:message.author.id;

const obj = { id : json.data.id, name : json.data.names.international, url:json.data.assets['cover-large'].uri};
const serverObject = storageObject[objtype];

if(serverObject && serverObject.find(x => x.id == json.data.id)) return message.reply('i can\'t add a game thats already in the list');

if (storageObject[objtype] instanceof Array) {
storageObject[objtype].push(obj);
} else {
storageObject[objtype] = [ obj ];
}
await fs.promises.writeFile('./storage.json', JSON.stringify(storageObject));
// const content = await fs.promises.readFile('./storage.json');
// const storageObject = JSON.parse(content);
const [game,] = await Game.findOrCreate({
where: {
id: json.data.id
},
defaults: {
name: json.data.names.international,
url: json.data.assets['cover-large'].uri
}
}),
[guild,] = await Guild.findOrCreate({
where: {
id: message.guild ? message.guild.id : message.author.id
},
defaults: {
channel: message.guild && message.guild.channels.cache.find(x => x.name == "new-runs")?.id || null,
isUser: !message.guild
}
}),
isGameInGuild = await guild?.hasGame(game);
if (isGameInGuild) return message.reply('i can\'t add a game thats already in the list');
await guild.addGame(game);
// if (storageObject[objtype] instanceof Array) {
// storageObject[objtype].push(obj);
// } else {
// storageObject[objtype] = [obj];
// }
message.channel.send(`the game ${json.data.names.international} has been successfully added to the runlist`);
}
};
};
49 changes: 29 additions & 20 deletions commands/addgames.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@ module.exports = {
aliases: ['ags'],
usage: '<website-name|id>',
description: 'adds the games you want to see their runs to the gamelist',
async execute(message, args) {
async execute(message, args, Guild, Game) {
if (message.guild && !message.member.permissions.has("MANAGE_MESSAGES")) return message.reply('only staff can change game');
let argz = args.join(' ').split('|');
argz = argz.map(x => x.replace(' ', '%20'))
const content = await fs.promises.readFile('./storage.json');
const storageObject = JSON.parse(content);
const objtype = message.guild ? message.guild.id : message.author.id
argz = argz.map(x => x.replace(' ', '%20'));
const [guild,] = await Guild.findOrCreate({
where: {
id: message.guild ? message.guild.id : message.author.id
},
defaults: {
channel: message.guild && message.guild.channels.cache.find(x => x.name == "new-runs")?.id || null,
isUser: !message.guild
}
});
let games = [];
for (const x of argz) {
x = x.trim();
const errormsg = `are you sure https://www.speedrun.com/${x} exists
||if it didn't work try deleting unnecessary spaces||`;
const errormsg = `are you sure https://www.speedrun.com/${x} exists\n||if it didn't work try deleting unnecessary spaces||`;
const res = await fetch(`https://www.speedrun.com/api/v1/games/${x}`);
let json = await res.json();
if (!json.data) {
const ares = await fetch(`https://www.speedrun.com/api/v1/games?name=${x}`);
let ajson = await ares.json();
if (!ajson.data) {
message.reply(eerrormsg);
message.reply(errormsg);
continue;
}
if (ajson.data[0]) {
Expand All @@ -39,23 +45,26 @@ module.exports = {
}
}
}
const obj = { id: json.data.id, name: json.data.names.international, url: json.data.assets['cover-large'].uri };

if (storageObject[objtype] &&
storageObject[objtype].find(y => y.id == json.data.id)) {
const [game,] = await Game.findOrCreate({
where: {
id: json.data.id
},
defaults: {
name: json.data.names.international,
url: json.data.assets['cover-large'].uri
}
}),
isGameInGuild = await guild?.hasGame(game);
if (isGameInGuild) {
message.reply('i can\'t add a game thats already in the list');
continue;
}

if (storageObject[objtype] instanceof Array) {
storageObject[objtype].push(obj);
} else {
storageObject[objtype] = [obj];
}
}
games.push(game)
message.channel.send(`the game ${json.data.names.international} has been successfully added to the runlist`);

}
await fs.promises.writeFile('./storage.json', JSON.stringify(storageObject));
await guild.addGames(games);


}
};
70 changes: 37 additions & 33 deletions commands/deletegame.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
const fetch = require('node-fetch');
const fs = require ('fs');
const fs = require('fs');
module.exports = {
name:'deletegame',
aliases:['dg'],
usage:'<website-name|id>',
description:'delete the game you don\'t want to see its runs from the gamelist',
async execute(message,args){
if(message.guild&&!message.member.permissions.has("MANAGE_MESSAGES")) return message.reply('only staff can change game');
const input = args.join("%20")
const res = await fetch(`https://www.speedrun.com/api/v1/games/${input}`);
let json = await res.json();
if(!json.data){

name: 'deletegame',
aliases: ['dg'],
usage: '<website-name|id>',
description: 'delete the game you don\'t want to see its runs from the gamelist',
async execute(message, args, Guild, Game) {
if (message.guild && !message.member.permissions.has("MANAGE_MESSAGES")) return message.reply('only staff can change game');
const [guild,] = await Guild.findOrCreate({
where: {
id: message.guild ? message.guild.id : message.author.id
},
defaults: {
channel: message.guild && message.guild.channels.cache.find(x => x.name == "new-runs")?.id || null,
isUser: !message.guild
}
});
const input = args.join("%20")
const res = await fetch(`https://www.speedrun.com/api/v1/games/${input}`);
let json = await res.json();
if (!json.data) {
const ares = await fetch(`https://www.speedrun.com/api/v1/games?name=${input}`);
let ajson = await ares.json();
if(!ajson.data) return message.reply('please input a valid name, abbreviation or id');
if(ajson.data[0]){
json.data = ajson.data.find(x => x.names.international == args.join(" "))
if(!json.data) return message.reply('please input a valid name, abbreviation or id');
} else {
json = ajson;
let ajson = await ares.json();
if (!ajson.data) return message.reply('please input a valid name, abbreviation or id');
if (ajson.data[0]) {
json.data = ajson.data.find(x => x.names.international == args.join(" "))
if (!json.data) return message.reply('please input a valid name, abbreviation or id');
} else {
json = ajson;
}
}
}
const objtype = message.guild?message.guild.id:message.author.id;
const content = await fs.promises.readFile('./storage.json');
const storageObject = JSON.parse(content);
const list = storageObject[objtype]

if(!list) return message.reply('i can\'t delete a game from a list thats empty');
if(!list.find(x => x.id == json.data.id)) return message.reply('i can\'t delete a game thats not in the list')
storageObject[objtype] = list.filter(x => x.id != json.data.id)

await fs.promises.writeFile('./storage.json', JSON.stringify(storageObject));



const game = await Game.findOne({
where: {
id: json.data.id
}
});
if(!game) return message.reply('i can\'t delete a game that never got added');
const isGameInGuild = guild.hasGame(game);
if (!isGameInGuild) return message.reply('i can\'t delete a game thats not in the list');
guild.removeGame(game)
message.channel.send(`the game ${json.data.names.international} got successfully deleted`);
}
};
109 changes: 59 additions & 50 deletions commands/deletegames.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,66 @@
const fs = require ('fs');
const fs = require('fs');
const fetch = require('node-fetch');
module.exports = {
name:'deletegames',
aliases:['ags'],
usage:'<website-name|id>',
description:'deletes the games you don\'t want to see their runs to the gamelist',
async execute(message,args){
if(message.guild&&!message.member.permissions.has("MANAGE_MESSAGES")) return message.reply('only staff can change game');
let argz = args.join(' ').split('|');
argz = argz.map(x => x.replace(' ','%20'));
const content = await fs.promises.readFile('./storage.json');
const storageObject = JSON.parse(content);
const objtype = message.guild?message.guild.id:message.author.id
name: 'deletegames',
aliases: ['ags'],
usage: '<website-name|id>',
description: 'deletes the games you don\'t want to see their runs to the gamelist',
async execute(message, args, Guild, Game) {
if (message.guild && !message.member.permissions.has("MANAGE_MESSAGES")) return message.reply('only staff can change game');
let argz = args.join(' ').split('|'),
games = [];
argz = argz.map(x => x.replace(' ', '%20'));
const [guild,] = await Guild.findOrCreate({
where: {
id: message.guild ? message.guild.id : message.author.id
},
defaults: {
channel: message.guild && message.guild.channels.cache.find(x => x.name == "new-runs")?.id || null,
isUser: !message.guild
}
});

for(const x of argz){
x = x.trim();
const errormsg = `are you sure https://www.speedrun.com/${x} exists
||if it didn't work try deleting unnecessary spaces||`;
for (const x of argz) {
x = x.trim();
const errormsg = `are you sure https://www.speedrun.com/${x} exists\n||if it didn't work try deleting unnecessary spaces||`;
const res = await fetch(`https://www.speedrun.com/api/v1/games/${x}`);
let json = await res.json();
if(!json.data){

const ares = await fetch(`https://www.speedrun.com/api/v1/games?name=${x}`);
let ajson = await ares.json();
if(!ajson.data){
message.reply(errormsg)
continue;
}
if(ajson.data[0]){
json.data = ajson.data.find(y => y.names.international == x.replace('%20',' '));
if(!json.data){
message.reply(errormsg);
continue;
}
} else {
json = ajson;
}
}
const list = storageObject[objtype];
let json = await res.json();
if (!json.data) {

if(!list){
message.reply('i can\'t delete a game from a list thats empty');
continue;
}
if(!list.find(y => y.id == json.data.id)){
message.reply('i can\'t delete a game thats not in the list');
continue;
}
storageObject[objtype] = list.filter(y => y.id != json.data.id)

message.channel.send(`the game ${json.data.names.international} got successfully deleted`);
}
await fs.promises.writeFile('./storage.json', JSON.stringify(storageObject));
const ares = await fetch(`https://www.speedrun.com/api/v1/games?name=${x}`);
let ajson = await ares.json();
if (!ajson.data) {
message.reply(errormsg)
continue;
}
if (ajson.data[0]) {
json.data = ajson.data.find(y => y.names.international == x.replace('%20', ' '));
if (!json.data) {
message.reply(errormsg);
continue;
}
} else {
json = ajson;
}
}
const game = await Game.findOne({
where: {
id: json.data.id
}
});

}
if (!game) {
message.reply('i can\'t delete a game that never got added');
continue;
}
const isGameInGuild = guild.hasGame(game);
if (!isGameInGuild) {
message.reply('i can\'t delete a game thats not in the list');
continue;
}
games.push(game)
message.channel.send(`the game ${json.data.names.international} got successfully deleted`);
}
await guild.removeGames(games);
}
};
Loading

0 comments on commit 534592e

Please sign in to comment.