Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- import asyncio
- from google.oauth2.credentials import Credentials
- from googleapiclient.discovery import build
- from discord.ext import commands
- from discord.utils import get
- # Set up the Discord bot
- bot = commands.Bot(command_prefix='!')
- # Set up the Google Bard API
- credentials = Credentials(token=YOUR_TOKEN)
- bard_service = build('bard', 'v1', credentials=credentials)
- # Create a function to get the Bard response
- async def get_bard_response(prompt):
- request = bard_service.documents().create(body={'document': {'input': prompt}})
- response = await request.execute()
- return response['document']['outputs'][0]['text']
- # Create a function to handle chat messages
- async def handle_chat_message(message):
- if not message.author.bot:
- bard_response = await get_bard_response(message.content)
- await message.channel.send(bard_response)
- # Create a function to handle commands
- async def handle_command(message):
- if message.author.bot:
- return
- if message.content.startswith('!'):
- command = message.content[1:].split(' ')[0]
- args = message.content[1:].split(' ')[1:]
- if command == 'chat':
- if len(args) > 0:
- prompt = ' '.join(args)
- bard_response = await get_bard_response(prompt)
- await message.channel.send(bard_response)
- else:
- await message.channel.send('Please provide a message to chat with Bard.')
- # Create a function to handle reactions
- async def handle_reaction(reaction):
- if reaction.emoji == '👍':
- message = reaction.message
- bard_response = await get_bard_response(message.content)
- await message.channel.send(bard_response)
- # Set up the event listeners
- @bot.event
- async def on_ready():
- print('Bot is ready!')
- @bot.event
- async def on_message(message):
- await handle_chat_message(message)
- @bot.event
- async def on_command_error(ctx, error):
- if isinstance(error, commands.CommandNotFound):
- await ctx.send('Invalid command.')
- elif isinstance(error, commands.MissingRequiredArgument):
- await ctx.send('Please provide the required arguments.')
- else:
- await ctx.send(f'An error occurred: {error}')
- @bot.event
- async def on_raw_reaction_add(payload):
- message = await get(payload.channel_id, payload.message_id)
- for reaction in message.reactions:
- if reaction.count > 0:
- await handle_reaction(reaction)
- # Run the bot
- bot.run('YOUR_BOT_TOKEN')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement