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

PyCord开发Discord每日词库Bot:移除功能TypeError求助

PyCord机器人词汇移除命令修复方案

错误原因

你遇到的TypeError: replace expected at least 2 arguments, got 1是因为所有remove_*命令里的字符串处理逻辑完全错误:

  • str.replace()方法必须传入两个必填参数:要替换的目标字符串、替换后的新字符串,你只传了一个参数;
  • 更核心的问题是,你没有读取原文件内容就直接操作,等于根本没处理文件里的词汇列表,逻辑完全不成立。

修复后的完整代码

下面是修正后的所有命令代码,同时优化了添加命令的换行逻辑(避免文件开头出现空行):

# The commands!
@self.slash_command(name="add_word")
async def greet(ctx, add_word: Option(str, "What word do you wish to add", required=True, default='')):
    add_word = add_word.strip()
    with open("words.txt", "a+") as n:
        n.seek(0)
        content = n.read()
        if content:
            n.write("\n")
        n.write(add_word)
    await ctx.respond(f"Added the word: **{add_word}** to the list")

@self.slash_command(name="remove_word") 
async def greet(ctx, remove_word: Option(str, "What word do you wish to remove?", required=True, default='')):
    remove_word = remove_word.strip()
    with open("words.txt", 'r') as file:
        lines = file.readlines()
    # 过滤掉目标词汇,同时跳过空行
    new_lines = [line for line in lines if line.strip() != remove_word]
    with open("words.txt", 'w') as file:
        file.writelines(new_lines)
    await ctx.respond(f"Removed the word: **{remove_word}** from the list")

@self.slash_command(name="add_phrase")
async def greet(ctx, phrase_entry: Option(str, "What phrase do you wish to add", required=True, default='')):
    phrase_entry = phrase_entry.strip()
    with open("phrases.txt", "a+") as n:
        n.seek(0)
        content = n.read()
        if content:
            n.write("\n")
        n.write(phrase_entry)
    await ctx.respond(f"Added the phrase: **{phrase_entry}** to the list")

@self.slash_command(name="remove_phrase") 
async def greet(ctx, remove_phrase: Option(str, "What phrase do you wish to remove?", required=True, default='')):
    remove_phrase = remove_phrase.strip()
    with open("phrases.txt", 'r') as file:
        lines = file.readlines()
    new_lines = [line for line in lines if line.strip() != remove_phrase]
    with open("phrases.txt", 'w') as file:
        file.writelines(new_lines)
    await ctx.respond(f"Removed the phrase: **{remove_phrase}** from the list")

@self.slash_command(name="add_hard_word")
async def greet(ctx, add_hard_word: Option(str, "What difficult word do you wish to add", required=True, default='')):
    add_hard_word = add_hard_word.strip()
    with open("hard_words.txt", "a+") as n:
        n.seek(0)
        content = n.read()
        if content:
            n.write("\n")
        n.write(add_hard_word)
    await ctx.respond(f"Added the hard word: **{add_hard_word}** to the list")

@self.slash_command(name="remove_hard_word") 
async def greet(ctx, remove_hard_word: Option(str, "What difficult word do you wish to remove?", required=True, default='')):
    remove_hard_word = remove_hard_word.strip()
    with open("hard_words.txt", 'r') as file:
        lines = file.readlines()
    new_lines = [line for line in lines if line.strip() != remove_hard_word]
    with open("hard_words.txt", 'w') as file:
        file.writelines(new_lines)
    await ctx.respond(f"Removed the hard word: **{remove_hard_word}** from the list")

@self.slash_command(name="add_hard_phrase")
async def greet(ctx, hard_phrase_entry: Option(str, "What difficult phrase do you wish to add", required=True, default='')):
    hard_phrase_entry = hard_phrase_entry.strip()
    with open("hard_phrases.txt", "a+") as n:
        n.seek(0)
        content = n.read()
        if content:
            n.write("\n")
        n.write(hard_phrase_entry)
    await ctx.respond(f"Added the hard phrase: **{hard_phrase_entry}** to the list")

@self.slash_command(name="remove_hard_phrase") 
async def greet(ctx, remove_hard_phrase: Option(str, "What difficult phrase do you wish to remove?", required=True, default='')):
    remove_hard_phrase = remove_hard_phrase.strip()
    with open("hard_phrases.txt", 'r') as file:
        lines = file.readlines()
    new_lines = [line for line in lines if line.strip() != remove_hard_phrase]
    with open("hard_phrases.txt", 'w') as file:
        file.writelines(new_lines)
    await ctx.respond(f"Removed the hard phrase: **{remove_hard_phrase}** from the list")

关键修复点

  1. 移除命令逻辑修正:先读取文件所有行,过滤掉要删除的词汇(通过strip()去除首尾空格和换行,确保匹配准确),再将过滤后的内容写回文件;
  2. 添加命令优化:去除词汇首尾空格,判断文件是否为空,避免添加第一个词汇时开头出现空行;
  3. 替换错误用法:舍弃了原来错误的str.replace()写法,改用按行过滤的逻辑(replace会替换所有匹配子串,容易误删包含目标词汇的其他内容,按行过滤更精准)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 07:45:29