From dd0776f08ba9e90ecdbd43562bf28b79ae00df0f Mon Sep 17 00:00:00 2001 From: James Flynn Date: Mon, 19 Jul 2021 13:09:33 +0100 Subject: [PATCH 01/14] Create requirements.txt --- James/requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 James/requirements.txt diff --git a/James/requirements.txt b/James/requirements.txt new file mode 100644 index 0000000..5adf628 --- /dev/null +++ b/James/requirements.txt @@ -0,0 +1 @@ +discord.py==1.7.3 From 53992b317c3058b3e0e95a3283a06841728363c5 Mon Sep 17 00:00:00 2001 From: James Flynn Date: Mon, 19 Jul 2021 13:16:03 +0100 Subject: [PATCH 02/14] Added bot functionality --- James/james-bot.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 James/james-bot.py diff --git a/James/james-bot.py b/James/james-bot.py new file mode 100644 index 0000000..eeb3222 --- /dev/null +++ b/James/james-bot.py @@ -0,0 +1,15 @@ +import discord +client = discord.Client() + +@client.event +async def on_ready(): + print(f”Bot user {client.user} is ready.”) + +@client.event +async def on_message(msg): + if msg.author == client.user: + return # Don’t respond to itself + if msg.content == “Ping”: # Check that the message content matches + await msg.channel.send(“Pong!”) # If it does, reply + +bot.run() \ No newline at end of file From 345a9fcc5b169dc7071368f9d622400ab51272da Mon Sep 17 00:00:00 2001 From: James Flynn Date: Mon, 19 Jul 2021 13:38:59 +0100 Subject: [PATCH 03/14] Working bot --- James/james-bot.py | 15 +++++++++++---- James/requirements.txt | 1 + 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/James/james-bot.py b/James/james-bot.py index eeb3222..c22a592 100644 --- a/James/james-bot.py +++ b/James/james-bot.py @@ -1,15 +1,22 @@ import discord +from dotenv import load_dotenv +import os + +load_dotenv() + client = discord.Client() +token = os.environ['DISCORD_TOKEN'] + @client.event async def on_ready(): - print(f”Bot user {client.user} is ready.”) + print("Bot user" +client.user.name+" is ready.") @client.event async def on_message(msg): if msg.author == client.user: return # Don’t respond to itself - if msg.content == “Ping”: # Check that the message content matches - await msg.channel.send(“Pong!”) # If it does, reply + if msg.content == "Ping": # Check that the message content matches + await msg.channel.send("Pong!") # If it does, reply -bot.run() \ No newline at end of file +client.run(token) \ No newline at end of file diff --git a/James/requirements.txt b/James/requirements.txt index 5adf628..8dd7936 100644 --- a/James/requirements.txt +++ b/James/requirements.txt @@ -1 +1,2 @@ discord.py==1.7.3 +python-dotenv \ No newline at end of file From c0166b63368ed9d0e3ef29d87f32c0b68d606eb8 Mon Sep 17 00:00:00 2001 From: James Flynn Date: Mon, 19 Jul 2021 13:56:39 +0100 Subject: [PATCH 04/14] Added command with command prefix --- James/james-bot.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/James/james-bot.py b/James/james-bot.py index c22a592..7dca4a6 100644 --- a/James/james-bot.py +++ b/James/james-bot.py @@ -1,10 +1,11 @@ import discord +from discord.ext import commands from dotenv import load_dotenv import os load_dotenv() -client = discord.Client() +client = commands.Bot(command_prefix='!') token = os.environ['DISCORD_TOKEN'] @@ -12,11 +13,17 @@ async def on_ready(): print("Bot user" +client.user.name+" is ready.") -@client.event -async def on_message(msg): - if msg.author == client.user: - return # Don’t respond to itself - if msg.content == "Ping": # Check that the message content matches - await msg.channel.send("Pong!") # If it does, reply +@client.command() +async def hi(cxt, args): + if cxt.author == client.user: + return + await cxt.channel.send("Hello "+ args +"!") + +#@client.event +#async def on_message(msg): +# if msg.author == client.user: +# return # Don’t respond to itself +# if msg.content == "Ping": # Check that the message content matches +# await msg.channel.send("Pong!") # If it does, reply client.run(token) \ No newline at end of file From 75293d123a4746ef5627c849fe602dc579f6ef1e Mon Sep 17 00:00:00 2001 From: James Flynn Date: Mon, 19 Jul 2021 14:29:18 +0100 Subject: [PATCH 05/14] Added argument sort --- James/james-bot.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/James/james-bot.py b/James/james-bot.py index 7dca4a6..1c6f256 100644 --- a/James/james-bot.py +++ b/James/james-bot.py @@ -14,10 +14,25 @@ async def on_ready(): print("Bot user" +client.user.name+" is ready.") @client.command() -async def hi(cxt, args): +async def hi(ctx, args = ""): + if ctx.author == client.user: + return + if (args == ""): + print("no args") + await ctx.channel.send("Hello "+ ctx.author.name +"!") + + else: + await ctx.channel.send("Hello "+ args +"!") + +@client.command() +async def sort(cxt,*args): if cxt.author == client.user: return - await cxt.channel.send("Hello "+ args +"!") + else: + print(list(args)) + argList = list(args) + sortedArgs = argList.sort() + await cxt.send('{} arguments: {}'.format(len(argList), ', '.join(argList))) #@client.event #async def on_message(msg): From f9f4d5cd9722ea526e00e95454542271cf27859c Mon Sep 17 00:00:00 2001 From: James Flynn Date: Tue, 20 Jul 2021 12:40:28 +0100 Subject: [PATCH 06/14] Cogs --- James/argsort.py | 15 +++++++++++++++ James/james-bot.py | 15 ++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 James/argsort.py diff --git a/James/argsort.py b/James/argsort.py new file mode 100644 index 0000000..84bb489 --- /dev/null +++ b/James/argsort.py @@ -0,0 +1,15 @@ +import discord +from discord.ext import commands + +class SearchCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + @bot.command() + async def sort(self, cxt,*args): + if cxt.author == bot.user: + return + else: + print(list(args)) + argList = list(args) + sortedArgs = argList.sort() + await cxt.send('{} arguments: {}'.format(len(argList), ', '.join(argList))) diff --git a/James/james-bot.py b/James/james-bot.py index 1c6f256..94470f0 100644 --- a/James/james-bot.py +++ b/James/james-bot.py @@ -1,4 +1,6 @@ +from inspect import classify_class_attrs import discord +from argsort import SearchCog from discord.ext import commands from dotenv import load_dotenv import os @@ -8,7 +10,8 @@ client = commands.Bot(command_prefix='!') token = os.environ['DISCORD_TOKEN'] - +client.add_cog(SearchCog(client)) +search = client.get_cog("SearchCog") @client.event async def on_ready(): print("Bot user" +client.user.name+" is ready.") @@ -24,16 +27,6 @@ async def hi(ctx, args = ""): else: await ctx.channel.send("Hello "+ args +"!") -@client.command() -async def sort(cxt,*args): - if cxt.author == client.user: - return - else: - print(list(args)) - argList = list(args) - sortedArgs = argList.sort() - await cxt.send('{} arguments: {}'.format(len(argList), ', '.join(argList))) - #@client.event #async def on_message(msg): # if msg.author == client.user: From 320896fcd11cbee822b784a3749dd3f185464971 Mon Sep 17 00:00:00 2001 From: James Flynn Date: Tue, 20 Jul 2021 13:55:21 +0100 Subject: [PATCH 07/14] updated --- James/argsort.py | 5 +++-- James/{james-bot.py => main.py} | 4 ++-- James/requirements.txt | 4 +++- James/tester.py | 27 +++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) rename James/{james-bot.py => main.py} (95%) create mode 100644 James/tester.py diff --git a/James/argsort.py b/James/argsort.py index 84bb489..2184ad7 100644 --- a/James/argsort.py +++ b/James/argsort.py @@ -4,9 +4,10 @@ class SearchCog(commands.Cog): def __init__(self, bot): self.bot = bot - @bot.command() + + @commands.command() async def sort(self, cxt,*args): - if cxt.author == bot.user: + if cxt.author == self.bot.user: return else: print(list(args)) diff --git a/James/james-bot.py b/James/main.py similarity index 95% rename from James/james-bot.py rename to James/main.py index 94470f0..e8e6046 100644 --- a/James/james-bot.py +++ b/James/main.py @@ -33,5 +33,5 @@ async def hi(ctx, args = ""): # return # Don’t respond to itself # if msg.content == "Ping": # Check that the message content matches # await msg.channel.send("Pong!") # If it does, reply - -client.run(token) \ No newline at end of file +if __name__ == "main": + client.run(token) \ No newline at end of file diff --git a/James/requirements.txt b/James/requirements.txt index 8dd7936..c99632b 100644 --- a/James/requirements.txt +++ b/James/requirements.txt @@ -1,2 +1,4 @@ discord.py==1.7.3 -python-dotenv \ No newline at end of file +python-dotenv +pytest +dpytest \ No newline at end of file diff --git a/James/tester.py b/James/tester.py new file mode 100644 index 0000000..c1d9ae6 --- /dev/null +++ b/James/tester.py @@ -0,0 +1,27 @@ +import discord.ext.test as dpytest +import discord +import pytest +from discord.ext import commands + +import main + +@pytest.fixture(autouse=True) +async def initalBot(event_loop): + print("Tests starting") + intents = discord.Intents.default() + intents.members = True + intents.guilds = True + bot = commands.Bot("!", intents) + for com in main.client.commands: + if com.name != "help": + bot.add_command(com) + await dpytest.empty_queue() + dpytest.configure(bot) + print("Starting bot tests") + return bot + + +@pytest.mark.asyncio +async def test_default_hi(): + await dpytest.message("!hi") + assert dpytest.verify.message.content("Hello james!") \ No newline at end of file From 2b568f95bad0276cf00d9d1f83111b3b96709484 Mon Sep 17 00:00:00 2001 From: James Flynn Date: Tue, 20 Jul 2021 14:43:06 +0100 Subject: [PATCH 08/14] main fixed --- James/main.py | 4 ++-- James/tester.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/James/main.py b/James/main.py index e8e6046..2c6bbe7 100644 --- a/James/main.py +++ b/James/main.py @@ -7,7 +7,7 @@ load_dotenv() -client = commands.Bot(command_prefix='!') +client = commands.Bot(command_prefix='!', help_command=None) token = os.environ['DISCORD_TOKEN'] client.add_cog(SearchCog(client)) @@ -33,5 +33,5 @@ async def hi(ctx, args = ""): # return # Don’t respond to itself # if msg.content == "Ping": # Check that the message content matches # await msg.channel.send("Pong!") # If it does, reply -if __name__ == "main": +if __name__ == "__main__": client.run(token) \ No newline at end of file diff --git a/James/tester.py b/James/tester.py index c1d9ae6..4116fe3 100644 --- a/James/tester.py +++ b/James/tester.py @@ -11,14 +11,14 @@ async def initalBot(event_loop): intents = discord.Intents.default() intents.members = True intents.guilds = True - bot = commands.Bot("!", intents) + tBot = commands.Bot("!", intents) for com in main.client.commands: - if com.name != "help": - bot.add_command(com) + if com.name != "help_command" and com.name != "help": + tBot.add_command(com) await dpytest.empty_queue() - dpytest.configure(bot) + dpytest.configure(tBot) print("Starting bot tests") - return bot + return tBot @pytest.mark.asyncio From 7bc66a402361ea78c4d3a35258e7e0599f94b21a Mon Sep 17 00:00:00 2001 From: James Flynn Date: Tue, 20 Jul 2021 15:02:19 +0100 Subject: [PATCH 09/14] Added more tests, started on ReactCog --- James/argsort.py | 2 +- James/main.py | 3 ++- James/reaction.py | 16 ++++++++++++++++ James/tester.py | 27 ++++++++++++++++++++++++++- 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 James/reaction.py diff --git a/James/argsort.py b/James/argsort.py index 2184ad7..5fa2e4c 100644 --- a/James/argsort.py +++ b/James/argsort.py @@ -12,5 +12,5 @@ async def sort(self, cxt,*args): else: print(list(args)) argList = list(args) - sortedArgs = argList.sort() + argList.sort() await cxt.send('{} arguments: {}'.format(len(argList), ', '.join(argList))) diff --git a/James/main.py b/James/main.py index 2c6bbe7..2539411 100644 --- a/James/main.py +++ b/James/main.py @@ -7,11 +7,12 @@ load_dotenv() -client = commands.Bot(command_prefix='!', help_command=None) +client = commands.Bot(command_prefix='!') token = os.environ['DISCORD_TOKEN'] client.add_cog(SearchCog(client)) search = client.get_cog("SearchCog") + @client.event async def on_ready(): print("Bot user" +client.user.name+" is ready.") diff --git a/James/reaction.py b/James/reaction.py new file mode 100644 index 0000000..d3b9565 --- /dev/null +++ b/James/reaction.py @@ -0,0 +1,16 @@ +import discord +from discord.ext import commands + +class ReactCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.command() + async def sort(self, cxt,*args): + if cxt.author == self.bot.user: + return + else: + print(list(args)) + argList = list(args) + argList.sort() + await cxt.send('{} arguments: {}'.format(len(argList), ', '.join(argList))) diff --git a/James/tester.py b/James/tester.py index 4116fe3..a6ecf49 100644 --- a/James/tester.py +++ b/James/tester.py @@ -24,4 +24,29 @@ async def initalBot(event_loop): @pytest.mark.asyncio async def test_default_hi(): await dpytest.message("!hi") - assert dpytest.verify.message.content("Hello james!") \ No newline at end of file + assert dpytest.verify.message.content("Hello james!") + +@pytest.mark.asyncio +async def test_single_hi(): + await dpytest.message("!hi John") + assert dpytest.verify.message.content("Hello John!") + +@pytest.mark.asyncio +async def test_inorder_sort(): + await dpytest.message("!sort hi hello test") + assert dpytest.verify.message.content("3 arguments: hi, hello, test") + +@pytest.mark.asyncio +async def test_blank_sort(): + await dpytest.message("!sort") + assert dpytest.verify.message.content("0 arguments: ") + +@pytest.mark.asyncio +async def test_unordered_sort(): + await dpytest.message("!sort test hello hi") + assert dpytest.verify.message.content("3 arguments: hi, hello, test") + +@pytest.mark.asyncio +async def test_same_sort(): + await dpytest.message("!sort test hi hi hi hi hi") + assert dpytest.verify.message.content("6 arguments: hi hi hi hi hi test") \ No newline at end of file From 87f253ac0f18680b5c4ab89d9c246ed84309bc37 Mon Sep 17 00:00:00 2001 From: James Flynn Date: Wed, 21 Jul 2021 13:44:32 +0100 Subject: [PATCH 10/14] Fixed tests --- James/argsort.py | 3 ++- James/tester.py | 17 +++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/James/argsort.py b/James/argsort.py index 5fa2e4c..9150461 100644 --- a/James/argsort.py +++ b/James/argsort.py @@ -10,7 +10,8 @@ async def sort(self, cxt,*args): if cxt.author == self.bot.user: return else: - print(list(args)) argList = list(args) argList.sort() + print(('{} arguments: {}'.format(len(argList), ', '.join(argList)))) + await cxt.send('{} arguments: {}'.format(len(argList), ', '.join(argList))) diff --git a/James/tester.py b/James/tester.py index a6ecf49..1b03331 100644 --- a/James/tester.py +++ b/James/tester.py @@ -11,7 +11,7 @@ async def initalBot(event_loop): intents = discord.Intents.default() intents.members = True intents.guilds = True - tBot = commands.Bot("!", intents) + tBot = commands.Bot("!", intents=intents) for com in main.client.commands: if com.name != "help_command" and com.name != "help": tBot.add_command(com) @@ -24,29 +24,30 @@ async def initalBot(event_loop): @pytest.mark.asyncio async def test_default_hi(): await dpytest.message("!hi") - assert dpytest.verify.message.content("Hello james!") + assert dpytest.verify().message().content("Hello "+ +"!") @pytest.mark.asyncio async def test_single_hi(): await dpytest.message("!hi John") - assert dpytest.verify.message.content("Hello John!") + assert dpytest.verify().message().content("Hello John!") @pytest.mark.asyncio async def test_inorder_sort(): - await dpytest.message("!sort hi hello test") - assert dpytest.verify.message.content("3 arguments: hi, hello, test") + await dpytest.message("!sort hello hi test") + assert dpytest.verify().message().content("3 arguments: hello, hi, test") @pytest.mark.asyncio async def test_blank_sort(): await dpytest.message("!sort") - assert dpytest.verify.message.content("0 arguments: ") + assert dpytest.verify().message().content("0 arguments: ") @pytest.mark.asyncio async def test_unordered_sort(): await dpytest.message("!sort test hello hi") - assert dpytest.verify.message.content("3 arguments: hi, hello, test") + print() + assert dpytest.verify().message().content("3 arguments: hello, hi, test") @pytest.mark.asyncio async def test_same_sort(): await dpytest.message("!sort test hi hi hi hi hi") - assert dpytest.verify.message.content("6 arguments: hi hi hi hi hi test") \ No newline at end of file + assert dpytest.verify().message().content("6 arguments: hi, hi, hi, hi, hi, test") \ No newline at end of file From 2a56fdd05587ab3003f0ef504fc00879c657b380 Mon Sep 17 00:00:00 2001 From: James Flynn Date: Wed, 21 Jul 2021 15:27:04 +0100 Subject: [PATCH 11/14] Midway through inventory management cog Group project to learn databases with @abluey by creating an inventory management cog --- James/inventory.py | 125 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 James/inventory.py diff --git a/James/inventory.py b/James/inventory.py new file mode 100644 index 0000000..0c8a14e --- /dev/null +++ b/James/inventory.py @@ -0,0 +1,125 @@ +import sqlite3 +import discord +from discord.ext import commands +import datetime + + +# for reference :) +# import datetime +# f"{datetime.datetime.now():%d-%m-%Y}" +# outputs date in dd-mm-YYYY + + +class InventoryCog(commands.Cog): + + def __init__(self, bot): + self.bot = bot + self.con = sqlite3.connect('inventory.db') + self.cur = self.con.cursor() + + # Create tables + cur.executescript('''CREATE TABLE Items ( + ItemID integer AUTO_INCREMENT, + ItemName varchar(255), + ItemDescription varchar(255), + Amount integer, + PRIMARY KEY (ItemID) + ); + + CREATE TABLE Checkout ( + CheckoutID varchar(255), + User varchar(255), + TakenAmount integer, + TakenDate datetime, + ItemID integer, + PRIMARY KEY (CheckoutID), + FOREIGN KEY (ItemID) + );''' + ) + + # Inserting values into ITEMS + cur.executescript('''INSERT INTO Items (ItemID, ItemName, ItemDesc, Amount) VALUES + (1, "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);''') + + # Inserting values into CHECKOUT + cur.executescript('''INSERT INTO Checkout (CheckoutID, User, TakenAmount, TakenDate) VALUES + ("C1", )''') + + + #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: + amount = args[1] + itemId = args[0] + #Find the total checked out for the item + checkedOut = self.cur.execute('SELECT Sum(TakenAmount) FROM Checkout WHERE (?)', (itemId)) + #Should check to see if the item exists + total = self.cur.execute('SELECT Amount FROM Items WHERE (?)', (itemId)) + if(checkedOut + amount < total): + checkedOut = self.cur.execute('INSERT INTO Checkout (?,?,?,?,?)', (checkID, cxt.author, amount, date, itemId)) # 1st and 4th should be autocreated + 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 availible") + + + #Return item + @commands.command() + async def return_item(self, cxt,*args): + if cxt.author == self.bot.user: + return + else: + if(userCheckedOutItem): + await cxt.send("") + else: + await cxt.send("") + + + + + #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 (have to loop through all checkouts) + @commands.command() + async def view_stock(self, cxt,*args): + if cxt.author == self.bot.user: + return + else: + argList = list(args) + argList.sort() + print(('{} arguments: {}'.format(len(argList), ', '.join(argList)))) + + await cxt.send('{} arguments: {}'.format(len(argList), ', '.join(argList))) + + #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 (?)', (searchString)) + await cxt.send("") + From b1734f882a0e50d16c2e7a8049d60cc2d861c550 Mon Sep 17 00:00:00 2001 From: James Flynn Date: Wed, 21 Jul 2021 15:40:23 +0100 Subject: [PATCH 12/14] Update inventory.py --- James/inventory.py | 50 +++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/James/inventory.py b/James/inventory.py index 0c8a14e..91069fc 100644 --- a/James/inventory.py +++ b/James/inventory.py @@ -6,8 +6,8 @@ # for reference :) # import datetime -# f"{datetime.datetime.now():%d-%m-%Y}" -# outputs date in dd-mm-YYYY +# datetime.datetime.now().strftime("%d-%m-%Y") +# outputs date in dd-mm-YYYY, a string class InventoryCog(commands.Cog): @@ -27,10 +27,10 @@ def __init__(self, bot): ); CREATE TABLE Checkout ( - CheckoutID varchar(255), + CheckoutID integer AUTO_INCREMENT, User varchar(255), TakenAmount integer, - TakenDate datetime, + TakenDate varchar(10), ItemID integer, PRIMARY KEY (CheckoutID), FOREIGN KEY (ItemID) @@ -50,8 +50,23 @@ def __init__(self, bot): ("Mousemat", "Flat thing to put mouse on", 4);''') # Inserting values into CHECKOUT - cur.executescript('''INSERT INTO Checkout (CheckoutID, User, TakenAmount, TakenDate) VALUES - ("C1", )''') + cur.executescript('''INSERT INTO Checkout (CheckoutID, User, TakenAmount, TakenDate, ItemID) VALUES + ("1", "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); + ''') #Checkout item (almost completed) @@ -64,14 +79,17 @@ async def checkout(self, cxt,*args): if cxt.author == self.bot.user: return else: - amount = args[1] itemId = args[0] + amount = args[1] #Find the total checked out for the item - checkedOut = self.cur.execute('SELECT Sum(TakenAmount) FROM Checkout WHERE (?)', (itemId)) + #checkedOut = self.cur.execute('SELECT Sum(TakenAmount) FROM Checkout WHERE ItemID = (?)', (itemId)) #Should check to see if the item exists - total = self.cur.execute('SELECT Amount FROM Items WHERE (?)', (itemId)) - if(checkedOut + amount < total): - checkedOut = self.cur.execute('INSERT INTO Checkout (?,?,?,?,?)', (checkID, cxt.author, amount, date, itemId)) # 1st and 4th should be autocreated + total = self.cur.execute('SELECT Amount FROM Items WHERE ItemID = (?)', (itemId)) + if(amount <= total): + checkedOut = self.cur.execute('''INSERT INTO Checkout (User, TakenAmount,ItemID) + (?,?,?) + ''', (cxt.message.author.id, amount, itemId)) + checkedOut = self.cur.execute('''INSERT INTO Checkout (User, TakenAmount,ItemID)(?,?,?)''', (cxt.message.author.id, 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 availible") @@ -83,10 +101,10 @@ async def return_item(self, cxt,*args): if cxt.author == self.bot.user: return else: - if(userCheckedOutItem): - await cxt.send("") - else: - await cxt.send("") + itemId = args[0] + amounts = args[1] + + takenAmount = self.cur.execute('SELECT TakenAmount FROM Checkout WHERE User = (?)', (cxt.message.author.id)) @@ -120,6 +138,6 @@ async def search(self, cxt,*args): return else: searchString - args[0] - result = self.cur.execute('SELECT * FROM Items WHERE (?)', (searchString)) + result = self.cur.execute('SELECT * FROM Items WHERE ItemName = (?)', (searchString)) await cxt.send("") From 8b75de9e076cc1e54cd7affcc23fab0b809ff04d Mon Sep 17 00:00:00 2001 From: James Flynn Date: Wed, 21 Jul 2021 18:39:28 +0100 Subject: [PATCH 13/14] Fixes to the checkout system --- James/.gitignore | 1 + James/inventory.py | 113 ++++++++++++++++++++++++++++++++------------- James/main.py | 4 ++ 3 files changed, 85 insertions(+), 33 deletions(-) create mode 100644 James/.gitignore diff --git a/James/.gitignore b/James/.gitignore new file mode 100644 index 0000000..3997bea --- /dev/null +++ b/James/.gitignore @@ -0,0 +1 @@ +*.db \ No newline at end of file diff --git a/James/inventory.py b/James/inventory.py index 91069fc..4686d7d 100644 --- a/James/inventory.py +++ b/James/inventory.py @@ -18,28 +18,32 @@ def __init__(self, bot): self.cur = self.con.cursor() # Create tables - cur.executescript('''CREATE TABLE Items ( - ItemID integer AUTO_INCREMENT, + self.cur.execute('''CREATE TABLE IF NOT EXISTS Items ( + ItemID integer PRIMARY KEY AUTOINCREMENT, ItemName varchar(255), - ItemDescription varchar(255), - Amount integer, - PRIMARY KEY (ItemID) - ); - - CREATE TABLE Checkout ( - CheckoutID integer AUTO_INCREMENT, + 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, - PRIMARY KEY (CheckoutID), + CONSTRAINT fk_extensions FOREIGN KEY (ItemID) + REFERENCES Items (ItemID) );''' ) + self.con.commit() # Inserting values into ITEMS - cur.executescript('''INSERT INTO Items (ItemID, ItemName, ItemDesc, Amount) VALUES - (1, "Headphones", "Pair of headphones", 10); + print("inserting 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 @@ -48,10 +52,14 @@ def __init__(self, bot): ("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() + + print("commited items") # Inserting values into CHECKOUT - cur.executescript('''INSERT INTO Checkout (CheckoutID, User, TakenAmount, TakenDate, ItemID) VALUES - ("1", "A", 2, "14-07-21", 1); + print("inserting checkouts") + 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 @@ -67,6 +75,10 @@ def __init__(self, bot): INSERT INTO Checkout (User, TakenAmount, TakenDate, ItemID) VALUES ("D", 4, "14-07-21", 5); ''') + + self.con.commit() + + print("commited checkouts") #Checkout item (almost completed) @@ -79,33 +91,65 @@ async def checkout(self, cxt,*args): if cxt.author == self.bot.user: return else: - itemId = args[0] - amount = args[1] - #Find the total checked out for the item - #checkedOut = self.cur.execute('SELECT Sum(TakenAmount) FROM Checkout WHERE ItemID = (?)', (itemId)) + 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 = (?)', (itemId)) - if(amount <= total): + 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 (amount <= total): checkedOut = self.cur.execute('''INSERT INTO Checkout (User, TakenAmount,ItemID) (?,?,?) ''', (cxt.message.author.id, amount, itemId)) - 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)) + + self.con.commit() + await cxt.send("User "+ cxt.author + " successfully checked out " + amount + " of item " + itemId) + + elif (0 > amount): + await cxt.send("You're trying to checkout negative items?!") + else: - await cxt.send("The guild doesn't currently have that item availible") + 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 = args[0] - amounts = args[1] + 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.") @@ -119,17 +163,17 @@ async def view_checkouts(self, cxt): for row in allCheckedOut: await cxt.send(row) - #View instock (have to loop through all checkouts) + + #View instock @commands.command() async def view_stock(self, cxt,*args): if cxt.author == self.bot.user: - return + return else: - argList = list(args) - argList.sort() - print(('{} arguments: {}'.format(len(argList), ', '.join(argList)))) + inStock = self.cur.execute('SELECT * FROM Items WHERE Amount != 0') + for row in inStock: + await cxt.send(row) - await cxt.send('{} arguments: {}'.format(len(argList), ', '.join(argList))) #Search society items @commands.command() @@ -137,7 +181,10 @@ async def search(self, cxt,*args): if cxt.author == self.bot.user: return else: - searchString - args[0] + searchString = args[0] result = self.cur.execute('SELECT * FROM Items WHERE ItemName = (?)', (searchString)) - await cxt.send("") - + 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) diff --git a/James/main.py b/James/main.py index 2539411..253beb7 100644 --- a/James/main.py +++ b/James/main.py @@ -1,6 +1,7 @@ from inspect import classify_class_attrs import discord from argsort import SearchCog +from inventory import InventoryCog from discord.ext import commands from dotenv import load_dotenv import os @@ -11,7 +12,10 @@ token = os.environ['DISCORD_TOKEN'] client.add_cog(SearchCog(client)) +client.add_cog(InventoryCog(client)) + search = client.get_cog("SearchCog") +inventory = client.get_cog("InventoryCog") @client.event async def on_ready(): From f5a06f952773fc693096531323b419b85f3eddef Mon Sep 17 00:00:00 2001 From: James Flynn Date: Thu, 22 Jul 2021 01:50:58 +0100 Subject: [PATCH 14/14] Update inventory.py --- James/inventory.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/James/inventory.py b/James/inventory.py index 4686d7d..bdf1388 100644 --- a/James/inventory.py +++ b/James/inventory.py @@ -42,7 +42,7 @@ def __init__(self, bot): # Inserting values into ITEMS print("inserting items") - self.cur.executescript('''INSERT INTO Items ( ItemName, ItemDesc, Amount) VALUES + 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); @@ -58,7 +58,7 @@ def __init__(self, bot): # Inserting values into CHECKOUT print("inserting checkouts") - self.cur.executescript('''INSERT INTO Checkout ( User, TakenAmount, TakenDate, ItemID) VALUES + 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); @@ -104,14 +104,14 @@ async def checkout(self, cxt,*args): elif (amount <= total): checkedOut = self.cur.execute('''INSERT INTO Checkout (User, TakenAmount,ItemID) (?,?,?) - ''', (cxt.message.author.id, amount, itemId)) + ''', (cxt.message.author.id, amount, str(itemId))) updateAmount = self.cur.execute('''UPDATE Items SET Amount = (?) - WHERE ItemID = (?)''', (total - amount, itemId)) + WHERE ItemID = (?)''', (total - amount, str(itemId))) self.con.commit() - await cxt.send("User "+ cxt.author + " successfully checked out " + amount + " of item " + itemId) + await cxt.send("User "+ cxt.author + " successfully checked out " + amount + " of item " + str(itemId)) elif (0 > amount): await cxt.send("You're trying to checkout negative items?!") @@ -131,7 +131,7 @@ async def return_item(self, cxt,*args): 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)) + checkoutId = self.cur.execute('SELECT CheckoutID FROM Checkout WHERE User = (?) AND ItemID = (?)', (cxt.message.author.id, str(itemId))) takenAmount = self.cur.execute('SELECT TakenAmount FROM Checkout WHERE User = (?)', (cxt.message.author.id)) if (amount == 0): @@ -141,10 +141,12 @@ async def return_item(self, cxt,*args): else: if (takenAmount == amount): self.cur.execute('DELETE FROM Checkout WHERE CheckoutID = (?)', (checkoutId)) + self.con.commit() 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)) + self.con.commit() await cxt.send("Some items returned. Checkout entry {0} updated.".format(checkoutId)) # if user is returning a negative value