Skip to content

Commit

Permalink
Develop (#31)
Browse files Browse the repository at this point in the history
* v1.7.6 (#28)

* EMBED, documentation

* from_discord_embed, documentation

* fixed methods from base class being referenced to inherited class

* Fixed EMBED.from_discord_embed method copying base class methods

* Better error reporting (TRACE)

* embed copy fix

* Better error checking, get_client() function

* Better error reporting

* README

* v1.7.6 - WIP (#29)

* Improved error reporting, retreive client object

* removed warning

* v1.7.6 (#30)

# What's changed
- get_client()
  - Gives returns discord.Client object
- Improved logging
- Added send modes : ["send", "edit", "clear-send"]
- Update Examples w/ application layer example
  • Loading branch information
davidhozic committed Feb 20, 2022
1 parent 7797d63 commit e2c79d5
Show file tree
Hide file tree
Showing 155 changed files with 4,018 additions and 1,191 deletions.
Binary file removed DOC_src/framework_server_log_1.png
Binary file not shown.
Binary file removed DOC_src/function_as_data_parameter_1.png
Binary file not shown.
Binary file removed DOC_src/function_decorator_1.png
Binary file not shown.
29 changes: 29 additions & 0 deletions Examples/Additional Application Layer Example/Coffee/app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import framework, datetime, os, random



already_sent = False
randomized_images = []

IMAGE_PATH = "./app/images/"

@framework.data_function
def get_data():
global already_sent, randomized_images
datum=datetime.datetime.now()
if datum.hour == 10 and not already_sent:
already_sent = True
if not randomized_images:
found_images = [os.path.join(IMAGE_PATH,x) for x in os.listdir("./app/images")]
while found_images:
randomized_images.append(found_images.pop(random.randrange(0,len(found_images))))
image = randomized_images.pop(0)
text = \
"""\
Good morning @everyone\nDate: {:02d}.{:02d}.{:02d} - {:02d}:{:02d}\
""".format(datum.day,datum.month,datum.year,datum.hour,datum.minute)
return text, framework.FILE(image) # Return message to be sent

elif datum.hour == 11 and already_sent:
already_sent = False
return None # Return None if nothing is to be send
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Description:
This is an example of an additional application layer you can build with this framework.
The application sends a message saying 'Good morning' every day at 10 AM and then sends a picture of a coffe cup from a randomized list.
"""
import framework as fw
import app.app
from framework import discord




servers = [
fw.GUILD(
guild_id=123456789,
messages_to_send=[

fw.MESSAGE(start_period=None, end_period=10, data=app.app.get_data(), channel_ids=[123456789], mode="send", start_now=True)
],
generate_log=True
)
]


############################################################################################
fw.run( token="YOUR TOKEN", server_list=servers)


16 changes: 8 additions & 8 deletions Examples/main_data_function.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import framework, datetime, secret

from framework import discord



############################################################################################
# GUILD MESSAGES DEFINITION #
############################################################################################

# VERY IMPORTANT that you use @framework.FUNCTION to convert your function into a __FUNCTION_CLS__ class which creates an object when you "call" it below
# It's VERY IMPORTANT that you use @framework.data_function to convert your function into a __FUNCTION_CLS__ class which creates an object when you "call" it below
#######################################


@framework.FUNCTION
@framework.data_function
def get_data(parameter):
return f"Parameter: {parameter}\nTimestamp: {datetime.datetime.now()}"

l_time = datetime.datetime.now()
return f"Parameter: {parameter}\nTimestamp: {l_time.day}.{l_time.month}.{l_time.year} :: {l_time.hour}:{l_time.minute}:{l_time.second}"

guilds = [
framework.GUILD(
Expand All @@ -28,21 +28,21 @@ def get_data(parameter):
# or function that returns any of above types(or returns None if you don't have any data to send yet),
# where if you pass a function you need to use the framework.FUNCTION decorator on top of it ).
channel_ids=[123456789], # List of ids of all the channels you want this message to be sent into
clear_previous=True, # Clear all discord messages that originated from this MESSAGE object
mode="send", # Clear all discord messages that originated from this MESSAGE object
start_now=True # Start sending now (True) or wait until period
),
],
generate_log=True ## Generate file log of sent messages (and failed attempts) for this server
)
]

############################################################################################

if __name__ == "__main__":
framework.run( token=secret.C_TOKEN, # MANDATORY,
server_list=guilds, # MANDATORY
is_user=False, # OPTIONAL
user_callback=None, # OPTIONAL
server_log_output="Logging", # OPTIONAL
server_log_output="History", # OPTIONAL
debug=True) # OPTIONAL

66 changes: 47 additions & 19 deletions Examples/main_send_embed.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,71 @@
import framework, secret

import framework as fw, secret
from framework import discord

############################################################################################
# EMBED VARIABLE DEFINITON #
############################################################################################
# NOTE! There can only be one embed per message but you can add more fields inside that embed!
test_embed = framework.EMBED(


# framework.EMBED example
test_embed1 = fw.EMBED(
author_name="Developer",
author_image_url="https://solarsystem.nasa.gov/system/basic_html_elements/11561_Sun.png",
author_icon="https://solarsystem.nasa.gov/system/basic_html_elements/11561_Sun.png",
fields=\
[
framework.EMBED_FIELD("Test 1", "Hello World", True),
framework.EMBED_FIELD("Test 2", "Hello World 2", True),
framework.EMBED_FIELD("Test 3", "Hello World 3", True),
framework.EMBED_FIELD("No Inline", "This is without inline", False),
framework.EMBED_FIELD("Test 4", "Hello World 4", True),
framework.EMBED_FIELD("Test 5", "Hello World 5", True)
]
fw.EMBED_FIELD("Test 1", "Hello World", True),
fw.EMBED_FIELD("Test 2", "Hello World 2", True),
fw.EMBED_FIELD("Test 3", "Hello World 3", True),
fw.EMBED_FIELD("No Inline", "This is without inline", False),
fw.EMBED_FIELD("Test 4", "Hello World 4", True),
fw.EMBED_FIELD("Test 5", "Hello World 5", True)
],
## ... for other arguments, see https://github.com/davidhozic/discord-advertisement-framework
)


# pycord (discord.py) Embed
test_embed2 = fw.discord.Embed(
color= fw.discord.Color.dark_orange(),
title="Test Embed Title",
description="This is a discord embed",
# ... other, refer to Pycord documentation
)

# framework.EMBED from discord.Embed
test_embed_fw_2 = fw.EMBED.from_discord_embed(test_embed2) ## Converts discord.Embed into framework.EMBED



############################################################################################
# GUILD MESSAGES DEFINITION #
############################################################################################
guilds = [
framework.GUILD(
fw.GUILD(
guild_id=123456789, # ID of server (guild)
messages_to_send=[ # List MESSAGE objects
framework.MESSAGE(
fw.MESSAGE(
start_period=None, # If None, messages will be send on a fixed period (end period)
end_period=15, # If start_period is None, it dictates the fixed sending period,
# If start period is defined, it dictates the maximum limit of randomized period
data=test_embed, # Data you want to sent to the function (Can be of types : str, embed, file, list of types to the left
data=test_embed1, # Data you want to sent to the function (Can be of types : str, embed, file, list of types to the left
# or function that returns any of above types(or returns None if you don't have any data to send yet),
# where if you pass a function you need to use the framework.FUNCTION decorator on top of it ).
# where if you pass a function you need to use the fw.FUNCTION decorator on top of it ).
channel_ids=[123456789], # List of ids of all the channels you want this message to be sent into
clear_previous=True, # Clear all discord messages that originated from this MESSAGE object
mode="send", # Clear all discord messages that originated from this MESSAGE object
start_now=True # Start sending now (True) or wait until period
),
),

fw.MESSAGE(
start_period=None,
end_period=15,

data=test_embed_fw_2,

channel_ids=[123456789],
mode="send",
start_now=True
),
],
generate_log=True ## Generate file log of sent messages (and failed attempts) for this server
)
Expand All @@ -46,9 +74,9 @@
############################################################################################

if __name__ == "__main__":
framework.run( token=secret.C_TOKEN, # MANDATORY,
fw.run( token=secret.C_TOKEN, # MANDATORY,
server_list=guilds, # MANDATORY
is_user=False, # OPTIONAL
user_callback=None, # OPTIONAL
server_log_output="Logging", # OPTIONAL
server_log_output="History", # OPTIONAL
debug=True) # OPTIONAL
6 changes: 3 additions & 3 deletions Examples/main_send_file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import framework, secret

from framework import discord



Expand All @@ -22,7 +22,7 @@
# or function that returns any of above types(or returns None if you don't have any data to send yet),
# where if you pass a function you need to use the framework.FUNCTION decorator on top of it ).
channel_ids=[123456789], # List of ids of all the channels you want this message to be sent into
clear_previous=True, # Clear all discord messages that originated from this MESSAGE object
mode="send", # Clear all discord messages that originated from this MESSAGE object
start_now=True # Start sending now (True) or wait until period
),
],
Expand All @@ -37,6 +37,6 @@
server_list=guilds, # MANDATORY
is_user=False, # OPTIONAL
user_callback=None, # OPTIONAL
server_log_output="Logging", # OPTIONAL
server_log_output="History", # OPTIONAL
debug=True) # OPTIONAL

8 changes: 4 additions & 4 deletions Examples/main_send_multiple.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import framework, secret

from framework import discord



Expand All @@ -14,7 +14,7 @@
## Embedded
l_embed = framework.EMBED(
author_name="Developer",
author_image_url="https://solarsystem.nasa.gov/system/basic_html_elements/11561_Sun.png",
author_icon="https://solarsystem.nasa.gov/system/basic_html_elements/11561_Sun.png",
fields=\
[
framework.EMBED_FIELD("Test 1", "Hello World", True),
Expand All @@ -40,7 +40,7 @@
l_file2, # where if you pass a function you need to use the framework.FUNCTION decorator on top of it ).
l_embed],
channel_ids=[123456789], # List of ids of all the channels you want this message to be sent into
clear_previous=True, # Clear all discord messages that originated from this MESSAGE object
mode="send", # Clear all discord messages that originated from this MESSAGE object
start_now=True # Start sending now (True) or wait until period
),
],
Expand All @@ -56,5 +56,5 @@
server_list=guilds, # MANDATORY
is_user=False, # OPTIONAL
user_callback=None, # OPTIONAL
server_log_output="Logging", # OPTIONAL
server_log_output="History", # OPTIONAL
debug=True) # OPTIONAL
8 changes: 4 additions & 4 deletions Examples/main_send_string.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import framework, secret

from framework import discord



Expand All @@ -19,10 +19,10 @@
# or function that returns any of above types(or returns None if you don't have any data to send yet),
# where if you pass a function you need to use the framework.FUNCTION decorator on top of it ).
channel_ids=[123456789], # List of ids of all the channels you want this message to be sent into
clear_previous=True, # Clear all discord messages that originated from this MESSAGE object
mode="send", # Clear all discord messages that originated from this MESSAGE object
start_now=True # Start sending now (True) or wait until period
),
framework.MESSAGE(start_period=5, end_period=10, data="Second Message", channel_ids=[12345], clear_previous=True, start_now=True)
framework.MESSAGE(start_period=5, end_period=10, data="Second Message", channel_ids=[12345], mode="send", start_now=True)
],
generate_log=True ## Generate file log of sent messages (and failed attempts) for this server
)
Expand All @@ -35,5 +35,5 @@
server_list=guilds, # MANDATORY
is_user=False, # OPTIONAL
user_callback=None, # OPTIONAL
server_log_output="Logging", # OPTIONAL
server_log_output="History", # OPTIONAL
debug=True) # OPTIONAL
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
**Shiller Licence:**<br>
**Discord Advertisement Framework Licence:**<br>
MIT License

Copyright (c) 2022 David Hozic
Expand Down
85 changes: 85 additions & 0 deletions Patches/http.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
diff --git a/Patches/http_rate_limit.patch b/Patches/http_rate_limit.patch
deleted file mode 100644
index 757b434..0000000
--- a/Patches/http_rate_limit.patch
+++ /dev/null
@@ -1,40 +0,0 @@
-diff --git a/discord/http.py b/discord/http.py
-index 3f63a75d..19fe6959 100644
---- a/discord/http.py
-+++ b/discord/http.py
-@@ -211,33 +211,9 @@ class HTTPClient:
-
- # we are being rate limited
- if r.status == 429:
-- if not r.headers.get('Via'):
-- # Banned by Cloudflare more than likely.
-- raise HTTPException(r, data)
--
-- fmt = 'We are being rate limited. Retrying in %.2f seconds. Handled under the bucket "%s"'
--
-- # sleep a bit
-- retry_after = data['retry_after'] / 1000.0
-- log.warning(fmt, retry_after, bucket)
--
-- # check if it's a global rate limit
-- is_global = data.get('global', False)
-- if is_global:
-- log.warning('Global rate limit has been hit. Retrying in %.2f seconds.', retry_after)
-- self._global_over.clear()
--
-- await asyncio.sleep(retry_after)
-- log.debug('Done sleeping for the rate limit. Retrying...')
--
-- # release the global lock now that the
-- # global rate limit has passed
-- if is_global:
-- self._global_over.set()
-- log.debug('Global rate limit is now over.')
--
-- continue
-+ raise HTTPException(r, data)
-
-+
- # we've received a 500 or 502, unconditional retry
- if r.status in {500, 502}:
- await asyncio.sleep(1 + tries * 2)
diff --git a/src/_discord/http.py b/src/_discord/http.py
index cdefd1f..4e6ecd5 100644
--- a/src/_discord/http.py
+++ b/src/_discord/http.py
@@ -295,32 +295,7 @@ class HTTPClient:

# we are being rate limited
if response.status == 429:
- if not response.headers.get('Via') or isinstance(data, str):
- # Banned by Cloudflare more than likely.
- raise HTTPException(response, data)
-
- fmt = 'We are being rate limited. Retrying in %.2f seconds. Handled under the bucket "%s"'
-
- # sleep a bit
- retry_after: float = data['retry_after']
- _log.warning(fmt, retry_after, bucket)
-
- # check if it's a global rate limit
- is_global = data.get('global', False)
- if is_global:
- _log.warning('Global rate limit has been hit. Retrying in %.2f seconds.', retry_after)
- self._global_over.clear()
-
- await asyncio.sleep(retry_after)
- _log.debug('Done sleeping for the rate limit. Retrying...')
-
- # release the global lock now that the
- # global rate limit has passed
- if is_global:
- self._global_over.set()
- _log.debug('Global rate limit is now over.')
-
- continue
+ raise HTTPException(response, data)

# we've received a 500, 502, or 504, unconditional retry
if response.status in {500, 502, 504}:

Loading

0 comments on commit e2c79d5

Please sign in to comment.