Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import numpy as np
  2. import pyaudio
  3. import discord
  4. import time
  5.  
  6.  
  7. CHANNELS = 1
  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, 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. stream = p.open(format=p.get_format_from_width(2), channels=CHANNELS, rate=RATE, input=True, start=False)
  32.  
  33. @client.event
  34. async def on_message(message):
  35. # we do not want the bot to reply to itself
  36. if message.author == client.user:
  37. return
  38.  
  39. if message.content.startswith('!test'):
  40. voice = await client.join_voice_channel(message.author.voice_channel)
  41. voice.encoder_options(sample_rate=RATE, channels=CHANNELS)
  42.  
  43. stream.start_stream()
  44. player = voice.create_stream_player(StreamReader(stream), after=lambda: finalize(stream, p))
  45. player.start()
  46. else:
  47. print(message.content)
  48.  
  49. @client.event
  50. async def on_ready():
  51. print('Logged in as')
  52. print(client.user.name)
  53. print(client.user.id)
  54. print('------')
  55.  
  56. client.run(TOKEN)
  57.  
  58.  
  59. if __name__ == '__main__':
  60. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement