Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
77 lines (65 loc) · 2.23 KB
/
Copy pathserver.js
File metadata and controls
77 lines (65 loc) · 2.23 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
const tmi = require('tmi.js');
const OpenAI = require('openai').default;
const BadWordsFilter = require('bad-words');
require('dotenv').config();
const filter = new BadWordsFilter();
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const client = new tmi.Client({
options: { debug: true },
connection: {
secure: true,
reconnect: true
},
identity: {
username: process.env.TWITCH_USERNAME,
password: process.env.TWITCH_OAUTH_TOKEN
},
channels: [process.env.TWITCH_USERNAME]
});
client.connect();
client.on('cheer', async (channel, tags, message) => {
if (!message.startsWith('!createimage')) return;
const bitsCheered = parseInt(tags.bits, 10);
const promptText = message.slice('!createimage'.length).trim();
if (filter.isProfane(promptText)) {
client.say(channel, `@${tags.username}, please avoid using inappropriate language.`);
return;
}
if (bitsCheered >= 100) {
const imageUrl = await generateImage(promptText);
if (imageUrl) {
client.say(channel, `@${tags.username}, your image has been created: ${imageUrl}`);
} else {
client.say(channel, `@${tags.username}, there was an error creating your image.`);
}
}
// if (imageUrl) {
// client.say(channel, `@${tags.username}, your image has been created: ${imageUrl}`);
// } else {
// client.say(channel, `@${tags.username}, there was an error creating your image.`);
// }
});
async function generateImage(promptText) {
try {
console.log("Connecting to OpenAI...")
const response = await openai.images.generate({
model: "dall-e-3",
prompt: promptText,
n: 1, // Number of images to generate
size: "1024x1024" // Image size
});
// return response.data.data[0].url; // URL of the generated image
console.log("Response from OpenAI:", response);
if (response.data && response.data.length > 0) {
return response.data[0].url;
} else {
console.error("No data found in response");
return null;
}
} catch (error) {
console.error("Error generating image:", error);
return null;
}
}