-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeInfoBot.py
65 lines (48 loc) · 2.1 KB
/
DeInfoBot.py
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
import discord
import shodan
import json
from discord.ext import commands
class ShodanSearchBot(discord.Client):
def __init__(self, shodan_api_key):
intents = discord.Intents.default()
super().__init__(command_prefix='!', intents=intents)
self.api = shodan.Shodan(shodan_api_key)
self.history = []
async def on_ready(self):
print(f"Logged in as {self.user.name}")
async def on_message(self, message):
if message.author == self.user:
return
if message.content.startswith("!search"):
query = message.content[7:].strip()
try:
results = self.api.search(query)
# Save results to history
self.history.append(results["matches"])
# Send response to Discord channel
response = f"Showing {len(results['matches'])} results for '{query}':\n\n"
for match in results["matches"]:
response += f"{match['ip_str']}:{match['port']}\n"
await message.channel.send(response)
except shodan.APIError as e:
await message.channel.send(f"Error: {e}")
elif message.content.startswith("!history"):
try:
num_results = int(message.content[9:].strip())
except ValueError:
await message.channel.send("Invalid number specified")
# Ensure num_results is within the bounds of the history list
num_results = min(num_results, len(self.history))
# Construct response
response = f"Showing last {num_results} search results:\n\n"
for matches in self.history[-num_results:]:
for match in matches:
response += f"{match['ip_str']}:{match['port']}\n"
response += "\n"
await message.channel.send(response)
# Insert your Shodan API key here
SHODAN_API_KEY = "YOUR_SHODAN_API_KEY"
# Insert your discord token here
DISCORD_TOKEN = "YOUR_DISCORD_TOKEN_HERE"
bot = ShodanSearchBot(SHODAN_API_KEY)
bot.run(DISCORD_TOKEN)