discord-tools/moviebot/discordbot.py

112 lines
2.9 KiB
Python

#!/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)