如何从列表中删除引号?
How can you remove a quote from a list?
我正在为我的一个朋友制作一个 discord.py
机器人。我想这样做,以便您可以将愚蠢的引号添加到列表中,然后随机选择它们。就这么多,我做到了。
但是,我也希望能够删除不相关的、拼写错误的引号等。我想知道如何才能做到这一点。
这是我目前得到的:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import random
import pickle
import os
Client = discord.Client()
bot_prefix = "."
bot = commands.Bot(command_prefix=bot_prefix)
@bot.event
async def on_ready():
print("Bot Online!")
print("Name: {}".format(bot.user.name))
print("ID: {}".format(bot.user.id))
class Main_Commands():
def __init__(self, bot):
self.bot = bot
# .addquote
@bot.command(pass_context = True)
async def addquote(ctx, *answer):
answer=" ".join(answer)
if not os.path.isfile("quote_file.pk1"):
quote_list = []
else:
with open("quote_file.pk1", "rb") as quote_file:
quote_list = pickle.load(quote_file)
quote_list.append(answer)
with open("quote_file.pk1", "wb") as quote_file:
pickle.dump(quote_list, quote_file)
await bot.say('Quote: "{}" added!'.format(answer))
# .quote
@bot.command(pass_context = True)
async def quote(ctx):
with open("quote_file.pk1", "rb") as quote_file:
quote_list = pickle.load(quote_file)
await bot.say(random.choice(quote_list))
bot.run("TOKEN")
基本上,我想添加另一个功能,.removequote()
可以删除特定引号,同时保持其他引号不变。
删除引号与添加引号非常相似,只有一行需要替换。代替 .append,使用列表 comp 来过滤掉不需要的引用。其他一切都可以保持不变。
# .removequote
@bot.command(pass_context = True)
async def removequote(ctx, *answer):
answer=" ".join(answer)
if not os.path.isfile("quote_file.pk1"):
quote_list = []
else:
with open("quote_file.pk1", "rb") as quote_file:
quote_list = pickle.load(quote_file)
# This line:
quote_list = [quote for quote in quote_list if quote != answer]
with open("quote_file.pk1", "wb") as quote_file:
pickle.dump(quote_list, quote_file)
await bot.say('Quote: "{}" deleted!'.format(answer))
每个服务器 pickling,你可以为每个服务器使用不同的文件名,或者为你的 pickle 使用字典(我个人认为存储在一个单独的文件中更有效,因为你不必加载其他服务器的引号):
# .removequote
@bot.command(pass_context = True)
async def removequote(ctx, *answer):
answer=" ".join(answer)
filename = "quote_file_{}.pkl".format(ctx.message.server.id)
if not os.path.isfile(filename):
quote_list = []
else:
with open(filename, "rb") as quote_file:
quote_list = pickle.load(quote_file)
quote_list = [quote for quote in quote_list if quote != answer]
with open(filename, "wb") as quote_file:
pickle.dump(quote_list, quote_file)
await bot.say('Quote: "{}" deleted!'.format(answer))
我正在为我的一个朋友制作一个 discord.py
机器人。我想这样做,以便您可以将愚蠢的引号添加到列表中,然后随机选择它们。就这么多,我做到了。
但是,我也希望能够删除不相关的、拼写错误的引号等。我想知道如何才能做到这一点。
这是我目前得到的:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import random
import pickle
import os
Client = discord.Client()
bot_prefix = "."
bot = commands.Bot(command_prefix=bot_prefix)
@bot.event
async def on_ready():
print("Bot Online!")
print("Name: {}".format(bot.user.name))
print("ID: {}".format(bot.user.id))
class Main_Commands():
def __init__(self, bot):
self.bot = bot
# .addquote
@bot.command(pass_context = True)
async def addquote(ctx, *answer):
answer=" ".join(answer)
if not os.path.isfile("quote_file.pk1"):
quote_list = []
else:
with open("quote_file.pk1", "rb") as quote_file:
quote_list = pickle.load(quote_file)
quote_list.append(answer)
with open("quote_file.pk1", "wb") as quote_file:
pickle.dump(quote_list, quote_file)
await bot.say('Quote: "{}" added!'.format(answer))
# .quote
@bot.command(pass_context = True)
async def quote(ctx):
with open("quote_file.pk1", "rb") as quote_file:
quote_list = pickle.load(quote_file)
await bot.say(random.choice(quote_list))
bot.run("TOKEN")
基本上,我想添加另一个功能,.removequote()
可以删除特定引号,同时保持其他引号不变。
删除引号与添加引号非常相似,只有一行需要替换。代替 .append,使用列表 comp 来过滤掉不需要的引用。其他一切都可以保持不变。
# .removequote
@bot.command(pass_context = True)
async def removequote(ctx, *answer):
answer=" ".join(answer)
if not os.path.isfile("quote_file.pk1"):
quote_list = []
else:
with open("quote_file.pk1", "rb") as quote_file:
quote_list = pickle.load(quote_file)
# This line:
quote_list = [quote for quote in quote_list if quote != answer]
with open("quote_file.pk1", "wb") as quote_file:
pickle.dump(quote_list, quote_file)
await bot.say('Quote: "{}" deleted!'.format(answer))
每个服务器 pickling,你可以为每个服务器使用不同的文件名,或者为你的 pickle 使用字典(我个人认为存储在一个单独的文件中更有效,因为你不必加载其他服务器的引号):
# .removequote
@bot.command(pass_context = True)
async def removequote(ctx, *answer):
answer=" ".join(answer)
filename = "quote_file_{}.pkl".format(ctx.message.server.id)
if not os.path.isfile(filename):
quote_list = []
else:
with open(filename, "rb") as quote_file:
quote_list = pickle.load(quote_file)
quote_list = [quote for quote in quote_list if quote != answer]
with open(filename, "wb") as quote_file:
pickle.dump(quote_list, quote_file)
await bot.say('Quote: "{}" deleted!'.format(answer))