-
-
Notifications
You must be signed in to change notification settings - Fork 45
[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_variablesRPC 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,PATHandDELETE). Ifuser_idis a required parameter, then the Dashboard will request the OAuth login of the current user. Ifguild_idis required, then the currentdashboard.htmlpage will be displayed to allow the choice of a server: the html file has been modified to allow theBASE_GUILD_URLvariable to be changed optionally.user_id,guild_id,member_id,role_idandchannel_idare 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
dictlike 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
HEADandGET, 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 thePOSTmethod. By default, the current URLwindow.location.hrefwill 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_ThirdPartiesextension has been added and is accessible atDashboard.rpc.third_parties_handler. A third party is linked to acommands.Cogobject which must be loaded, in order to be used.The
DashboardRPC_ThirdParties.add_third_partymethod 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_pageallows 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:Noneso 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_receivereceives 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_idsvariable is provided (user_id,guild_id,member_id,role_idorchannel_id), the code checks if the bot has access to it and if the Discord objects exist. The parametersuser,guild,member,roleandchannelare then added eventually.The parameters received from the Red-Dashboard (and passed to cogs) are
method,**context_ids,**kwargsandlang_code. Cogs should use**kwargslast, 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.highlightfilter 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