Advertisement
Guest User

DiscordBotTut2

a guest
Jun 25th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. import discord
  2. import asyncio
  3. from discord.voice_client import VoiceClient
  4. from discord.ext.commands import Bot
  5. from discord.ext import commands
  6.  
  7. Client = discord.Client()
  8. bot_prefix= "."
  9. client = commands.Bot(command_prefix=bot_prefix)
  10. vc_clients = {}
  11.  
  12. @client.event
  13. async def on_ready():
  14. global playingsong
  15. print("Bot Online!")
  16. print("Name: {}".format(client.user.name))
  17. print("ID: {}".format(client.user.id))
  18. await client.change_presence(game=discord.Game(name='type .help'))
  19. playingsong = False
  20.  
  21. @client.command(pass_context=True)
  22. async def ping(ctx):
  23. await client.say("Server is responsive!")
  24.  
  25. #Allows the bot to connect:
  26. @client.command(pass_context=True)
  27. async def connect(ctx):
  28. if client.is_voice_connected(ctx.message.server):
  29. return await client.say("I am already connected to a voice channel. Do not disconnect me if I am in use!")
  30. author = ctx.message.author
  31. voice_channel = author.voice_channel
  32. vc = await client.join_voice_channel(voice_channel)
  33.  
  34. #Allows the bot to disconnect:
  35. @client.command(pass_context = True)
  36. async def disconnect(ctx):
  37. if ctx.message.server.id not in vc_clients:
  38. return await client.say("I am not in any voice channel!")
  39.  
  40. try:
  41. vc_clients[ctx.message.server.id][1].stop()
  42. except:
  43. pass
  44.  
  45. await vc_clients[ctx.message.server.id][0].disconnect()
  46. return await client.say("Disconnected from voice channel!")
  47.  
  48. #Allows the bot to play music:
  49. @client.command(pass_context = True)
  50. async def play(ctx, url):
  51. global playingsong
  52. message = ctx.message
  53. print(url);
  54.  
  55. if ctx.message.server.id not in vc_clients:
  56. try:
  57. vc = await client.join_voice_channel(message.author.voice_channel)
  58. vc_clients[message.server.id] = [vc]
  59.  
  60. except Exception as e:
  61. print(e)
  62. return await client.say("You are not in any voice channel.")
  63.  
  64. try:
  65.  
  66. mg = await client.say("Loading...")
  67. player = await vc_clients[ctx.message.server.id][0].create_ytdl_player(url)
  68. if (player.is_playing() == False):
  69. playingsong = False
  70. if (playingsong == True):
  71. await client.say("I am playing a song! Queue function will be added later.")
  72. else:
  73. vc_clients[message.server.id].append(player)
  74. player.start()
  75. playingsong = True
  76. embed = discord.Embed(title = "Now playing :musical_note:", description = str(player.title), color = 0x0000FF)
  77. embed.add_field(name = "Duration (in seconds)", value = str(player.duration))
  78. embed.add_field(name = "Requested by", value = str(message.author))
  79. await client.delete_message(mg)
  80. return await client.say(embed = embed)
  81.  
  82. except Exception as e:
  83. return await client.say("ERROR: `%s"%e)
  84. #Allows the bot to clear chat (up to fourteen days.)
  85. @client.command(pass_context = True)
  86. async def clear(ctx, number):
  87. mgs = []
  88. number = int(number) #Converting the amount of messages to delete to an integer
  89. async for x in client.logs_from(ctx.message.channel, limit = number):
  90. mgs.append(x)
  91. await client.delete_messages(mgs)
  92. await client.say(str(number) + " messages cleared!")
  93.  
  94. #Gets a list of all banned users on the server:
  95. @client.command(pass_context = True)
  96. async def getbans(ctx):
  97. x = await client.get_bans(ctx.message.server)
  98. x = '\n'.join([y.name for y in x])
  99. embed = discord.Embed(title = "List of Banned Members", description = x, color = 0xFFFFF)
  100. return await client.say(embed = embed)
  101. #Stops the song that is playing
  102. @client.command(pass_context = True)
  103. async def stop(ctx):
  104. global playingsong
  105. player = vc_clients[ctx.message.server.id][1]
  106. player.stop()
  107. playingsong = False
  108.  
  109. #General commands:
  110.  
  111. @client.command(pass_context = True)
  112. async def purpose(*args):
  113. return await client.say("I am a very complex AI that was designed to serve my owner.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement