所以發生的事情是我正在創建一個不和諧的機器人,它可以從 express 訪問路由,如果我將它保存在某個服務器上,它就可以作業,但是如果我嘗試在服務器運行時在其他服務器上訪問 express,我會得到

重新啟動服務器會洗掉“分配不是功能”,因此這僅在我嘗試從完全不同的服務器訪問這些路由端點時才有效。更多資訊,如果我嘗試使用不同的頻道或公會 ID 資訊向這些端點發出 curl 請求,我也會收到該錯誤。所以我假設這就是很多問題的來源。我不知道為什么不同的資訊會導致功能失效。
索引.js
import express, { json, query, request, response } from 'express';
import { assignments , announcements , discussions} from "./Fetch/fetchCalls.js";
var app = express();
app.use(express.json());
const PORT = 8080;
app.listen(PORT);
var guild;
var channel;
app.all('/assignments',(request) => {assignments(guild = request.body.guild, channel = request.body.channel);});
app.all('/discussions',(request) => {discussions(guild = request.body.guild,channel = request.body.channel); });
app.all('/announcements', (request) => {announcements(guild = request.body.guild,channel = request.body.channel);});
fetchCalls.js
import fetch from 'node-fetch';
import { createRequire } from "module";
import { getRecord, updateChannelID } from './dbUtil.js';
import { clearData } from './clear.js';
const require = createRequire(import.meta.url);
const config = require("../Canvas Bot/Source/Data/config.json")
var obj;
var course;
var url;
var access_token;
var guildid;
var channelid;
export function discussions(guild,channel) {
guildid = guild;
channelid = channel;
discussionsFunc();
async function discussionsFunc(){
try {
updateChannelID(guildid,channelid);
await getRecord({ guild_id : `${guildid}`}, getFetchData);
const res1 = await fetch(url `courses/${course}/discussion_topics?scope=unlocked`, {
method: "GET",
headers: {
Authorization: `Bearer ${access_token}`,
"Content-Type": "application/json",
},
});
const apiData = await res1.json();
for(discussions of apiData){
const string = ["**TOPIC: " discussions.title "**", discussions.message "\n"];
const res2 = await fetch(
`https://discordapp.com/api/channels/${channelid}/messages`,
{
method: "POST",
headers: {
"Authorization": `Bot ${config.TOKEN}`,
Accept: "application/json",
"Content-Type": "application/json",
},
"Connection": "close",
body: JSON.stringify({
content: string.join('\n').replace(/(<([^>] )>)/gi, "")
}),
}
);
const apiResponse = await res2.json();
}
} catch (err) {
console.log(err);
}
}
clearData();
};
export function announcements(guild,channel) {
guildid = guild;
channelid = channel;
announcementsFunc();
async function announcementsFunc(){
updateChannelID(guildid,channelid);
try {
await getRecord({ guild_id : `${guildid}`}, getFetchData);
const res1 = await fetch(url `/announcements?context_codes[]=${obj}&latest_only=true`, {
method: "GET",
headers: {
Authorization: `Bearer ${access_token}` ,
"Content-Type": "application/json",
},
});
const apiData = await res1.json();
console.log(apiData);
for( announcements of apiData){
const string = [`\`\`\`${announcements.title}\n`, `${announcements.message}\n\`\`\``];
const res2 = await fetch(
`https://discordapp.com/api/channels/${channelid}/messages`,
{
method: "POST",
headers: {
"Authorization": `Bot ${config.TOKEN}`,
Accept: "application/json",
"Content-Type": "application/json",
},
"Connection": "close",
body: JSON.stringify({
content: string.join('\n').replace(/(<([^>] )>)/gi, "")
}),
}
);
const apiResponse = await res2.json();
console.log(apiResponse);
}
} catch (err) {
console.log(err);
}
}
clearData();
}
export function assignments(guild, channel) {
guildid = guild;
channelid = channel;
assignmentsFunc();
async function assignmentsFunc(){
updateChannelID(guildid,channelid);
try {
await getRecord({guild_id : `${guildid}`}, getFetchData);
const res1 = await fetch(url `/courses/${course}/assignments?order_by=due_at`, {
method: "GET",
headers: {
Authorization: `Bearer ${access_token}`,
"Content-Type": "application/json",
},
});
const apiData = await res1.json();
console.log(apiData);
var size = apiData.length-1;
for( assignments of apiData){
const string = [`\`\`\`Name: ${assignments.name}`, `Description:\n ${assignments.description}`,`Due Date: ${assignments.due_at}\n\`\`\``];
const res2 = await fetch(
`https://discordapp.com/api/channels/${channelid}/messages`,
{
method: "POST",
headers: {
"Authorization": `Bot ${config.TOKEN}`,
Accept: "application/json",
"Content-Type": "application/json",
},
"Connection": "close",
body: JSON.stringify({
content: string.join('\n').replace(/(<([^>] )>)/gi, "")
}),
}
);
const apiResponse = await res2.json();
console.log(apiResponse);
}
} catch (err) {
console.log(err);
}
}
clearData();
}
function getFetchData(document) {
obj = 'course_' document._courseid;
course = document._courseid;
course1 = "_" course;
url = 'https://' document.prefix '.instructure.com/api/v1/';
access_token = document.access_token;
console.log('obj = ' obj '\ncourse = ' course '\nurl = ' url '\naccess_token = ' access_token);
}
如有必要
資料庫實用程式
import { MongoClient } from 'mongodb';
const uri = 'cluser uri';
const client = new MongoClient(uri)
export async function updateChannelID(guildids, channelids) {
try{
await client.connect();
// db name and collection
const database = client.db("Users");
const docs = database.collection("user_info");
var query = {guild_id: `${guildids}`};
var insert = {$set: {channel_id: `${channelids}`}};
// find the first record matching the given query
docs.updateOne(query,insert);
}
catch(error){
console.log(error);
}
}
uj5u.com熱心網友回復:
問題出在fetchCall.js檔案上。首先,您以錯誤的方式匯出事物。如果你沒有使用像 babel 這樣的工具,語法export function ...不正確,你必須使用module.exports從 Node.js 模塊匯出東西。
例如,您可以通過以下方式更改檔案。
function discussions(guild, channel) {
guildid = guild;
channelid = channel;
discussionsFunc();
async function discussionsFunc() {
try {
updateChannelID(guildid, channelid);
await getRecord({
guild_id: `${guildid}`
}, getFetchData);
const res1 = await fetch(url `courses/${course}/discussion_topics?scope=unlocked`, {
method: "GET",
headers: {
Authorization: `Bearer ${access_token}`,
"Content-Type": "application/json",
},
});
const apiData = await res1.json();
for (discussions of apiData) {
const string = ["**TOPIC: " discussions.title "**", discussions.message "\n"];
const res2 = await fetch(
`https://discordapp.com/api/channels/${channelid}/messages`, {
method: "POST",
headers: {
"Authorization": `Bot ${config.TOKEN}`,
Accept: "application/json",
"Content-Type": "application/json",
},
"Connection": "close",
body: JSON.stringify({
content: string.join('\n').replace(/(<([^>] )>)/gi, "")
}),
}
);
const apiResponse = await res2.json();
}
} catch (err) {
console.log(err);
}
}
clearData();
};
function announcements(guild, channel) {
guildid = guild;
channelid = channel;
announcementsFunc();
async function announcementsFunc() {
updateChannelID(guildid, channelid);
try {
await getRecord({
guild_id: `${guildid}`
}, getFetchData);
const res1 = await fetch(url `/announcements?context_codes[]=${obj}&latest_only=true`, {
method: "GET",
headers: {
Authorization: `Bearer ${access_token}`,
"Content-Type": "application/json",
},
});
const apiData = await res1.json();
console.log(apiData);
for (announcements of apiData) {
const string = [`\`\`\`${announcements.title}\n`, `${announcements.message}\n\`\`\``];
const res2 = await fetch(
`https://discordapp.com/api/channels/${channelid}/messages`, {
method: "POST",
headers: {
"Authorization": `Bot ${config.TOKEN}`,
Accept: "application/json",
"Content-Type": "application/json",
},
"Connection": "close",
body: JSON.stringify({
content: string.join('\n').replace(/(<([^>] )>)/gi, "")
}),
}
);
const apiResponse = await res2.json();
console.log(apiResponse);
}
} catch (err) {
console.log(err);
}
}
clearData();
}
function assignments(guild, channel) {
guildid = guild;
channelid = channel;
assignmentsFunc();
async function assignmentsFunc() {
updateChannelID(guildid, channelid);
try {
await getRecord({
guild_id: `${guildid}`
}, getFetchData);
const res1 = await fetch(url `/courses/${course}/assignments?order_by=due_at`, {
method: "GET",
headers: {
Authorization: `Bearer ${access_token}`,
"Content-Type": "application/json",
},
});
const apiData = await res1.json();
console.log(apiData);
var size = apiData.length - 1;
for (assignments of apiData) {
const string = [`\`\`\`Name: ${assignments.name}`, `Description:\n ${assignments.description}`, `Due Date: ${assignments.due_at}\n\`\`\``];
const res2 = await fetch(
`https://discordapp.com/api/channels/${channelid}/messages`, {
method: "POST",
headers: {
"Authorization": `Bot ${config.TOKEN}`,
Accept: "application/json",
"Content-Type": "application/json",
},
"Connection": "close",
body: JSON.stringify({
content: string.join('\n').replace(/(<([^>] )>)/gi, "")
}),
}
);
const apiResponse = await res2.json();
console.log(apiResponse);
}
} catch (err) {
console.log(err);
}
}
clearData();
}
function getFetchData(document) {
obj = 'course_' document._courseid;
course = document._courseid;
course1 = "_" course;
url = 'https://' document.prefix '.instructure.com/api/v1/';
access_token = document.access_token;
console.log('obj = ' obj '\ncourse = ' course '\nurl = ' url '\naccess_token = ' access_token);
}
module.exports = {assignments, discussions, announcements}
還有另一個重命名函式的建議,函式是動詞,而不是名詞,例如,不要assignments命名它getAssignments或它正在做什么。
更新:
看起來您使用了錯誤的模塊,Node.js 使用了 CommonJS 模塊,因此require除非您使用 TypeScript 或 Babel,否則您必須使用匯入其他模塊。
將您的 index.js 檔案更改為
const express = require('express');
const{ assignments , announcements , discussions}
= require("./Fetch/fetchCalls.js"); var app = express(); app.use(express.json()); 常量埠 = 8080; app.listen(埠);
var guild;
var channel;
app.all('/assignments',(request) => {assignments(guild = request.body.guild, channel = request.body.channel);});
app.all('/discussions',(request) => {discussions(guild = request.body.guild,channel = request.body.channel); });
app.all('/announcements', (request) => {announcements(guild = request.body.guild,channel = request.body.channel);});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/343618.html
上一篇:將值傳遞給EJS
