Skip to content

Commit

Permalink
refactor: rewrite discord.js to remove jQuery dependency (#155)
Browse files Browse the repository at this point in the history
Co-authored-by: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com>
  • Loading branch information
TheElixZammuto and ReenigneArcher authored Dec 16, 2023
1 parent 52517ce commit a268ce0
Showing 1 changed file with 18 additions and 47 deletions.
65 changes: 18 additions & 47 deletions dist/js/discord.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,27 @@
// this script requires jquery to be loaded on the source page, like so...
// <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
}

function randomQuote(quote, crate) {
let the_quote = null
if (quote['quote_safe']) {
the_quote = quote['quote_safe'];
} else {
the_quote = quote['quote'];
}

let the_quote = quote['quote_safe'] || quote['quote']
crate.notify(the_quote)
}

let quote = null

// get random video game quotes
$.ajax({
url: `https://app.lizardbyte.dev/uno/random-quotes/games.json`,
type: "GET",
dataType: "json",
success: function (result) {
let quote_index = getRandomIntInclusive(0, result.length - 1);
quote = result[quote_index]
}
});

// use Jquery to load other javascript
$.getScript('https://cdn.jsdelivr.net/npm/@widgetbot/crate@3', function()
{
const crate = new Crate({
//Widgetbot initialization
let widgetbot = document.createElement('script')
widgetbot.setAttribute('src', 'https://cdn.jsdelivr.net/npm/@widgetbot/crate@3')
widgetbot.async = true;
widgetbot.onload = () => {
new Crate({
server: '804382334370578482',
channel: '804383092822900797',
defer: false,
})
}
document.head.appendChild(widgetbot)

$.getScript('https://app.lizardbyte.dev/js/sleep.js')
// This won't work when running locally, so we will have a fallback sleep function for development testing
// https://stackoverflow.com/a/26851894/11214013
.done(function() {
// sleep for 7 minutes -> 420000
sleep(420000).then(() => {randomQuote(quote, crate)})
})
.fail(function() {
let sleep = ms => {
return new Promise(resolve => setTimeout(resolve, ms));
};
// sleep for 1 second
sleep(1000).then(() => {randomQuote(quote, crate)})
})
});
// get random video game quotes and notify the user on Widgetbot after 7 minutes
fetch('https://app.lizardbyte.dev/uno/random-quotes/games.json').then(r => r.json()).then(result => {
let quote = result[Math.floor(Math.random() * result.length)]
setTimeout(() => {
if (crate) {
randomQuote(quote, crate)
}
}, 7 * 60 * 1000)
})

0 comments on commit a268ce0

Please sign in to comment.