Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
55 changes: 55 additions & 0 deletions JackA/InventoryManager.py
Original file line number Diff line number Diff line change
@@ -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))
20 changes: 20 additions & 0 deletions JackA/hi.py
Original file line number Diff line number Diff line change
@@ -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))
26 changes: 26 additions & 0 deletions JackA/main.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions JackA/ping.py
Original file line number Diff line number Diff line change
@@ -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))
7 changes: 7 additions & 0 deletions JackA/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions JackA/sort.py
Original file line number Diff line number Diff line change
@@ -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))
74 changes: 74 additions & 0 deletions JackA/tests.py
Original file line number Diff line number Diff line change
@@ -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")

34 changes: 34 additions & 0 deletions JackA/twitter.py
Original file line number Diff line number Diff line change
@@ -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))