英文:
Localized and ephemeral replies using Discord.JS?
问题
最近我一直在我的Discord机器人上进行本地化的工作,我有一个疑问。
在使用本地化时,您可以创建一个回复,根据用户的语言设置使用不同的语言。
如代码中所示此处:
client.on(Events.InteractionCreate, interaction => {
const locales = {
pl: 'Witaj Świecie!',
de: 'Hallo Welt!',
};
interaction.reply(locales[interaction.locale] ?? 'Hello World (default is English)');
});
我的问题是,我想做同样的事情,只是使用ephemeral参数,就像这样:
await interaction.followUp({ content: 'Pong !', ephemeral: true });
但是当我尝试混合两者时:
async execute(interaction) {
const locales = {
pl: 'Witaj Świecie!',
de: 'Hallo Welt!',
};
await interaction.reply(locales[interaction.locale] ?? 'Hello World!', ephemeral: true);
});
它不起作用,因为您应该以这种方式编写ephemeral答案:
await interaction.followUp({ content: 'Pong again!', ephemeral: true });
所以我想知道是否有人有想法,我如何使回复本地化和ephemeral,并可能更改我的代码以使其工作。
那将非常有帮助,谢谢!
英文:
I've recently been playing a lot with localization on my Discord Bot, and I was wondering something.
When using localization, you can create a reply that uses different languages based of the user's language settings.
As seen in the code here :
client.on(Events.InteractionCreate, interaction => {
const locales = {
pl: 'Witaj Świecie!',
de: 'Hallo Welt!',
};
interaction.reply(locales[interaction.locale] ?? 'Hello World (default is english)');
});
My problem is that I would like to do the same thing except using the ephemeral parameter, like so:
await interaction.followUp({ content: 'Pong !', ephemeral: true });
But when I try to mix the two :
async execute(interaction) {
const locales = {
pl: 'Witaj Świecie!',
de: 'Hallo Welt!',
};
await interaction.reply(locales[interaction.locale] ?? 'Hello World!', ephemeral: true);
});
It doesn't work since the way you're supposed to write an ephemeral answer is like this:
await interaction.followUp({ content: 'Pong again!', ephemeral: true });
So I was wondering if anyone had an idea, on how I could make the reply localized and ephemeral, and maybe change up my code so that it works.
That would be super helpful, thanks!
答案1
得分: 1
你的第一个例子几乎正确,只需要将你的内容添加为content
的值:
await interaction.reply({
content: locales[interaction.locale] ?? '你好,世界!',
ephemeral: true,
});
你的第一个示例有效,因为reply
接受字符串作为消息的内容。
// ✅ 有效
interaction.reply(locales[interaction.locale] ?? '你好,世界(默认是英文)');
第二个示例不起作用,因为你仍然将字符串作为第一个参数传递,但如果你想使用临时消息,你需要传递一个对象,而不是带有content
和ephemeral
键的字符串。
// ⛔️ 不起作用
await interaction.reply(
locales[interaction.locale] ?? '你好,世界!',
ephemeral: true
);
// ✅ 有效
await interaction.reply({
content: locales[interaction.locale] ?? '你好,世界!',
ephemeral: true,
});
英文:
You were pretty close, you just need to add your content as the value of content
:
await interaction.reply({
content: locales[interaction.locale] ?? 'Hello World!',
ephemeral: true,
});
Your first example works because reply
accepts a string as the content of the message.
// ✅ works
interaction.reply(locales[interaction.locale] ?? 'Hello World (default is English)');
The second one doesn't work because you still pass a string as the first argument but if you want to use ephemeral messages, you need to pass an object instead of that string with a content
and an ephemeral
key.
// ⛔️ doesn't work
await interaction.reply(
locales[interaction.locale] ?? 'Hello World!',
ephemeral: true
);
// ✅ works
await interaction.reply({
content: locales[interaction.locale] ?? 'Hello World!',
ephemeral: true,
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论