Browse Source

Scrub token from shrekbot code

Getty Ritter 1 year ago
commit
9fc43019f3
7 changed files with 136 additions and 0 deletions
  1. 6 0
      .gitignore
  2. 4 0
      Makefile
  3. BIN
      imgs/cringe-compilation.jpg
  4. BIN
      imgs/snadwich.jpg
  5. 3 0
      run.sh
  6. 14 0
      setup.sh
  7. 109 0
      shrekbot.py

+ 6 - 0
.gitignore

@@ -0,0 +1,6 @@
+/target
+**/*.rs.bk
+*~
+bin
+include
+lib

+ 4 - 0
Makefile

@@ -0,0 +1,4 @@
+.PHONY: deploy
+deploy:
+	git push deploy master:staging
+	ssh guildenstern "bash -c 'cd /srv/bots/shrekbot && git rebase staging && ./setup.sh'"

BIN
imgs/cringe-compilation.jpg


BIN
imgs/snadwich.jpg


+ 3 - 0
run.sh

@@ -0,0 +1,3 @@
+#!/bin/bash -eu
+
+exec ./bin/python3 ./shrekbot.py

+ 14 - 0
setup.sh

@@ -0,0 +1,14 @@
+#!/bin/bash -eu
+
+if ! command -v virtualenv3 >/dev/null; then
+    echo "cannot find virtualenv3" >&2
+    exit 1
+fi
+
+if [ ! -e ./bin/activate ]; then
+    echo "creating new virtualenv"
+    virtualenv3 .
+fi
+
+./bin/python3 -c 'import discord' >/dev/null 2>&1 ||
+    ./bin/pip install discord

+ 109 - 0
shrekbot.py

@@ -0,0 +1,109 @@
+#!/usr/bin/env python3
+
+import discord
+import random
+import re
+
+# TODO: don't keep this token in the code itself!
+TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
+NONLETTER = re.compile('[^A-Za-z0-9]')
+
+client = discord.Client()
+
+def occasionally() -> bool:
+    '''
+    returns True one in every twenty times
+    '''
+    return random.randint(0, 20) == 0
+
+class Msg:
+    '''
+    Just a nice wrapper class to wrap messages in, which makes it easier
+    to write high-level response functions
+    '''
+    def __init__(self, msg: str):
+        self.msg = msg
+        self.raw = NONLETTER.split(msg.content.lower())
+        self.words = set(self.raw)
+
+    def __contains__(self, substr: str):
+        '''
+        Check to see if the message contains this substring or sequence of
+        substrings (if it's a list)
+        '''
+        if type(substr) is str:
+            return substr in self.words
+        elif type(substr) is list:
+            find_len = len(substr)
+            for x in range(len(self.raw)):
+                if self.raw[x:x+find_len] == substr:
+                    return True
+        return False
+
+    async def respond_with_image(self, image_path: str, target=None):
+        '''
+        Respond to a message with an image present in our image path
+        '''
+        with open('./imgs/{}'.format(image_path), mode='rb') as f:
+            f = discord.File(f)
+            if target is None:
+                target = self.msg.author.mention
+            await self.msg.channel.send(target, files=[f])
+
+    async def respond(self, text):
+        '''
+        Respond to a message with a simple piece of text
+        '''
+        await self.msg.channel.send(text)
+
+@client.event
+async def on_message(message):
+    # we do not want the bot to reply to itself
+    if message.author == client.user:
+        return
+
+    # wrap the message in our Msg wrapper
+    msg = Msg(message)
+
+    # TODO: turn this into a data-driven table (and maybe put a web
+    # frontend here?) instead of just having a bunch of conditions
+    if 'oof' in msg and occasionally():
+        await msg.respond_with_image('cringe-compilation.jpg')
+
+    if 'sandwich' in msg and occasionally():
+        await msg.respond_with_image('snadwich.jpg')
+
+    if 'anime' in msg and occasionally():
+        response = random.choice([
+            '"anime" lmao',
+            'anime? you mean pervert cartoons?',
+        ]);
+        await msg.respond(response)
+
+    # song lyric section
+    if ['almost','heaven'] in msg:
+        await msg.respond('west virginia')
+
+    if ['blue','ridge','mountains'] in msg:
+        await msg.respond('shenandoah river')
+
+    if ['my','life','into','pieces'] in msg:
+        await msg.respond('THIS IS MY LAST RESORT')
+
+@client.event
+async def on_reaction_add(reaction, user):
+    if reaction.emoji == '🐱':
+        msg = reaction.message
+        modified_message = msg.content.lower().replace('l', 'w').replace('r', 'w')
+        face = random.choice(['OwO', 'UwU', '^w^', 'owo', ':3c', ':3cccc'])
+        await msg.channel.send('{}: {} {}'.format(msg.author.mention, modified_message, face))
+    else:
+        print(reaction.emoji)
+
+@client.event
+async def on_ready():
+    c_name, c_id = client.user.name, client.user.id
+    print('Logged in as {} ({})'.format(c_name, c_id))
+
+if __name__ == '__main__':
+    client.run(TOKEN)