@elizaos/plugin-discord 1.3.1 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -4047,11 +4047,10 @@ var voiceStateProvider = {
4047
4047
  // src/service.ts
4048
4048
  import {
4049
4049
  ChannelType as ChannelType7,
4050
- EventType as EventType2,
4050
+ EventType,
4051
4051
  Role,
4052
4052
  Service,
4053
- createUniqueUuid as createUniqueUuid5,
4054
- logger as logger13
4053
+ createUniqueUuid as createUniqueUuid5
4055
4054
  } from "@elizaos/core";
4056
4055
  import {
4057
4056
  ChannelType as DiscordChannelType5,
@@ -4140,7 +4139,6 @@ function getDiscordSettings(runtime) {
4140
4139
  // src/messages.ts
4141
4140
  import {
4142
4141
  ChannelType as ChannelType5,
4143
- EventType,
4144
4142
  ServiceType as ServiceType4,
4145
4143
  MemoryType,
4146
4144
  createUniqueUuid as createUniqueUuid3,
@@ -4714,14 +4712,10 @@ var MessageManager = class {
4714
4712
  if (this.discordSettings.shouldRespondOnlyToMentions) {
4715
4713
  const shouldProcess = isDM || isBotMentioned || isReplyToBot;
4716
4714
  if (!shouldProcess) {
4717
- logger11.debug(
4718
- "[Discord] Strict mode: ignoring message (no @mention or reply)"
4719
- );
4715
+ logger11.debug("[Discord] Strict mode: ignoring message (no @mention or reply)");
4720
4716
  return;
4721
4717
  }
4722
- logger11.debug(
4723
- "[Discord] Strict mode: processing message (has @mention or reply)"
4724
- );
4718
+ logger11.debug("[Discord] Strict mode: processing message (has @mention or reply)");
4725
4719
  }
4726
4720
  const entityId = createUniqueUuid3(this.runtime, message.author.id);
4727
4721
  const userName = message.author.bot ? `${message.author.username}#${message.author.discriminator}` : message.author.username;
@@ -4756,7 +4750,10 @@ var MessageManager = class {
4756
4750
  try {
4757
4751
  const canSendResult = canSendMessage(message.channel);
4758
4752
  if (!canSendResult.canSend) {
4759
- return logger11.warn(`Cannot send message to channel ${message.channel}`, canSendResult.reason || void 0);
4753
+ return logger11.warn(
4754
+ `Cannot send message to channel ${message.channel}`,
4755
+ canSendResult.reason || void 0
4756
+ );
4760
4757
  }
4761
4758
  const { processedContent, attachments } = await this.processMessage(message);
4762
4759
  const audioAttachments = message.attachments.filter(
@@ -4815,7 +4812,7 @@ var MessageManager = class {
4815
4812
  },
4816
4813
  createdAt: message.createdTimestamp
4817
4814
  };
4818
- const callback = async (content, files) => {
4815
+ const callback = async (content) => {
4819
4816
  try {
4820
4817
  if (content.target && typeof content.target === "string" && content.target.toLowerCase() !== "discord") {
4821
4818
  return [];
@@ -4848,7 +4845,12 @@ var MessageManager = class {
4848
4845
  await u.send(content.text || "");
4849
4846
  messages = [content];
4850
4847
  } else {
4851
- messages = await sendMessageInChunks(channel, content.text ?? "", message.id, files || []);
4848
+ messages = await sendMessageInChunks(
4849
+ channel,
4850
+ content.text ?? "",
4851
+ message.id,
4852
+ []
4853
+ );
4852
4854
  }
4853
4855
  const memories = [];
4854
4856
  for (const m of messages) {
@@ -4886,11 +4888,19 @@ var MessageManager = class {
4886
4888
  return [];
4887
4889
  }
4888
4890
  };
4889
- this.runtime.emitEvent(["DISCORD_MESSAGE_RECEIVED" /* MESSAGE_RECEIVED */, EventType.MESSAGE_RECEIVED], {
4890
- runtime: this.runtime,
4891
- message: newMessage,
4892
- callback
4893
- });
4891
+ if (this.runtime.hasElizaOS()) {
4892
+ logger11.debug("[Discord] Using unified messaging API");
4893
+ await this.runtime.elizaOS.sendMessage(
4894
+ this.runtime.agentId,
4895
+ newMessage,
4896
+ {
4897
+ onResponse: callback
4898
+ }
4899
+ );
4900
+ } else {
4901
+ logger11.debug("[Discord] Using direct message service");
4902
+ await this.runtime.messageService.handleMessage(this.runtime, newMessage, callback);
4903
+ }
4894
4904
  setTimeout(() => {
4895
4905
  if (typingData.started && typingData.interval && !typingData.cleared) {
4896
4906
  clearInterval(typingData.interval);
@@ -4920,7 +4930,7 @@ var MessageManager = class {
4920
4930
  processedContent += " Description:" + (embed.description ?? "(none)") + "\n";
4921
4931
  }
4922
4932
  }
4923
- if (message.reference) {
4933
+ if (message.reference && message.reference.messageId) {
4924
4934
  const messageId = createUniqueUuid3(this.runtime, message.reference.messageId);
4925
4935
  processedContent += "\nReferencing MessageID " + messageId + " (discord: " + message.reference.messageId + ")";
4926
4936
  if (message.reference.channelId !== message.channel.id) {
@@ -5056,7 +5066,10 @@ function createOpusDecoder(options) {
5056
5066
  const report = generateDependencyReport();
5057
5067
  logger12.debug("Voice dependency report:", report);
5058
5068
  } catch (reportError) {
5059
- logger12.warn("Could not generate dependency report:", reportError);
5069
+ logger12.warn(
5070
+ "Could not generate dependency report:",
5071
+ reportError instanceof Error ? reportError.message : String(reportError)
5072
+ );
5060
5073
  }
5061
5074
  throw error;
5062
5075
  }
@@ -5320,7 +5333,10 @@ var VoiceManager = class extends EventEmitter {
5320
5333
  }
5321
5334
  });
5322
5335
  connection.on("error", (error) => {
5323
- logger12.log("Voice connection error:", error);
5336
+ logger12.log(
5337
+ "Voice connection error:",
5338
+ error instanceof Error ? error.message : String(error)
5339
+ );
5324
5340
  logger12.log("Connection error - will attempt to recover...");
5325
5341
  });
5326
5342
  this.connections.set(channel.id, connection);
@@ -5330,7 +5346,10 @@ var VoiceManager = class extends EventEmitter {
5330
5346
  await me.voice.setDeaf(false);
5331
5347
  await me.voice.setMute(false);
5332
5348
  } catch (error) {
5333
- logger12.log("Failed to modify voice state:", error);
5349
+ logger12.log(
5350
+ "Failed to modify voice state:",
5351
+ error instanceof Error ? error.message : String(error)
5352
+ );
5334
5353
  }
5335
5354
  }
5336
5355
  connection.receiver.speaking.on("start", async (entityId) => {
@@ -5354,7 +5373,10 @@ var VoiceManager = class extends EventEmitter {
5354
5373
  }
5355
5374
  });
5356
5375
  } catch (error) {
5357
- logger12.log("Failed to establish voice connection:", error);
5376
+ logger12.log(
5377
+ "Failed to establish voice connection:",
5378
+ error instanceof Error ? error.message : String(error)
5379
+ );
5358
5380
  connection.destroy();
5359
5381
  this.connections.delete(channel.id);
5360
5382
  throw error;
@@ -5696,11 +5718,7 @@ var VoiceManager = class extends EventEmitter {
5696
5718
  return [];
5697
5719
  }
5698
5720
  };
5699
- this.runtime.emitEvent(["DISCORD_VOICE_MESSAGE_RECEIVED", "VOICE_MESSAGE_RECEIVED"], {
5700
- runtime: this.runtime,
5701
- message: memory,
5702
- callback
5703
- });
5721
+ await this.runtime.messageService.handleMessage(this.runtime, memory, callback);
5704
5722
  } catch (error) {
5705
5723
  console.error("Error processing voice message:", error);
5706
5724
  }
@@ -5873,7 +5891,6 @@ var DiscordService = class _DiscordService extends Service {
5873
5891
  discordSettings;
5874
5892
  userSelections = /* @__PURE__ */ new Map();
5875
5893
  timeouts = [];
5876
- clientReadyPromise;
5877
5894
  /**
5878
5895
  * List of allowed channel IDs (parsed from CHANNEL_IDS env var).
5879
5896
  * If undefined, all channels are allowed.
@@ -5898,7 +5915,7 @@ var DiscordService = class _DiscordService extends Service {
5898
5915
  const channelIdsRaw = runtime.getSetting("CHANNEL_IDS");
5899
5916
  if (channelIdsRaw?.trim && channelIdsRaw.trim()) {
5900
5917
  this.allowedChannelIds = channelIdsRaw.split(",").map((s) => s.trim()).filter((s) => s.length > 0);
5901
- this.runtime.logger.debug("Locking down discord to", this.allowedChannelIds);
5918
+ this.runtime.logger.debug("Locking down discord to", this.allowedChannelIds.join(", "));
5902
5919
  }
5903
5920
  const token = runtime.getSetting("DISCORD_API_TOKEN");
5904
5921
  if (!token || token?.trim && token.trim() === "") {
@@ -5925,17 +5942,18 @@ var DiscordService = class _DiscordService extends Service {
5925
5942
  this.runtime = runtime;
5926
5943
  this.voiceManager = new VoiceManager(this, runtime);
5927
5944
  this.messageManager = new MessageManager(this);
5928
- this.clientReadyPromise = new Promise((resolver) => {
5929
- this.client.once(Events.ClientReady, (readyClient) => {
5930
- resolver();
5931
- this.onReady(readyClient);
5932
- });
5933
- this.client.login(token).catch((error) => {
5934
- this.runtime.logger.error(
5935
- `Failed to login to Discord: ${error instanceof Error ? error.message : String(error)}`
5936
- );
5937
- this.client = null;
5938
- });
5945
+ this.client.once(Events.ClientReady, (readyClient) => {
5946
+ this.onReady(readyClient);
5947
+ });
5948
+ this.client.login(token).catch((error) => {
5949
+ this.runtime.logger.error(
5950
+ `Failed to login to Discord: ${error instanceof Error ? error.message : String(error)}`
5951
+ );
5952
+ if (this.client) {
5953
+ this.client.destroy().catch(() => {
5954
+ });
5955
+ }
5956
+ this.client = null;
5939
5957
  });
5940
5958
  this.setupEventListeners();
5941
5959
  this.registerSendHandler();
@@ -6062,8 +6080,6 @@ var DiscordService = class _DiscordService extends Service {
6062
6080
  }
6063
6081
  const listenCidsRaw = this.runtime.getSetting("DISCORD_LISTEN_CHANNEL_IDS");
6064
6082
  const listenCids = Array.isArray(listenCidsRaw) ? listenCidsRaw : listenCidsRaw && typeof listenCidsRaw === "string" && listenCidsRaw.trim() ? listenCidsRaw.trim().split(",").map((s) => s.trim()).filter((s) => s.length > 0) : [];
6065
- const talkCids = this.allowedChannelIds ?? [];
6066
- const allowedCids = [...listenCids, ...talkCids];
6067
6083
  this.client.on("messageCreate", async (message) => {
6068
6084
  if (message.author.id === this.client?.user?.id || message.author.bot && this.discordSettings.shouldIgnoreBotMessages) {
6069
6085
  this.runtime.logger.info(
@@ -6073,26 +6089,25 @@ var DiscordService = class _DiscordService extends Service {
6073
6089
  }
6074
6090
  if (listenCids.includes(message.channel.id)) {
6075
6091
  const entityId = createUniqueUuid5(this.runtime, message.author.id);
6076
- const userName = message.author.bot ? `${message.author.username}#${message.author.discriminator}` : message.author.username;
6077
- const name = message.author.displayName;
6078
- const channelId = message.channel.id;
6079
- const roomId = createUniqueUuid5(this.runtime, channelId);
6092
+ const roomId = createUniqueUuid5(this.runtime, message.channel.id);
6080
6093
  let type;
6081
- let serverId;
6094
+ let _serverId;
6082
6095
  if (message.guild) {
6083
6096
  const guild = await message.guild.fetch();
6084
6097
  type = await this.getChannelType(message.channel);
6085
6098
  if (type === null) {
6086
- this.runtime.logger.warn("null channel type, discord message", message);
6099
+ this.runtime.logger.warn("null channel type, discord message", message.id);
6087
6100
  }
6088
- serverId = guild.id;
6101
+ _serverId = guild.id;
6089
6102
  } else {
6090
6103
  type = ChannelType7.DM;
6091
- serverId = message.channel.id;
6104
+ _serverId = message.channel.id;
6092
6105
  }
6093
6106
  const { processedContent, attachments } = await this.messageManager.processMessage(message);
6094
6107
  const messageId = createUniqueUuid5(this.runtime, message.id);
6095
6108
  const sourceId = entityId;
6109
+ const userName = message.author.bot ? `${message.author.username}#${message.author.discriminator}` : message.author.username;
6110
+ const name = message.member?.displayName ?? message.author.displayName ?? message.author.globalName ?? userName;
6096
6111
  const newMessage = {
6097
6112
  id: messageId,
6098
6113
  entityId,
@@ -6235,7 +6250,7 @@ var DiscordService = class _DiscordService extends Service {
6235
6250
  const tag = member.user.bot ? `${member.user.username}#${member.user.discriminator}` : member.user.username;
6236
6251
  const worldId = createUniqueUuid5(this.runtime, guild.id);
6237
6252
  const entityId = createUniqueUuid5(this.runtime, member.id);
6238
- this.runtime.emitEvent([EventType2.ENTITY_JOINED], {
6253
+ this.runtime.emitEvent([EventType.ENTITY_JOINED], {
6239
6254
  runtime: this.runtime,
6240
6255
  entityId,
6241
6256
  worldId,
@@ -6290,7 +6305,7 @@ var DiscordService = class _DiscordService extends Service {
6290
6305
  server: fullGuild,
6291
6306
  source: "discord"
6292
6307
  });
6293
- this.runtime.emitEvent([EventType2.WORLD_JOINED], standardizedData);
6308
+ this.runtime.emitEvent([EventType.WORLD_JOINED], standardizedData);
6294
6309
  }
6295
6310
  /**
6296
6311
  * Handles interactions created by the user, specifically commands and message components.
@@ -6666,7 +6681,7 @@ var DiscordService = class _DiscordService extends Service {
6666
6681
  },
6667
6682
  source: "discord"
6668
6683
  };
6669
- this.runtime.emitEvent([EventType2.WORLD_CONNECTED], standardizedData);
6684
+ this.runtime.emitEvent([EventType.WORLD_CONNECTED], standardizedData);
6670
6685
  } catch (error) {
6671
6686
  this.runtime.logger.error(`Error during Discord world connection: ${error instanceof Error ? error.message : String(error)}`);
6672
6687
  }
@@ -6732,7 +6747,7 @@ var DiscordService = class _DiscordService extends Service {
6732
6747
  } else {
6733
6748
  this.runtime.logger.info(`Fetching members for guild ${guild.name}`);
6734
6749
  members = await guild.members.fetch();
6735
- logger13.info(`Fetched ${members.size} members`);
6750
+ this.runtime.logger.info(`Fetched ${members.size} members`);
6736
6751
  }
6737
6752
  } catch (error) {
6738
6753
  this.runtime.logger.error(`Error fetching members: ${error}`);