Advertisement
Sweetening

Discord GPT

Nov 19th, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. import discord
  2. import asyncio
  3. from google.oauth2.credentials import Credentials
  4. from googleapiclient.discovery import build
  5. from discord.ext import commands
  6. from discord.utils import get
  7.  
  8. # Set up the Discord bot
  9. bot = commands.Bot(command_prefix='!')
  10.  
  11. # Set up the Google Bard API
  12. credentials = Credentials(token=YOUR_TOKEN)
  13. bard_service = build('bard', 'v1', credentials=credentials)
  14.  
  15. # Create a function to get the Bard response
  16. async def get_bard_response(prompt):
  17. request = bard_service.documents().create(body={'document': {'input': prompt}})
  18. response = await request.execute()
  19. return response['document']['outputs'][0]['text']
  20.  
  21. # Create a function to handle chat messages
  22. async def handle_chat_message(message):
  23. if not message.author.bot:
  24. bard_response = await get_bard_response(message.content)
  25. await message.channel.send(bard_response)
  26.  
  27. # Create a function to handle commands
  28. async def handle_command(message):
  29. if message.author.bot:
  30. return
  31. if message.content.startswith('!'):
  32. command = message.content[1:].split(' ')[0]
  33. args = message.content[1:].split(' ')[1:]
  34.  
  35. if command == 'chat':
  36. if len(args) > 0:
  37. prompt = ' '.join(args)
  38. bard_response = await get_bard_response(prompt)
  39. await message.channel.send(bard_response)
  40. else:
  41. await message.channel.send('Please provide a message to chat with Bard.')
  42.  
  43. # Create a function to handle reactions
  44. async def handle_reaction(reaction):
  45. if reaction.emoji == '👍':
  46. message = reaction.message
  47. bard_response = await get_bard_response(message.content)
  48. await message.channel.send(bard_response)
  49.  
  50. # Set up the event listeners
  51. @bot.event
  52. async def on_ready():
  53. print('Bot is ready!')
  54.  
  55. @bot.event
  56. async def on_message(message):
  57. await handle_chat_message(message)
  58.  
  59. @bot.event
  60. async def on_command_error(ctx, error):
  61. if isinstance(error, commands.CommandNotFound):
  62. await ctx.send('Invalid command.')
  63. elif isinstance(error, commands.MissingRequiredArgument):
  64. await ctx.send('Please provide the required arguments.')
  65. else:
  66. await ctx.send(f'An error occurred: {error}')
  67.  
  68. @bot.event
  69. async def on_raw_reaction_add(payload):
  70. message = await get(payload.channel_id, payload.message_id)
  71. for reaction in message.reactions:
  72. if reaction.count > 0:
  73. await handle_reaction(reaction)
  74.  
  75. # Run the bot
  76. bot.run('YOUR_BOT_TOKEN')
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement