Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

node.js - message.guild returns null in discordjs

Im making a discord bot that sends a user that sends a message to every member in the server. It worked a while ago but without chaning anything it suddenly stopped working. Not to sound egotistical but i doubt its anything with my code and more just something i forgot to setup but in the case its me, i've sent the code below

client.on("message", message => {
    const { guild } = message

    console.log(guild)

i've tried to get the guild at different spots in the process but it doesn't seem to work. I've looked at the docs but that didn't help a lot. They also just use message.guild. I tried just printing the message object and it returns

Message {
  channel: DMChannel {
    type: 'dm',
    deleted: false,
    id: '795038485052522546',
    recipient: User {
      id: '358317103834726401',
      system: null,
      locale: null,
      flags: [UserFlags],
      username: 'T3CH',
      bot: false,
      discriminator: '6969',
      avatar: 'a_578305126220c42a2de15defb32ad2b6',
      lastMessageID: null,
      lastMessageChannelID: null
    },
    lastMessageID: '797538660227481650',
    lastPinTimestamp: null,
    messages: MessageManager {
      cacheType: [class LimitedCollection extends Collection],
      cache: [LimitedCollection [Map]],
      channel: [Circular]
    },
    _typing: Map {}
  },
  deleted: false,
  id: '797538660227481650',
  type: 'DEFAULT',
  system: false,
  content: 'There was an error finding the guild',
  author: ClientUser {
    id: '764065338592591873',
    system: null,
    locale: null,
    flags: UserFlags { bitfield: 0 },
    username: 'Greg Michaelson',
    bot: true,
    discriminator: '6184',
    avatar: '4fc90843924c172c60c126c0284e9561',
    lastMessageID: '797538660227481650',
    lastMessageChannelID: '795038485052522546',
    verified: true,
    mfaEnabled: true,
    _typing: Map {}
  },
  pinned: false,
  nonce: null,
  embeds: [],
  attachments: Collection [Map] {},
  createdTimestamp: 1610218434150,
  editedTimestamp: 0,
  reactions: ReactionManager {
    cacheType: [class Collection extends Collection],
    cache: Collection [Map] {},
    message: [Circular]
  },
  mentions: MessageMentions {
    everyone: false,
    users: Collection [Map] {},
    roles: Collection [Map] {},
    _members: null,
    _channels: null,
    crosspostedChannels: Collection [Map] {}
  },
  webhookID: null,
  application: null,
  activity: null,
  _edits: [],
  flags: MessageFlags { bitfield: 0 },
  reference: null
}

Thanks in advance

question from:https://stackoverflow.com/questions/65646517/message-guild-returns-null-in-discordjs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For anyone else wondering why the error occurs it is because JanuZz_dk was direct messaging the bot, this means that there is no <Guild> object inside of the <Message> response.

You can check the type of channel that the message has come from programmatically like this:

if(<Channel>.type == 'dm') {
    // Message has come from a direct message
}

if(<Channel>.type == 'text') {
    // Message has come from a channel in a guild
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...