简体   繁体   中英

Discord.js image attachment

const { Client, Intents, MessageAttachment, Message } = require('discord.js');
const client = new Client({intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_PRESENCES", "GUILD_MEMBERS"]});
client.once('ready', () => {
    console.log('In sfarsit joc DnD. Ssssssp.');
});
let x = 1;

client.on('message', msg =>
{
   
    if (msg.content === "+help")
    {
        let msg1 = "You should know how to use me by now. Sssssssp. Times you were unworthy: ";
        let msg2 = x.toString();
        let msgf = msg1.concat(msg2);
        x++;
        msg.reply(msgf);
    }
    name = ["Triceps","Babuska","Dero","Special","Radw","Pucin","Eclipsentiu","Eugen","Tugen","Elgen","Ha Ha Bitch","Helium", "Saradin", "Chevrolet", "Gogo", "Cactus", "Sarkofag", "Guru", "Trubius", "Storyline", "Giorgio", "Wiggler", "Wig", "Stetoskop", "Tracu", "Mizil", "Trafalet", "Stepmom", "Yridim", "Soul","Lyfjaberg","Netflix","Gugutza","Rakshasa","Trubilion","Sufftard","Panu","Radulescu","Infatuated","Sex'n'Fund","Tuc","Streche","Nebanuit","Bust","Ecologie","Surf","Bag","0110","Pork","Fork","Redbuff"];
    let no_changed = 0;
    if (msg.content === "+exhale_the_truth")
    {
        if (Math.random() <= 0.05)
        {
            msg.reply("No. I don't think I will. Sssssssp.");
            return;
        }
        msg.reply("Look what you made me do. Sssssssp.");
        msg.guild.members.cache.forEach((a) => {
            if (!a.permissions.has("ADMINISTRATOR"))
            {
                do {
                    nick = "";
                    if (Math.random() <= 0.08)
                        nick = "El ";
                    nick = nick + name[Math.floor(Math.random() * name.length)] + ' ' + name[Math.floor(Math.random() * name.length)] + ' ';
                    if (Math.random() <= 0.02)
                        nick = nick + "d'";
                    else
                        if (Math.random() <= 0.001)
                            nick = nick + "von";
                    nick = nick + name[Math.floor(Math.random() * name.length)];

                }
                while (nick.length > 32)
                a.setNickname(nick);
                console.log(nick);
                no_changed++;
            }
        });
        console.log(no_changed);
       
    }
    if (msg.content === "+DnD") {
         var attc = new MessageAttachment('https://upload.wikimedia.org/wikipedia/commons/f/ff/Stefan_Banica_Jr.jpg');
        msg.reply(attc);
    }

});
client.login('token');

At

  if (msg.content === "+DnD") {
         var attc = new MessageAttachment('https://upload.wikimedia.org/wikipedia/commons/f/ff/Stefan_Banica_Jr.jpg');
        msg.reply(attc);
    }

Apparently this attempts to send an empty message. I just want to attach an image. It could be a path from my computer too, I don't really think I care about how it is provided or that there is a difference in the implementation. Discord.js v13 btw. Also I would prefer a pretty straightforward solution, without using too many complicated concepts if possible. Obviously anything will do.

Use messagw.reply({files: [attc]}) And i suggest use messageCreate event instead of message

The reason the reply is empty is that Message.reply() does not take a MessageAttachement. It takes string, MessagePayload, or ReplyMessageOptions.

You'll have to build a MessagePayload with files attached.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM