【问题标题】:Discord.js Client issueDiscord.js 客户端问题
【发布时间】:2021-02-25 18:21:59
【问题描述】:

今天我尝试使用 express 路由器和 discord.js,但出现错误:无法读取未定义的属性“通道”。我不知道问题出在哪里。也许我设置了错误的路径。感谢您的帮助。

//app.js
const express = require('express')
const router = express.Router()
const discord = require('discord.js')
const client = new discord.Client()


router.get('/', (req, res) => {
    res.render('register', {
        title: 'Register'
    })
})


router.get('/Dashboard', (req, res) => {
    var chans = []
    client.guilds.cache.first().channels.cache
        .filter(c => c.type == 'text')
        .forEach(c => {
            chans.push({ id: c.id, name: c.name })
        })

    res.render('dashboard', {
        title: "Dashboard",
        chans,
    })
})
router.post('/sendMessage', (req, res) => {
    var channelid = req.body.channelid
    var text = req.body.text

    if (!channelid || !text)
        return res.sendStatus(400);

    var chan = client.guilds.cache.first().channels.cache.get(channelid)

    if (chan) {
        chan.send(text)
        res.sendStatus(200)
    }
    else
        res.sendStatus(406)
})

module.exports = router

【问题讨论】:

    标签: node.js express routes discord.js router


    【解决方案1】:

    为了使用 Discord 客户端并访问其频道,您必须首先使用您的令牌登录

    await client.login(token_here)
    

    client.login(token_here).then(() => {...})
    

    并且 Tomedic 是正确的,您使用的是 v11 和 v12 语法的混合,如果您阅读 v12 discord.js 文档,您会发现您可以通过这样做直接获取您的频道

    let channel = await client.channels.fetch(channel_id)
    

    client.channels.fetch(channel_id).then((channel) => {...})
    

    【讨论】:

      【解决方案2】:

      替换:

      var chan = client.guilds.cache.first().channels.cache.get(channelid) 
      
      

      用这个:

      var chan = client.guilds.cache.first().channels.cache.fetch(channelid)
      

      【讨论】:

        猜你喜欢
        • 2018-09-25
        • 1970-01-01
        • 2011-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-26
        • 2017-05-13
        • 2011-09-30
        相关资源
        最近更新 更多