Skip to content

Commit

Permalink
Merge pull request #15 from Appelsiini1/dev
Browse files Browse the repository at this point in the history
V1.10.0
  • Loading branch information
Appelsiini1 authored Apr 8, 2021
2 parents 882bf3e + 9e8d00e commit de5e774
Show file tree
Hide file tree
Showing 15 changed files with 1,261 additions and 455 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,4 @@ modules/test_module.py
Coralog.txt

#Database
databases.db
Databases/
24 changes: 24 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
from logging import exception
from sys import exit


def get_tokens():
"""Gets environment variables. Returns a list."""
try:
with open(".env", "r") as f:
tokens = f.readlines()
except Exception:
exception("Could not acquire environment variables. Stopping.")
exit(1)
return tokens


PREFIX = "!c "
AUTHOR = "This bot is maintained by Appelsiini1"
GIT = "Source code for this bot can be found at https://github.com/Appelsiini1/CoraBot"
DB_F = os.path.dirname(os.path.realpath(__file__)) + "\\Databases\\database.db"
TOKENS = get_tokens()
DISCORD_TOKEN = TOKENS[0].lstrip("TOKEN").strip()[1:]
TWIT_API_KEY = TOKENS[1].lstrip("API_KEY").strip()[1:]
TWIT_API_SECRET = TOKENS[2].lstrip("API_SECRET").strip()[1:]
53 changes: 32 additions & 21 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
# CoraBot
# V1.10.0
# Copyright 2021 (c) Appelsiini1


import discord
import logging
import os
#import tweepy

# import scripts
# import tweepy

# scripts, functions & constants
from constants import *
from modules import common
from modules import quote
from modules import insult
from modules import commands
from modules import choose
#from modules import get_tweet
from modules import giveaway
from modules import pressF
from modules import vaccine
from modules import tirsk
from modules import poll
from modules import vote

PREFIX = "!c "

AUTHOR = "This bot is maintained by Appelsiini1"
GIT = "Source code for this bot can be found at https://github.com/Appelsiini1/CoraBot"
# TODO Commit viestin lähetystä ennen

logging.basicConfig(filename="Coralog.txt", level=logging.INFO, format='%(asctime)s %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
logging.basicConfig(
filename="Coralog.txt",
level=logging.INFO,
format="%(asctime)s %(levelname)s - %(message)s",
datefmt="%d/%m/%Y %H:%M:%S",
)
client = discord.Client()
tokens = common.get_tokens()
discordToken = tokens[0].lstrip("TOKEN").strip()[1:]
Twit_API_key = tokens[1].lstrip("API_KEY").strip()[1:]
Twit_API_secret = tokens[2].lstrip("API_SECRET").strip()[1:]

common.initializeDatabase()

#twitter_auth = tweepy.AppAuthHandler(Twit_API_key, Twit_API_secret)
# twitter_auth = tweepy.AppAuthHandler(Twit_API_key, Twit_API_secret)


@client.event
async def on_ready():
print('{0.user} is online & ready.'.format(client))
logging.info('{0.user} is online & ready.'.format(client))
print("{0.user} is online & ready.".format(client))
logging.info("{0.user} is online & ready.".format(client))


# main event, parses commands
@client.event
async def on_message(message):
if message.author == client.user:
return
elif message.content.find("sairasta") != -1 or message.content.find("ei oo normaalii") != -1:
elif (
message.content.find("sairasta") != -1
or message.content.find("ei oo normaalii") != -1
):
print("sairasta")
msg = "https://cdn.discordapp.com/attachments/693166291468681227/823282434203189258/eioonormaalii.gif"
await message.channel.send(msg)
Expand All @@ -53,7 +61,7 @@ async def on_message(message):
cmd = message.content.split(" ")[1]

if cmd == "hi" or cmd == "hello":
await message.channel.send('Hello!')
await message.channel.send("Hello!")
elif cmd == "help":
await commands.cmds(message)
elif cmd == "author":
Expand All @@ -69,7 +77,7 @@ async def on_message(message):
elif cmd.lower() == "f":
await pressF.pressF(message)
elif cmd == "tweet":
#await get_tweet.get_tweet(message, twitter_auth)
# await get_tweet.get_tweet(message, twitter_auth)
await message.channel.send("This feature is not yet implemented! Sorry!")
elif cmd == "giveaway":
await giveaway.initiate_giveaway(message)
Expand All @@ -81,8 +89,11 @@ async def on_message(message):
await tirsk.tirskCount(message)
elif cmd == "poll":
await poll.Poll(message)
elif cmd == "vote":
await vote.vote(message)

else:
await message.channel.send("What was that?")

client.run(discordToken)

client.run(DISCORD_TOKEN)
17 changes: 12 additions & 5 deletions modules/choose.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import random
import discord
from modules.common import get_hex_colour #pylint: disable=import-error
from modules.common import get_hex_colour # pylint: disable=import-error


async def choose(message):
repl = message.content[9:]
args = repl.strip().split("|")
valid = [x for x in args if x.strip() != '']
valid = [x for x in args if x.strip() != ""]

if len(valid) < 2:
msg = "**I need at least 2 options! Syntax: [option1] | [option2] ...**"
else:
random_n = random.randint(0, len(valid)-1)
random_n = random.randint(0, len(valid) - 1)
chosen = valid[random_n]
msg = "**"+ message.author.display_name + ", I choose '" + chosen.strip()+"'!**"

msg = (
"**"
+ message.author.display_name
+ ", I choose '"
+ chosen.strip()
+ "'!**"
)

emb = discord.Embed(title=msg, color=get_hex_colour(cora_eye=True))
await message.channel.send(embed=emb)
13 changes: 11 additions & 2 deletions modules/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import discord
from modules import common


async def cmds(message):
cmd_list = """hi (alias: hello)
help
Expand All @@ -12,9 +13,17 @@ async def cmds(message):
choose [option1 | option2 | ...]
vacc [Area code (or empty for all areas) | help]
poll [new|end|help]
vote help
Admin commands:
giveaway
endgiveaway [GiveawayID]"""
emb = discord.Embed(title="List of available commands:", description=cmd_list, colour=common.get_hex_colour(cora_blonde=True))
endgiveaway [GiveawayID]
poll [set|edit|del]role
poll new -r
"""
emb = discord.Embed(
title="List of available commands:",
description=cmd_list,
colour=common.get_hex_colour(cora_blonde=True),
)
await message.channel.send(embed=emb)
146 changes: 62 additions & 84 deletions modules/common.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
import random
import discord
import sys
import logging
from modules.emoji_list import _EMOJIS
import sqlite3
import os
from constants import DB_F

CURR_DIR = os.path.dirname(os.path.realpath(__file__))

def get_hex_colour(cora_blonde=False, cora_eye=False, error=False):
"""Returns a hex colour as a discord.Colour object
Args: cora_blonde = [True|False] Default: False
cora_eye = [True|False] Default: False
error = [True|False] Default: False"""
Args: cora_blonde = [True|False] Default: False
cora_eye = [True|False] Default: False
error = [True|False] Default: False"""
if cora_blonde == True:
color = discord.Colour(value=0xffcc99)
color = discord.Colour(value=0xFFCC99)
elif cora_eye == True:
color = discord.Colour(value=0x338b41)
color = discord.Colour(value=0x338B41)
elif error == True:
color = discord.Colour(value=0xFF0000)
else:
random_n = random.randint(0,16777215)
random_n = random.randint(0, 16777215)
color = discord.Colour(value=random_n)

return color

def get_tokens():
"""Gets environment variables. Returns a list."""
try:
with open(".env", "r") as f:
tokens = f.readlines()
except Exception:
logging.exception("Could not acquire environment variables. Stopping.")
sys.exit(1)
return tokens

def selectReactionEmoji(n, indexes=False):
selected = []
r_ns = []
i = 0
while(i<n):
r_n = random.randint(0, len(_EMOJIS)-1)
while i < n:
r_n = random.randint(0, len(_EMOJIS) - 1)
if _EMOJIS[r_n] not in selected:
selected.append(_EMOJIS[r_n])
r_ns.append(r_n)
Expand All @@ -52,77 +40,67 @@ def selectReactionEmoji(n, indexes=False):
else:
return selected


async def sendEmoji(message):
txt = ""
for emoji in _EMOJIS:
txt += emoji + " ; "
await message.channel.send(txt)


def initializeDatabase():
db_file = CURR_DIR + "\\databases.db"
conn = sqlite3.connect(db_file)
c = conn.cursor()
#BasicPolls Table
c.execute('''CREATE TABLE IF NOT EXISTS BasicPolls(
Poll_ID INT UNIQUE,
Ch_ID INT,
Guild_ID INT,
Author_ID INT,
Emojis TEXT,
PollName TEXT,
PRIMARY KEY (Poll_ID)
);''')
with sqlite3.connect(DB_F) as conn:
c = conn.cursor()
# BasicPolls Table
c.execute(
"""CREATE TABLE IF NOT EXISTS BasicPolls(
Poll_ID INT UNIQUE,
Ch_ID INT,
Guild_ID INT,
Author_ID INT,
Emojis TEXT,
PollName TEXT,
PRIMARY KEY (Poll_ID)
);"""
)

#RolePolls Table
c.execute('''CREATE TABLE IF NOT EXISTS RolePolls(
Poll_ID INT UNIQUE,
Ch_ID INT,
Guild_ID INT,
Author_ID INT,
Options TEXT,
PollName TEXT,
PRIMARY KEY (Poll_ID)
);''')
# RolePolls Table
c.execute(
"""CREATE TABLE IF NOT EXISTS RolePolls(
Poll_ID INT UNIQUE,
Ch_ID INT,
Guild_ID INT,
Author_ID INT,
Options TEXT,
PollName TEXT,
PRIMARY KEY (Poll_ID)
);"""
)

#MaxVotes
c.execute('''CREATE TABLE IF NOT EXISTS RolesMaxVotes(
Role_ID INT UNIQUE,
Guild_ID INT,
MaxVotes INT,
PRIMARY KEY (Role_ID)
);''')
# MaxVotes
c.execute(
"""CREATE TABLE IF NOT EXISTS RolesMaxVotes(
Role_ID INT UNIQUE,
Role_name TEXT,
Guild_ID INT,
MaxVotes INT,
PRIMARY KEY (Role_ID)
);"""
)

#Votes for RolePolls Table
c.execute('''CREATE TABLE IF NOT EXISTS RolePolls_Votes(
Vote_ID INT UNIQUE,
Poll_ID INT,
Voter_ID INT,
option1 INT,
option2 INT,
option3 INT,
option4 INT,
option5 INT,
option6 INT,
option7 INT,
option8 INT,
option9 INT,
option10 INT,
option11 INT,
option12 INT,
option13 INT,
option14 INT,
option15 INT,
option16 INT,
option17 INT,
option18 INT,
option19 INT,
option20 INT,
PRIMARY KEY (Vote_ID)
FOREIGN KEY (Poll_ID) REFERENCES RolePolls(Poll_ID)
ON DELETE CASCADE
);''')
# Votes for RolePolls Table
c.execute(
"""CREATE TABLE IF NOT EXISTS RolePolls_Votes(
Vote_ID INT UNIQUE,
Poll_ID INT,
Voter_ID INT,
Votes TXT,
PRIMARY KEY (Vote_ID)
FOREIGN KEY (Poll_ID) REFERENCES RolePolls(Poll_ID)
ON DELETE CASCADE
);"""
)

#other databases here
# other databases here

conn.commit()
conn.close()
conn.commit()
Loading

0 comments on commit de5e774

Please sign in to comment.