Skip to content

Commit 26c945d

Browse files
committedDec 24, 2020
initial raffle bot
1 parent ca19a6f commit 26c945d

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed
 

‎README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# PyRVA Bots
2+
3+
Bot | Service | Description
4+
---|---|---
5+
raffle-bot | Discord | Monitor text channels to help with raffles

‎raffle-bot/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Raffle Bot
2+
3+
When PyRVA has something to raffle off.
4+
This bot monitors all channels and tracks who wants to enter the raffle.
5+
6+
## Entering a Raffle
7+
8+
To enter the raffle, members have to enter `pick me` or `select me` in any text channel.
9+
The key text is case-insensitive and may have additional text (eg. `You should PICK ME!!!!`).
10+
Admins are not eligible to win, so their submissions are ignored.
11+
Submissions are stored in a set to prevent duplicate entries.
12+
13+
## Selecting a Winner
14+
15+
To select a winner, an admin can type `pick winner` or `select winner`.
16+
A winner is selected, their submission is removed from the list, and their victory is announced.
17+
18+
## Running the Bot
19+
20+
You must have access to the bot's authentication token.
21+
The bot runs locally on a machine and must be spun up for meetings.
22+
23+
Python 3.9 does not quite work due to the async, so this bot runs on Python 3.8.
24+
To run the bot, type
25+
26+
```
27+
python raffle_bot.py
28+
```

‎raffle-bot/raffle_bot.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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'))

‎raffle-bot/test_raffle_bot.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import raffle_bot
2+
3+
4+
def test_admin_says_pick_me():
5+
raffle_bot.submissions = set()
6+
7+
msg = raffle_bot.response(
8+
msg='pick me',
9+
author='admin',
10+
admin=True,
11+
)
12+
13+
assert raffle_bot.submissions == set()
14+
assert msg is None
15+
16+
17+
def test_user_says_pick_me():
18+
raffle_bot.submissions = set()
19+
20+
msg = raffle_bot.response(
21+
msg='pick me',
22+
author='user',
23+
admin=False,
24+
)
25+
26+
assert raffle_bot.submissions == {'user'}
27+
assert 'good luck' in msg.lower()
28+
29+
30+
def test_user_already_submitted():
31+
raffle_bot.submissions = {'user'}
32+
33+
msg = raffle_bot.response(
34+
msg='pick me',
35+
author='user',
36+
admin=False,
37+
)
38+
39+
assert raffle_bot.submissions == {'user'}
40+
assert 'no worries' in msg.lower()
41+
42+
43+
def test_user_picks_winner():
44+
raffle_bot.submissions = {'user'}
45+
46+
msg = raffle_bot.response(
47+
msg='pick winner',
48+
author='user',
49+
admin=False,
50+
)
51+
52+
assert raffle_bot.submissions == {'user'}
53+
assert msg is None
54+
55+
56+
def test_admin_picks_winner_with_entries():
57+
raffle_bot.submissions = {'user'}
58+
59+
msg = raffle_bot.response(
60+
msg='pick winner',
61+
author='admin',
62+
admin=True,
63+
)
64+
65+
assert raffle_bot.submissions == set()
66+
assert 'congratulations' in msg.lower()
67+
assert 'user' in msg
68+
69+
70+
def test_admin_picks_winner_without_entries():
71+
raffle_bot.submissions = set()
72+
73+
msg = raffle_bot.response(
74+
msg='pick winner',
75+
author='admin',
76+
admin=True,
77+
)
78+
79+
assert raffle_bot.submissions == set()
80+
assert 'nobody' in msg.lower()

‎requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
python-dotenv==0.15.0
2+
discord.py==1.5.1
3+
pytest==6.2.1

0 commit comments

Comments
 (0)
Please sign in to comment.