example of the difference between gpt-3 and gpt-4 generated 4/5/2023. took 1 query. a lot better. Fuck
How do I write a discord bot which, for all users unless they have a "notifications muted" role, sends a message to a specified channel when a user starts streaming on Twitch. The Streaming activity on Discord should be used to check stream status. Techstack must be NodeJS, TypeScript, discord.js
To create a Discord bot that sends a message to a specified channel when a user starts streaming on Twitch using Node.js, TypeScript, and discord.js, you'll need to follow these steps:
- Install Node.js and set up your project
- Create a Discord bot and invite it to your server
- Install discord.js and other dependencies
- Write the bot code
- Run the bot
Here's a guide to help you through the process:
Step 1: Install Node.js and set up your project
Install Node.js from the official website: https://nodejs.org/
Create a new directory for your project and navigate to it in your terminal/command prompt
Run npm init -y
to create a package.json file
Step 2: Create a Discord bot and invite it to your server
Go to the Discord Developer Portal: https://discord.com/developers/applications
Create a new application, and then create a bot within that application
Copy the bot token; you'll need it later
Invite the bot to your server using the OAuth2 URL generator in the Developer Portal
Step 3: Install discord.js
and other dependencies
Run the following commands in your terminal/command prompt to install the necessary packages:
npm install discord.js
npm install typescript ts-node @types/node -D
Step 4: Write the bot code
Create a new file named tsconfig.json
in your project directory and add the following configuration:
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src"],
"exclude": ["node_modules"]
}
Create a new src directory and create a file named index.ts
inside it:
import { Client, Intents, ActivityType, TextChannel } from 'discord.js';
const token = 'YOUR_BOT_TOKEN';
const channelId = 'SPECIFIED_CHANNEL_ID';
const client = new Client({
intents: [Intents.FLAGS.Guilds, Intents.FLAGS.GuildMembers, Intents.FLAGS.GuildMessages],
});
client.on('ready', () => {
console.log(`Logged in as ${client.user?.tag}!`);
});
client.on('presenceUpdate', async (_, newPresence) => {
const streamingActivity = newPresence.activities.find(
(activity) => activity.type === ActivityType.Streaming,
);
if (!streamingActivity) return;
const member = newPresence.member;
if (!member) return;
const hasMutedRole = member.roles.cache.some((role) => role.name === 'notifications muted');
if (hasMutedRole) return;
const channel = client.channels.cache.get(channelId) as TextChannel;
if (!channel) {
console.error(`Couldn't find channel with ID: ${channelId}`);
return;
}
const streamUrl = streamingActivity.url;
channel.send(
`Hey everyone! ${member.displayName} has started streaming on Twitch! Check it out: ${streamUrl}`,
);
});
client.login(token);
Replace 'YOUR_BOT_TOKEN' with your bot token from the Developer Portal and 'SPECIFIED_CHANNEL_ID' with the ID of the channel where you want to send notifications.
Step 5: Run the bot
In your terminal/command prompt, navigate to your project directory and run the following command:
```bash
> Here gpt-4 stopped generating. I then subsequently ask it to finish generation, which it does.
> ## [USER PROMPT]
> please continue from where you left off
Apologies for the cut-off. To continue from where we left off, you'll need to run the bot using the following command in your terminal/command prompt:
```bash
npx ts-node src/index.ts
This command will run your TypeScript code using ts-node and will start the bot. If everything is configured correctly, you should see the bot's login message in your terminal.
Make sure your bot has the necessary permissions to read and send messages in the specified channel. If the bot doesn't have the required permissions, it won't be able to send notifications.
To keep your bot running continuously, you may want to deploy it to a server or use a process manager like pm2 to run it in the background. You can install pm2 using the following command:
bash
npm install pm2 -g
Then, you can use pm2 to run your bot:
bash
pm2 start --name my-discord-bot --interpreter npx --interpreter-args=--package=ts-node -i max -- src/index.ts
With this setup, your Discord bot will now send a message to the specified channel whenever a user starts streaming on Twitch, unless they have the "notifications muted" role.