Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 如何重用命令函数discord.py_Python_Python 3.x_Discord_Discord.py - Fatal编程技术网

Python 如何重用命令函数discord.py

Python 如何重用命令函数discord.py,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,我有一个音乐机器人,它可以播放广播,每一个音乐流都是一个易于使用的命令。问题是现在有100多个站点,更新每个站点的代码变得越来越困难 @bot.command(aliases=["VAR1", "VAR2"]) @commands.check(if_channel_private) async def VAR(ctx): current_time = datetime.now().strftime("%H:%M") st

我有一个音乐机器人,它可以播放广播,每一个音乐流都是一个易于使用的命令。问题是现在有100多个站点,更新每个站点的代码变得越来越困难

@bot.command(aliases=["VAR1", "VAR2"])
@commands.check(if_channel_private)
async def VAR(ctx):
    current_time = datetime.now().strftime("%H:%M")
    station = 'VAR3'
    if len(embed_history.fields) <= 4:
        embed_history.add_field(inline=False, name=f"[{current_time}] Played:", value=f'`{station}`')
    elif len(embed_history.fields) > 4:
        embed_history.remove_field(0)
        embed_history.add_field(inline=False, name=f"[{current_time}] Played:", value=f'`{station}`')
    stream = 'URL VAR4'
    resume = stream
    if len(pause_list) != 0 or len(radio_station) != 0:
        pause_list.clear()
        radio_station.clear()
    voice = get(bot.voice_clients, guild=ctx.guild)
    if voice.is_playing():
        voice.stop()
    try:
        voice.play(FFmpegPCMAudio(stream))
        print(f'Playing {station}')
    except:
        print(f"An error occurred while trying to play {station}")
        await ctx.send(error_message)
    radio_station.append(station)
    pause_list.append(resume)
    embed_st.clear_fields()
    embed_st.add_field(inline=False, name=embed_name,
                       value="VAR5")
    await ctx.send(embed=embed_st)

@VAR.error
async def _pr_error(ctx, error):
    if isinstance(error, commands.CheckFailure):
        if ctx.message.author.voice is None:
            return
        await ctx.send(f"Only users in __the same private voice channel__ can change the station!")
    else:
        await ctx.send(f"```css\n"
                       "[ERROR: Unexpected error has occured!]"
                       "```")
        print(error)

这是每个电台的情况。是否可以只写一次,然后为每个电台调用它?只需要改变变量VAR?否则,它会用重复的代码膨胀整个文本文件…

我不检查此代码,但可能是它的工作。创建函数,并向其发送变量station和stream:

@bot.commandAlias=[VAR,VAR] @commands.checkif\u channel\u private 异步def VARctx: 等待一些函数'VAR','URL\u VAR' 您的功能适用于所有电台 异步定义某些函数站、流: 当前时间=日期时间。现在。strftime%H:%M 如果选择“历史记录”字段4: 嵌入\u历史记录。删除\u字段0 embed_history.add_fieldinline=False,name=f[{current_time}]Played:,value=f'`{station}`' 恢复=流 如果要暂停列表!=0或Lenu电台!=0: 暂停列表。清除 无线电台,明白了 voice=getbot.voice\u客户端,guild=ctx.guild 如果voice.u正在播放: 声音,停 尝试: voice.playffmpegpcaudiostream printf'Playing{station}' 除: 尝试播放{station}时发生printfAn错误 等待ctx.senderror\u消息 电台 暂停列表。继续 嵌入\u st.清除\u字段 embed\u st.add\u fieldinline=False,name=embed\u name, 价值=风险值 等待ctx.sendembed=embed\u st