From 82fdec04ff02e66d4ad7b06c354b8b04297a5e23 Mon Sep 17 00:00:00 2001 From: Jan Speckamp Date: Mon, 4 Apr 2022 15:08:21 +0200 Subject: [PATCH] add moviebot --- moviebot/.env | 3 + moviebot/Dockerfile | 13 +++++ moviebot/discordbot.py | 112 ++++++++++++++++++++++++++++++++++++++ moviebot/requirements.txt | 3 + 4 files changed, 131 insertions(+) create mode 100644 moviebot/.env create mode 100644 moviebot/Dockerfile create mode 100644 moviebot/discordbot.py create mode 100644 moviebot/requirements.txt diff --git a/moviebot/.env b/moviebot/.env new file mode 100644 index 0000000..e8944fe --- /dev/null +++ b/moviebot/.env @@ -0,0 +1,3 @@ +TOKEN= +MOVIE_CHANNEL=movies +BOT_NAME=MovieBot diff --git a/moviebot/Dockerfile b/moviebot/Dockerfile new file mode 100644 index 0000000..af42542 --- /dev/null +++ b/moviebot/Dockerfile @@ -0,0 +1,13 @@ +# syntax=docker/dockerfile:1 + +FROM python:3.8-slim-buster + +RUN mkdir app +COPY requirements.txt /app +COPY discordbot.py /app + +WORKDIR /app + +RUN pip install -r requirements.txt + +CMD ["python", "discordbot.py"] diff --git a/moviebot/discordbot.py b/moviebot/discordbot.py new file mode 100644 index 0000000..49f3b47 --- /dev/null +++ b/moviebot/discordbot.py @@ -0,0 +1,112 @@ +#!/usr/bin/python3 + +from dotenv import dotenv_values +from discord.ext import commands +from tinydb import TinyDB +import random +import discord + +db = TinyDB('./db.json') +movies = db.table('movies') + +# TODO: validate .env +env = dotenv_values() + +activity = discord.Game(name="!help in #movies") +bot = commands.Bot(command_prefix='!', activity=activity, status=discord.Status.online, help_command=None) +bot.remove_command('help') + + +@bot.event +async def on_ready(): + print(f'{bot.user.name} has connected to Discord!') + + +@bot.command('add', help='Adds to the watchlist') +async def add(ctx): + if not validateCommand(ctx): + return + + movie_link = ctx.message.content[5:] + data = { + 'author': ctx.author.name, + 'link': movie_link + } + + movies.insert(data) + await ctx.send(f"Added <{movie_link}> to the list") + + +@bot.command('rm', help='Removes a movie by id') +async def remove(ctx): + if not validateCommand(ctx): + return + id = int(ctx.message.content[4:]) + response = movies.get(doc_id=id) + + if response is None: + await ctx.send(f"Could not find Movie with id {id} in the watchlist") + else: + movies.remove(doc_ids=[id]) + await ctx.send(f'removed <{response["link"]}> from the watchlist') + + +@bot.command('list', help='Shows all movies.', aliases=["l"]) +async def list(ctx): + if not validateCommand(ctx): + return + raw = movies.all() + formatted = "Current watchlist: Part I" + + parts = 2 + index = 0 + for mov in raw: + formatted += f'\n[{mov.doc_id}] [{mov["author"]}] <{mov["link"]}>' + index += 1 + if index == 20: + await ctx.send(formatted) + index = 0 + formatted = "Part: " + "I" * parts + parts += 1 + + await ctx.send(formatted) + +@bot.command('random', help='Selects a random movie', aliases=["rnd"]) +async def rand(ctx): + if not validateCommand(ctx): + return + all = movies.all() + mov = random.choice(all) + out = f"Choosing out of {len(all)} movies:\n" + out += "...\n" + out += "...\n" + out += "...\n" + out += f'I have chosen [{mov.doc_id}] <{mov["link"]}> proposed by [{mov["author"]}]\n' + await ctx.send(out) + +@bot.command('help') +async def help(ctx): + if not validateCommand(ctx): + return + out = "```" + out += "*" + env["BOT_NAME"] + " 3000*\n\n" + out += "add Adds to the watchlist\n" + out += "help Shows this message\n" + out += "list Shows all movies.\n" + out += "random Selects a random movie\n" + out += 'rm Removes a movie by id\n' + + out += "\n" + out += "If you have further questions @SpeckiJ#5233\n" + out += "```" + + await ctx.send(out) + +def validateCommand(ctx): + if ctx.author == bot.user: + return False + if ctx.channel.name != env["MOVIE_CHANNEL"]: + return False + return True + +bot.run(env["TOKEN"], bot=True) \ No newline at end of file diff --git a/moviebot/requirements.txt b/moviebot/requirements.txt new file mode 100644 index 0000000..3863ae2 --- /dev/null +++ b/moviebot/requirements.txt @@ -0,0 +1,3 @@ +python-dotenv +discord.py +tinydb \ No newline at end of file