Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pyaudio
- import discord
- import time
- CHANNELS = 2
- RATE = 48000
- TOKEN = 'NDY5NTE2MDQ0OTgyMjg4Mzg0.DjJIhg.NAkdkg2IdKSuPs12qC4OEg3PyfA'
- class StreamReader(object):
- def __init__(self, stream: pyaudio.Stream):
- self.stream = stream
- def read(self, frame_size: int):
- return self.stream.read(frame_size // (2 * CHANNELS), False)
- def finalize(stream, p):
- stream.stop_stream()
- print("Stream is stopped")
- stream.close()
- p.terminate()
- def main():
- client = discord.Client()
- p = pyaudio.PyAudio()
- device_names = [p.get_device_info_by_index(i).get('name') for i in range(p.get_device_count())]
- vb_out = [i for i, name in enumerate(device_names) if name.startswith('CABLE Output (VB')]
- idx = vb_out[0]
- print('Starting stream with input device {} (index: {})'.format(device_names[idx], idx))
- stream = p.open(format=p.get_format_from_width(2), channels=CHANNELS, rate=RATE, input=True, input_device_index=idx)
- @client.event
- async def on_message(message):
- # we do not want the bot to reply to itself
- if message.author == client.user:
- return
- if message.content.startswith('!test'):
- voice = await client.join_voice_channel(message.author.voice_channel)
- voice.encoder_options(sample_rate=RATE, channels=CHANNELS)
- player = voice.create_stream_player(StreamReader(stream), after=lambda: finalize(stream, p))
- player.start()
- else:
- print(message.content)
- @client.event
- async def on_ready():
- print('Logged in as')
- print(client.user.name)
- print(client.user.id)
- print('------')
- client.run(TOKEN)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement