You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js 13(TypeScript)中为MessageEmbed标题添加Reddit帖子超链接报错求助

Fixing "this function is not callable" when setting a linked title in Discord.js v13 + TypeScript

I’ve run into this exact type of snag before with TypeScript’s strict type checking and Discord.js’s type definitions. Let’s break down what’s happening and how to fix it:

The Root Cause

The error message "this function is not callable" is a bit misleading here—what’s actually going on is that TypeScript can’t guarantee allowed[randomnumber].data.title or allowed[randomnumber].data.permalink are valid, non-undefined strings. Since MessageEmbed.setTitle() requires a definite string parameter, TypeScript throws an error because it can’t confirm your template string meets that requirement.

The Solution

Let’s clean up the code to make type checking easier, and explicitly define types for your Reddit post data to eliminate ambiguity:

// Define a clear type for Reddit post data to enforce required properties
type RedditPost = {
  data: {
    over_18: boolean;
    title: string;
    permalink: string;
  };
};

const allowed = body.data.children.filter((post: RedditPost) => !post.data.over_18);
if (!allowed.length) return message.channel.send('It seems we are out of fresh memes!, Try again later.');

// Extract the random post to a single variable (avoids repeated array access and improves type clarity)
const randomPost = allowed[Math.floor(Math.random() * allowed.length)];

const embed = new MessageEmbed()
  .setTitle(`[${randomPost.data.title}](https://reddit.com${randomPost.data.permalink})`);

return embed;

Why This Works

  1. Explicit Type Definition: By creating a RedditPost type, we tell TypeScript exactly what properties to expect on each post—including that title and permalink are non-optional strings. This removes the ambiguity that was triggering the type error.
  2. Single Variable Access: Pulling randomPost from the array once means TypeScript only needs to validate its type once, instead of multiple times for each repeated allowed[randomnumber].data call. This makes the code cleaner and more reliable.
  3. Valid Markdown Link: Discord fully supports Markdown links in embed titles, so the [Title Text](URL) format works perfectly once TypeScript accepts the string as valid.

Quick Troubleshooting

If you still hit issues:

  • Double-check that your Reddit API response always includes title and permalink for every post (our type definition assumes they exist—if the API sometimes omits them, add optional checks like randomPost.data.title ?? 'Untitled Post').
  • Confirm you’re using a stable minor version of Discord.js v13 (older builds might have slightly different type definitions).

内容的提问来源于stack exchange,提问作者knify

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 15:47:28