-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
76 lines (66 loc) · 2.19 KB
/
bot.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
66
67
68
69
70
71
72
73
74
75
76
import os
import sys
import aiohttp
import io, base64
from discord import Webhook, SyncWebhook, File
from twitchio.ext import commands
from dotenv import load_dotenv
from simpleobsws import WebSocketClient, Request, RequestResponse, RequestStatus
load_dotenv()
INITIAL_CHANNELS = [
"",
]
class Bot(commands.Bot):
def __init__(self):
self.password = sys.argv[1]
self.source = sys.argv[2]
self.webhook = sys.argv[3]
self.ws = None
super().__init__(
token=sys.argv[5],
prefix="!",
initial_channels=[sys.argv[4]],
nick="ShottyBot",
)
async def identify_obs(self):
data = {
"ip": "localhost",
"port": os.environ.get("OBS_PORT"),
"password": self.password,
"source": self.source,
}
self.ws = WebSocketClient(
url=f"ws://{data['ip']}:{data['port']}", password=data["password"]
)
await self.ws.connect()
identified = await self.ws.wait_until_identified()
async def event_ready(self):
print(f"Logged in as | {self.nick}", flush=True)
await self.identify_obs()
@commands.command()
async def sc(self, ctx: commands.Context):
name = self.source
print(f"{ctx.author.name} has issued a screenshot command!", flush=True)
async with aiohttp.ClientSession() as session:
req_data = {
"sourceName": name,
"imageFormat": "jpg",
}
await self.identify_obs()
req = await self.ws.call(
Request("GetSourceScreenshot", requestData=req_data)
)
if not req.ok:
return
img = req.responseData["imageData"]
img = img[img.find("/9") :]
tmp = File(io.BytesIO(base64.b64decode(img)), filename=f"{name}.png")
webhook = Webhook.from_url(
self.webhook,
session=session,
)
await webhook.send(f"Screenshot from {ctx.author.name}:", file=tmp)
await ctx.send(f"{ctx.author.name}'s screenshot has been sent!")
print("Running bot")
bot = Bot()
bot.run()