FoxSwitch.link - A Novelty Internet Switch Controlled by you, yes you!

in Programming & Dev4 years ago

Hi there,

Over the last couple of days, @rishi556 and I decided to take a break from some of our other responsibilities to make an idea we've been thinking about for a little while.

So, you may or may not know, but I am part of the @nftmart team. In our team, we have @foxon. Foxon occasionally switches his name to foxoff. This gave us the silly idea to make a website with a switch where you could control foxon's name between the 2 states (foxon and foxoff).

We managed to make a working prototype just setting and fetching the name through discord using Get and Post requests, using the code below:

router.options("/", async(req, res) => {
  let name = await discord.getName()
  res.json({name : name})
})

router.post("/", (req, res) => {
  if (req.body.on === true){
    discord.updateName("foxon")
  } else {
    discord.updateName("foxoff")
  }
  res.json({success : true})
})

This was all fine and dandy, but it caused a lot of issues, like updating the switch in an infinite loop. We tried many filters to make it work, but it wasn't the way to go for this project. It was also incredibly slow, and we were constantly getting rate limited by discord, which is probably one of the biggest contributors to the failure of this version.

We were literally just fetching Foxon's name from discord (we were changing his nickname using the switch in the NFTMart Discord Server since Nicknames are per-server, so we can't do it globally [well we could but our bot would need to be in all the servers foxon is in.])

Friendly Robot

This was very inefficient, and due to the rate limiting broke that solution. Late last night, a new solution hit me, it was the ever-elusive websocket.

image.png

This project probably doesn't deserve this sort of respect, but at least we open sourced it so someone can hopefully get something valuable out of the time we wasted.

I was in charge of websockets, as @rishi556 is still new to the idea, in fact we thought it could be a great way for him to see how it works, as some of my other projects use rather complex-looking websocket setups (they're not, it's just that it was my first time using websockets, which lead to a big mess).

So how are websockets going to solve the problem? They don't inherently, but by combining them with other solutions, we managed to make the site work nice and smooth.

So now, the websocket only updates a value on the server, instead of pissing off discord. Discord no longer like us 😂. Every 5 seconds, the current state is sent to discord from the server if it is different from the last one. This is no-where near as cool as the near-instant update of the previous version, but if we were to do it that way, this would be a borked mess.


setInterval(async () => {
    if (state !== lastState) {
        await discord.updateName(state ? "foxon" : "foxoff");
    }

    lastState = state;
}, 5000);

If you want to see the rather slow effect in action, join the NFTMart Discord and make sure the switch is toggled to the opposite state of fox(on/off)'s current name here:

https://foxswitch.link

If you went past this part without clicking on that link, are you even any fun at parties?

You'll notice it's rate limited to half a second, this is not a necessity, but it's no fun if everyone is messing with the switch at the same time. This is enforced on the backend so don't try any funny business!

Want to switch battle your friends, you weirdo?

It's stupidly fun (or maybe my brain is broken)... just add ?group=groupid (where group id is all lowercase alphabetical characters), anyone who joins in the same group will share the same switch, please note, these group switches DO NOT affect fox(on/off)'s discord name, I've already had 1 bug report about that from @rishi556 :/

For example, here's a valid group name:

https://foxswitch.link?group=bestgroupyay

Knock yourself out, and help yourself to the resources below if they tickle your fancy:

GitHub: https://github.com/Snaddyvitch-Dispenser/foxswitch.link

NFTMart Discord: https://discord.gg/SbHRBn9

Rishi556: @rishi556 (you don't want to go there)

Foxon Lightswitch: https://foxswitch.link

Foxon Lightswitch Groups: https://foxswitch.link?group=dave

P.S. Thanks foxon for being a good sport

image.png

Sort:  

This is stupid

We created you, be nice.

This is awesome!

Thanks fox(on/off)!

Very, very rude. I don't approve.
But it is funny.

But what does the Fox say?

@foxon plz reply.