【问题标题】:How to execute a python discord bot function in another python script?如何在另一个 python 脚本中执行 python discord bot 函数?
【发布时间】:2021-01-19 07:38:35
【问题描述】:

我正在尝试创建一个 python 脚本,该脚本会在执行时使特定语音通道的用户静音。到目前为止,我可以通过在不和谐文本频道中键入命令来使所有人静音,但是当另一个 python 脚本告诉机器人时,我想使所有人静音。下面是我的尝试。

bot.py 是 python discord bot:

import discord
from discord.ext import commands
from time import sleep

client = commands.Bot(command_prefix="./")

@client.event
async def on_ready():
    print("Bot is online. ")

@client.command()
async def play_music_test():
    channel = await client.get_channel(voice_channel_number)
    voice = await channel.connect()
    await voice.play(
        discord.FFmpegPCMAudio(executable="C:/Users/USER/ffmpeg/bin/ffmpeg.exe",
                               source="C:/Users/USER/Desktop/song.mp3"))

client.run(TOKEN)

abc.py 是另一个尝试调用函数的 Python 脚本:

from bot import play_music_test
import asyncio

print("")
asyncio.run(play_music_test())

我先运行bot.py,然后尝试执行abc.py,bot 上线但执行abc.py 时,它没有打印或执行任何操作。我刚开始学习discord.py所以如果这是一个愚蠢的问题,请原谅我。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    你可以导入它,或者将它变成一个 cog,而不是运行 python 文件 举个例子:

    导入:bot.py

    import discord
    from discord.ext import commands
    from time import sleep
    from abc import *
    
    client = commands.Bot(command_prefix="./")
    
    @client.event
    async def on_ready():
        print("Bot is online. ")
    
    @client.command(name = "play_music", aliases = ["you can asign aliases like this", "multiple ones too"])
    async def play_music_test(ctx, channel): # If the python file doesn't know what channel is, it can be asigned when calling the function in discord, think of it like sys.argv or argparse
        if channel == None: channel = ctx.channel # This sets the current channel you are in if none was provided when executing it in discord    
    #    channel = await client.get_channel(channel) now you don't need this line
        voice = await channel.connect()
        await voice.play(
            discord.FFmpegPCMAudio(executable="C:/Users/USER/ffmpeg/bin/ffmpeg.exe",
                                   source="C:/Users/USER/Desktop/song.mp3"))
    
    client.run(TOKEN)
    

    导入:abc.py remove asyncio.run(play_music_test()) 而是在不和谐中使用它!前任。 play_music_test #general

    把它变成一个齿轮:bot.py

    import discord
    from discord.ext import commands
    from time import sleep
    import os
    
    client = commands.Bot(command_prefix="./")
    
    @client.event
    async def on_ready():
        print("Bot is online. ")
    
    
    
    for filename in os.listdir("./"):
        if filename == "bot.py": continue
        else: client.load_extension(f"cogs.{filename[:-3]}")
    
    client.run(TOKEN)
    

    把它变成一个齿轮:abc.py

    from bot import play_music_test
    import asyncio
    class Mycog(commands.Cog):
        def __init__(self, client):
            self.client = client
        
    
    
        @client.command(name = "play_music", aliases = ["you can asign aliases like this", 
        "multiple ones too"])
        async def play_music_test(ctx, channel): # If the python file doesn't know what 
        channel is, it can be asigned when calling the function in discord, think of it like sys.argv or argparse
            if channel == None: channel = ctx.channel # This sets the current channel you are in if none was provided when executing it in discord    
        #    channel = await client.get_channel(channel) now you don't need this line
            voice = await channel.connect()
            await voice.play(
                discord.FFmpegPCMAudio(executable="C:/Users/USER/ffmpeg/bin/ffmpeg.exe",
                                       source="C:/Users/USER/Desktop/song.mp3"))
    
    
    
    
    def setup(client):
        client.add_cog(Mycog(client))
    

    这个anwser不是最好的,但希望这会有所帮助!

    【讨论】:

    • 我要解决的主要问题是从外部 python 代码调用函数,而不是在不和谐聊天中调用它。让我解释。我正在尝试创建一个 python GUI,它将在不和谐的语音通道中播放不同的声音或歌曲。那么如何创建一个在我单击 python GUI 中的按钮时播放声音的函数呢? (我想我们不能这样做)
    【解决方案2】:

    我不确定,如果我真的明白,你想做什么,但要从另一个脚本打开一个脚本,我使用subprocess.Popen()

    但我认为,在你的情况下,它更适合改变你尝试解决问题的方式。也许您应该查看on_message 事件并使用message.content ?

    【讨论】:

    • 当我执行 abc.py 时,我想将 Voice Channel 中的所有人静音。我需要某种方式来命令机器人静音,而不是通过在 Discord 聊天中键入命令,而是通过执行 abc.py。谢谢。
    【解决方案3】:

    您可以导入 client 变量 (from bot import play_music_test, client),然后导入 client.loop.create_task(play_music_test())

    (虽然我不明白你为什么要这么做)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多