【问题标题】:How can you remove a quote from a list?如何从列表中删除报价?
【发布时间】:2018-05-13 02:49:50
【问题描述】:

我正在为我的一个朋友制作一个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(),它可以删除特定的引用,同时保持其他引用不变。

【问题讨论】:

    标签: python bots discord discord.py


    【解决方案1】:

    删除引号与添加引号非常相似,只需替换一行。而不是 .append,使用列表组合过滤掉不需要的引用。其他一切都可以保持不变。

    # .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))
    

    每个服务器腌制,您可以为每个服务器使用不同的文件名,或者为您的腌制使用 dict(我个人认为存储在单独的文件中更有效,因为您不必加载其他服务器的报价) :

    # .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))
    

    【讨论】:

    • 谢谢!有没有办法为每台服务器制作一个单独的列表?
    • @Lemon 根据消息来自哪个服务器分离泡菜文件,或将信息存储在某种数据库中(并将原始服务器位置信息与报价本身一起存储)。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2022-06-10
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    相关资源
    最近更新 更多