Skip to content

Commit

Permalink
custom prefix, unused cmds removed
Browse files Browse the repository at this point in the history
  • Loading branch information
ygorpontelo committed May 6, 2023
1 parent 39a921b commit b1cd669
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

Once you configure your own personal bot, [see tutorial here](https://discordpy.readthedocs.io/en/stable/discord.html), place the token in a txt file in the same folder called token.txt.

This bot uses Pyaudio to read any available input and stream the audio to discord. Only mono and stereo inputs are supported (1 and 2 channels). The script will present a nice interface to select those.
This bot uses Pyaudio to read any available input and stream the audio to discord. Only mono and stereo inputs are supported (1 and 2 channels). The script will present a nice CLI interface to select those.

This bot does not create a device input, only reads available ones. If you want to redirect audio using virtual inputs, you need an interface to do that, like [Jack audio](https://jackaudio.org/) on Linux or [VB-Cable](https://vb-audio.com/Cable/) on Windows.

Poetry is the recommended way to manage dependencies, but you can install them via pip normally with requirements.txt (only includes main dependencies).

To compile the bot to exe, use this command:

```
pyinstaller --console --onefile --collect-all discord --collect-all samplerate --collect-all pyaudio main.py
```
34 changes: 8 additions & 26 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,15 @@ def read(self) -> bytes:
return frame.tobytes()


def create_bot(device, audio) -> commands.Bot:
def create_bot(bot_prefix, device, audio) -> commands.Bot:
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", description="Discord Audio Stream Bot", intents=intents)
bot = commands.Bot(command_prefix=bot_prefix, description="Discord Audio Stream Bot", intents=intents)

@bot.event
async def on_ready():
print(f'Logged in as {bot.user} (ID: {bot.user.id})')
print('------')

@bot.command(name="join", aliases=["j"], help="Join user channel")
async def join(ctx):
"""Joins a voice channel"""

channel = ctx.author.voice.channel
if ctx.voice_client is not None:
return await ctx.voice_client.move_to(channel)

await channel.connect()
print('-'*100)

@bot.command(name="play", aliases=["p"], help="Play audio")
async def play(ctx):
Expand All @@ -68,22 +58,12 @@ async def play(ctx):

await ctx.send(f'Streaming From {device["name"]}')

@bot.command(name="volume", aliases=["v"], help="Change bot volume")
async def volume(ctx, volume: int):
"""Changes the player's volume"""

if ctx.voice_client is None:
return await ctx.send("Not connected to a voice channel.")

ctx.voice_client.source.volume = volume / 100
await ctx.send(f"Changed volume to {volume}%")

@bot.command(name="stop", aliases=["s"], help="Disconnect bot")
async def stop(ctx):
"""Stops and disconnects the bot from voice"""

await ctx.voice_client.disconnect()

@play.before_invoke
async def ensure_voice(ctx):
if ctx.voice_client is None:
Expand All @@ -94,7 +74,7 @@ async def ensure_voice(ctx):
raise commands.CommandError("Author not connected to a voice channel.")
elif ctx.voice_client.is_playing():
ctx.voice_client.stop()

return bot


Expand All @@ -107,6 +87,8 @@ async def ensure_voice(ctx):
input("Press Enter to exit")
sys.exit(1)

bot_prefix = questionary.text("Bot Prefix: ").ask().strip()

p = pyaudio.PyAudio()

# get all available inputs, mono or stereo, with all possible drivers
Expand All @@ -127,4 +109,4 @@ async def ensure_voice(ctx):
).ask()

# start bot
create_bot(device_inputs[answer], p).run(token)
create_bot(bot_prefix, device_inputs[answer], p).run(token)

0 comments on commit b1cd669

Please sign in to comment.