Skip to content

Commit

Permalink
feat: basic discord login process and minor
Browse files Browse the repository at this point in the history
  • Loading branch information
LoganTann committed Jul 1, 2022
1 parent 0e5c623 commit ba1fb75
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/fileloader.ts
framework/todo.md
database/db.sqlite
redbeansoup-v2.wiki/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

This is a rewriting of [redbeansoup-bot](https://github.com/LoganTann/redbeansoup-bot) with much more modules.

**Information** as I am the only one working in this project, there is no branch strategy. Dev code will be pushed as-is to the main branch. It is not recommanded to touch the code yet.

## Run the project in your hardware

See the dedicated file [doc: Run the project](documentation/run.md)
Expand Down
6 changes: 3 additions & 3 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// todo upgrade to rc45
// Important : See import maps to see all the dependencies. This file only export discordeno-related stuff.

// discordeno lib
export * from "https://deno.land/x/[email protected]rc31/mod.ts";
export * from "https://deno.land/x/[email protected]rc45/mod.ts";

// discordeno plugin
export * from "https://deno.land/x/[email protected]rc31/plugins/mod.ts";
export * from "https://deno.land/x/[email protected]rc45/plugins/mod.ts";

// Terminal Colors!
export * from "https://deno.land/[email protected]/fmt/colors.ts";
3 changes: 0 additions & 3 deletions deps/aqua.ts

This file was deleted.

1 change: 0 additions & 1 deletion deps/denodb.ts

This file was deleted.

8 changes: 0 additions & 8 deletions deps/discordeno.ts

This file was deleted.

10 changes: 5 additions & 5 deletions import_map.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"imports": {
"discordeno": "./deps/discordeno.ts",
"denodb": "./deps/denodb.ts",
"aqua": "./deps/aqua.ts",
"denodb": "https://raw.githubusercontent.com/Aiko-Suzuki/denodb/master/mod.ts",
"aqua": "https://deno.land/x/[email protected]/mod.ts",
"discordeno": "./deps.ts",
"config": "./config.ts",
"framework/": "./framework/",
"db": "./database/mod.ts"
"db": "./database/mod.ts",
"framework/": "./framework/"
}
}
17 changes: 7 additions & 10 deletions src/utils/MeteoFrance/MeteoFrance.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import {
Forecast,
ForecastResponse,
iconToEmoji,
RainResponse,
unixTimestamp,
} from "./types.ts";
import EmbedField from "../../types/embedField.ts";
import fetchWithType from "../../../framework/utils/fetchWithType.ts";
import { ForecastResponse, RainResponse } from "./types.ts";
import fetchWithType from "framework/utils/fetchWithType.ts";

/**
* Partial Typescript port of hacf-fr's meteofrance-api python client.
Expand All @@ -18,8 +11,12 @@ import fetchWithType from "../../../framework/utils/fetchWithType.ts";
* @author LoganTann
*/
export default class MeteoFranceClient {
/**
* This token is graciously shared by Home Assistant Communauté Francophone.
* Please do not abuse it.
*/
static meteoFranceApiToken =
"__Wj7dVSTjV9YGu1guveLyDq0g7S7TfTjaHBTPTpO0kj8__"; // is that a leaked token ? owo
"__Wj7dVSTjV9YGu1guveLyDq0g7S7TfTjaHBTPTpO0kj8__";
static meteoFranceApiEndpoint = "https://webservice.meteofrance.com";
static defaultCoord = {
// iut.paris
Expand Down
4 changes: 4 additions & 0 deletions src/utils/OpenAI/OpenAiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export function generateUsageChart(
if (!usage.data || usage.data.length <= 0) {
return "Error : no data : " + JSON.stringify(usage);
}
if (!usage.amount_grant_used_usd || !usage.amount_granted_usd) {
// as of 2022-07-01, 100% sure user will see this error.
return "Error : api might have changed, unable to fetch amount";
}
const bar = createProgressBar(
48,
usage.amount_grant_used_usd,
Expand Down
30 changes: 30 additions & 0 deletions web-back/login/_discord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import env from "config";

export default class DiscordUtils {
public static readonly oauthRedirectUrl =
"https://discord.com/api/oauth2/authorize?client_id=942097556371021924&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fapi%2Flogin&response_type=code&scope=identify%20guilds";
public static readonly redirectUri = "http://localhost:3000/api/login";
public static readonly API_ENDPOINT = "https://discord.com/api/v10";

static async exchangeCode(code: string) {
const data = {
client_id: env.CLIENT_ID,
client_secret: env.CLIENT_SECRET,
grant_type: "authorization_code",
code: code,
redirect_uri: DiscordUtils.redirectUri,
};
const headers = {
"Content-Type": "application/x-www-form-urlencoded",
};
const request = await fetch(
DiscordUtils.API_ENDPOINT.concat("/oauth2/token"),
{
headers: headers,
method: "POST",
body: new URLSearchParams(data).toString(),
}
);
return await request.json();
}
}
20 changes: 20 additions & 0 deletions web-back/login/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import app from "../app.ts";
import { jsonResponse } from "../utils.ts";
import DiscordUtils from "./_discord.ts";
const oauthRedirectUrl =
"https://discord.com/api/oauth2/authorize?client_id=942097556371021924&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fapi%2Flogin&response_type=code&scope=identify%20guilds";

app.get("/api/login", async (req) => {
const { code } = req.query;
if (!code) {
return jsonResponse(400, { redirect: oauthRedirectUrl });
}
const result = await DiscordUtils.exchangeCode(code);
// const request = await fetch("https://discord.com/api/users/@me", {
// headers: {
// authorization: `${tokenType} ${accessToken}`,
// },
// });
// const result = await request.json();
return jsonResponse(200, result);
});
28 changes: 9 additions & 19 deletions web-back/lore/[name].ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import { RoutingOptions, mustExist, Response } from "aqua";
import { mustExist } from "aqua";
import app from "../app.ts";

import { jsonResponse } from "../utils.ts";
import { loreRepo } from "db";

function buildResponse(code: number, body: object) {
return {
statusCode: code,
headers: {
"Content-Type": "application/json",
},
content: JSON.stringify(body),
};
}

app.get(
"/api/lore/:name",
async (req) => {
const name = req.parameters.name;
const results = await loreRepo.getLore(name);
if (!results) {
return buildResponse(404, { error: "No matches found" });
return jsonResponse(404, { error: "No matches found" });
}
return buildResponse(200, results);
return jsonResponse(200, results);
},
{
schema: {
Expand All @@ -40,16 +30,16 @@ app.put(
const description = req.body.description as string;
const title = req.body.title as string;
if (!description || !title) {
return buildResponse(404, {
return jsonResponse(404, {
error: "Missing description or title",
});
}

const results = await loreRepo.upsertLore(name, title, description);
if (!results) {
return buildResponse(404, { error: "Upsert failed." });
return jsonResponse(404, { error: "Upsert failed." });
}
return buildResponse(200, results);
return jsonResponse(200, results);
},
{
schema: {
Expand All @@ -68,9 +58,9 @@ app.delete(
const name = req.parameters.name;
const results = await loreRepo.deleteLore(name);
if (results) {
return buildResponse(200, { success: true });
return jsonResponse(200, { success: true });
}
return buildResponse(404, {
return jsonResponse(404, {
error: "Delete failed. Maybe the entry you requested doesn't exist.",
});
},
Expand Down
1 change: 1 addition & 0 deletions web-back/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import "./lore/[name].ts";
import "./login/index.ts";
12 changes: 12 additions & 0 deletions web-back/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function jsonResponse(
code: number,
body: Record<string, unknown> | Array<unknown>
) {
return {
statusCode: code,
headers: {
"Content-Type": "application/json",
},
content: JSON.stringify(body),
};
}

0 comments on commit ba1fb75

Please sign in to comment.