-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
206 lines (162 loc) · 6.41 KB
/
bot.py
File metadata and controls
206 lines (162 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import os
import discord
from discord.ext import commands, tasks
from dataclasses import dataclass
from WebScraping import Scraper
from news import News
from gpt import AI
import asyncio
from fixblocking import NoBlock
BOT_TOKEN = os.environ["DISCORD_API_KEY"]
CHANNEL_ID = 996336625556652102
bot = commands.Bot(command_prefix="/", intents=discord.Intents.all())
class Web:
def __init__(self, web_link):
self.web_link = web_link
#Program varibles
stocknames=[]
channelID=996336625556652102
website=Web("")
@bot.command()
async def sync(ctx: commands.Context, guild: discord.Guild = None) -> None:
print("Syncing commands")
sync=0
if guild is None:
print("Guild is None")
sync=len(await bot.tree.sync())
else:
print("Guild found")
sync=len(await bot.tree.sync(guild=guild))
print(f"Synced {sync} command(s)")
@bot.command()
async def setCID(ctx: commands.Context):
channelID=ctx.channel.id
print(channelID)
await ctx.send("Chennel ID has been set!")
@bot.event
async def on_ready():
print("Is ready!")
channel = bot.get_channel(CHANNEL_ID)
await channel.send("Wussup")
@bot.tree.command(name="news")
async def news(interaction: discord.Interaction, link: str):
await interaction.response.send_message("Please wait...", ephemeral=True)
website.web_link=link
await get_news()
await interaction.delete_original_response()
# print(args[2])
@tasks.loop(minutes=60)
async def get_news():
media = Scraper(website.web_link)
string = media.scrape()
await bot.get_channel(channelID).send(string)
@tasks.loop(minutes=60)
async def stocknews():
try:
msg=await bot.get_channel(channelID).send("Please wait...")
print("len of stocknames: "+str(len(stocknames)))
for stock in stocknames:
news_obj=News(stock)
result = await newsAPI(stock)
embed = discord.Embed(title="Stock: " + stock, colour=discord.Colour(0x3e038c))
embed.add_field(name="Investment case:", value=result, inline=False)
embed.add_field(name="Current price:", value=news_obj.getStockPrice()[1], inline=False)
await bot.get_channel(channelID).send(embed=embed)
await msg.delete()
except Exception as e:
# Handle other exceptions
print(f"An error occurred: {e}")
@bot.tree.command(name="ai")
async def ai(interaction: discord.Interaction, prompt: str):
try:
await interaction.response.send_message("Please wait...", ephemeral=True)
channel = bot.get_channel(interaction.channel.id)
task = asyncio.create_task(run_ai(prompt, interaction.channel.id))
result = await task
print(result)
await interaction.delete_original_response()
embed = discord.Embed(title="Author: " + interaction.user.name, colour=discord.Colour(0x3e038c))
embed.add_field(name="Prompt:", value=prompt, inline=False)
await channel.send(embed=embed)
await channel.send(f"{interaction.user.mention} " + result)
except Exception as e:
print(f"An error occurred: {e}")
@NoBlock.to_thread
def run_ai(prompt, channel_id):
ai_chat = AI(prompt)
string = ai_chat.askAI()
channel = bot.get_channel(channel_id)
return string
async def newsAPI(prompt):
news_obj = News(prompt)
resp = await news_obj.getNews()
return resp
@bot.tree.command(name="startstockloop")
async def startStock(interaction: discord.Interaction):
await interaction.response.send_message("Starting stock loop", ephemeral=True)
try:
await stocknews.start()
except Exception as e:
# Handle other exceptions
print(f"An error occurredj: {e}")
await interaction.delete_original_response()
# @bot.tree.command(name="stopstockloop")
@bot.tree.command(name="stopstockloop")
async def startStock(interaction: discord.Interaction):
await interaction.response.send_message("Stopping stock loop", ephemeral=True)
try:
await newsAPI.stop()
except Exception as e:
# Handle other exceptions
print(f"An error occurred: {e}")
await interaction.delete_original_response()
@bot.tree.command(name="startnewsloop")
async def startStock(interaction: discord.Interaction):
await interaction.response.send_message("Starting news loop", ephemeral=True)
try:
await get_news.start()
except Exception as e:
# Handle other exceptions
print(f"An error occurred: {e}")
await interaction.delete_original_response()
# @bot.tree.command(name="stopnewsloop")
@bot.tree.command(name="stopnewsloop")
async def startStock(interaction: discord.Interaction):
await interaction.response.send_message("Stopping news loop", ephemeral=True)
try:
await get_news.stop()
except Exception as e:
# Handle other exceptions
print(f"An error occurred: {e}")
await interaction.delete_original_response()
@bot.tree.command(name="addstock")
async def startStock(interaction: discord.Interaction, prompt: str):
await interaction.response.send_message("Please wait", ephemeral=True)
news_obj=News(prompt)
stock_name=news_obj.getStock()
if stock_name==-1:
await bot.get_channel(channelID).send("Invalid stock or description")
await interaction.delete_original_response()
else:
stocknames.append(stock_name)
await bot.get_channel(channelID).send("Successfully added: "+stock_name)
await interaction.delete_original_response()
@bot.tree.command(name="getprice")
async def getprice(interaction: discord.Interaction, prompt: str):
await interaction.response.send_message("Please wait", ephemeral=True)
news_obj=News(prompt)
price=news_obj.getStockPrice()
if price==-1:
await bot.get_channel(channelID).send("Invalid stock or description")
else:
embed = discord.Embed(title="Stock: " + price[0], colour=discord.Colour(0x3e038c))
embed.add_field(name="Price:", value=price[1], inline=False)
await bot.get_channel(channelID).send(embed=embed)
await interaction.delete_original_response()
@bot.tree.command(name="addnews")
async def startStock(interaction: discord.Interaction, prompt: str):
await interaction.response.send_message("Please wait", ephemeral=True)
website.web_link=prompt
await bot.get_channel(channelID).send("Adding the website to the list: "+website.web_link)
await interaction.delete_original_response()
bot.run(BOT_TOKEN)