亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用不和諧.JS的DM一些用戶?

如何使用不和諧.JS的DM一些用戶?

至尊寶的傳說 2022-09-23 10:01:31
我遇到了有關對特定用戶進行 DM 的問題。我的機器人中的此命令基本上將今天的日期與分配的日期進行比較,并且如果用戶的截止日期臨近或已過期,它應該通知具有此分配的用戶。因此,我獲取帶有用戶 ID 的字段,然后將其轉換為數字 ID ()。On if 語句消息應發送給具有此 ID 的用戶,當 if 語句為 true 時。useralertuseralertID按照“不和諧.js指南的指示,我定義了一個用戶并向該用戶發送了一條消息。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}!`);      }    }  }
查看完整描述

3 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

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


一些例子:


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`);

                }


            }

        }

    }


查看完整回答
反對 回復 2022-09-23
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

發生錯誤的原因是 不存在或為 null。也許試試.
我真的不能再幫你了,因為你沒有告訴我們你想做什么。userbot.users.cache.get("YOURIDHERE").send("hi")

查看完整回答
反對 回復 2022-09-23
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

好吧,問題是用戶是未定義的,就像內部和錯誤說的那樣,

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

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

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


查看完整回答
反對 回復 2022-09-23
  • 3 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號