-
-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Dashboard] Add third parties. #83
base: backend_rewrite
Are you sure you want to change the base?
[Dashboard] Add third parties. #83
Conversation
I tried to integrate two of my cogs with this PR as a third party to Dashboard, and I noticed a small problem.
When Cog-Creators/Red-DiscordBot#5570 is merged into the Red repo, the cog Dashboard will use the new |
I also added a new "Third Parties" page that appears once you log in. |
Type
Description of the changes
Hello,
I tried to integrate myself the third parties in Red-Dashboard. I personally think that every cog should be able to add their own pages to the dashboard, easily, without having to modify the source code of Red-Dashboard or cog Dashboard.
I haven't done any documentation.
How does it work in practice:
On the Red-Dashboard side:
An API endpoint point has been added:
/third_party/<cog_name>/[page]?**[params]
. The local Dashboard cog sends the list of third parties and pages to Red-Dashboard, in theget_variables
RPC method, which is already called at regular intervals. Thus, the code checks if the cog, and the page exist. Depending on the parameters provided by the cog creator, the code will deny requests if the method used is not one of the allowed ones (HEAD
,GET
,OPTIONS
,POST
,PATH
andDELETE
). Ifuser_id
is a required parameter, then the Dashboard will request the OAuth login of the current user. Ifguild_id
is required, then the currentdashboard.html
page will be displayed to allow the choice of a server: the html file has been modified to allow theBASE_GUILD_URL
variable to be changed optionally.user_id
,guild_id
,member_id
,role_id
andchannel_id
are context variables, which should be `int': at the moment, choice is not possible for members, roles and channels, but these parameters could be provided by cogs manually in Discord. If parameters are required, the Dashboard will display an error on the browser. A web request will be sent to the local cog Dashboard which will dispatch the data correctly and get a response.Types of responses from third parties:
The third parties would return to the local cog Dashboard a
dict
like a real RPC method would.Several keys are supported by the API endpoint:
status
: Any request response should have it, but it is not used.web-content
: The Flask/Django/Jinja2 template will be displayed on the browser. It can contain HTML, CSS and JavaScript, and should start with{% extends "base-site.html" %}
to display the base dashboard layout. The variables in the response will be passed in.error_message
: Using the new html fileerror_message.html
, the provided message will be displayed directly to the user, without having to code a different html content.redirect
: The existing template with its name will be displayed. The variables of the response will be passed on.If the request methods are other than
HEAD
andGET
, the data will be returned directly as JSON.\app\base\static\assets\js\plugins\utils.js
:A new JavaScript script has been added as a utility to the Dashboard. It contains several methods that can be called in a template.
$.sendNotification
: Easily display notifications on the Dashboard, with only the type and message as required parameters.$.postData
: Easily send data to the bot with thePOST
method. By default, the current URLwindow.location.href
will be used for the request.$.showTableRegular
: Allows to display the provided data in tabular form.$.generateForm
: Generates a form with many features and supporting all possible inputs (name
,label
,type
,placeholder
,required
,validate
,error
). By default, data is sent to the bot with$.postData
.On the Dashboard local cog side:
A
DashboardRPC_ThirdParties
extension has been added and is accessible atDashboard.rpc.third_parties_handler
. A third party is linked to acommands.Cog
object which must be loaded, in order to be used.The
DashboardRPC_ThirdParties.add_third_party
method must be used to add a cog as a third party. The page parameters are stored inDashboardRPC_ThirdParties.third_parties
.The decorator
dashboard.rpc.thirdparties.dashboard_page
allows to provide parameters for each page. All attributes of the cog class that have a__dashboard_params__
attribute will be automatically added to the Dashboard when the add third party method is called. Context parameters (user_id
/user
,guild_id
/guild
,member_id
/member
,role_id
/role
,channel_id
/channel
) and required parameters are detected in the parameter names.Here are its parameters:
name
:None
so that the user does not have to specify the name to get this page. A name will have the same limitations as the Discord slash command names for ease of use.methods
: The web request methods allowed to call the third party page.required_kwargs
: To manually specify required parameters.permissions_required
: The user's required permissions on the server.hidden
: A parameter not used at this time. Maybe the pages will be listed somewhere someday.The RPC method
DashboardRPC_ThirdParties.data_receive
receives the data from Red-Dashboard for the endpoint API I mentioned earlier. In case, the existence of the third party and the page is checked as new. If the cog is no longer loaded, the request is "refused" with an error message. If acontext_ids
variable is provided (user_id
,guild_id
,member_id
,role_id
orchannel_id
), the code checks if the bot has access to it and if the Discord objects exist. The parametersuser
,guild
,member
,role
andchannel
are then added eventually.The parameters received from the Red-Dashboard (and passed to cogs) are
method
,**context_ids
,**kwargs
andlang_code
. Cogs should use**kwargs
last, as the user (or Flask) is free to add whatever parameters they wish to the pages.Quick cog example:
Minor corrections:
str
.except:
withexcept Exception
.highlight
filter to Flask to allow formatted code to be displayed in multiple languages. (<code class="language-python">' + hljs.highlight('python', item).value + '</code>
)Thank you very much in advance,
Have a nice day,
AAA3A