From f5e14f3e2c7a7a9f4ea4eda8118d122638077f29 Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Fri, 2 Jun 2017 00:06:23 +0000 Subject: [PATCH] initial --- .gitignore | 3 + LICENCE | 8 +++ README.md | 15 +++++ main.js | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 26 ++++++++ 5 files changed, 223 insertions(+) create mode 100644 .gitignore create mode 100755 LICENCE create mode 100755 README.md create mode 100755 main.js create mode 100755 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96e5c4a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +db.json +config.json +node_modules diff --git a/LICENCE b/LICENCE new file mode 100755 index 0000000..a45bc49 --- /dev/null +++ b/LICENCE @@ -0,0 +1,8 @@ +The MIT License (MIT) +Copyright © 2017 Tadeo Kondrak + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100755 index 0000000..04d1690 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# ArchBot + +Bot for the /r/archlinux Discord. Right now, it has 3 commands: + +- `!info @user`: gives you the set info on a user +- `!chinfo `: changes your listing +- `!rminfo `: removes your listing + +## Installation + +Move `db_sample.json` to `db.json` and `config_sample.json` to `config.json` +In config.json, add your Discord bot token. +```sh +$ npm install +$ node main.js``` diff --git a/main.js b/main.js new file mode 100755 index 0000000..919b66f --- /dev/null +++ b/main.js @@ -0,0 +1,171 @@ +const jsonfile = require('jsonfile') +const Discord = require('discord.js'); +const cowsay_mod = require("cowsay"); +const fortunes = require('fortunes'); +const figlet_mod = require('figlet'); +const client = new Discord.Client(); +const config = require('./config.json'); + +var file = 'db.json' +var db = jsonfile.readFileSync(file); + +function saveFile(file) { + jsonfile.writeFile(file, db, function (err) { + if(err) console.error(err); + }) +} +function help(msg) { + msg.channel.sendMessage("Commands:\n\n`!info @user`: gives you the set info on a user\n`!chinfo `: changes your listing\n`!rminfo`: removes your listing\n\nOther commands:\n\n`!interject`\n`!norichard`\n`!gentoo`\n`!paste`\n`!cowsay `\n`!fortune`\n`!step`\n`!figlet`"); +} +function info(msg) { + let userId + if(msg.content.substr(8,1) === '!') { + userId = msg.content.substr(9,18); + } + else if(msg.content.substr(6) === '') { + userId = msg.author.id; + } + else { + userId = msg.content.substr(8,18); + } + if(db.info[userId]) { + msg.channel.sendMessage('```'+db.info[userId]+'```'); + } + else { + msg.channel.sendMessage('No info found for <@' + userId + '>') + } +} + +function chinfo(msg) { + if(msg.content.substr(8) === '') { + delete db.info[msg.author.id] + msg.channel.sendMessage('Info removed.') + } + else { + db.info[msg.author.id] = msg.content.substr(8).replace(/[`]/g, ''); + msg.channel.sendMessage('Info changed to: ```'+db.info[msg.author.id]+'```'); + } + saveFile(file); +} + +function rminfo(msg) { + delete db.info[msg.author.id] + msg.channel.sendMessage('Info removed.') + saveFile(file); +} + +function slap(msg) { + if(msg.content.substr(6) === "") { + msg.channel.sendMessage('<@'+msg.author.id+'> slaps around a bit with a large trout'); + } + else if(!/everyone/g.test(msg.content) && !/here/g.test(msg.content)) { + msg.channel.sendMessage('<@'+msg.author.id+'> slaps '+ msg.content.substr(6) +' around a bit with a large trout'); + } + else { + msg.channel.sendMessage('!slap: Permission denied') + } +} + +function interject(msg) { + const pasta = "I'd just like to interject for a moment. What you’re referring to as Linux, is in fact, GNU/Linux, or as I’ve recently taken to calling it, GNU plus Linux. Linux is not an operating system unto itself, but rather another free component of a fully functioning GNU system made useful by the GNU corelibs, shell utilities and vital system components comprising a full OS as defined by POSIX. Many computer users run a modified version of the GNU system every day, without realizing it. Through a peculiar turn of events, the version of GNU which is widely used today is often called “Linux”, and many of its users are not aware that it is basically the GNU system, developed by the GNU Project. There really is a Linux, and these people are using it, but it is just a part of the system they use. Linux is the kernel: the program in the system that allocates the machine’s resources to the other programs that you run. The kernel is an essential part of an operating system, but useless by itself; it can only function in the context of a complete operating system. Linux is normally used in combination with the GNU operating system: the whole system is basically GNU with Linux added, or GNU/Linux. All the so-called “Linux” distributions are really distributions of GNU/Linux."; + let args = msg.content.substr(11).split('/'); + if (args[1]) msg.channel.sendMessage(pasta.replace(/GNU/g, args[0]).replace(/Linux/g, args[1])); + else msg.channel.sendMessage(pasta); +} + +function norichard(msg) { + msg.channel.sendMessage("No, Richard, it's 'Linux', not 'GNU/Linux'. The most important contributions that the FSF made to Linux were the creation of the GPL and the GCC compiler. Those are fine and inspired products. GCC is a monumental achievement and has earned you, RMS, and the Free Software Foundation countless kudos and much appreciation.\nFollowing are some reasons for you to mull over, including some already answered in your FAQ.\nOne guy, Linus Torvalds, used GCC to make his operating system (yes, Linux is an OS -- more on this later). He named it 'Linux' with a little help from his friends. Why doesn't he call it GNU/Linux? Because he wrote it, with more help from his friends, not you. You named your stuff, I named my stuff -- including the software I wrote using GCC -- and Linus named his stuff. The proper name is Linux because Linus Torvalds says so. Linus has spoken. Accept his authority. To do otherwise is to become a nag. You don't want to be known as a nag, do you?\n(An operating system) != (a distribution). Linux is an operating system. By my definition, an operating system is that software which provides and limits access to hardware resources on a computer. That definition applies whereever you see Linux in use. However, Linux is usually distributed with a collection of utilities and applications to make it easily configurable as a desktop system, a server, a development box, or a graphics workstation, or whatever the user needs. In such a configuration, we have a Linux (based) distribution. Therein lies your strongest argument for the unwieldy title 'GNU/Linux' (when said bundled software is largely from the FSF). Go bug the distribution makers on that one. Take your beef to Red Hat, Mandrake, and Slackware. At least there you have an argument. Linux alone is an operating system that can be used in various applications without any GNU software whatsoever. Embedded applications come to mind as an obvious example."); + msg.channel.sendMessage("Next, even if we limit the GNU/Linux title to the GNU-based Linux distributions, we run into another obvious problem. XFree86 may well be more important to a particular Linux installation than the sum of all the GNU contributions. More properly, shouldn't the distribution be called XFree86/Linux? Or, at a minimum, XFree86/GNU/Linux? Of course, it would be rather arbitrary to draw the line there when many other fine contributions go unlisted. Yes, I know you've heard this one before. Get used to it. You'll keep hearing it until you can cleanly counter it.\nYou seem to like the lines-of-code metric. There are many lines of GNU code in a typical Linux distribution. You seem to suggest that (more LOC) == (more important). However, I submit to you that raw LOC numbers do not directly correlate with importance. I would suggest that clock cycles spent on code is a better metric. For example, if my system spends 90% of its time executing XFree86 code, XFree86 is probably the single most important collection of code on my system. Even if I loaded ten times as many lines of useless bloatware on my system and I never excuted that bloatware, it certainly isn't more important code than XFree86. Obviously, this metric isn't perfect either, but LOC really, really sucks. Please refrain from using it ever again in supporting any argument.\nLast, I'd like to point out that we Linux and GNU users shouldn't be fighting among ourselves over naming other people's software. But what the heck, I'm in a bad mood now. I think I'm feeling sufficiently obnoxious to make the point that GCC is so very famous and, yes, so very useful only because Linux was developed. In a show of proper respect and gratitude, shouldn't you and everyone refer to GCC as 'the Linux compiler'? Or at least, 'Linux GCC'? Seriously, where would your masterpiece be without Linux? Languishing with the HURD?"); + msg.channel.sendMessage("Be grateful for your abilities and your incredible success and your considerable fame. Continue to use that success and fame for good, not evil. Also, be especially grateful for Linux' huge contribution to that success. You, RMS, the Free Software Foundation, and GNU software have reached their current high profiles largely on the back of Linux. You have changed the world. Now, go forth and don't be a nag.\nThanks for listening."); +} + +function gentoo(msg) { + msg.channel.sendMessage("http://i.imgur.com/RLF8XyU.gif"); + msg.delete(); +} +function paste(msg) { + msg.channel.sendMessage("Please paste your log/message here: https://paste.fedoraproject.org/") +} +function cowsay(msg) { + msg.channel.sendMessage("```\n"+ cowsay_mod.say({text: msg.content.substr(8)}) +"\n```"); +} +function fortune(msg) { + fortunes.search(function(results){ + msg.channel.sendMessage('```'+results[1]+'```'); + }); +} +function step(msg) { + if(msg.content.substr(6) === "") { + msg.channel.sendMessage('<@'+msg.author.id+'> steps'); + } + else if(msg.content.substr(6,2) === "on" && !/everyone/g.test(msg.content) && !/here/g.test(msg.content)) { + msg.channel.sendMessage('<@'+msg.author.id+'> steps on '+ msg.content.substr(9)); + } + else if(!/everyone/g.test(msg.content) && !/here/g.test(msg.content)) { + msg.channel.sendMessage('<@'+msg.author.id+'> steps on '+ msg.content.substr(6)); + } + else { + msg.channel.sendMessage('!step: Permission denied') + } +} +function figlet(msg) { + msg.channel.sendMessage("```\n"+ figlet_mod.textSync(msg.content.substr(8)) +"\n```"); +} +function sudo(msg) { + if(!msg.member.roles.exists('name', 'sudoers')) { + msg.channel.sendMessage('<@'+msg.author.id+'> is not in the sudoers file. This incident will be reported.') + return; + } + if(msg.content.substr(8, 4) === 'info') { + let userId + let message + if(msg.content.substr(8,1) === '!') { + userId = msg.content.substr(16,18); + message = msg.content.substr(36) + } + else { + userId = msg.content.substr(15,18); + message = msg.content.substr(35) + } + if(msg.content.substr(6, 6) === 'chinfo') { + db.info[userId] = message; + msg.channel.sendMessage('Info changed to: ```'+db.info[userId]+'```'); + } + else if(msg.content.substr(6, 6) === 'rminfo') { + delete db.info[userId]; + msg.channel.sendMessage('Info removed.'); + } + saveFile(file); + } + else if(msg.content.substr(6,4) === 'slap') { + msg.channel.sendMessage('<@'+msg.author.id+'> slaps '+ msg.content.substr(11) +' around a bit with a large trout'); + } +} + +client.on('ready', () => { + console.log(`Logged in as ${client.user.username}!`); +}); + +client.on('message', msg => { + if(msg.author === client.user) return; + if(msg.content.substr(0, 5) === '!help') help(msg); + else if(msg.content.substr(0, 5) === '!info') info(msg); + else if(msg.content.substr(0, 7) === '!chinfo') chinfo(msg); + else if(msg.content.substr(0, 7) === '!rminfo') rminfo(msg); + else if(msg.content.substr(0, 5) === '!slap') slap(msg); + else if(msg.content.substr(0, 10) === '!interject') interject(msg); + else if(msg.content.substr(0, 10) === '!norichard') norichard(msg); + else if(msg.content.substr(0, 7) === '!gentoo') gentoo(msg); + else if(msg.content.substr(0, 6) === '!paste') paste(msg); + else if(msg.content.substr(0, 7) === '!cowsay') cowsay(msg); + else if(msg.content.substr(0, 8) === '!fortune') fortune(msg); + else if(msg.content.substr(0, 5) === '!step') step(msg); + else if(msg.content.substr(0, 7) === '!figlet') figlet(msg); + else if(msg.content.substr(0, 5) === '!sudo') sudo(msg); +}); + +client.on('guildMemberAdd', member => { + client.channels.get('31589004683195188').sendMessage('Joined: <@' + member.id + '>') +}); + +client.login(config.token).then(function() {client.user.setGame("I can't remove this")}); diff --git a/package.json b/package.json new file mode 100755 index 0000000..27ac2be --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "archbot", + "version": "1.0.0", + "description": "Bot for archlinux discord", + "main": "main.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/tadeokondrak/archbot.git" + }, + "author": "Tadeo Kondrak", + "license": "MIT", + "bugs": { + "url": "https://github.com/tadeokondrak/archbot/issues" + }, + "homepage": "https://github.com/tadeokondrak/archbot#readme", + "dependencies": { + "cowsay": "^1.1.9", + "discord.js": "^11.1.0", + "figlet": "^1.2.0", + "fortunes": "0.0.3", + "jsonfile": "^3.0.0" + } +}