-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
117 lines (89 loc) · 3.38 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import discord
import os
import requests
import json
import random
import re
from decouple import config
# token = os.environ['DISCORD_AUTH_TOKEN']
token = config('DISCORD_AUTH_TOKEN')
def extract_author_name(message):
pattern = r"name='(.*?)'"
matches = re.findall(pattern, message)
if len(matches) >= 2:
return matches[1]
return "No author name found"
def generate_random_number():
return random.randint(1, 100)
def rps_bot_selection():
rps_options = ['rock', 'paper', 'scissors']
return random.choice(rps_options)
def get_quote():
response = requests.get('https://zenquotes.io/api/random/')
json_data = json.loads(response.text)
quote = json_data[0]['q'] + " - " + json_data[0]['a']
return quote
def get_dadjoke():
headers = {'Accept': 'application/json'}
response = requests.get('https://icanhazdadjoke.com/', headers=headers)
json_response = response.json()
return json_response['joke']
class DiscordClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
print(message)
parsed_data = str(message)
print('--------------')
username = extract_author_name(parsed_data)
# don't respond to ourselves
if message.author == self.user:
return
if message.content == '!dadjoke':
await message.channel.send(get_dadjoke())
if message.content == '!hello':
await message.channel.send(f'Hello, {username}!')
if message.content == '!stella':
await message.channel.send(
'--- *** THIS CHANNEL IS NOW IN STELLA MODE *** ---')
if message.content == '!charlie':
await message.channel.send(
'--- *** NEVER FEAR - THE DUCKMAN IS HERE! *** ---')
if message.content == '!jack':
await message.channel.send(
"--- *** WHYYYYYYYYYYYYYYYYYYYYY?????? *** ---")
if message.content == '!quote':
quote = get_quote()
await message.channel.send(quote)
if message.content == '!bruh':
await message.channel.send('BRO!')
if message.content == '!hungry':
await message.channel.send(
f"{username} is {generate_random_number()}% hungry right now.")
if message.content == "!commands":
await message.channel.send(
"***BOT COMMANDS:*** enter '!' and any one of the following: charlie, jack, stella, hello, rock, paper, scissors, hungry, duck"
)
if message.content == '!duck':
await message.channel.send(
"\U0001F986 --- ***QUACK! QUACK!*** --- \U0001F986")
if message.content in ['!rock', '!paper', '!scissors']:
rps_response = rps_bot_selection()
if rps_response == message.content[1:]:
await message.channel.send(
f"**Rock Paper Scissors SHOOT!** MCB Chatbot shows {rps_response.upper()} - it's a tie!"
)
else:
win_map = {'rock': 'scissors', 'paper': 'rock', 'scissors': 'paper'}
if win_map[rps_response] == message.content[1:]:
await message.channel.send(
f"**Rock Paper Scissors SHOOT!** MCB Chatbot shows {rps_response.upper()}. Sorry, ***{username}*** but you lose!"
)
else:
await message.channel.send(
f"**Rock Paper Scissors SHOOT!** MCB Chatbot shows {rps_response.upper()}. ***{username}*** is the winner!"
)
intents = discord.Intents.default()
intents.message_content = True
client = DiscordClient(intents=intents)
client.run(token)