diff --git a/JackA/InventoryManager.py b/JackA/InventoryManager.py new file mode 100644 index 0000000..a5446ea --- /dev/null +++ b/JackA/InventoryManager.py @@ -0,0 +1,55 @@ +import sqlite3 +import discord +from discord.ext import commands + + +class InventoryManager(commands.Cog): + def __init__(self, client): + self.bot = client + + con = sqlite3.connect('Inventory.db') + cur = con.cursor() + #cur.execute('''CREATE TABLE inventory + # (item_id INTEGER, count INTEGER, society_id INTEGER)''') + #cur.execute('''CREATE TABLE removedItems + # (item_id INTEGER, count_removed INTEGER, date_removed DATE DEFAULT current_date , user_id INTEGER, society_id INTEGER)''') + #cur.execute('''CREATE TABLE users + # (user_id INTEGER AUTO_INCREMENT, user_name TEXT, society_id INTEGER)''') + #cur.execute('''CREATE TABLE societies + # (society_id INTEGER AUTO_INCREMENT, society_name TEXT, server_id INTEGER, role_id INTEGER)''') + #cur.execute('''CREATE TABLE items + # (item_id INTEGER AUTO_INCREMENT, item_name TEXT, item_description TEXT)''') + + @commands.command() + async def take(self, ctx, item_id, count, society_id): + con = sqlite3.connect('Inventory.db') + cur = con.cursor() + # Decrease the count in the inventory table for item_id and society_id by count + # If count = 0, remove item from table + cur.execute('''INSERT INTO removedItems (item_id, count, date_removed, user_id, society_id) VALUES (item_id, count, current_date, user_id, society_id)''') + + @commands.command() + async def return_item(self, ctx, item_id, count, society_id): + con = sqlite3.connect('Inventory.db') + cur = con.cursor() + # Increase the count in the inventory table for item_id and society_id by count + # If count was 0, add item to table + cur.execute('''INSERT INTO inventory (item_id, count, society_id) VALUES (item_id, count, society_id)''') + + @commands.command() + async def view_inventory(self, ctx): + con = sqlite3.connect('Inventory.db') + cur = con.cursor() + cur.execute("SELECT * FROM inventory") + await ctx.channel.send(cur.fetchall()) + + @commands.command() + async def view_removed(self, ctx, society_id): + con = sqlite3.connect('Inventory.db') + cur = con.cursor() + cur.execute("SELECT * FROM removed WHERE society_id==society_id") + await ctx.channel.send(cur.fetchall()) + + +def setup(bot): + bot.add_cog(InventoryManager(bot)) diff --git a/JackA/hi.py b/JackA/hi.py new file mode 100644 index 0000000..cd36389 --- /dev/null +++ b/JackA/hi.py @@ -0,0 +1,20 @@ +import discord +from discord.ext import commands + + +class Hi(commands.Cog): + def __init__(self, client): + self.bot = client + + @commands.command() + async def hi(self, ctx, arg): + await ctx.channel.send("hello " + arg + "!") + + @commands.command() + async def hey(self, ctx): + print("hello " + ctx.author.display_name + "!") + await ctx.send("hello " + ctx.author.display_name + "!") + + +def setup(bot): + bot.add_cog(Hi(bot)) diff --git a/JackA/main.py b/JackA/main.py new file mode 100644 index 0000000..2ad599b --- /dev/null +++ b/JackA/main.py @@ -0,0 +1,26 @@ +import discord +from discord.ext import commands +from dotenv import load_dotenv +import os + +load_dotenv() + +client = discord.Client() + +token = os.environ['DISCORD_TOKEN'] +COMMAND_PREFIX = "!" + +client = commands.Bot(command_prefix=COMMAND_PREFIX) + + +class Bot(commands.Bot): + async def on_ready(self): + print(f"Bot user {client.user} is ready.") + + +client.load_extension("ping") +client.load_extension("hi") +client.load_extension("sort") + +if __name__ == "__main__": + client.run(token) diff --git a/JackA/ping.py b/JackA/ping.py new file mode 100644 index 0000000..bc03ba8 --- /dev/null +++ b/JackA/ping.py @@ -0,0 +1,15 @@ +import discord +from discord.ext import commands + + +class Ping(commands.Cog): + def __init__(self, client): + self.bot = client + + @commands.command() + async def ping(self, ctx): + await ctx.channel.send("Pong!") + + +def setup(bot): + bot.add_cog(Ping(bot)) diff --git a/JackA/requirements.txt b/JackA/requirements.txt new file mode 100644 index 0000000..0ad2ba4 --- /dev/null +++ b/JackA/requirements.txt @@ -0,0 +1,7 @@ +discord.py==1.7.3 +python-dotenv~=0.18.0 +pytest~=6.2.4 +dpytest +discord~=1.7.3 +tests~=0.7 +requests \ No newline at end of file diff --git a/JackA/sort.py b/JackA/sort.py new file mode 100644 index 0000000..6808e2f --- /dev/null +++ b/JackA/sort.py @@ -0,0 +1,15 @@ +import discord +from discord.ext import commands + + +class Sort(commands.Cog): + def __init__(self, client): + self.bot = client + + @commands.command() + async def sort(self, ctx, *args): + await ctx.send(str(len(args)) + " argument(s)" + "\n" + "Sorted arguments: " + ", ".join(sorted(args))) + + +def setup(bot): + bot.add_cog(Sort(bot)) diff --git a/JackA/tests.py b/JackA/tests.py new file mode 100644 index 0000000..1019f95 --- /dev/null +++ b/JackA/tests.py @@ -0,0 +1,74 @@ +import asyncio + +import discord +import discord.ext.test as dpytest +import pytest +from discord.ext import commands +from discord.ext.commands import MissingRequiredArgument + +import main +import ping +import hi +import sort + +intents = discord.Intents.default() +intents.members = True +intents.guilds = True +intents.messages = True + + +@pytest.fixture(autouse=True) +def ping_cog(bot: commands.Bot): + ping_cog = ping.Ping(bot) + bot.add_cog(ping_cog) + dpytest.configure(bot) + return ping_cog + + +@pytest.fixture(autouse=True) +def hi_cog(bot: commands.Bot): + hi_cog = hi.Hi(bot) + bot.add_cog(hi_cog) + dpytest.configure(bot) + return hi_cog + + +@pytest.fixture(autouse=True) +def sort_cog(bot: commands.Bot): + sort_cog = sort.Sort(bot) + bot.add_cog(sort_cog) + dpytest.configure(bot) + return sort_cog + + +@pytest.fixture(autouse=True) +def bot(event_loop): + bot = commands.Bot("!", loop=event_loop, intents=intents) + dpytest.configure(bot) + print("Starting bot tests") + return bot + + +@pytest.mark.asyncio +async def test_ping_returns_pong(bot): + await dpytest.message("!ping") + assert dpytest.verify().message().contains().content("Pong!") + + +@pytest.mark.asyncio +async def test_hi_name_correct_return(bot): + await dpytest.message("!hi Jack") + assert dpytest.verify().message().contains().content("hello Jack!") + + +@pytest.mark.asyncio +async def test_hey_correct_return(bot): + await dpytest.message("!hey") + assert dpytest.verify().message().contains().content("hello TestUser0_0_nick!") + + +@pytest.mark.asyncio +async def test_sort(bot): + await dpytest.message("!sort e d a c b") + assert dpytest.verify().message().contains().content("5 argument(s)" + "\n" + "Sorted arguments: a, b, c, d, e") + diff --git a/JackA/twitter.py b/JackA/twitter.py new file mode 100644 index 0000000..a095055 --- /dev/null +++ b/JackA/twitter.py @@ -0,0 +1,34 @@ +import os + +import discord +import requests +from discord.ext import commands + + +class Twitter(commands.Cog): + def __init__(self, client): + self.bot = client + + def bearer_oauth(self, r): + bearer_token = os.environ.get("BEARER_TOKEN") + r.headers["Authorization"] = f"Bearer {bearer_token}" + r.headers["User-Agent"] = "v2RecentSearchPython" + return r + + @commands.command() + async def latest_tweet(self, ctx, arg): + query_params = {'query': '(from:'+arg+' -is:retweet)', 'tweet.fields': 'author_id', 'max_results' : 10} + url = "https://api.twitter.com/2/tweets/search/recent" + tweets = requests.get(url, auth=self.bearer_oauth, params=query_params) + if tweets.json() == {'meta': {'result_count': 0}}: + await ctx.channel.send("No tweets for user @" + arg) + else: + await ctx.channel.send(arg + "'s latest tweet: \n" + tweets.json()['data'][0]['text']) + + @commands.command() + async def tweet(self, ctx, *args): + await ctx.channel.send("Pong!") + + +def setup(bot): + bot.add_cog(Twitter(bot))