Facebook
From Diminutive Elephant, 1 Year ago, written in Python.
This paste is a reply to Discord Chat bot from Adrian Maximus - view diff
Embed
Download Paste or View Raw
Hits: 214
  1. #main.py
  2. import discord
  3. from keep_alive import keep_alive
  4. from discord.ext import commands
  5. import os
  6. import openai
  7.  
  8. #make sure you have import all the above
  9.  
  10. bot = commands.Bot(
  11.   command_prefix='?', #any prefix you want
  12.   case_insensitive=False,
  13.   description=None,
  14.   intents=discord.Intents.all(), #enable intents in discord developer portal
  15.   help_command=None
  16. )
  17.  
  18. @bot.command()
  19. async def test(ctx,*,arg): # * is used to make sure your complete arguement is used rather than first word
  20.   query = ctx.message.content
  21.   response = openai.Completion.create(
  22.     api_key = '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', #put your own api key here, mine doesnt work i deleted mine ?
  23.     model="text-davinci-003",
  24.     prompt=query,
  25.     temperature=0.5,
  26.     max_tokens=60,
  27.     top_p=0.3,
  28.     frequency_penalty=0.5,
  29.     presence_penalty=0.0
  30.   )
  31.   await ctx.channel.send(content=response['choices'][0]['text'].replace(str(query), ""))
  32.  
  33. @bot.event
  34. async def on_ready():
  35.   await bot.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.listening, name=f"Hi"))
  36.   print(f"Logged in as {bot.user.name}")
  37.  
  38. keep_alive()
  39.  
  40.  
  41. bot.run(os.environ['BOT KEY']) #create a secret token named BOT KEY and paste ur token
  42.  
  43. #keep_alive.py -> new file
  44.  
  45. from flask import Flask
  46. from threading import Thread
  47.  
  48. app = Flask('')
  49.  
  50. @app.route('/')
  51. def home():
  52.     return "Hello. I am alive!"
  53.  
  54. def run():
  55.   app.run(host='0.0.0.0',port=8080)
  56.  
  57. def keep_alive():
  58.     t = Thread(target=run)
  59.     t.start()