javascript 机器人无法检测DM上的消息(AI)||Discord.JS V.14

z3yyvxxp  于 2023-06-04  发布在  Java
关注(0)|答案(1)|浏览(101)

这是我的代码:

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: client.config.openai,
});
const openai = new OpenAIApi(configuration);

client.on("messageCreate", async (message) => {
  if (message.author.bot) return;
  if (message.content.startsWith("!")) return;
  if (message.channel.type !== ChannelType.DM) return;

  let conversationLog = [
    { role: "system", content: "Hello, how can I help you?" },
  ];

  conversationLog.push({
    role: "user",
    content: message.content,
  });

  await message.channel.sendTyping();

  const result = await openai.createChatCompletion({
    model: "gpt-3.5-turbo",
    messages: conversationLog,
  });

  message.reply(result.data.choices[0].message);
});

这是我的问题,我试图创建一个聊天机器人,将只响应DM,而不是任何其他渠道/公会,但它失败了!当我把ChannelType.DM转到ChannelType.GuildText并在文本频道上聊天时,它会响应!我该怎么办?顺便说一句,这些是我的意图:

const {
  Guilds,
  GuildMembers,
  GuildMessages,
  MessageContent,
  DirectMessages,
  GuildVoiceStates,
} = GatewayIntentBits;

这些是我的部分:

const { User, Message, GuildMember, ThreadMember } = Partials;

任何答案都可以接受!

注意:

这个或this答案似乎不起作用!

fwzugrvs

fwzugrvs1#

DM通道可以不缓存,因此您还需要添加Channel部分:

const { GatewayIntentBits, Partials } = require('discord.js');

const client = new Discord.Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.MessageContent,
    // ...
  ],
  partials: [Partials.Channel, Partials.Message],
});

相关问题