簡體   English   中英

如何使用 Discord.JS DM 一些用戶?

[英]How to DM some user using Discord.JS?

我遇到了一個關於 DM 特定用戶的問題。

我的機器人中的這個命令基本上將今天的日期與任務的日期進行比較,如果他的截止日期臨近或已過期,它應該通知用戶這個任務。 因此,我將useralert字段與用戶 ID 一起使用,然后將其轉換為數字 ID ( useralertID )。 當 if 語句為真時,應向具有此 ID 的用戶發送 On if 語句消息。

按照“Discord.js Guide”的說明,我定義了一個用戶let user = bot.users.cache.get('useralertID'); 並向該用戶發送消息user.send('Works;'); .

不幸的是,它不是向用戶發送消息,而是輸出一個未定義的值或UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined

我無法弄清楚這個問題,所以任何建議都會非常有幫助!

這是代碼。 提前致謝!

 var i; var d = new Date; var month = d.getMonth() + 1; var day = d.getDate(); const s = await Tags.count(); if (message.member.hasPermission('KICK_MEMBERS')) { for (i = 1; i <= s; i++) { const tag = await Tags.findOne({ where: { key: i } }); if (tag) { var date = tag.get('description'); let useralert = (tag.get("usernameid")).toString(); let useralertID = useralert.replace(/[<@>]/g, ''); let deadday = parseInt(date.slice(0, 2)); let deadmonth = parseInt(date.slice(3, 5)); let dayn = deadday - day; let monthn = deadmonth - month; console.log(dayn.toString() + ' ' + monthn.toString() + ' ' + useralertID); if (((dayn <= 2) && (monthn == 0)) || (monthn < 0)) { let user = bot.users.cache.get('useralertID'); user.send('Works;'). return message.channel.send(`Sent msg to ${user;username}!`); } } }

client.users它唯一被客戶端緩存的用戶。 因此,如果您重新啟動機器人,此集合將為空。 您需要將消息發送到您的機器人或將消息發送到機器人可以處理此消息以緩存在此集合中的通道。 你的方式,如果你在公會上運行這個命令或者如果這個用戶是你公會的成員,你可以使用guild.members.cache.get('ID HERE')或者嘗試通過 id 獲取公會,然后在這個公會中獲取用戶他的身份證。

一些例子:


var i;
var d = new Date;
var month = d.getMonth() + 1;
var day = d.getDate();
const s = await Tags.count();
if (message.member.hasPermission('KICK_MEMBERS')) {

}
    for (i = 1; i <= s; i++) {
        const tag = await Tags.findOne({
            where: {
                key: i
            }
        });
        if (tag) {
            var date = tag.get('description');
            let useralert = (tag.get("usernameid")).toString();
            let useralertID = useralert.replace(/[<@>]/g, '');
            let deadday = parseInt(date.slice(0, 2));
            let deadmonth = parseInt(date.slice(3, 5));
            let dayn = deadday - day;
            let monthn = deadmonth - month;
            console.log(dayn.toString() + ' ' + monthn.toString() + ' ' + useralertID);
            if (((dayn <= 2) && (monthn == 0)) || (monthn < 0)) {
                let user = message.guild.members.cache.get(useralertID);
                if (user) {
                    user.send('Works!').then(() => {
                        return message.channel.send(`Sent msg to ${user.username}!`);
                    }).catch(() => {
                        return message.channel.send(`${user.username} not allow to send DM message!`);
                    })
                } else {
                    return message.channel.send(`the ${useralertID} not guild Member, can\`t send DM to him`);
                }

            }
        }
    }

發生錯誤是因為user不存在或者是 null。 也許嘗試bot.users.cache.get("YOURIDHERE").send("hi")
我真的無法為您提供任何進一步的幫助,因為您沒有告訴我們您要做什么。

那么問題是用戶像內部一樣未定義,錯誤說,

let user = bot.users.cache.get('useralertID')

您在此處使用字符串而不是變量useralertID

無論您是否應該檢查用戶是否存在,因為您是從間接的東西中提取 id,不確定Tags是什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM