Discord Rich Presence Not Connecting - javascript

Objective: I am trying to make a rich presence for discord.
Code:
const RPC = require("discord-rpc");
const rpc = new RPC.Client({
transport: 'ipc',
});
const chalk = require('chalk');
const settings = require('./settings.json');
const details = settings.details;
const state = settings.state;
const LIK = settings.largeImageKey;
const LIT = settings.largeImageText;
const SIK = settings.smallImageKey;
const SIT = settings.smallImageText;
const ID = settings.clientID;
rpc.on("ready", () => {
rpc.setActivity({
details: `${details}`,
state: `${state}`,
startTimestamp: new Date(),
largeImageKey: `${LIK}`,
largeImageText: `${LIT}`,
smallImageKey: `${SIK}`,
smallImageText: `${SIT}`
});
console.log(chalk.green("Rich Prescence is on: " + rpc.user.username))
});
rpc.login({
clientId: `${ID}`,
})
Error:
(node:2128) UnhandledPromiseRejectionWarning: Error: Could not connect
at Socket.onerror (C:\Users\Familia\OneDrive\Documents\Other Stuff\Visual Studio code\Discord Bots\Testing\Art Prescence\node_modules\discord-rpc\src\transports\ipc.js:32:16)
at Object.onceWrapper (events.js:422:26)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:2128) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2128) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
It says it's not connecting, despite doing everything right. I do not know how to fix this. Help will be appreciated.
Edit: Noticing that I didn't include for websites so I did the following:
const RPC = require("discord-rpc");
const browser = typeof window !== 'undefined';
const rpc = new RPC.Client({ transport: browser ? "websocket" : "ipc"});
The same error came up

As #mark-oe stated, you have to be running the Desktop Version of Discord for the RPC to connect to the WSS (WebSockets)

Related

I have an error for discord.js rpc! Can someone please help me?

This is my error:
(node:42087) UnhandledPromiseRejectionWarning: Error: Could not connect
at Socket.onerror (/Users/vinesh/RPC/node_modules/discord-rpc/src/transports/ipc.js:32:16)
at Object.onceWrapper (events.js:422:26)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:42087) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:42087) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
My code:
const RPC = require('discord-rpc');
const client = new RPC.Client({ transport: 'ipc' });
client.on('ready', () =>
{
client.request('SET_ACTIVITY', {
pid: process.pid,
activity: {
assets: {
large_image: 'Coding'
},
details: 'Coding',
buttons: [
{ label: 'Emperor', url: 'https://dsc.gg/invite-emperor' },
{ label: 'https://dsc.gg/invite-emperor', url: 'https://dsc.gg/invite-emperor' }
]
}
});
console.log('started!');
});
client.login({ clientId: 'My_ID' });
How do I fix this? Is it because the code is old? I don't know why it does that. I did npm init -y, npm install discord, and npm install discord-rpc.
If you are running the web browser version of Discord, RPC will not work. Instead download Discord for Desktop which is here.
If you are running Discord desktop then try this code. Also remember to add your assets in https://discord.com/developers/applications/Application_ID/rich-presence/assets and make sure they are the same name in application and code. You need large_text so don't remove it.
var rpc = require("discord-rpc")
const client = new rpc.Client({ transport: 'ipc' })
client.on('ready', () => {
client.request('SET_ACTIVITY', {
pid: process.pid,
activity : {
details : "Coding",
assets : {
large_image : "Your Image",
large_text : "Your Status"
},
buttons : [{label : "Emperor" , url : "https://dsc.gg/invite-emperor"},{label : "Invite 2?",url : "https://dsc.gg/invite-emperor"}]
}
})
})
client.login({ clientId : "CLIENT_ID" }).catch(console.error);

I am getting a 400 bad request while using google custom search with discord.js! what am I doing wrong here?

I am building a discord bot which searches a query on google using the custom search api but i am getting this error! here's my code, what am i doing wrong?
const Discord = require("discord.js");
const request = require("node-superfetch");
var fs = require('fs');
module.exports = {
name: 'google',
description: "searches google ",
cooldown: 10,
permissions: [],
async execute(message, args, cmd, client, Discord) {
let googleKey = "XXXX";
let csx = "be4b47b9b3b849a71";
let query = args.join(" ");
let result;
if(!query) return message.reply("Please enter a Valid Query");
result = await search(query);
if (!result) return message.reply("Invalid Search");
const embed = new Discord.MessageEmbed()
.setTite(result.title)
.setDescription(result.snippet)
.setImage(result.pagemap ? result.pagemap.cse_thumbnail[0].src : null)
.setURL(result.link)
.setColor(0x7289DA)
.setFooter("Powered by Google")
return message.channel.send(embed);
async function search(query) {
const { body } = await request.get("https://customsearch.googleapis.com/customsearch/v1").query({
key: googleKey, cs: csx, safe: "off", q: query
});
if(!body.items) return null;
return body.items[0];
}
}
}
ERROR MESSAGE: (node:10944) UnhandledPromiseRejectionWarning: Error: 400 Bad Request
at Request._request (D:\Coding\FLASH\node_modules\node-superfetch\index.js:58:16)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async search (D:\Coding\FLASH\commands\google.js:31:26)
at async Object.execute (D:\Coding\FLASH\commands\google.js:17:14)
(Use node --trace-warnings ... to show where the warning was created)
(node:10944) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:10944) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

DiscordAPIErrorL: Missing Access

I can't seem to figure out the problem with my code. I don't see no errors but when it joins a server, it'll error out and possibly stop running. I would appreciate the help!
index.js
const Discord = require('discord.js');
const client = new Discord.Client();
const KeepAlive = require('./server');
client.on('message', function (msg) {
const Logged = new Discord.MessageEmbed()
.setTitle(msg.author.tag)
.setDescription(msg.content)
.setColor('0x00AAFF')
.setTimestamp();
if(msg.author.id === 'BOT-ID-GOES-HERE') {
console.log('')
} else if(msg.author.username) {
const channel = client.channels.cache.find(channel => channel.name === 'logs')
channel.send(Logged)
}
});
client.on('ready', ()=> {
console.log('Connected as '+client.user.tag)
});
KeepAlive()
client.login(process.env.token);
server.js
const express = require('express')
const server = express()
server.all('/', (req, res) => {
res.send('Bot is running!')
});
function KeepAlive() {
server.listen(671, () => {});
console.log('Server is ready!')
}
module.exports = KeepAlive
now the error is
(node:313) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Access
at RequestHandler.execute (/home/runner/Censored-Logging/node_modules/discord.js/src/rest/RequestHandler.js:154:13)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async RequestHandler.push (/home/runner/Censored-Logging/node_modules/discord.js/src/rest/RequestHandler.js:39:14)
(node:313) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:313) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I'm still pretty new with errors, I can't read them properly.

Cannot read property 'get' of undefined (node js discord js "discord bot")

Dicord bot and sorry by my english is some bad:
client.on("ready", () => {
console.log(`Genesis bot online!`);
client.user.setPresence({
status: "online",
game: {
name: "Official Discord Bot",
type: "PLAYING" //PLAYING, LISTENING, WATCHING
}
});
setInterval(async function(){
let msg = await client.channels.cache.get(channelID).channel.messages.cache.get(messageID);
let stafflist = new MessageEmbed()
.setColor("GOLD")
.setTitle("Staff Members")
.setDescription(client.guilds.cache.get(serverID).roles.cache.find((r) => r.name == "➦「Staff Member」").members.map((m) => `\`${m.user.tag}\``).join(", "));
msg.edit(stafflist)
}, 3000);
});
this is the complete error:
(node:276) UnhandledPromiseRejectionWarning: TypeError: Cannot read
property 'get' of undefined
at Timeout._onTimeout (N:\Genesis bot\Genesisbot2.0\index.js:34:47)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7) (Use node --trace-warnings ... to show where the warning was created) (node:276) UnhandledPromiseRejectionWarning: Unhandled promise
rejection. This error originated either by throwing inside of an async
function without a catch block, or by rejecting a promise which was
not handled with .catch(). To terminate the node process on unhandled
promise rejection, use the CLI flag --unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
(rejection id: 1) (node:276) [DEP0018] DeprecationWarning: Unhandled
promise rejections are deprecated. In the future, promise rejections
that are not handled will terminate the Node.js process with a
non-zero exit code.

I want a Discord bot to enter voice channel and play a mp3 file

I suspect there is something wrong with my code but I don't know.
const Discord = require('discord.js');
const client = new Discord.Client();
const config = require('./config.json');
client.once('ready', () => {
console.log('Bot online!');
});
client.on('message', async message => {
if(message.content.voice.channel) {
const connection = await message.member.voice.channel.join();
const dispatcher = connection.playFile(require("path").join(__dirname, './audio.mp3'));
dispatcher.on('start', () => {
console.log('Now playing!');
});
dispatcher.on('finish', () => {
console.log('Finished playing!');
voiceChannel.leave();
});
dispatcher.on('error', console.error);
}
});
client.login(config.TOKEN);
I get error :
UnhandledPromiseRejectionWarning: TypeError: Cannot read property
'channel' of undefined
at Client. (C:\Users\djd18\Desktop\Bot\index.js:10:30)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (C:\Users\djd18\Desktop\Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\djd18\Desktop\Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\djd18\Desktop\Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (C:\Users\djd18\Desktop\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:\Users\djd18\Desktop\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\djd18\Desktop\Bot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (C:\Users\djd18\Desktop\Bot\node_modules\ws\lib\websocket.js:825:20)
(Use node --trace-warnings ... to show where the warning was
created) (node:16536) UnhandledPromiseRejectionWarning: Unhandled
promise rejection. This error originated either by throwing inside of
an async function without a catch block, or by rejecting a promise
which was not handled with .catch(). To terminate the node process on
unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict (see
https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
(rejection id: 1) (node:16536) [DEP0018] DeprecationWarning: Unhandled
promise rejections are deprecated. In the future, promise rejections
that are not handled will terminate the Node.js process with a
non-zero exit code.

Categories