Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import numpy as np
  2. import pyaudio
  3. import discord
  4. import time
  5.  
  6.  
  7. CHANNELS = 2
  8. RATE = 48000
  9.  
  10. TOKEN = 'NDY5NTE2MDQ0OTgyMjg4Mzg0.DjJIhg.NAkdkg2IdKSuPs12qC4OEg3PyfA'
  11.  
  12.  
  13. class StreamReader(object):
  14. def __init__(self, stream: pyaudio.Stream):
  15. self.stream = stream
  16.  
  17. def read(self, frame_size: int):
  18. return self.stream.read(frame_size // (2 * CHANNELS), False)
  19.  
  20.  
  21. def finalize(stream, p):
  22. stream.stop_stream()
  23. print("Stream is stopped")
  24. stream.close()
  25. p.terminate()
  26.  
  27.  
  28. def main():
  29. client = discord.Client()
  30. p = pyaudio.PyAudio()
  31. device_names = [p.get_device_info_by_index(i).get('name') for i in range(p.get_device_count())]
  32. vb_out = [i for i, name in enumerate(device_names) if name.startswith('CABLE Output (VB')]
  33. idx = vb_out[0]
  34.  
  35. print('Starting stream with input device {} (index: {})'.format(device_names[idx], idx))
  36. stream = p.open(format=p.get_format_from_width(2), channels=CHANNELS, rate=RATE, input=True, input_device_index=idx)
  37.  
  38. @client.event
  39. async def on_message(message):
  40. # we do not want the bot to reply to itself
  41. if message.author == client.user:
  42. return
  43.  
  44. if message.content.startswith('!test'):
  45. voice = await client.join_voice_channel(message.author.voice_channel)
  46. voice.encoder_options(sample_rate=RATE, channels=CHANNELS)
  47. player = voice.create_stream_player(StreamReader(stream), after=lambda: finalize(stream, p))
  48. player.start()
  49. else:
  50. print(message.content)
  51.  
  52. @client.event
  53. async def on_ready():
  54. print('Logged in as')
  55. print(client.user.name)
  56. print(client.user.id)
  57. print('------')
  58.  
  59. client.run(TOKEN)
  60.  
  61.  
  62. if __name__ == '__main__':
  63. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement