From b756c33dccd6382ad866c7d5651026d83955670a Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Mon, 19 Jul 2021 13:14:14 +0100 Subject: [PATCH 01/16] Added requirements.txt --- JackA/requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 JackA/requirements.txt diff --git a/JackA/requirements.txt b/JackA/requirements.txt new file mode 100644 index 0000000..7750a0e --- /dev/null +++ b/JackA/requirements.txt @@ -0,0 +1 @@ +discord.py==1.7.3 \ No newline at end of file From 092c53ed2ae1459e5bba92efcf4e22137eac0425 Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Mon, 19 Jul 2021 13:37:05 +0100 Subject: [PATCH 02/16] Added simple ping pong command --- JackA/jack-bot.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 JackA/jack-bot.py diff --git a/JackA/jack-bot.py b/JackA/jack-bot.py new file mode 100644 index 0000000..a979031 --- /dev/null +++ b/JackA/jack-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 + +client.run("ODY2NjQxODMwOTE2Nzg0MTM4.YPVhGQ.0xRxmgt0WJJutUquVkxMNTvaRXM") #insert token here From fb2cfa4ab0af886a52daf4522495a34275ce1e4d Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Mon, 19 Jul 2021 13:38:18 +0100 Subject: [PATCH 03/16] Added simple ping pong command --- JackA/jack-bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JackA/jack-bot.py b/JackA/jack-bot.py index a979031..0d9a339 100644 --- a/JackA/jack-bot.py +++ b/JackA/jack-bot.py @@ -12,4 +12,4 @@ async def on_message(msg): if msg.content == "Ping": # Check that the message content matches await msg.channel.send("Pong!") # If it does, reply -client.run("ODY2NjQxODMwOTE2Nzg0MTM4.YPVhGQ.0xRxmgt0WJJutUquVkxMNTvaRXM") #insert token here +client.run() #insert token here From 9c26006d77f199eabc5ba1a1ba45e008bd2adcdc Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Mon, 19 Jul 2021 13:44:51 +0100 Subject: [PATCH 04/16] Removed bot token from jack-bot.py --- JackA/jack-bot.py | 9 ++++++++- JackA/requirements.txt | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/JackA/jack-bot.py b/JackA/jack-bot.py index 0d9a339..b1ac7ae 100644 --- a/JackA/jack-bot.py +++ b/JackA/jack-bot.py @@ -1,6 +1,13 @@ 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.") @@ -12,4 +19,4 @@ async def on_message(msg): if msg.content == "Ping": # Check that the message content matches await msg.channel.send("Pong!") # If it does, reply -client.run() #insert token here +client.run(token) #insert token here diff --git a/JackA/requirements.txt b/JackA/requirements.txt index 7750a0e..8dd7936 100644 --- a/JackA/requirements.txt +++ b/JackA/requirements.txt @@ -1 +1,2 @@ -discord.py==1.7.3 \ No newline at end of file +discord.py==1.7.3 +python-dotenv \ No newline at end of file From 172ca7316ce07536c2dbb95bea8cbbcd89689969 Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Mon, 19 Jul 2021 14:25:14 +0100 Subject: [PATCH 05/16] Added hi command, with ! command prefix --- JackA/jack-bot.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/JackA/jack-bot.py b/JackA/jack-bot.py index b1ac7ae..e0ea648 100644 --- a/JackA/jack-bot.py +++ b/JackA/jack-bot.py @@ -1,4 +1,5 @@ import discord +from discord.ext import commands from dotenv import load_dotenv import os @@ -7,16 +8,30 @@ client = discord.Client() token = os.environ['DISCORD_TOKEN'] +COMMAND_PREFIX = "!" + +client = commands.Bot(command_prefix=COMMAND_PREFIX) + @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 -client.run(token) #insert token here +@client.command() +async def Ping(ctx): + await ctx.channel.send("Pong!") # If it does, reply + +@client.command() +async def hi(ctx, arg, name = None): + if ctx.author == client.user: + return + arg = arg or ctx.author.display_name + await ctx.channel.send("hello " + arg + "!") + +@hi.error +async def hi_error(ctx, error): + if isinstance(error, commands.MissingRequiredArgument): + await ctx.send("hello " + ctx.author.display_name + "!") + +client.run(token) From 8aa528dc97c07f4e9bf5fb23e81080477a1f71f9 Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Mon, 19 Jul 2021 14:37:37 +0100 Subject: [PATCH 06/16] Added sort command --- JackA/jack-bot.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/JackA/jack-bot.py b/JackA/jack-bot.py index e0ea648..7e5ad4d 100644 --- a/JackA/jack-bot.py +++ b/JackA/jack-bot.py @@ -23,10 +23,9 @@ async def Ping(ctx): await ctx.channel.send("Pong!") # If it does, reply @client.command() -async def hi(ctx, arg, name = None): +async def hi(ctx, arg): if ctx.author == client.user: return - arg = arg or ctx.author.display_name await ctx.channel.send("hello " + arg + "!") @hi.error @@ -34,4 +33,12 @@ async def hi_error(ctx, error): if isinstance(error, commands.MissingRequiredArgument): await ctx.send("hello " + ctx.author.display_name + "!") +@client.command() +async def sort(ctx, *args): + if ctx.author == client.user: + return + await ctx.send(str(len(args)) + " argument(s)") + await ctx.send("Sorted arguments: " + ", ".join(sorted(args))) + + client.run(token) From 48b786e1eb653209a66ee8d240313abdd8d7e488 Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Mon, 19 Jul 2021 15:32:36 +0100 Subject: [PATCH 07/16] Added cogs --- JackA/hi.py | 20 ++++++++++++++++++++ JackA/ping.py | 15 +++++++++++++++ JackA/sort.py | 16 ++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 JackA/hi.py create mode 100644 JackA/ping.py create mode 100644 JackA/sort.py diff --git a/JackA/hi.py b/JackA/hi.py new file mode 100644 index 0000000..2f065da --- /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 + "!") + + @hi.error + async def hi_error(self, ctx, error): + if isinstance(error, commands.MissingRequiredArgument): + await ctx.send("hello " + ctx.author.display_name + "!") + + +def setup(bot): + bot.add_cog(Hi(bot)) 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/sort.py b/JackA/sort.py new file mode 100644 index 0000000..20faed9 --- /dev/null +++ b/JackA/sort.py @@ -0,0 +1,16 @@ +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)") + await ctx.send("Sorted arguments: " + ", ".join(sorted(args))) + + +def setup(bot): + bot.add_cog(Sort(bot)) From 2750a4de3e4570975b6df8120a4c05e2401b8ebd Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Tue, 20 Jul 2021 13:59:28 +0100 Subject: [PATCH 08/16] Added ping test --- JackA/jack-bot.py | 44 ------------------------------------------ JackA/main.py | 26 +++++++++++++++++++++++++ JackA/requirements.txt | 6 +++++- JackA/tests.py | 34 ++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 45 deletions(-) delete mode 100644 JackA/jack-bot.py create mode 100644 JackA/main.py create mode 100644 JackA/tests.py diff --git a/JackA/jack-bot.py b/JackA/jack-bot.py deleted file mode 100644 index 7e5ad4d..0000000 --- a/JackA/jack-bot.py +++ /dev/null @@ -1,44 +0,0 @@ -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) - - -@client.event -async def on_ready(): - print(f"Bot user {client.user} is ready.") - - -@client.command() -async def Ping(ctx): - await ctx.channel.send("Pong!") # If it does, reply - -@client.command() -async def hi(ctx, arg): - if ctx.author == client.user: - return - await ctx.channel.send("hello " + arg + "!") - -@hi.error -async def hi_error(ctx, error): - if isinstance(error, commands.MissingRequiredArgument): - await ctx.send("hello " + ctx.author.display_name + "!") - -@client.command() -async def sort(ctx, *args): - if ctx.author == client.user: - return - await ctx.send(str(len(args)) + " argument(s)") - await ctx.send("Sorted arguments: " + ", ".join(sorted(args))) - - -client.run(token) diff --git a/JackA/main.py b/JackA/main.py new file mode 100644 index 0000000..a37564a --- /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") + + +client.run(token) diff --git a/JackA/requirements.txt b/JackA/requirements.txt index 8dd7936..28f9566 100644 --- a/JackA/requirements.txt +++ b/JackA/requirements.txt @@ -1,2 +1,6 @@ discord.py==1.7.3 -python-dotenv \ No newline at end of file +python-dotenv~=0.18.0 +pytest~=6.2.4 +dpytest +discord~=1.7.3 +tests~=0.7 \ No newline at end of file diff --git a/JackA/tests.py b/JackA/tests.py new file mode 100644 index 0000000..ba4c5ef --- /dev/null +++ b/JackA/tests.py @@ -0,0 +1,34 @@ +import asyncio + +import discord.ext.test as dpytest +import pytest +from discord.ext import commands + +import main +import ping +import hi + + +@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 bot(event_loop): + bot = commands.Bot("!", loop=event_loop) + dpytest.configure(bot) + print("Starting bot tests") + return bot + + +@pytest.mark.asyncio +async def test_ping_returns_pong(bot): + # send !ping and make sure the bot sends Pong! + print("test") + await dpytest.message("!ping") + assert dpytest.verify().message().contains().content("Ping!") + From 864ca3507437ea5fd070bbaff24fb918774136f9 Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Tue, 20 Jul 2021 14:06:43 +0100 Subject: [PATCH 09/16] Fixed tests --- JackA/main.py | 4 ++-- JackA/tests.py | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/JackA/main.py b/JackA/main.py index a37564a..2ad599b 100644 --- a/JackA/main.py +++ b/JackA/main.py @@ -22,5 +22,5 @@ async def on_ready(self): client.load_extension("hi") client.load_extension("sort") - -client.run(token) +if __name__ == "__main__": + client.run(token) diff --git a/JackA/tests.py b/JackA/tests.py index ba4c5ef..70cefcc 100644 --- a/JackA/tests.py +++ b/JackA/tests.py @@ -1,5 +1,6 @@ import asyncio +import discord import discord.ext.test as dpytest import pytest from discord.ext import commands @@ -8,6 +9,11 @@ import ping import hi +intents = discord.Intents.default() +intents.members = True +intents.guilds = True +intents.messages = True + @pytest.fixture(autouse=True) def ping_cog(bot: commands.Bot): @@ -19,7 +25,7 @@ def ping_cog(bot: commands.Bot): @pytest.fixture(autouse=True) def bot(event_loop): - bot = commands.Bot("!", loop=event_loop) + bot = commands.Bot("!", loop=event_loop, intents=intents) dpytest.configure(bot) print("Starting bot tests") return bot @@ -32,3 +38,4 @@ async def test_ping_returns_pong(bot): await dpytest.message("!ping") assert dpytest.verify().message().contains().content("Ping!") + From d31bcd91b491b06082a85773c972856202f4a9af Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Tue, 20 Jul 2021 14:11:52 +0100 Subject: [PATCH 10/16] Fixed ping test --- JackA/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JackA/tests.py b/JackA/tests.py index 70cefcc..9056816 100644 --- a/JackA/tests.py +++ b/JackA/tests.py @@ -36,6 +36,6 @@ async def test_ping_returns_pong(bot): # send !ping and make sure the bot sends Pong! print("test") await dpytest.message("!ping") - assert dpytest.verify().message().contains().content("Ping!") + assert dpytest.verify().message().contains().content("Pong!") From 5525c3033d60cdbf45e53bb3accee3e439657904 Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Tue, 20 Jul 2021 14:55:50 +0100 Subject: [PATCH 11/16] Added sort test --- JackA/sort.py | 3 +-- JackA/tests.py | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/JackA/sort.py b/JackA/sort.py index 20faed9..6808e2f 100644 --- a/JackA/sort.py +++ b/JackA/sort.py @@ -8,8 +8,7 @@ def __init__(self, client): @commands.command() async def sort(self, ctx, *args): - await ctx.send(str(len(args)) + " argument(s)") - await ctx.send("Sorted arguments: " + ", ".join(sorted(args))) + await ctx.send(str(len(args)) + " argument(s)" + "\n" + "Sorted arguments: " + ", ".join(sorted(args))) def setup(bot): diff --git a/JackA/tests.py b/JackA/tests.py index 9056816..544f1f5 100644 --- a/JackA/tests.py +++ b/JackA/tests.py @@ -8,6 +8,7 @@ import main import ping import hi +import sort intents = discord.Intents.default() intents.members = True @@ -23,6 +24,22 @@ def ping_cog(bot: commands.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) @@ -33,9 +50,22 @@ def bot(event_loop): @pytest.mark.asyncio async def test_ping_returns_pong(bot): - # send !ping and make sure the bot sends Pong! - print("test") 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_hi_without_name_correct_return(bot): +# await dpytest.message("!hi") +# assert dpytest.verify().message().contains().content("hello KoalaJackA!") + +@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") From 6fdb50d40d9e2867da94ce41f66ada274039702e Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Tue, 20 Jul 2021 15:39:12 +0100 Subject: [PATCH 12/16] Added a test for the hey command --- JackA/hi.py | 8 ++++---- JackA/tests.py | 11 +++++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/JackA/hi.py b/JackA/hi.py index 2f065da..cd36389 100644 --- a/JackA/hi.py +++ b/JackA/hi.py @@ -10,10 +10,10 @@ def __init__(self, client): async def hi(self, ctx, arg): await ctx.channel.send("hello " + arg + "!") - @hi.error - async def hi_error(self, ctx, error): - if isinstance(error, commands.MissingRequiredArgument): - await ctx.send("hello " + ctx.author.display_name + "!") + @commands.command() + async def hey(self, ctx): + print("hello " + ctx.author.display_name + "!") + await ctx.send("hello " + ctx.author.display_name + "!") def setup(bot): diff --git a/JackA/tests.py b/JackA/tests.py index 544f1f5..1019f95 100644 --- a/JackA/tests.py +++ b/JackA/tests.py @@ -4,6 +4,7 @@ import discord.ext.test as dpytest import pytest from discord.ext import commands +from discord.ext.commands import MissingRequiredArgument import main import ping @@ -60,12 +61,14 @@ async def test_hi_name_correct_return(bot): assert dpytest.verify().message().contains().content("hello Jack!") -# @pytest.mark.asyncio -# async def test_hi_without_name_correct_return(bot): -# await dpytest.message("!hi") -# assert dpytest.verify().message().contains().content("hello KoalaJackA!") +@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") + From ecdde35ac6fc1329082b0af5d222358835aaf061 Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Wed, 21 Jul 2021 16:02:03 +0100 Subject: [PATCH 13/16] Added inventory manager --- JackA/InventoryManager.py | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 JackA/InventoryManager.py 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)) From f0573f3246308813a26fc255899fd7190ef8c26c Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Thu, 22 Jul 2021 15:05:35 +0100 Subject: [PATCH 14/16] Added new twitter cog with command that displays a user's latest tweet --- JackA/requirements.txt | 3 ++- JackA/twitter.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 JackA/twitter.py diff --git a/JackA/requirements.txt b/JackA/requirements.txt index 28f9566..0ad2ba4 100644 --- a/JackA/requirements.txt +++ b/JackA/requirements.txt @@ -3,4 +3,5 @@ python-dotenv~=0.18.0 pytest~=6.2.4 dpytest discord~=1.7.3 -tests~=0.7 \ No newline at end of file +tests~=0.7 +requests \ No newline at end of file diff --git a/JackA/twitter.py b/JackA/twitter.py new file mode 100644 index 0000000..83ddeaa --- /dev/null +++ b/JackA/twitter.py @@ -0,0 +1,36 @@ +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 request_tweets(self, ctx, arg): + #tweet_fields = "tweet.fields=lang,author_id" + #ids = "ids=1278747501642657792,1255542774432063488" + #url = "https://api.twitter.com/2/tweets?{}&{}".format(ids, tweet_fields) + #tweets = requests.get(url, auth=self.bearer_oauth) + + 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) + await ctx.channel.send(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)) From 4b711c3bd600b25a6d7c2f9913f908ff067e05ab Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Thu, 22 Jul 2021 15:19:47 +0100 Subject: [PATCH 15/16] Tidied up latest_tweet output --- JackA/twitter.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/JackA/twitter.py b/JackA/twitter.py index 83ddeaa..c0f8e4c 100644 --- a/JackA/twitter.py +++ b/JackA/twitter.py @@ -16,16 +16,11 @@ def bearer_oauth(self, r): return r @commands.command() - async def request_tweets(self, ctx, arg): - #tweet_fields = "tweet.fields=lang,author_id" - #ids = "ids=1278747501642657792,1255542774432063488" - #url = "https://api.twitter.com/2/tweets?{}&{}".format(ids, tweet_fields) - #tweets = requests.get(url, auth=self.bearer_oauth) - + 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) - await ctx.channel.send(tweets.json()['data'][0]['text']) + await ctx.channel.send(arg + "'s latest tweet: \n" + tweets.json()['data'][0]['text']) @commands.command() async def tweet(self, ctx, *args): From 3f40b87ab259c8bfdc03ea2256f79a867e81e018 Mon Sep 17 00:00:00 2001 From: Jaddison011 Date: Thu, 22 Jul 2021 15:28:12 +0100 Subject: [PATCH 16/16] Fixed crash when user has no tweets --- JackA/twitter.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/JackA/twitter.py b/JackA/twitter.py index c0f8e4c..a095055 100644 --- a/JackA/twitter.py +++ b/JackA/twitter.py @@ -20,7 +20,10 @@ 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) - await ctx.channel.send(arg + "'s latest tweet: \n" + tweets.json()['data'][0]['text']) + 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):