Skip to content

Instantly share code, notes, and snippets.

@zudsniper
Last active April 5, 2023 17:21
Show Gist options
  • Save zudsniper/d2b85c9b441acf5e463c0cf3e27266e5 to your computer and use it in GitHub Desktop.
Save zudsniper/d2b85c9b441acf5e463c0cf3e27266e5 to your computer and use it in GitHub Desktop.
A small program written in javascript to notify your server with a rich discord embed when any user starts to stream. Written by ChatGPT
/* Written by ChatGPT-3 on 3/5/2023. Took two queries. Fuck
* human involved: @zudsniper
*/
import Discord from 'discord.js';
import dotenv from 'dotenv';
// Load environment variables
dotenv.config();
// Create a new Discord client
const client = new Discord.Client();
// Log in to the Discord API
client.login(process.env.DISCORD_TOKEN);
// Listen for the client to connect to Discord
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
// Check the activity status of all users
client.users.cache.forEach((user) => {
if (user.presence.activities.some((activity) => activity.type === 'STREAMING')) {
// Get the Discord channel where the message will be sent
const channel = client.channels.cache.get(process.env.DISCORD_CHANNEL_ID);
// Check if the user has the "notifications muted" role
const member = channel.guild.members.cache.find(
(member) => member.user.id === user.id
);
if (member.roles.cache.some((role) => role.name === 'notifications muted')) {
return;
}
// Send an embed message to the specified Discord channel
const embed = new Discord.MessageEmbed()
.setColor('#6441A4')
.setAuthor(user.username, user.avatarURL())
.setTitle(activity.details)
.setURL(activity.url);
if (activity.assets && activity.assets.largeImageURL()) {
embed.setImage(activity.assets.largeImageURL());
}
channel.send({ embeds: [embed] });
}
});
});
// Listen for a user's activity status to change
client.on('presenceUpdate', (oldPresence, newPresence) => {
if (
newPresence.activities.some((activity) => activity.type === 'STREAMING') &&
!oldPresence.activities.some((activity) => activity.type === 'STREAMING')
) {
// Get the Discord channel where the message will be sent
const channel = client.channels.cache.get(process.env.DISCORD_CHANNEL_ID);
// Check if the user has the "notifications muted" role
const member = channel.guild.members.cache.find(
(member) => member.user.id === newPresence.user.id
);
if (member.roles.cache.some((role) => role.name === 'notifications muted')) {
return;
}
// Send an embed message to the specified Discord channel
const embed = new Discord.MessageEmbed()
.setColor('#6441A4')
.setAuthor(newPresence.user.username, newPresence.user.avatarURL())
.setTitle(newPresence.activities[0].details)
.setURL(newPresence.activities[0].url);
if (newPresence.activities[0].assets && newPresence.activities[0].assets.largeImageURL()) {
embed.setImage(newPresence.activities[0].assets.largeImageURL());
}
channel.send({ embeds: [embed] });
}
});

TWITCH NOTIFY (by GPT4)

example of the difference between gpt-3 and gpt-4 generated 4/5/2023. took 1 query. a lot better. Fuck

BEGIN MADNESS


[USER PROMPT]

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:

  1. Install Node.js and set up your project
  2. Create a Discord bot and invite it to your server
  3. Install discord.js and other dependencies
  4. Write the bot code
  5. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment