countdown-bot

A Discord bot that runs countdown games and generates analytics
git clone https://git.ashermorgan.net/countdown-bot/
Log | Files | Refs | README

commit 81e2b50e59702f3f48dbf8fae20c6e55c601bfb1
Author: AsherMorgan <59518073+AsherMorgan@users.noreply.github.com>
Date:   Wed, 20 Jan 2021 17:30:19 -0800

Add files.

Diffstat:
A.gitignore | 3+++
AREADME.md | 26++++++++++++++++++++++++++
Abot.py | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Arequirements.txt | 2++
4 files changed, 104 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,3 @@ +.env +.venv +channels.txt diff --git a/README.md b/README.md @@ -0,0 +1,26 @@ +# countdown-bot +A Discord bot to facilitate countdowns. + +# Setup +1. Install the Python dependencies + ``` + pip install -r requirements.txt + ``` + +2. Go to the [Discord Developer Portal](https://discord.com/developers/) and create an application and a bot. + +3. Copy your bot's token into a `.env` file in the root directory. ex: + ``` + DISCORD_TOKEN="YOUR_DISCORD_TOKEN_HERE" + ``` + +4. Add your bot to the countdown channel and copy the ID of the channel into a `channels.txt` file in the root directory. ex: + ``` + <ID OF CHANNEL #1> + <ID OF CHANNEL #2> + ``` + +5. Run the bot + ``` + python bot.py + ``` diff --git a/bot.py b/bot.py @@ -0,0 +1,73 @@ +# Import dependencies +from discord.ext import commands +from dotenv import load_dotenv +import os +import re + + + +# Load list of channels +channels = [] +with open("channels.txt", "a+") as f: + f.seek(0) + lines = f.readlines() + for line in lines: + try: + channels += [int(line)] + except: + pass + + + +class Message: + def __init__(self, obj): + self.channel = obj.channel.id + self.number = int(re.findall("^[0-9,]+", obj.content)[0].replace(",","")) + self.author = f"{obj.author.name}#{obj.author.discriminator}" + + + +async def loadMessages(channel, depth=1000): + # Read messages + messages = await channel.history(limit=depth).flatten() + messages.reverse() + + # Parse messages + parsedMessages = [] + for message in messages: + try: + # Parse message + parsedMessage = Message(message) + + # Process message + if (len(parsedMessages) != 0 and parsedMessage.author == parsedMessages[-1].author): + await message.add_reaction("⛔") + elif (len(parsedMessages) != 0 and parsedMessage.number + 1 != parsedMessages[-1].number): + await message.add_reaction("❌") + else: + parsedMessages += [parsedMessage] + except: + pass + + + +# Create Discord bot +bot = commands.Bot(command_prefix = "!") + + + +@bot.event +async def on_ready(): + for channel in channels: + await loadMessages(bot.get_channel(channel)) + +@bot.event +async def on_message(obj): + if (obj.channel.id in channels and obj.author.name != "countdown-bot"): + await loadMessages(bot.get_channel(obj.channel.id), depth=15) + + + +# Run bot +load_dotenv() +bot.run(os.getenv("DISCORD_TOKEN")) diff --git a/requirements.txt b/requirements.txt @@ -0,0 +1,2 @@ +discord +python-dotenv