@elizaos/plugin-discord 1.0.0-beta.3 → 1.0.0-beta.33

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 Shaw Walters, aka Moon aka @lalalune
3
+ Copyright (c) 2025 Shaw Walters and elizaOS Contributors
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.js CHANGED
@@ -134,7 +134,14 @@ var chatWithAttachments = {
134
134
  return;
135
135
  }
136
136
  const { objective, attachmentIds } = attachmentData;
137
- const attachments = state.data.recentMessages.filter((msg) => msg.content.attachments && msg.content.attachments.length > 0).flatMap((msg) => msg.content.attachments).filter(
137
+ const conversationLength = runtime.getConversationLength();
138
+ const recentMessages = await runtime.getMemories({
139
+ tableName: "messages",
140
+ roomId: message.roomId,
141
+ count: conversationLength,
142
+ unique: false
143
+ });
144
+ const attachments = recentMessages.filter((msg) => msg.content.attachments && msg.content.attachments.length > 0).flatMap((msg) => msg.content.attachments).filter(
138
145
  (attachment) => attachmentIds.map((attch) => attch.toLowerCase().slice(0, 5)).includes(attachment.id.toLowerCase().slice(0, 5)) || // or check the other way
139
146
  attachmentIds.some((id) => {
140
147
  const attachmentId = id.toLowerCase().slice(0, 5);
@@ -850,7 +857,14 @@ var transcribeMedia = {
850
857
  );
851
858
  return;
852
859
  }
853
- const attachment = state.data.recentMessages.filter((msg) => msg.content.attachments && msg.content.attachments.length > 0).flatMap((msg) => msg.content.attachments).find((attachment2) => attachment2.id.toLowerCase() === attachmentId.toLowerCase());
860
+ const conversationLength = runtime.getConversationLength();
861
+ const recentMessages = await runtime.getMemories({
862
+ tableName: "messages",
863
+ roomId: message.roomId,
864
+ count: conversationLength,
865
+ unique: false
866
+ });
867
+ const attachment = recentMessages.filter((msg) => msg.content.attachments && msg.content.attachments.length > 0).flatMap((msg) => msg.content.attachments).find((attachment2) => attachment2.id.toLowerCase() === attachmentId.toLowerCase());
854
868
  if (!attachment) {
855
869
  console.error(`Couldn't find attachment with ID ${attachmentId}`);
856
870
  await runtime.createMemory(
@@ -962,7 +976,7 @@ var joinVoice = {
962
976
  return false;
963
977
  }
964
978
  const room = state.data.room ?? await runtime.getRoom(message.roomId);
965
- if (room?.type !== ChannelType2.GROUP) {
979
+ if (room?.type !== ChannelType2.GROUP && room?.type !== ChannelType2.VOICE_GROUP) {
966
980
  return false;
967
981
  }
968
982
  const client = runtime.getService(ServiceType2.DISCORD);
@@ -978,7 +992,7 @@ var joinVoice = {
978
992
  if (!room) {
979
993
  throw new Error("No room found");
980
994
  }
981
- if (room.type !== ChannelType2.GROUP) {
995
+ if (room?.type !== ChannelType2.GROUP && room?.type !== ChannelType2.VOICE_GROUP) {
982
996
  return false;
983
997
  }
984
998
  const serverId = room.serverId;
@@ -1269,7 +1283,7 @@ var leaveVoice = {
1269
1283
  return false;
1270
1284
  }
1271
1285
  const room = state.data.room ?? await runtime.getRoom(message.roomId);
1272
- if (room?.type !== ChannelType3.GROUP) {
1286
+ if (room?.type !== ChannelType3.GROUP && room?.type !== ChannelType3.VOICE_GROUP) {
1273
1287
  return false;
1274
1288
  }
1275
1289
  const isConnectedToVoice = service.client.voice.adapters.size > 0;
@@ -1281,7 +1295,7 @@ var leaveVoice = {
1281
1295
  if (!room) {
1282
1296
  throw new Error("No room found");
1283
1297
  }
1284
- if (room.type !== ChannelType3.GROUP) {
1298
+ if (room?.type !== ChannelType3.GROUP && room?.type !== ChannelType3.VOICE_GROUP) {
1285
1299
  throw new Error("Not a group");
1286
1300
  }
1287
1301
  const serverId = room.serverId;
@@ -2062,23 +2076,6 @@ import {
2062
2076
  PermissionsBitField,
2063
2077
  ThreadChannel
2064
2078
  } from "discord.js";
2065
- function getWavHeader(audioLength, sampleRate, channelCount = 1, bitsPerSample = 16) {
2066
- const wavHeader = Buffer.alloc(44);
2067
- wavHeader.write("RIFF", 0);
2068
- wavHeader.writeUInt32LE(36 + audioLength, 4);
2069
- wavHeader.write("WAVE", 8);
2070
- wavHeader.write("fmt ", 12);
2071
- wavHeader.writeUInt32LE(16, 16);
2072
- wavHeader.writeUInt16LE(1, 20);
2073
- wavHeader.writeUInt16LE(channelCount, 22);
2074
- wavHeader.writeUInt32LE(sampleRate, 24);
2075
- wavHeader.writeUInt32LE(sampleRate * bitsPerSample * channelCount / 8, 28);
2076
- wavHeader.writeUInt16LE(bitsPerSample * channelCount / 8, 32);
2077
- wavHeader.writeUInt16LE(bitsPerSample, 34);
2078
- wavHeader.write("data", 36);
2079
- wavHeader.writeUInt32LE(audioLength, 40);
2080
- return wavHeader;
2081
- }
2082
2079
  var MAX_MESSAGE_LENGTH = 1900;
2083
2080
  async function sendMessageInChunks(channel, content, _inReplyTo, files) {
2084
2081
  const sentMessages = [];
@@ -2207,6 +2204,9 @@ var MessageManager = class {
2207
2204
  if (this.runtime.character.settings?.discord?.shouldIgnoreDirectMessages && message.channel.type === DiscordChannelType2.DM) {
2208
2205
  return;
2209
2206
  }
2207
+ if (this.runtime.character.settings?.discord?.shouldRespondOnlyToMentions && !message.mentions.users?.has(this.client.user?.id)) {
2208
+ return;
2209
+ }
2210
2210
  const entityId = createUniqueUuid4(this.runtime, message.author.id);
2211
2211
  const userName = message.author.bot ? `${message.author.username}#${message.author.discriminator}` : message.author.username;
2212
2212
  const name = message.author.displayName;
@@ -2432,6 +2432,7 @@ import {
2432
2432
  ChannelType as ChannelType8,
2433
2433
  ModelType as ModelType8,
2434
2434
  createUniqueUuid as createUniqueUuid5,
2435
+ getWavHeader,
2435
2436
  logger as logger5
2436
2437
  } from "@elizaos/core";
2437
2438
  import {
@@ -2872,8 +2873,7 @@ var VoiceManager = class extends EventEmitter {
2872
2873
  * @param {BaseGuildVoiceChannel} channel - The voice channel the user is in.
2873
2874
  * @param {Readable} audioStream - The audio stream to monitor.
2874
2875
  */
2875
- async handleUserStream(userId, name, userName, channel, audioStream) {
2876
- const entityId = createUniqueUuid5(this.runtime, userId);
2876
+ async handleUserStream(entityId, name, userName, channel, audioStream) {
2877
2877
  logger5.debug(`Starting audio monitor for user: ${entityId}`);
2878
2878
  if (!this.userStates.has(entityId)) {
2879
2879
  this.userStates.set(entityId, {
@@ -2965,9 +2965,10 @@ var VoiceManager = class extends EventEmitter {
2965
2965
  return { text: "", actions: ["IGNORE"] };
2966
2966
  }
2967
2967
  const roomId = createUniqueUuid5(this.runtime, channelId);
2968
+ const uniqueEntityId = createUniqueUuid5(this.runtime, entityId);
2968
2969
  const type = await this.getChannelType(channel);
2969
2970
  await this.runtime.ensureConnection({
2970
- entityId,
2971
+ entityId: uniqueEntityId,
2971
2972
  roomId,
2972
2973
  userName,
2973
2974
  name,
@@ -2979,7 +2980,7 @@ var VoiceManager = class extends EventEmitter {
2979
2980
  const memory = {
2980
2981
  id: createUniqueUuid5(this.runtime, `${channelId}-voice-message-${Date.now()}`),
2981
2982
  agentId: this.runtime.agentId,
2982
- entityId,
2983
+ entityId: uniqueEntityId,
2983
2984
  roomId,
2984
2985
  content: {
2985
2986
  text: message,
@@ -3009,7 +3010,7 @@ var VoiceManager = class extends EventEmitter {
3009
3010
  createdAt: Date.now()
3010
3011
  };
3011
3012
  if (responseMemory.content.text?.trim()) {
3012
- await this.runtime.createMemory(responseMemory);
3013
+ await this.runtime.createMemory(responseMemory, "messages");
3013
3014
  const responseStream = await this.runtime.useModel(
3014
3015
  ModelType8.TEXT_TO_SPEECH,
3015
3016
  content.text
@@ -3301,6 +3302,11 @@ var DiscordService = class _DiscordService extends Service {
3301
3302
  logger6.error(`Error handling interaction: ${error}`);
3302
3303
  }
3303
3304
  });
3305
+ this.client.on("userStream", (entityId, name, userName, channel, opusDecoder) => {
3306
+ if (entityId !== this.client?.user?.id) {
3307
+ this.voiceManager.handleUserStream(entityId, name, userName, channel, opusDecoder);
3308
+ }
3309
+ });
3304
3310
  }
3305
3311
  /**
3306
3312
  * Handles the event when a new member joins a guild.
@@ -3312,10 +3318,12 @@ var DiscordService = class _DiscordService extends Service {
3312
3318
  logger6.log(`New member joined: ${member.user.username}`);
3313
3319
  const guild = member.guild;
3314
3320
  const tag = member.user.bot ? `${member.user.username}#${member.user.discriminator}` : member.user.username;
3321
+ const worldId = createUniqueUuid6(this.runtime, guild.id);
3322
+ const entityId = createUniqueUuid6(this.runtime, member.id);
3315
3323
  this.runtime.emitEvent([EventType2.ENTITY_JOINED], {
3316
3324
  runtime: this.runtime,
3317
- entityId: createUniqueUuid6(this.runtime, member.id),
3318
- worldId: createUniqueUuid6(this.runtime, guild.id),
3325
+ entityId,
3326
+ worldId,
3319
3327
  source: "discord",
3320
3328
  metadata: {
3321
3329
  originalId: member.id,
@@ -3327,7 +3335,8 @@ var DiscordService = class _DiscordService extends Service {
3327
3335
  });
3328
3336
  this.runtime.emitEvent(["DISCORD_USER_JOINED" /* ENTITY_JOINED */], {
3329
3337
  runtime: this.runtime,
3330
- entityId: createUniqueUuid6(this.runtime, member.id),
3338
+ entityId,
3339
+ worldId,
3331
3340
  member,
3332
3341
  guild
3333
3342
  });