Files
Gwendolyn/Gwendolyn.py
2020-07-28 11:18:47 +02:00

391 lines
18 KiB
Python

# -*- coding: utf-8 -*-
import discord
import asyncio
import pickle
import codecs
import string
import json
import random
import funcs
funcs.makeFiles()
funcs.shuffle()
# Gets secret bot token
with open("token.txt","r") as f:
token = f.read().replace("\n","")
client = discord.Client()
# Logs in
@client.event
async def on_ready():
funcs.logThis("Logged in as "+client.user.name+", "+str(client.user.id))
game = discord.Game("Some weeb shit")
await client.change_presence(activity=game)
# Reads messages and tests if they are Gwendolyn commands
@client.event
async def on_message(message):
# Sends the contents of "help.txt"
if message.content.lower().startswith("!help"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
if message.content.lower() == "!help" or message.content.lower() == "!help ":
with codecs.open("resources/help/help.txt",encoding="utf-8") as f:
text = f.read()
em = discord.Embed(title = "Help", description = text,colour = 0x59f442)
await message.channel.send(embed = em)
else:
command = message.content.lower().replace(" ","-").replace("!","")
funcs.logThis("Looking for "+command+".txt")
try:
with codecs.open("resources/help/"+command+".txt",encoding="utf-8") as f:
text = f.read()
em = discord.Embed(title = command.replace("help-","").capitalize(), description = text,colour = 0x59f442)
await message.channel.send(embed = em)
except:
await message.channel.send("Couldn't find help for that command")
# Stops the bot
elif message.content.lower().startswith("!stop"):
if message.author.name == "Nikolaj":
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
await message.channel.send("Logging out...")
with open("resources/games/games.json","r") as f:
data = json.load(f)
data["trivia questions"] = {}
data["blackjack games"] = {}
with open("resources/games/games.json","w") as f:
json.dump(data,f,indent=4)
await client.logout()
else:
funcs.logThis(message.author.name+" tried to run "+message.content)
await message.channel.send("I don't think I will, "+message.author.name)
# Does a hello with the helloFunc function from funcs/gwendolynFuncs.py
elif message.content.lower().startswith("!hello"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
await message.channel.send(funcs.helloFunc(message.author.name))
# Rolls dice with the roll_dice function from funcs/roll/dice.py
elif message.content.lower().startswith("!roll"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
if message.content.lower() == "!roll" or message.content.lower() == "!roll ":
await message.channel.send(funcs.roll_dice(message.author.name))
else:
await message.channel.send(funcs.roll_dice(message.author.name, message.content.lower().replace("!roll","")))
# Looks up a spell with the spellFunc function from funcs/lookup/lookupFuncs.py
elif message.content.lower().startswith("!spell "):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
await message.channel.send(funcs.spellFunc(message.content))
# Looks up a monster with the monsterFuncs() from funcs/lookup/lookupFuncs.py
elif message.content.lower().startswith("!monster "):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
title, text1, text2, text3, text4, text5 = funcs.monsterFunc(message.content)
em1 = discord.Embed(title = title, description = text1, colour=0xDEADBF)
# Sends the received information. Seperates into seperate messages if
# there is too much text
await message.channel.send(embed = em1)
if text2 != "":
if len(text2) < 2048:
em2 = discord.Embed(title = "Special Abilities", description = text2, colour=0xDEADBF)
await message.channel.send(embed = em2)
else:
em2 = discord.Embed(title = "Special Abilities", description = text2[:2048], colour=0xDEADBF)
await message.channel.send(embed = em2)
em2_2 = discord.Embed(title = "", description = text2[2048:], colour=0xDEADBF)
await message.channel.send(embed = em2_2)
if text3 != "":
if len(text3) < 2048:
em3 = discord.Embed(title = "Actions", description = text3, colour=0xDEADBF)
await message.channel.send(embed = em3)
else:
em3 = discord.Embed(title = "Actions", description = text3[:2048], colour=0xDEADBF)
await message.channel.send(embed = em3)
em3_2 = discord.Embed(title = "", description = text3[2048:], colour=0xDEADBF)
await message.channel.send(embed = em3_2)
if text4 != "":
if len(text4) < 2048:
em4 = discord.Embed(title = "Reactions", description = text4, colour=0xDEADBF)
await message.channel.send(embed = em4)
else:
em4 = discord.Embed(title = "Reactions", description = text4[:2048], colour=0xDEADBF)
await message.channel.send(embed = em4)
em4_2 = discord.Embed(title = "", description = text4[2048:], colour=0xDEADBF)
await message.channel.send(embed = em4_2)
if text5 != "":
if len(text5) < 2048:
em5 = discord.Embed(title = "Legendary Actions", description = text5, colour=0xDEADBF)
await message.channel.send(embed = em5)
else:
em5 = discord.Embed(title = "Legendary Actions", description = text5[:2048], colour=0xDEADBF)
await message.channel.send(embed = em5)
em5_2 = discord.Embed(title = "", description = text5[2048:], colour=0xDEADBF)
await message.channel.send(embed = em5_2)
# Sends an image of the Senkulpa map
elif message.content.lower().startswith("!map"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
await message.channel.send("https://i.imgur.com/diMXXJs.jpg")
# Finds a random image on the internet with the imageFuncs function from
# funcs/gwendolynFuncs.py
elif message.content.lower().startswith("!image"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
await message.channel.send(funcs.imageFunc())
# Sends information about a random movie with the movieFunc function from
# funcs/other/movie.py
elif message.content.lower().startswith("!movie"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
async with message.channel.typing():
title, plot, cover, cast = funcs.movieFunc()
if title == "error":
await message.channel.send("An error occurred. Try again")
else:
embed = discord.Embed(title=title, description=plot, color=0x24ec19)
embed.set_thumbnail(url=cover)
embed.add_field(name="Cast", value=cast,inline = True)
await message.channel.send(embed = embed)
# Generates a random name with the nameGen function from funcs/other/generators.py
elif message.content.lower().startswith("!name"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
await message.channel.send(funcs.nameGen())
# Generates a random tavern name with the tavernGen function from funcs/other/generators.py
elif message.content.lower().startswith("!tavern"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
await message.channel.send(funcs.tavernGen())
# Changes the "Playing this game" thing in Discord
elif message.content.lower().startswith("!game "):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
gamePlaying = funcs.cap(message.content.lower().replace("!game ",""))
game = discord.Game(gamePlaying)
await client.change_presence(activity=game)
# Rolls star wars dice with the parseRoll function from funcs/swfuncs/swroll.py
elif message.content.lower().startswith("!swroll"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
command = funcs.cap(message.content.lower().replace("!swroll",""))
newMessage = funcs.parseRoll(message.author.name,command)
messageList = newMessage.split("\n")
for messageItem in messageList:
await message.channel.send(messageItem)
# Deals with Destiny Points and stuff
elif message.content.lower().startswith("!swd"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
command = message.content.lower().replace("!swd","")
newMessage = funcs.parseDestiny(message.author.name,command)
messageList = newMessage.split("\n")
for messageItem in messageList:
await message.channel.send(messageItem)
# Rolls for critical injuries
elif message.content.lower().startswith("!swcrit"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
command = message.content.lower().replace("!swcrit","").replace(" ","").replace("+","")
if command == "":
command = 0
try:
newMessage = funcs.critRoll(int(command))
except:
newMessage = "Try using a number, stupid"
messageList = newMessage.split("\n")
for messageItem in messageList:
await message.channel.send(messageItem)
# Accesses and changes character sheet data with the parseChar function
# from funcs/swfuncs/swchar.py
elif message.content.lower().startswith("!swchar") or message.content.lower().startswith("!sw"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
command = string.capwords(message.content.lower().replace("!swchar","").replace("!sw","").replace("+","+ ").replace("-","- ").replace(",",", "))
title, desc = funcs.parseChar(message.author.name,command)
if title != "":
em1 = discord.Embed(title = title, description = desc, colour=0xDEADBF)
await message.channel.send(embed = em1)
else:
await message.channel.send(desc)
# Searches for a specific page on the Senkulpa Wiki
elif message.content.lower().startswith("!wiki "):
async with message.channel.typing():
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
command = string.capwords(message.content.lower().replace("!wiki ",""))
title, content, thumbnail = funcs.findWikiPage(command)
if title != "":
funcs.logThis("Sending the embedded message")
content += "\n[Læs mere](https://senkulpa.fandom.com/da/wiki/"+title.replace(" ","_")+")"
embed = discord.Embed(title = title, description = content, colour=0xDEADBF)
if thumbnail != "":
embed.set_thumbnail(url=thumbnail)
await message.channel.send(embed = embed)
else:
await message.channel.send(content)
# Runs a trivia game
elif message.content.lower().startswith("!trivia"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
if message.content.lower() == "!trivia" or message.content.lower() == "!trivia ":
question, answers, correctAnswer = funcs.triviaStart(str(message.channel))
if answers != "":
results = "**"+question+"**\n"
for answer in range(len(answers)):
results += chr(answer+97) + ") "+answers[answer]+"\n"
await message.channel.send(results)
await asyncio.sleep(60)
funcs.triviaCountPoints(str(message.channel))
with open("resources/games/games.json", "r") as f:
data = json.load(f)
del data["trivia questions"][str(message.channel)]
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
funcs.logThis("Time's up for the trivia question in "+str(message.channel))
await message.channel.send("Time's up! The answer was \""+chr(correctAnswer)+") "+answers[correctAnswer-97]+"\". Anyone who answered that has gotten 1 GwendoBuck")
else:
await message.channel.send(question)
elif message.content.lower().startswith("!trivia "):
command = message.content.lower().replace("!trivia ","")
response = funcs.triviaOtherThing(message.author.name.lower(),str(message.channel),command)
if response.startswith("Locked in "):
await message.add_reaction("👍")
else:
await message.channel.send(response)
else:
funcs.logThis("I didn't understand that")
await message.channel.send("I didn't understand that")
#Checks your GwendoBucks balance
elif message.content.lower().startswith("!balance"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
response = funcs.checkBalance(message.author.name.lower())
if response == 1:
new_message = message.author.name + " has " + str(response) + " GwendoBuck"
else:
new_message = message.author.name + " has " + str(response) + " GwendoBucks"
await message.channel.send(new_message)
#gives money to other player
elif message.content.lower().startswith("!give "):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
commands = message.content.lower().split(" ")
if len(commands) >= 3:
try:
amount = int(commands[2])
response = funcs.giveMoney(message.author.name.lower(),commands[1],amount)
await message.channel.send(response)
except:
funcs.logThis("I didn't quite understand that")
await message.channel.send("I didn't quite understand that")
else:
funcs.logThis("I didn't understand that")
await message.channel.send("I didn't understand that")
elif message.content.lower().startswith("!blackjack"):
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
if message.content.lower() == "!blackjack" or message.content.lower() == "!blackjack ":
new_message = funcs.blackjackStart(str(message.channel))
if new_message == "started":
cardsLeft = 0
with open("resources/games/blackjackCards.txt","r") as f:
for line in f:
cardsLeft += 1
if cardsLeft < 50:
funcs.shuffle()
await message.channel.send("Shuffling the deck...")
new_message = "Blackjack game started. Use \"!blackjack bet [amount]\" to enter the game within the next 30 seconds."
await message.channel.send(new_message)
old_image = await message.channel.send(file = discord.File("resources/games/tables/blackjackTable"+str(message.channel)+".png"))
await asyncio.sleep(30)
gamedone = False
with open("resources/games/games.json", "r") as f:
data = json.load(f)
if len(data["blackjack games"][str(message.channel)]["user hands"]) == 0:
gamedone = True
await message.channel.send("No one entered the game. Ending the game.")
while gamedone == False:
new_message, allStanding, gamedone = funcs.blackjackContinue(str(message.channel))
if new_message != "":
await message.channel.send(new_message)
if gamedone == False:
await old_image.delete()
old_image = await message.channel.send(file = discord.File("resources/games/tables/blackjackTable"+str(message.channel)+".png"))
if allStanding:
await asyncio.sleep(5)
else:
await asyncio.sleep(20)
new_message = funcs.blackjackFinish(str(message.channel))
await message.channel.send(new_message)
else:
await message.channel.send(new_message)
if message.content.lower().startswith("!blackjack bet"):
commands = message.content.lower().split(" ")
try:
amount = int(commands[2])
except:
funcs.logThis("I didn't understand that")
response = "I didn't understand that"
else:
response = funcs.blackjackPlayerDrawHand(str(message.channel),message.author.name,amount)
await message.channel.send(response)
if message.content.lower().startswith("!blackjack hit"):
response = funcs.blackjackHit(str(message.channel),message.author.name)
if response == "accept":
await message.add_reaction("👍")
else:
await message.channel.send(response)
if message.content.lower().startswith("!blackjack stand"):
response = funcs.blackjackStand(str(message.channel),message.author.name)
if response == "accept":
await message.add_reaction("👍")
else:
await message.channel.send(response)
# Is a bit sassy sometimes
meanWords = ["stupid", "bitch", "fuck", "dumb", "idiot"]
if ("gwendolyn" in message.content.lower() or message.content.startswith("!")) and any(x in message.content.lower() for x in meanWords) and "ikke" not in message.content.lower() and "not" not in message.content.lower():
funcs.logThis(message.author.name+" was a bit mean")
emoji = random.choice(["😠", "🖕", "👎"])
await message.add_reaction(emoji)
# Runs the whole shabang
client.run(token)