1
votes

I am trying to create a python script that would mute the users of a specific voice channel when executed. Till now I am able to mute everyone by typing the command in the discord text channel, but I would like to mute everyone when another python script tells the bot to. Below is my attempt to do that.

bot.py is the 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 is the other python script trying to call a function:

from bot import play_music_test
import asyncio

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

I ran the bot.py first and then tried to execute the abc.py next, bot came online but when executing abc.py, it didn't print or do anything at all. I just started to learn discord.py so if this is a silly question, forgive me.

3

3 Answers

2
votes

Instead of running the python file you can import it, or making it a cog heres an example:

importing: 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)

importing: abc.py remove asyncio.run(play_music_test()) use it in discord instead! ex. play_music_test #general

Making it a cog: 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)

Making it a cog: 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))

This anwser is not the best but hope this helps!

0
votes

i'm not sure, if i really understand, what you try to do, but to open a script from another script, i use subprocess.Popen()

but i think, in your case it fits more, to change the way you try to solve your problem. maybe you should take a look into the on_message event and use message.content ?

0
votes

You can importing your client variable (from bot import play_music_test, client) and then client.loop.create_task(play_music_test())

(Although I do not understand why you would do such a thing)