Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Discord News Bot With AI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Discord News Bot

A Simple discord news bot which fetches news articles using a news API and responds to them with AI generated prompts, thanks to the OpenAI API.

Note that this bot will take time to set up as it is important to set up all API keys and client secrets properly.

# Steps

- Replace ``YOUR_OPENAI_API_KEY`` with your OpenAI API key
- Replace ``'YOUR_DISCORD_TOKEN`` with your Discord bot's token
- Replace the ``"apiKey"`` value pair in the ``params`` dictionary with your NewsAPI key


Here are the links to all services used:

- https://newsapi.org
- https://platform.openai.com/docs/api-reference
- https://discord.com/developers/docs/intro

Feel free to report any issues
87 changes: 87 additions & 0 deletions Discord News Bot With AI/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# The required libraries

import discord
import schedule
import asyncio
import requests
import openai

openai.api_key = 'YOUR_OPENAI_API_KEY'
# Get a key at https://platform.openai.com/docs/api-reference



CLIENT_TOKEN = 'YOUR_DISCORD_TOKEN'
# Get a token at the discord developer portal https://discord.com/developers/docs/intro

CHANNEL_ID = "CHANNEL_ID"
# The channel ID, AKA the channel in which the bot will send news articles in

url = "https://newsapi.org/v2/top-headlines"
params = {
"country": "us",
"apiKey": "YOUR_NEWS_API_KEY"
# Generate your API key from https://newsapi.org
}



intents = discord.Intents.default()
intents.members = True

client = discord.Client(intents=intents)

latest_url = ""




def get_latest_article():
global latest_article
response = requests.get(url, params=params)
articles = response.json()['articles']
latest_article = articles[0]
return latest_article


async def send_article():
global latest_url
latest_article = get_latest_article()
if latest_article['url'] != latest_url:
channel = await client.fetch_channel(CHANNEL_ID)
message = f"New article: {latest_article['title']} - {latest_article['url']}"
await channel.send(message)
latest_url = latest_article['url']
response_text = f'\n {message}'

# Feel free to add your own prompts
prompt = "Come up with a sarcastic response to this news article " + response_text
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.7,
max_tokens=60,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
sarcastic_response = response.choices[0].text.strip()
if sarcastic_response:
await channel.send(sarcastic_response)
else:
return 'no update found'


@client.event
async def on_ready():
print('Bot is ready.')
schedule.every(10).minutes.do(send_article)

while True:
await send_article()
await asyncio.sleep(10*60) # wait for 10 minutes before sending the next article to avoid using to many resources




client.run(CLIENT_TOKEN)
18 changes: 18 additions & 0 deletions Discord News Bot With AI/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
aiohttp==3.8.4
aiosignal==1.3.1
async-timeout==4.0.2
asyncio==3.4.3
attrs==22.2.0
certifi==2022.12.7
charset-normalizer==3.1.0
discord==2.2.2
discord.py==2.2.2
frozenlist==1.3.3
idna==3.4
multidict==6.0.4
openai==0.27.2
requests==2.28.2
schedule==1.1.0
tqdm==4.65.0
urllib3==1.26.15
yarl==1.8.2