Amazing-Python-Scripts

Форк
0
82 строки · 2.2 Кб
1
# The required libraries
2

3
import discord
4
import schedule
5
import asyncio
6
import requests
7
import openai
8

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

12

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

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

19
url = "https://newsapi.org/v2/top-headlines"
20
params = {
21
    "country": "us",
22
    "apiKey": "YOUR_NEWS_API_KEY"
23
    # Generate your API key from https://newsapi.org
24
}
25

26

27
intents = discord.Intents.default()
28
intents.members = True
29

30
client = discord.Client(intents=intents)
31

32
latest_url = ""
33

34

35
def get_latest_article():
36
    global latest_article
37
    response = requests.get(url, params=params)
38
    articles = response.json()['articles']
39
    latest_article = articles[0]
40
    return latest_article
41

42

43
async def send_article():
44
    global latest_url
45
    latest_article = get_latest_article()
46
    if latest_article['url'] != latest_url:
47
        channel = await client.fetch_channel(CHANNEL_ID)
48
        message = f"New article: {latest_article['title']} - {latest_article['url']}"
49
        await channel.send(message)
50
        latest_url = latest_article['url']
51
        response_text = f'\n {message}'
52

53
        # Feel free to add your own prompts
54
        prompt = "Come up with a sarcastic response to this news article " + response_text
55
        response = openai.Completion.create(
56
            model="text-davinci-003",
57
            prompt=prompt,
58
            temperature=0.7,
59
            max_tokens=60,
60
            top_p=1,
61
            frequency_penalty=0,
62
            presence_penalty=0
63
        )
64
        sarcastic_response = response.choices[0].text.strip()
65
        if sarcastic_response:
66
            await channel.send(sarcastic_response)
67
    else:
68
        return 'no update found'
69

70

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

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

81

82
client.run(CLIENT_TOKEN)
83

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.