-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
36 lines (29 loc) · 1.64 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
#MeFunny Bot
import discord
import aiohttp #For making HTTP requests
intents = discord.Intents.all() #predefined list of all available intents
client = discord.Client(intents=intents)
@client.event #Event called the the bot is ready to interact with users
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event #Event that extracts infromation about the user and channel
async def on_message(message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
channel = str(message.channel)
print(f'{username}: {user_message} ({channel})') #basic information printed in the command line
print('------')
if message.author == client.user:
return #Do nothing if message was sent by the bot itself
if user_message.lower() == '!givejoke':
async with aiohttp.ClientSession() as session:
async with session.get('https://icanhazdadjoke.com/', headers={'Accept': 'application/json'}) as resp: #HTTP GET request to 'icanhazdadjoke' API for a random joke,
data = await resp.json() #
joke = data['joke'] #Stores the joke itself from the json file
jokeid = data["id"] #Stores the specific joke ID from the json file
print(f'joke id: {jokeid}')
bot_joke = await message.channel.send(f"{message.author.mention} {joke}")
await bot_joke.add_reaction("😂") #Creates reactions which the users can use to vote wheather the joke was funny or not
await bot_joke.add_reaction("🤢")
TOKEN = 'YOUR_TOKEN'
client.run(TOKEN)