Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

nec_discord_bot.py 2.7KB

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