【问题标题】:如何使用角色制作 dm 命令? / 不和谐 js V12
【发布时间】:2022-01-23 16:40:42
【问题描述】:

我想知道如何使用角色来制作 dm 命令,所以我制作了这段代码,

const { Client, Message, MessageEmbed } = require('discord.js');
//const { join } = require('path');

module.exports = {
    name: 'dm-role',
    /** 
     * @param {Client} client 
     * @param {Message} message 
     * @param {String[]} args 
     */
    run: async(client, message, args) => {

        if(!args[0]) return message.channel.send("who do you want to send a message to?")
        const role = message.mentions.roles.first() || message.guild.roles.cache.get(args[0]);
        if(!user) return message.channel.send("no user found?")
        const reason = args.slice(1).join(" ")
        if(!reason) return message.channel.send("what's the message?");
        try {
            await user.send(reason);
            return message.channel.send("done");

        } catch {
            return message.channel.send("i cant send the dm");
        }



        
       }   
}

好的,所以我尝试了这段代码,但机器人说“我无法发送 dm”,这意味着机器人有错误

我尝试使用此代码“https://stackoverflow.com/a/61618962/16849847” 代码是:

const { Client, Message, MessageEmbed } = require('discord.js');
const { join } = require('path');

module.exports = {
    name: 'dm-role',
    /** 
     * @param {Client} client 
     * @param {Message} message 
     * @param {String[]} args 
     */
    run: async(client, message, args) => {

        if (!args[0]) return message.reply('you need to provide a role')
if (!args[1]) return message.reply('you need to provide a message')
const role = message.mentions.roles.first()
message.guild.roles.cache.get(role.id).members.forEach(member => member.send(args[1]))

        
       }   
}

是的,这段代码有效,但只发送了 1 个句子,下一个句子没有发送, 像这样

我希望你们能帮助我,这取决于你,你想修复第一个代码或第二个代码 谢谢各位!

【问题讨论】:

  • 请注意在第一个 sn-p 中您如何使用 const reason = args.slice(1).join(" ") 而在第二个中您只发送 args[1]
  • args.slice(1).join(" ") 的意思是,删除数组的第一个元素,然后用一个空格将它们连接回字符串。

标签: javascript discord.js


【解决方案1】:

问题与您发送消息的方式有关

您通过发送args[1] 发送消息,这意味着您只会发送第一个字。

要解决这个问题,请在第一个代码 sn-p 中发送消息内容:

// Replace args[1] with args.slice(1).join(" ")
message.guild.roles.cache.get(role.id).members.forEach(member => member.send(args.slice(1).join(" ")))

【讨论】:

  • 哇,它成功了兄弟,谢谢兄弟
猜你喜欢
  • 1970-01-01
  • 2020-08-26
  • 2020-08-20
  • 2020-10-24
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
相关资源
最近更新 更多