Problem with guildCreate

Hello, I’m making a Discord bot using Glitch Discord Bot Starter. I added a “guildCreate” event handler to hears.js which looks like this:

controller.on(“guildCreate”, guild => {
console.log("Joined a new guild: " + guild.name);
});

The event is emitted every time the bot joins a new guild, but I can’t get any information about the guild. In this particular case I get this output:

Joined a new guild: undefined

Same happens for the other guild properties I need. I’ve seen many examples of guildCreate event being used this way, so I guess it should work. How can I fix that problem?

Hm…interesting. If you console.log guild what do you get?

I get a very long output which looks not like a guild object, but more like the bot itself and even contains the bot token which I replaced with “” here:

{ events:
   { guildCreate: [ [Object] ],
 direct_mention: [ [Object] ],
 mention: [ [Object] ],
 ambient:
  [ [Object],
    [Object],
    [Object],
    [Object],
    [Object],
    [Object],
    [Object],
    [Object],
    [Object] ] },
  config:
   { token:
  <my bot token>,
 client:
  Client {
    _events: [Object],
    _eventsCount: 46,
    _maxListeners: 10,
    options: [Object],
    rest: [RESTManager],
    dataManager: [ClientDataManager],
    manager: [ClientManager],
    ws: [WebSocketManager],
    resolver: [ClientDataResolver],
    actions: [ActionsManager],
    voice: [ClientVoiceManager],
    shard: null,
    users: [Collection],
    guilds: [Collection],
    channels: [Collection],
    presences: Collection [Map] {},
    user: [ClientUser],
    readyAt: 2019-10-14T11:26:37.821Z,
    broadcasts: [],
    pings: [Array],
    _timeouts: [Set],
    _intervals: [Set] },
 hostname: '0.0.0.0',
 logLevel: 'info' },
  tasks: [],
 taskCount: 0,
  convoCount: 0,
  my_version: '0.6.21',
  my_user_agent: null,
  memory_store: { users: {}, channels: {}, teams: {} },
  tickDelay: 1500,
  utterances:
   { yes: /^(yes|yea|yup|yep|ya|sure|ok|y|yeah|yah)/i,
 no: /^(no|nah|nope|n)/i,
 quit: /^(quit|cancel|end|stop|done|exit|nevermind|never mind)/i },
  middleware:
   { spawn: Ware { fns: [] },
 ingest: Ware { fns: [Array] },
 normalize: Ware { fns: [Array] },
 categorize: Ware { fns: [Array] },
 receive: Ware { fns: [Array] },
 heard: Ware { fns: [] },
 triggered: Ware { fns: [] },
 capture: Ware { fns: [] },
 format: Ware { fns: [Array] },
 send: Ware { fns: [] },
 conversationStart: Ware { fns: [] },
 conversationEnd: Ware { fns: [] } },
  excludedEvents: [],
  excludeFromConversations: [Function],
  ingest: [Function],
  normalize: [Function],
  categorize: [Function],
  receiveMessage: [Function],
  storage:
   { teams:
  { get: [Function: get],
    save: [Function: save],
    delete: [Function: delete],
    all: [Function: all] },
 users:
  { get: [Function: get],
    save: [Function: save],
    delete: [Function: delete],
    all: [Function: all] },
 channels:
  { get: [Function: get],
    save: [Function: save],
    delete: [Function: delete],
    all: [Function: all] } },
  hears_regexp: [Function],
  changeEars: [Function],
  hears: [Function],
  on: [Function],
  trigger: [Function],
  startConversation: [Function],
  createConversation: [Function],
  defineBot: [Function],
  spawn: [Function],
  setTickDelay: [Function],
  startTicking: [Function],
  shutdown: [Function],
  startTask: [Function],
  tick: [Function],
  setupWebserver: [Function],
  worker: [Function: botDefinition],
  userAgent: [Function],
  version: [Function],
  logger: { log: [Function: log] },
  log:
   { [Function]
 emergency: [Function: bound log],
 alert: [Function: bound log],
 critical: [Function: bound log],
 error: [Function: bound log],
 warning: [Function: bound log],
 notice: [Function: bound log],
 info: [Function: bound log],
 debug: [Function: bound log] },
  debug: [Function: bound log],
  hears_test: [Function],
  studio:
   { evaluateTrigger: [Function],
 identify: [Function],
 getScripts: [Function],
 createScript: [Function],
 getScriptById: [Function],
 getScript: [Function],
 validate: [Function],
 beforeThread: [Function],
 before: [Function],
 after: [Function],
 run: [Function],
 get: [Function],
 getById: [Function],
 runTrigger: [Function],
 testTrigger: [Function],
 compileScript: [Function] },
  RichEmbed: [Function: RichEmbed],
  Attachment: [Function: Attachment],
  handleMessageRecieve: [Function: newMessageHandler],
  tickInterval:
   Timeout {
 _called: true,
 _idleTimeout: 1500,
 _idlePrev: [TimersList],
 _idleNext: [TimersList],
 _idleStart: 51556,
 _onTimeout: [Function],
 _timerArgs: undefined,
 _repeat: 1500,
 _destroyed: false,
 [Symbol(unrefed)]: false,
 [Symbol(asyncId)]: 14,
 [Symbol(triggerId)]: 1 } }

So, without looking at your code in context, the only variation that I see between the examples online (which seem only a year or two old?) and your code, is that your event listener is being applied to something referred to as a controller – is the controller variable here an instance of a new Discord.client() ?
If that’s not the issue, maybe the API has changed…? You could try to reach inside the returned object and see if what you want is in there (i.e., log out guild.events.guildCreate and see if it might have what you’re looking for) also?

Solution is found. The Discord Bot Starter uses botkit-discord, a wrapper for Discord.js. On the Github page it is stated that Discord.js events in it work the same way (even the example from discord.js.org is used), but I noticed that for “standard” botkit events there are two parameters, first of them being bot parameter. So I tried using (bot, guild) instead of guild and it worked! The second parameter turned out to be an array containing one single element which was the guild object I was looking for. So the code for logging out guild name should look like this:

controller.on(“guildCreate”, (bot, guild) => {
    console.log("Joined a new guild: " + guild[0].name);
});
3 Likes

Ah so cool! Thanks for following up with the solution!

2 Likes

you can also add an async to your event callback function

client.on('guildCreate', async guild => {
	console.log(`Joined a new guild: ${guild.id} ${guild.name}\n`)
	// other logic like initializing bot here
})

Based on what is returned by guild when you print it, that shows that all the data might not be loading

Warning
Hello @Cryptc, please don’t bump old topics - that makes them go to the top of the list. This one already has a solution and is 4 years old.

1 Like