qeled / discordie

Predictable JavaScript abstractions for Discord API.
https://qeled.github.io/discordie/
BSD 2-Clause "Simplified" License
190 stars 45 forks source link

Events "namespaces" #31

Closed brawaru closed 5 years ago

brawaru commented 8 years ago

For some bots it will be very useful to subscribe only for events of specific channels/guilds/users.

Example:

As ID of #general channel always equal to guild id, then we can use # symbol to define, that we're subscribed for event of channel, not guild.

Also, to define, that we're subscribed for events of user actions, we can use @ symbol: @133145125122605057.VOICE_USER_SELF_MUTE, @133145125122605057.MESSAGE_CREATE

bot.Dispatcher.on("@133145125122605057.MESSAGE_CREATE", (e) => {
  // 133145125122605057 = owner 
  if(e.message.content.startsWith("/eval ``") && e.message.content.endsWith("``")) {
    let js = e.message.content.substring("/eval ``".length, e.message.content.length - "``".length);
    try {
      let output = eval(js);
      if(output !== null || output !== undefined) {
         output = JSON.stringify(output);
      }
     e.message.reply(`:ok_hand::skin-tone-3: \n\`\`\`\n${output}\n\`\`\``);
    } catch (err) {
     e.message.reply(`:warning: Error, ouch! :head_bandage: \n\`\`\`\n${stringifyError(err).replace("\\n", "\n")}\n\`\`\``);
    }
  }
});

:eyes:

cking commented 8 years ago

You could always emit your own events on the Dispatcher (Dispatcher.emit(eventName, eventParams...)).