|
| 1 | +import os |
| 2 | +import random |
| 3 | + |
| 4 | +from dotenv import load_dotenv |
| 5 | +import discord |
| 6 | +load_dotenv() |
| 7 | + |
| 8 | + |
| 9 | +client = discord.Client() |
| 10 | +submissions = set() |
| 11 | +entry_keys = [ |
| 12 | + 'pick me', |
| 13 | + 'select me', |
| 14 | +] |
| 15 | +selection_keys = [ |
| 16 | + 'pick winner', |
| 17 | + 'select winner', |
| 18 | +] |
| 19 | + |
| 20 | + |
| 21 | +def response(msg: str, author: str, admin: bool) -> str: |
| 22 | + """Determine appropriate action to take. |
| 23 | +
|
| 24 | + msg: text of incomming message from discord |
| 25 | + author: the name of the user sending the message |
| 26 | + admin: is the user sending the message an admin |
| 27 | + """ |
| 28 | + if not admin and any([key in msg.lower() for key in entry_keys]): |
| 29 | + if author in submissions: |
| 30 | + return f'No worries, {author}, we already got you 😉' |
| 31 | + else: |
| 32 | + submissions.add(author) |
| 33 | + return f'Good Luck, {author}!' |
| 34 | + |
| 35 | + if admin and any([key in msg.lower() for key in selection_keys]): |
| 36 | + if submissions: |
| 37 | + winner = random.choice(list(submissions)) |
| 38 | + submissions.remove(winner) |
| 39 | + return f'Congratulations {winner}! 🎉' |
| 40 | + else: |
| 41 | + return 'Looks like there is nobody to win' |
| 42 | + |
| 43 | + |
| 44 | +@client.event |
| 45 | +async def on_ready(): |
| 46 | + print(f'raffle-bot is online!') |
| 47 | + |
| 48 | + |
| 49 | +@client.event |
| 50 | +async def on_message(message): |
| 51 | + if message.author == client.user: |
| 52 | + return |
| 53 | + |
| 54 | + msg = response( |
| 55 | + msg=message.content, |
| 56 | + author=str(message.author).split("#")[0], |
| 57 | + admin=message.author.guild_permissions.administrator, |
| 58 | + ) |
| 59 | + print(msg) |
| 60 | + await message.channel.send(msg) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == '__main__': |
| 64 | + client.run(os.getenv('TOKEN')) |
0 commit comments