Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

nec_discord_bot.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import discord
  2. import youtube_dl
  3. from discord.ext import commands
  4. from discord import FFmpegPCMAudio
  5. import sys
  6. client = commands.Bot(command_prefix = "!")
  7. youtube_dl.utils.bug_reports_message = lambda: ''
  8. ytdl_format_options = {
  9. 'format': 'bestaudio/best',
  10. 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
  11. 'restrictfilenames': True,
  12. 'noplaylist': True,
  13. 'nocheckcertificate': True,
  14. 'ignoreerrors': False,
  15. 'logtostderr': False,
  16. 'quiet': True,
  17. 'no_warnings': True,
  18. 'default_search': 'auto',
  19. 'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
  20. }
  21. ffmpeg_options = {
  22. 'options': '-vn'
  23. }
  24. ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
  25. class YTDLSource(discord.PCMVolumeTransformer):
  26. def __init__(self, source, *, data, volume=0.5):
  27. super().__init__(source, volume)
  28. self.data = data
  29. self.title = data.get('title')
  30. self.url = data.get('url')
  31. @classmethod
  32. async def from_url(cls, url, *, loop=None, stream=False):
  33. loop = loop or asyncio.get_event_loop()
  34. data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
  35. if 'entries' in data:
  36. # take first item from a playlist
  37. data = data['entries'][0]
  38. filename = data['url'] if stream else ytdl.prepare_filename(data)
  39. return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
  40. @client.event
  41. async def on_ready():
  42. print('We have logged in as {0.user}'.format(client))
  43. @client.command(pass_context=True)
  44. async def join(ctx):
  45. print("join")
  46. channel = ctx.message.author.voice.channel
  47. await channel.connect()
  48. @client.command(pass_context=True)
  49. async def leave(ctx):
  50. print("leave")
  51. channel = ctx.message.author.voice.channel
  52. await ctx.voice_client.disconnect()
  53. @client.command(pass_context=True)
  54. async def yt(ctx, url):
  55. async with ctx.typing():
  56. player = await YTDLSource.from_url(url, loop=client.loop, stream=True)
  57. ctx.voice_client.play(player, after=lambda e: print("Player error: %s" % e) if e else None)
  58. await ctx.send("Now playing: {}".format(player.title))
  59. @client.command(pass_context=True)
  60. async def volume(ctx, volume: int):
  61. if ctx.voice_client is None:
  62. return await ctx.send("Kein Audiochannel gefunden")
  63. ctx.voice_client.source.volume = volume / 100
  64. await ctx.send("Changed volume to {}".format(volume))
  65. @yt.before_invoke
  66. async def ensure_voice(ctx):
  67. if ctx.voice_client is None:
  68. if ctx.author.voice:
  69. await ctx.author.voice.channel.connect()
  70. else:
  71. await ctx.send("You are not connected to a voice channel.")
  72. raise commands.CommandError("Author not connected to a voice channel.")
  73. elif ctx.voice_client.is_playing():
  74. ctx.voice_client.stop()
  75. @client.command(pass_context=True)
  76. async def shutdown(ctx):
  77. sys.exit()
  78. client.run("NTAyMTY4Njk1MjIxNjQ5NDIw.DqkBOw.01VYdcjPC-wN1ARVVBPpix-1Fj8")