【发布时间】:2021-11-12 12:50:30
【问题描述】:
我想要实现的是创建三个简单的命令:
!添加昵称
!删除昵称
!list(昵称)
这是我不太好的脚本:
import json
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
def add(name):
list.append( name )
@bot.command()
async def dele(ctx, *args):
with open('list.json', 'r') as f:
lines = json.loads(f.read())
for name in args:
lines.remove(name)
with open('list.json', 'w') as f:
f.write(json.dumps(lines))
@bot.command()
async def add(ctx, *args):
with open('list.json', 'r') as f:
lines = json.loads(f.read())
for name in args:
lines.append(name)
with open('list.json', 'w') as f:
f.write(json.dumps(lines))
@bot.command()
async def list(ctx):
with open('list.json', 'r') as f:
list = json.loads(f.read())
await ctx.channel.send(f"List {list}")
bot.run("MY TOKEN HERE")
命令 !list 输出:
list.json
["test1", "test2", "test3", "test", "4", "test4", "name", "name", "test", "5", "test", "6"]
所以...添加和删除昵称可以正常工作(除了当我尝试添加带有空格的昵称时,它会添加两个单独的键)。
但是我遇到的最大问题是 list 命令。不知道如何将其格式化为某种列表,其中 \n 昵称一一显示,甚至嵌入不和谐的东西。 请帮忙。
【问题讨论】:
标签: python json discord command