Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
27e3ea9
added requirements.txt
abluey Jul 19, 2021
34edeee
Updated requirements; added pong for bot
abluey Jul 19, 2021
285b870
updated requirements; added discord token
abluey Jul 19, 2021
6ad5cb8
updated token calls
abluey Jul 19, 2021
a30ad40
added greeting command
abluey Jul 19, 2021
625a932
bot now processes commands
abluey Jul 19, 2021
72bcd14
Modified bot processing commands
abluey Jul 19, 2021
cf2dcfb
Modified greeting command again
abluey Jul 19, 2021
74872eb
Modified bot command again
abluey Jul 19, 2021
f3d8745
deleted client and changed everything to bot
abluey Jul 19, 2021
44c1332
Changed the hey command
abluey Jul 19, 2021
75c63c2
added thief easter egg
abluey Jul 19, 2021
322094a
updated hey command
abluey Jul 19, 2021
341205e
added sort command; fixed hey easter egg
abluey Jul 19, 2021
5dfaab7
fixed sort command
abluey Jul 19, 2021
5e95edf
fixed sort command!
abluey Jul 19, 2021
e3cb1fa
Put commands into cogs
abluey Jul 19, 2021
5fa6bc8
added "Easter egg" for testing purposes
abluey Jul 19, 2021
8ef6027
commented out ping pong
abluey Jul 19, 2021
5120d2b
uncommented ping pong
abluey Jul 19, 2021
5bc7a7a
updated cogs
abluey Jul 19, 2021
8525b55
cogs now work properly
abluey Jul 19, 2021
ac71565
cogs now work properly part 2
abluey Jul 19, 2021
e2a69c8
removed print statements
abluey Jul 19, 2021
a590d87
added greetings test
abluey Jul 20, 2021
a63a8a4
added tests for random.py and greetings.py
abluey Jul 20, 2021
aaacce4
fixed sort test
abluey Jul 20, 2021
d808310
added random number test
abluey Jul 20, 2021
cdf4761
fixed sort tests and added one more to it
abluey Jul 20, 2021
c1afb0d
Can look up tweets from twitter usernames!
abluey Jul 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"python.testing.pytestArgs": [
"Zoe"
],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true
}
20 changes: 20 additions & 0 deletions Zoe/cogs/greetings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import discord
from discord.ext import commands

class Greetings(commands.Cog):

def __init__(self, bot):
self.bot = bot

@commands.command()
async def hey(self, ctx, *, name=None):
if name:
if name.lower() == "esteban julio ricardo montoya de la rosa ramírez":
await ctx.send("Hey thief")
else:
await ctx.send("Hey " + name + " :)")
else:
await ctx.send("Hey " + ctx.message.author.name + " :)")

def setup(bot):
bot.add_cog(Greetings(bot))
185 changes: 185 additions & 0 deletions Zoe/cogs/inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import sqlite3
import discord
from discord.ext import commands
import datetime


# for reference :)
# import datetime
# datetime.datetime.now().strftime("%d-%m-%Y")
# outputs date in dd-mm-YYYY, a string


class InventoryCog(commands.Cog):

def __init__(self, bot):
self.bot = bot
self.con = sqlite3.connect('inventory.db')
self.cur = self.con.cursor()

# Create tables
self.cur.execute('''CREATE TABLE IF NOT EXISTS Items (
ItemID integer PRIMARY KEY AUTOINCREMENT,
ItemName varchar(255),
ItemDesc varchar(255),
Amount integer
);'''
)


self.cur.execute('''CREATE TABLE IF NOT EXISTS Checkout (
CheckoutID integer PRIMARY KEY AUTOINCREMENT,
User varchar(255),
TakenAmount integer,
TakenDate varchar(10),
ItemID integer,
CONSTRAINT fk_extensions
FOREIGN KEY (ItemID)
REFERENCES Items (ItemID)
);'''
)
self.con.commit()

# Inserting values into ITEMS
self.cur.executescript('''INSERT INTO Items ( ItemName, ItemDesc, Amount) VALUES
( "Headphones", "Pair of headphones", 10);
INSERT INTO Items (ItemName, ItemDesc, Amount) VALUES
("Mice", "Mice not sure what you expect", 5);
INSERT INTO Items (ItemName, ItemDesc, Amount) VALUES
("Keyboard", "Goes click click click", 6);
INSERT INTO Items (ItemName, ItemDesc, Amount) VALUES
("Computer", "Has input + output and sometimes runs", 10);
INSERT INTO Items (ItemName, ItemDesc, Amount) VALUES
("Mousemat", "Flat thing to put mouse on", 4);''')
self.con.commit()

# Inserting values into CHECKOUT
self.cur.executescript('''INSERT INTO Checkout ( User, TakenAmount, TakenDate, ItemID) VALUES
( "A", 2, "14-07-21", 1);
INSERT INTO Checkout (User, TakenAmount, TakenDate, ItemID) VALUES
("B", 8, "14-07-21", 1);
INSERT INTO Checkout (User, TakenAmount, TakenDate, ItemID) VALUES
("B", 2, "13-07-21", 2);
INSERT INTO Checkout (User, TakenAmount, TakenDate, ItemID) VALUES
("D", 1, "14-07-21", 2);
INSERT INTO Checkout (User, TakenAmount, TakenDate, ItemID) VALUES
("A", 3, "14-07-21", 3);
INSERT INTO Checkout (User, TakenAmount, TakenDate, ItemID) VALUES
("C", 6, "13-07-21", 4);
INSERT INTO Checkout (User, TakenAmount, TakenDate, ItemID) VALUES
("A", 1, "14-07-21", 4);
INSERT INTO Checkout (User, TakenAmount, TakenDate, ItemID) VALUES
("D", 4, "14-07-21", 5);
''')

self.con.commit()


#Checkout item (almost completed)
'''
Arg 1: item id
Arg 2: amount
'''
@commands.command()
async def checkout(self, cxt,*args):
if cxt.author == self.bot.user:
return
else:
itemId = int(args[0])
amount = int(args[1])

#Should check to see if the item exists
total = self.cur.execute('SELECT Amount FROM Items WHERE ItemID = (?)', (str(itemId)))

#Check to see if the item exists
if self.cur.fetchone() == None:
await cxt.send("The guild doesn't currently have that item")

elif (0 < amount):
await cxt.send("You're trying to checkout negative/no items?!")

elif(amount <= total):
checkedOut = self.cur.execute('''INSERT INTO Checkout (User, TakenAmount,ItemID)
(?,?,?)
''', (cxt.message.author.id, amount, itemId))
updateAmount = self.cur.execute('''UPDATE Items
SET Amount = (?)
WHERE ItemID = (?)''', (total - amount, itemId))

await cxt.send("User "+ cxt.author + " successfully checked out " + amount + " of item " + itemId)

else:
await cxt.send("The guild doesn't currently have that item in stock")


# SHOULD work???
#Return item
@commands.command()
async def return_item(self, cxt,*args):
if cxt.author == self.bot.user:
return
else:
itemId = int(args[0])
amount = int(args[1])

# Gets the CheckoutID and amount that was checked out
checkoutId = self.cur.execute('SELECT CheckoutID FROM Checkout WHERE User = (?) AND ItemID = (?)', (cxt.message.author.id, itemId))
takenAmount = self.cur.execute('SELECT TakenAmount FROM Checkout WHERE User = (?)', (cxt.message.author.id))

if (amount == 0):
await cxt.send("Why are you returning 0 items? Is this a joke?")

# user is returning something
else:
if (takenAmount == amount):
self.cur.execute('DELETE FROM Checkout WHERE CheckoutID = (?)', (checkoutId))
await cxt.send("All items returned. Checkout entry {0} deleted from table.".format(checkoutId))

elif (0 < takenAmount < amount):
self.cur.execute('UPDATE Checkout SET TakenAmount = (?) WHERE CheckoutID = (?)', (takenAmount - amount), (checkoutId))
await cxt.send("Some items returned. Checkout entry {0} updated.".format(checkoutId))

# if user is returning a negative value
else:
await cxt.send("If you want to borrow more items, please use !checkout.")



#View checked out items (completed)
@commands.command()
async def view_checkouts(self, cxt):
if cxt.author == self.bot.user:
return
else:
allCheckedOut = self.cur.execute('SELECT * FROM Checkout')
for row in allCheckedOut:
await cxt.send(row)


#View instock
@commands.command()
async def view_stock(self, cxt,*args):
if cxt.author == self.bot.user:
return
else:
inStock = self.cur.execute('SELECT * FROM Items WHERE Amount != 0')
for row in inStock:
await cxt.send(row)


#Search society items
@commands.command()
async def search(self, cxt,*args):
if cxt.author == self.bot.user:
return
else:
searchString = args[0]
result = self.cur.execute('SELECT * FROM Items WHERE ItemName = (?)', (searchString))
if (self.cur.fetchone() == None):
await cxt.send("The guild does not have an item with that name")
else:
for row in result:
await cxt.send(row)

def setup(bot):
bot.add_cog(InventoryCog(bot))
19 changes: 19 additions & 0 deletions Zoe/cogs/random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import discord
from discord.ext import commands

import random

class Random(commands.Cog):

def __init__(self, bot):
self.bot = bot

@commands.command()
async def random(self, ctx, number=None):
if number:
await ctx.send(random.randint(0, int(number)))
else:
await ctx.send(random.randint(0, 100))

def setup(bot):
bot.add_cog(Random(bot))
29 changes: 29 additions & 0 deletions Zoe/cogs/sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import discord
from discord.ext import commands

class Sort(commands.Cog):

def __init__(self, bot):
self.bot = bot

@commands.command()
async def sort(self, ctx, *args):
argsList = []
for item in args:
argsList.append(item)
argsList.sort()

if argsList:
output = "{} arguments: ".format(len(args))
for item in argsList:
if item == argsList[0]:
output += item
else:
output += " " + item
else:
output = "No arguments found."

await ctx.send(output)

def setup(bot):
bot.add_cog(Sort(bot))
54 changes: 54 additions & 0 deletions Zoe/cogs/twitter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os
import requests
import json

import discord
from discord.ext import commands

class Twitter(commands.Cog):

def __init__(self, bot):
self.bot = bot
self.apiKey = os.environ['KOALA_API_KEY']
self.apiSecret = os.environ['KOALA_SECRET_KEY']
self.accessToken = os.environ['KOALA_ACCESS']
self.accessSecret = os.environ['KOALA_ACCESS_SECRET']
self.bearerToken = os.environ['KOALA_BEARER']

# def create_url():
# return "https://api.twitter.com/2/users/1415763208477020163/tweets"

# def get_params():
# return {"tweets.fields":"created_at"}

def bearer_oauth(self, r):
r.headers["Authorization"] = f"Bearer {self.bearerToken}"
r.headers["User-Agent"] = "v2UserTweetsPython"
return r

@commands.command()
async def get_tweet(self, ctx, user):

try:
userIDResponse = requests.request("GET", "https://api.twitter.com/2/users/by/username/{}".format(user), auth=self.bearer_oauth, params="")
userResponseJson = userIDResponse.json()
userID = userResponseJson["data"]["id"]

response = requests.request("GET", "https://api.twitter.com/2/users/{}/tweets".format(userID), auth=self.bearer_oauth, params="")
json_response = response.json()

url = "https://twitter.com/i/web/status/{}".format(json_response["data"][0]["id"])
description = json_response["data"][0]["text"]

username = userResponseJson["data"]["username"]

embed = discord.Embed(title="{}'s last tweet".format(username), description=description, url=url)

await ctx.send(embed=embed)

except:
await ctx.send(":(")


def setup(bot):
bot.add_cog(Twitter(bot))
44 changes: 44 additions & 0 deletions Zoe/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import discord
from discord.ext import commands

from dotenv import load_dotenv

bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
print(f"Bot user {bot.user} is ready.")
bot.load_extension("cogs.greetings")
bot.load_extension("cogs.sort")
bot.load_extension("cogs.random")
bot.load_extension("cogs.twitter")


@bot.event
async def on_message(msg):
if msg.author == bot.user:
return

if msg.content == "Ping":
await msg.channel.send("Pong!")

if msg.content == "easter":
await msg.channel.send("egg")

if msg.content.lower() == "no one calls esteban julio ricardo montoya de la rosa ramírez a thief!":
await msg.channel.send("NO ONE'S GOT THE TIME")

await bot.process_commands(msg)


#command errors
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Missing argument.")


load_dotenv()
BOT_TOKEN=os.environ['TOKEN']
bot.run(BOT_TOKEN)
5 changes: 5 additions & 0 deletions Zoe/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
discord.py==1.7.3
dpytest==0.5.3
pytest==6.2.4
python-dotenv==0.18.0
TwitterAPI==2.7.5
Loading