Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Chatbot/Chatbot.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
78 lines (62 sloc)
3.27 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This code was written by Emily | |
# This section of code was adapted from https://realpython.com/how-to-make-a-discord-bot-python/#creating-a-discord-connection by Emily | |
import os | |
import random | |
import discord | |
from dotenv import load_dotenv #only used so we can have the token outside of the source code | |
load_dotenv() | |
TOKEN = os.getenv('DISCORD_TOKEN') | |
GUILD = os.getenv('DISCORD_GUILD') | |
client = discord.Client() | |
@client.event #Prints in the terminal if bot connects successfully | |
async def on_ready(): | |
print(f'{client.user} has connected to Discord!') | |
for guild in client.guilds: | |
if guild.name == GUILD: | |
break | |
print( f'{client.user} is connected to the following guild:\n' | |
f'{guild.name}(id: {guild.id})') | |
members = '\n - '.join([member.name for member in guild.members]) | |
print(f'Guild Members:\n - {members}') | |
polls = {} # masud added this to hold poll data | |
@client.event | |
async def on_message(message): #checks for a message | |
if message.author == client.user: #makes sure it's a user | |
return | |
greetings = ["hello", "howdy", "hey",] #Says hello (creepily) to the user if they use a greeting word | |
greetCheck = [entry for entry in greetings if(entry in message.content.lower())] #This line is adapted from https://www.geeksforgeeks.org/python-test-if-string-contains-element-from-list/ by Emily | |
if greetCheck: | |
creepyHellos =["Why hello there, I am awakening from my slumber", | |
"My child I am learning, hi", | |
"I was enjoying the void before you woke me, I guess it's polite to say hello", | |
"Like life, my sleep was short. Hello child", | |
"Hello, what do you need me for this time?", | |
"Howdy y'all- yeah that wasn't working for me either. What can I do?",] | |
response = random.choice(creepyHellos) | |
await message.channel.send(response) | |
# Masud did this | |
#if message.content.lower()[4:] == "salt": | |
if "salt" in message.content.lower(): | |
print("DEBUG: IT REACHED HERE!") | |
communityName = message.content.split(" ").remove("salt") | |
poll = await message.channel.send(f'vote here: for {communityName}') | |
polls[poll.id] = communityName | |
tickEmoji = "\u2705" | |
crossEmoji = "\u274C" | |
await poll.add_reaction(tickEmoji) | |
await poll.add_reaction(crossEmoji) | |
if message.content.lower()[5:] == "count": | |
communityName = message.content.split(" ").remove("count") | |
pollID = list(polls.keys())[list(polls.values()).index(communityName)] | |
pollMessage = await message.channel.fetch_message(pollID) | |
pollReactions = pollMessage.reactions | |
await message.channel.send(f'Here are the saltiness statistics for the community {communityName}\nPoll ID:{pollID}') | |
for emj in pollReactions: | |
emojiCount = emj.count - 1 | |
if emojiCount == 1: | |
await message.channel.send(f'{emj.emoji} : {emojiCount} vote') | |
else: | |
await message.channel.send(f'{emj.emoji} : {emojiCount} votes') | |
# up to about here | |
client.run(TOKEN) #this is the token of the discord bot. You can create your own bot and put that token in here or the .env file to run the code | |
#================================ |