Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何缩短代码而不是编写相同的重复命令?(discord.py)_Python 3.x_Performance_Discord.py - Fatal编程技术网

Python 3.x 如何缩短代码而不是编写相同的重复命令?(discord.py)

Python 3.x 如何缩短代码而不是编写相同的重复命令?(discord.py),python-3.x,performance,discord.py,Python 3.x,Performance,Discord.py,在编写discord机器人时,我决定发出命令,从一个包含约50幅图像的文件夹中发送图像 import os from PIL import Image from discord.ext import commands import asyncio d = 'reactions/' async def send_reaction(ctx, file): f = discord.File(d+file) await ctx.send(file=f) class memes(co

在编写discord机器人时,我决定发出命令,从一个包含约50幅图像的文件夹中发送图像

import os
from PIL import Image
from discord.ext import commands
import asyncio

d = 'reactions/'

async def send_reaction(ctx, file):
    f = discord.File(d+file)
    await ctx.send(file=f)

class memes(commands.Cog):
    def __init__(self, bot):
        self.bot=bot

    @commands.command()
    async def a(self, ctx):
        await send_reaction(ctx, 'a.mp4')
    
    @commands.command()
    async def drugged(self, ctx):
        await send_reaction(ctx, 'amehypno.png')

    @commands.command()
    async def ameshame(self, ctx):
        await send_reaction(ctx, 'ameshame.jpg')
    
    @commands.command()
    async def amestfu(self, ctx):
        await send_reaction(ctx, 'amestfu.jpg')
    
    @commands.command()
    async def angryshuba(self, ctx):
        await send_reaction(ctx, 'angry.png')
    
    @commands.command()
    async def aproveshuba(self, ctx):
        await send_reaction(ctx, 'aprove.png')

    @commands.command()
    async def aquawhat(self, ctx):
        await send_reaction(ctx, 'aquawhat.png')

    @commands.command()
    async def cringe(self, ctx):
        await send_reaction(ctx, 'bear cringe.jpg')

def setup(bot):
    bot.add_cog(memes(bot))

我本来打算做更多的事情,但我很快意识到,我必须发出40多个基本相同的命令。如何缩短代码?

我将提出以下解决方案:
我无法测试它,因为我不做Discord机器人。所以要小心虫子:D

首先创建一个dict,命令作为键,图像名称作为值

command\u image\u dict={
“麻醉”:“amehypno.png”,
“amesham”:“amesham.jpg”
}
然后创建一个通用函数,该函数根据命令名使用图像进行响应。(这不是实际的命令,而是辅助函数。)

最后,我们使用
setattr
为dict中的每个键创建一个函数。这将创建一个具有所需名称的新函数,并为其分配
memefunction
的功能。由于装饰程序
@commands.command
的存在,我们需要将
memefunction
包装为
commands.command(memefunction)


这(至少在理论上是因为没有测试:D)应该会产生40多个函数/命令。

您真的希望一张图片有一个命令吗?你为什么不做一个模因命令,从文件夹中随机选择一张图片呢?对不起,如果我的代码有点误导的话,它们不是真的模因,而是反应图片。所以,如果我只是从文件夹中随机选取了一幅图像,那实际上是没有用的。你不能只做一个函数,接受期望的反应吗?然后你有一个映射反应->图像的映射。我可能遗漏了一些东西,但这和我现在做的有什么不同?命令的名称告诉他们想要发送什么图像,然后机器人获取相关图像并发送。嗨,谢谢你的帮助!我不熟悉
setattr
,因此我为我的错误道歉。它说,
这个
没有定义。我应该用什么替换它吗?我的错,它是
self
而不是
this
。找不到命令。这是我的代码,你能确保我做的每件事都正确吗?我觉得不错。但是a我说,我不能测试它,所以我不能告诉你为什么它不工作。希望这有助于作为一个起点。否则,使用带有reation的函数作为参数,如@gold\u cy provided。好的,再次感谢您的帮助!
async def memefunction(self, ctx):
    try:
        await send_meme(ctx, command_image_dict[ctx.command])
    except KeyError:
        pass
def __init__(self, bot):
    self.bot = bot
    for key in command_image_dict:
        setattr(self, key, commands.command(memefunction))