簡體   English   中英

發布自定義歡迎消息的 Discord 機器人

[英]Discord bot that posts custom welcome message

我希望我的機器人在“歡迎”文本頻道中為每個新成員發布一條簡單的歡迎消息。

我在這里和那里閱讀了很多帖子,但仍然無法按預期工作

這是代碼:

console.log('Beep Boop Beep'); //prints 'Beep Boop Beep'

require('dotenv').config(); //load the dotenv node pachage and call a config() func to load thea values from .env

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

//EDIT: const client = new Client()
const client = new Client({ ws: { intents: [
    'GUILDS',
    'GUILD_MESSAGES',
    'GUILD_PRESENCES',
    'GUILD_MEMBERS'
] } });

const embed = new MessageEmbed()
    .setTitle("DM thing")
    .setColor(0xFF0000)
    .setDescription("Add describtion here")

client.login(process.env.BOTTOKEN); 

module.exports = {client, embed};

function readyDiscord() {
    console.log('readyDiscord func executed. Discord is ready.'); 
}

client.on('ready', readyDiscord);

const commandHandler = require('./commands'); 

client.on('message', commandHandler);

//EDIT: 'guildMembersAdd'
client.on('guildMemberAdd', async member => { 
    console.log(member)

    const message = `Hey, <@${member.id}>! Welcome. DM me if anytime you want.`
    
    //EDIT: const channel = member.guild.channels.cache.get(WELCOME_CHANNEL_ID)
    const channel = await 

//EDIT: pocess.env
member.guild.channels.resolve(process.env.WELCOME_CHANNEL_ID);
    channel.send(message)
})

PRESENCE INTENT 和 SERVER MEMBERS INTENT 已啟用。 每次我嘗試添加測試帳戶機器人時都會吐出這些錯誤:

錯誤:

(base) admin@MacBook-Pro-11 discord_binder % node bot.js
Beep Boop Beep
readyDiscord func executed. Discord is ready.
GuildMember {
  guild: <ref *1> Guild {
    members: GuildMemberManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]],
      guild: [Circular *1]
    },
    channels: GuildChannelManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]],
      guild: [Circular *1]
    },
    roles: RoleManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]],
      guild: [Circular *1]
    },
    presences: PresenceManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]]
    },
    voiceStates: VoiceStateManager {
      cacheType: [class Collection extends Collection],
      cache: Collection(0) [Map] {},
      guild: [Circular *1]
    },
    deleted: false,
    available: true,
    id: '779767191012638721',
    shardID: 0,
    name: 'testovoe_nazvaniye',
    icon: '0b54ebc0b473b6571b8157d207392af0',
    splash: null,
    discoverySplash: null,
    region: 'russia',
    memberCount: 5,
    large: false,
    features: [],
    applicationID: null,
    afkTimeout: 300,
    afkChannelID: null,
    systemChannelID: '779768178296750080',
    embedEnabled: undefined,
    premiumTier: 0,
    premiumSubscriptionCount: 0,
    verificationLevel: 'NONE',
    explicitContentFilter: 'DISABLED',
    mfaLevel: 0,
    joinedTimestamp: 1620485504837,
    defaultMessageNotifications: 'ALL',
    systemChannelFlags: SystemChannelFlags { bitfield: 0 },
    maximumMembers: 100000,
    maximumPresences: null,
    approximateMemberCount: null,
    approximatePresenceCount: null,
    vanityURLCode: null,
    vanityURLUses: null,
    description: null,
    banner: null,
    rulesChannelID: null,
    publicUpdatesChannelID: null,
    preferredLocale: 'en-US',
    ownerID: '660217837016842260',
    emojis: GuildEmojiManager {
      cacheType: [class Collection extends Collection],
      cache: Collection(0) [Map] {},
      guild: [Circular *1]
    }
  },
  joinedTimestamp: 1620486298424,
  lastMessageID: null,
  lastMessageChannelID: null,
  premiumSinceTimestamp: 0,
  deleted: false,
  nickname: null,
  _roles: [],
  user: User {
    id: '462330953734291457',
    system: null,
    locale: null,
    flags: UserFlags { bitfield: 0 },
    username: 'Test Account',
    bot: false,
    discriminator: '4317',
    avatar: '343601cdd6da4d03329351def9ffbffb',
    lastMessageID: null,
    lastMessageChannelID: null
  }
}
(node:2459) UnhandledPromiseRejectionWarning: ReferenceError: pocess is not defined
    at Client.<anonymous> (/Users/admin/Desktop/projects/re_bot/discord_binder/bot.js:43:57)
    at Client.emit (events.js:315:20)
    at Object.module.exports [as GUILD_MEMBER_ADD] (/Users/admin/Desktop/projects/re_bot/discord_binder/node_modules/discord.js/src/client/websocket/handlers/GUILD_MEMBER_ADD.js:16:14)
    at WebSocketManager.handlePacket (/Users/admin/Desktop/projects/re_bot/discord_binder/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/Users/admin/Desktop/projects/re_bot/discord_binder/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/Users/admin/Desktop/projects/re_bot/discord_binder/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/Users/admin/Desktop/projects/re_bot/discord_binder/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (/Users/admin/Desktop/projects/re_bot/discord_binder/node_modules/ws/lib/websocket.js:825:20)
    at Receiver.emit (events.js:315:20)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2459) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2459) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

編輯:

代碼的總體思路是正確的,但是有很多錯別字,你可以看到 //EDIT: 注釋。 所以也許你想 go 並檢查你的代碼是否有相同的錯別字

您能否讓我們知道從console.log(member)記錄到控制台的內容?

我最好的選擇是 WELCOME_CHANNEL_ID 是錯誤的,因此返回了一個虛假的頻道。

如果不是這種情況,那么機器人可能沒有緩存頻道,所以使用這個:

const channel = await member.guild.channels.resolve(WELCOME_CHANNEL_ID);

讓我們知道它是否不起作用。

我看到您的代碼中有一條評論說WELCOME_CNAHHEL_ID是一個環境變量。 就像您對BOTTOKEN所做的一樣,您可能希望在變量前面加上process.env ,所以它應該如下所示:

process.env.WELCOME_CHANNEL_ID

您的原始代碼中還有一個錯字: CNAHHEL而不是CHANNEL 確保 .env 文件中的名稱與您在代碼中使用的名稱匹配。

僅在 Developer Portal 中啟用 Intent 並不意味着您正在使用 Intent
您必須告訴您的客戶使用哪些意圖。

所以,而不是

const client = new Client();

你需要做

const client = new Client({ ws: { intents: [
    'GUILDS',
    'GUILD_MESSAGES',
    'GUILD_PRESENCES',
    'GUILD_MEMBERS'
] } });

這將告訴客戶端加載GUILD_MEMBERS Intent 以便您可以使用guildMemberAdd

此代碼應該在那之后工作。 如有錯誤請評論。

編輯:在您的代碼中,您將guildMemberAdd拼錯為guildMembersAdd您還應該修復它。

資源:

暫無
暫無

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

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