1

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 Answers 3

2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

The main problem I am trying to tackle is to call the function from external python code, rather than calling it in discord chat. Let me explain. I am trying to create a python GUI that will play different sounds or songs in the discord voice channel. So how to create a function that will play a sound when I click a button in the python GUI? ( I guess we cannot do such a thing )
0

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 ?

1 Comment

When I execute abc.py, I would like to mute everyone in the Voice Channel. I need some sort of way to command the bot to mute, not by typing the command in the Discord chat but rather by executing abc.py. Thank you.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.