我正在使用 Node.js 上的机器人来管理 twitch 聊天。 我们遇到了一个问题,有时机器人会袭击我们的频道并发送垃圾邮件,例如:
我不了解这些消息与普通聊天用户的相似之处以及如何区分这些消息
我现在的做法是添加最后一个字母和数字来检查它是否包含在消息中,但我认为它对未来不可靠,并且仅在垃圾邮件发生后才起作用
if (
text.toLowerCase().includes('a739') ||
text.toLowerCase().includes("e644") ||
text.toLowerCase().includes("e450")
//etc.
) {
await apiClient.asUser(
nyaka,
async (ctx) =>
await ctx.moderation.banUser(msg.channelId, {
reason: `${user}: ${text}`,
user: msg.userInfo.userId,
})
);
只需检查句子中有多少个西里尔字母。如果它们超过阈值,请将其过滤掉。
在下面的示例中,您可以过滤掉包含超过 70% 西里尔文字的任何消息。
const messages = [
'Привет, мир!',
'Hello, world!',
'Привет. How are you today?'
];
const validMessages = messages.filter(msg => !exceedsThreshold(msg, 0.7));
console.log({ validMessages });
function countCyrillicCharacters(str) {
return str.match(/[А-Яа-яЁё]/g)?.length ?? 0;
}
function exceedsThreshold(text, threshold=0.1) {
const ratio = countCyrillicCharacters(text) / text.length;
console.log({ text, ratio });
return ratio > threshold;
}
.as-console-wrapper { top: 0; max-height: 100% !important; }