MCPcopy Index your code
hub / github.com/AstrBotDevs/AstrBot / new

Function new

astrbot/cli/commands/cmd_plug.py:51–103  ·  view source on GitHub ↗

Create a new plugin

(name: str)

Source from the content-addressed store, hash-verified

49@plug.command()
50@click.argument("name")
51def new(name: str) -> None:
52 """Create a new plugin"""
53 base_path = _get_data_path()
54 plug_path = base_path / "plugins" / name
55
56 if plug_path.exists():
57 raise click.ClickException(f"Plugin {name} already exists")
58
59 author = click.prompt("Enter plugin author", type=str)
60 desc = click.prompt("Enter plugin description", type=str)
61 version = click.prompt("Enter plugin version", type=str)
62 if not re.match(r"^\d+\.\d+(\.\d+)?$", version.lower().lstrip("v")):
63 raise click.ClickException("Version must be in x.y or x.y.z format")
64 repo = click.prompt("Enter plugin repository URL:", type=str)
65 if not repo.startswith("http"):
66 raise click.ClickException("Repository URL must start with http")
67
68 click.echo("Downloading plugin template...")
69 get_git_repo(
70 "https://github.com/Soulter/helloworld",
71 plug_path,
72 )
73
74 click.echo("Rewriting plugin metadata...")
75 # Rewrite metadata.yaml
76 with open(plug_path / "metadata.yaml", "w", encoding="utf-8") as f:
77 f.write(
78 f"name: {name}\n"
79 f"desc: {desc}\n"
80 f"version: {version}\n"
81 f"author: {author}\n"
82 f"repo: {repo}\n",
83 )
84
85 # Rewrite README.md
86 with open(plug_path / "README.md", "w", encoding="utf-8") as f:
87 f.write(
88 f"# {name}\n\n{desc}\n\n# Support\n\n[Documentation](https://docs.astrbot.app)\n"
89 )
90
91 # Rewrite main.py
92 with open(plug_path / "main.py", encoding="utf-8") as f:
93 content = f.read()
94
95 new_content = content.replace(
96 '@register("helloworld", "YourName", "一个简单的 Hello World 插件", "1.0.0")',
97 f'@register("{name}", "{author}", "{desc}", "{version}")',
98 )
99
100 with open(plug_path / "main.py", "w", encoding="utf-8") as f:
101 f.write(new_content)
102
103 click.echo(f"Plugin {name} created successfully")
104
105
106@plug.command()

Callers

nothing calls this directly

Calls 6

_get_data_pathFunction · 0.85
get_git_repoFunction · 0.85
startswithMethod · 0.80
matchMethod · 0.45
writeMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected