Fix update feeds

master
Cristiano Pires 2 years ago
parent 899af5007b
commit e6924a2fa4

@ -281,9 +281,11 @@ exports.addFeed = (bot)=>
var headers= req.headers; var headers= req.headers;
var args = [headers.gid, headers.dcchannelid, headers.ytchannelid] var args = [headers.gid, headers.dcchannelid, headers.ytchannelid]
const validationResult = await confirmArgs(args, bot); const validationResult = await confirmArgs(args, bot);
if(headers.isnew=="true") // console.log('Before new or old check', validationResult)
if (validationResult.error) return res.json({error:validationResult.error});
if(!headers.feedid)
{ {
if (validationResult.error) return res.json({error:validationResult.error}) //new
var feed = new feedsM(); var feed = new feedsM();
feed.gID = headers.gid; feed.gID = headers.gid;
feed.ChannelId = validationResult.channelId feed.ChannelId = validationResult.channelId
@ -299,7 +301,6 @@ exports.addFeed = (bot)=>
{ {
feedsM.findOne({id:args.id}).then(feed=> feedsM.findOne({id:args.id}).then(feed=>
{ {
console.log('here', feed)
if(!feed) return res.json({error:'DB Error'}); if(!feed) return res.json({error:'DB Error'});
feed.gID = headers.gid; feed.gID = headers.gid;
feed.ChannelId = validationResult.channelId feed.ChannelId = validationResult.channelId
@ -320,7 +321,7 @@ async function confirmArgs(args, bot)
var channelURL = args[2] var channelURL = args[2]
async function getChannelId(url) async function getChannelId(url)
{ {
if(url.split('@').length<1) return false if(url.split('@').length<1) return
return await fetch(url) return await fetch(url)
.then(handleResponse) .then(handleResponse)
.then(handleData) .then(handleData)
@ -331,7 +332,7 @@ async function confirmArgs(args, bot)
} }
function handleError(error) function handleError(error)
{ {
return error; return {error:'Bad youtube channel link.'};
} }
function handleData(data) function handleData(data)
{ {
@ -340,8 +341,9 @@ async function confirmArgs(args, bot)
} }
const YTChannelId = await getChannelId(channelURL); const YTChannelId = await getChannelId(channelURL);
const exists = await bot.channels.fetch(channelId).then(channel=>{return channel }).catch(()=>{return null}); if(YTChannelId.error) return {error:YTChannelId.error}
if(!exists) return {error:'Provided channel id does not exist.'} const discordChannel = await bot.channels.fetch(channelId).then(channel=>{return channel }).catch(()=>{return null});
if(!discordChannel) return {error:'Provided discord channel id does not exist.'}
var name = await fetch(`https://www.youtube.com/feeds/videos.xml?channel_id=${YTChannelId}`) var name = await fetch(`https://www.youtube.com/feeds/videos.xml?channel_id=${YTChannelId}`)
.then(response => .then(response =>
{ {
@ -359,8 +361,7 @@ async function confirmArgs(args, bot)
return data.feed.author.name._text return data.feed.author.name._text
}) })
.catch(error=>{return error;}); .catch(error=>{return error;});
if(!name) return {error:'Provided Youtube channel does not exist.'} return {YTChannelId, channelId, name, channelName:discordChannel.name}
return {YTChannelId, channelId, name, channelName:exists.name}
} }
exports.deleteFeed = (req,res) => exports.deleteFeed = (req,res) =>
{ {

@ -473,6 +473,10 @@ input
{ {
left:70%; left:70%;
} }
#change2
{
left:70%;
}
#delete #delete
{ {
left:30%; left:30%;

@ -72,7 +72,13 @@ function handleSettigns(event)
function handleClose(element) function handleClose(element)
{ {
if(element.parentNode.id == 'roleRuleWrapper' || element.parentNode.id == 'FeedWrapper') document.getElementById('popup').classList.remove('hidden'); if(element.parentNode.id == 'roleRuleWrapper' || element.parentNode.id == 'FeedWrapper')
{
document.getElementById('popup').classList.remove('hidden');
document.getElementById('change').innerText = 'Change';
document.getElementById('change2').innerText = 'Change';
}
if(element.parentNode.id =="FeedWrapper") document.getElementById('ChannelName').classList = '';
element.parentNode.classList.add('hidden'); element.parentNode.classList.add('hidden');
} }
async function requestChange() async function requestChange()
@ -349,6 +355,7 @@ function addRule(element)
document.getElementById('emojiID').placeholder = 'Emoji ID' document.getElementById('emojiID').placeholder = 'Emoji ID'
document.getElementById('messageID').placeholder = 'Message ID' document.getElementById('messageID').placeholder = 'Message ID'
document.getElementById('emojiImg').src = 'images/favicon.ico'; document.getElementById('emojiImg').src = 'images/favicon.ico';
document.getElementById('change').innerText = 'Add';
} }
function updateRule(element) function updateRule(element)
{ {
@ -437,6 +444,7 @@ function addFeed(element)
document.getElementById('DCchannelID').placeholder = ''; document.getElementById('DCchannelID').placeholder = '';
document.getElementById('YTchannelID').placeholder = ''; document.getElementById('YTchannelID').placeholder = '';
document.getElementById('CostumMessageI').placeholder = ''; document.getElementById('CostumMessageI').placeholder = '';
document.getElementById('change2').innerText = 'Add';
} }
async function updateFeed(element) async function updateFeed(element)
{ {
@ -449,7 +457,7 @@ async function updateFeed(element)
document.getElementById('ChannelName').classList =''; document.getElementById('ChannelName').classList ='';
document.getElementById('ChannelName').classList.add(element.id); document.getElementById('ChannelName').classList.add(element.id);
document.getElementById('DCchannelID').value = feed.ChannelId; document.getElementById('DCchannelID').value = feed.ChannelId;
document.getElementById('YTchannelID').value = feed.YTChannelId; document.getElementById('YTchannelID').value = 'https://www.youtube.com/@' + await getChannelName(feed.YTChannelId);
document.getElementById('CostumMessageI').value = feed.CostumMessage; document.getElementById('CostumMessageI').value = feed.CostumMessage;
} }
@ -477,13 +485,8 @@ function deleteFeed(element)
function _updateFeed(element) function _updateFeed(element)
{ {
var isNew=false; feedID = document.getElementById('ChannelName').classList.value;
if(element.parentNode.children[1].innerText == 'New Feed') isNew = true;
var feedID;
if(!isNew) feedID = document.getElementById('ChannelName').classList[1];
console.log('fid', feedID)
var gid = document.getElementsByClassName('guildname')[0].id; var gid = document.getElementsByClassName('guildname')[0].id;
console.log('gid', gid)
var DCchannelID = document.getElementById('DCchannelID').value; var DCchannelID = document.getElementById('DCchannelID').value;
var YTchannelID = document.getElementById('YTchannelID').value; var YTchannelID = document.getElementById('YTchannelID').value;
var CostumMessageI = document.getElementById('CostumMessageI').value; var CostumMessageI = document.getElementById('CostumMessageI').value;
@ -491,7 +494,7 @@ function _updateFeed(element)
{ {
method: "POST", method: "POST",
mode: "cors", mode: "cors",
headers: {DCchannelID, YTchannelID, CostumMessageI, gid,isNew, feedID}, headers: {DCchannelID, YTchannelID, CostumMessageI, gid, feedID},
}) })
.then(result => result.json()) .then(result => result.json())
.then(res => .then(res =>

@ -60,14 +60,14 @@
<i onclick="handleClose(this)" class="close material-symbols-outlined" id="close_bt2"> <i onclick="handleClose(this)" class="close material-symbols-outlined" id="close_bt2">
cancel cancel
</i> </i>
<p id='DCchannel' class="channel labeldc">Discord Channel:</p> <p id='DCchannel' class="channel labeldc">Discord Channel ID:</p>
<p id="YTchannel" class="channel labelyt">Youtube Channel:</p> <p id="YTchannel" class="channel labelyt">Youtube Channel:</p>
<p id="CostumMessage" class="message CostumMessage">Costum message:</p> <p id="CostumMessage" class="message CostumMessage">Costum message:</p>
<input id ="DCchannelID" class='channel dc'></input> <input id ="DCchannelID" class='channel dc'></input>
<input id ="YTchannelID" class='channel yt'></input> <input id ="YTchannelID" class='channel yt'></input>
<textarea id="CostumMessageI" class='message CostumMessageI' cols="40" rows="5"></textarea> <textarea id="CostumMessageI" class='message CostumMessageI' cols="40" rows="5"></textarea>
<button id="delete" class="bt" onclick='deleteFeed(this)'>Delete</button> <button id="delete" class="bt" onclick='deleteFeed(this)'>Delete</button>
<button id="change" class="bt" onclick='_updateFeed(this)'>Change</button> <button id="change2" class="bt" onclick='_updateFeed(this)'>Change</button>
</div> </div>
</body> </body>

Loading…
Cancel
Save