WRITING DISCORD BOTS

or:

 how to write code that is technically fanfiction

fortunately cyrus has a cool friend and nobody got stabbed

(spoilers: the cool friend is me)

what's the wayback machine?

  • at the heart of the Internet Archive - "a non-profit library of millions of free books, movies, software, music, websites, and more"
  • Wayback Machine archives static snapshots of websites at a point in time
  • if you care about your website surviving bit rot, make sure it doesn't completely fall apart viewed statically

what's the wayback machine?

key insight:

for a given url you can just make the url

http://web.archive.org/web/<TS>/<URL>

where TS is a timestamp YYYYMMDDTTTTTT

and URL is the URL you want to access

what's discord?

  • discord is where all the cool kids hang out
  • initially i had a significantly more legitimate first bullet point
  • Like Slack But For Vidyagames
  • voice and text channels
  • server-centric model + free = huge userbase

how to build a discord bot

http://discordapp.com/developers/applications/me

New Application > name it > Save Changes

Bot > Add Bot > get auth token

Bot > Bot Permissions > figure out the permissions integer for the perms you'll need

menu: General Information > copy Client ID

https://discordapp.com/oauth2/authorize?&client_id=CLIENTID&scope=bot&permissions=N

invite it to your server!

how to build a discord bot

make a basic node project with an auth.json file containing "token": your seekrit bot token

 

install discord.io: https://github.com/izy521/discord.io

var Discord = require('discord.io');
var log = require('winston');
var auth = require('./auth.json');

var bot = new Discord.Client({
  token: auth.token,
  autorun: true
});

bot.on('ready', function(e) {
  log.info('Connected');
  log.info('Logged in as: ');
  log.info(bot.username + '- (' + bot.id + ')');
});

bot.on('message', function(u, uid, cid, m, e) {
  if (m.substring(0, 7) == "Hi Bot,") {
  	var args = m.substring(8).split(' ');
    var cmd = args[0];
    switch(cmd) {
      case 'ping':
        bot.sendMessage({to: cid, message: "pong!"});
      break;
    }
  }
}

how to build a discord bot that does the thing

  • bot detects URLs in incoming messages
  • when it detects a URL it generates a link to that URL's archived state at the current timestamp
  • also, fetches the URL that causes the archive to -save- the state of that URL in the internet archive, causing a backup to be created of anything linked at the time of its link
  • ignores people with a specific role
  • unless they specifically preface a message with links with "Archivist,"
if ((m.includes('http://') || m.includes('https://')) && !(hiddenFromTheEye(m, e))) {
  statement(m, cid);
}

function statement(m, cid) {
  var now = new Date();
  var matches = m.match(/(https?:[^\s]+)/g);
  var baselink = "<http://web.archive.org/web/" + 
  	now.toISOString().replace(/[-T:\.Z]/gi,'').substr(0,8) + "000000/"
  matches.forEach(function(url) {
    fetch("http://web.archive.org/save/" + url)
    .catch(err => log.warn('error saving: ' + err))
    .then(checkStatus)
    .then(archivist.sendMessage({to: cid, message: baselink + url + ">"}));
  });
}

function hiddenFromTheEye(m, e) {
  if (m.includes('web.archive.org')) {
    return true;
  }

  if (e["d"]["member"]["roles"].includes("621559754699046915")) {
    return true;
  }
}

how to build a discord bot that does the thing

while being as ridiculous as possible

  • you may have noticed that i am a huge nerd
  • we're fans of The Magnus Archives
  • so, i turned my bot into Magnus Archives fanfic
  • friendly reminder that fanfic won a Hugo Award
  • eventually i'll have the code for it up on github...?
  • i basically went from "no idea how to touch a discord bot" to "everything aforementioned" in a couple of hours, though, which was cool.
  • it's good to build things for your friends
  • especially when it means nobody gets stabbed
  • AND you can yell at devtricks about a cool podcast

WRITING DISCORD BOTS

By Jon Cantwell

WRITING DISCORD BOTS

  • 688