Overlapping commands? - javascript

I'm trying to make a fun little discord chat bot with JavaScript and node.js and I'd like to put in a specific command without it affecting another one I already have set up.
She works wonderfully on all the servers I have her on, and I've got it set up so that when someone in the server says anything with "rei are", she responds with a constant from areResponses.
//const!!!
const areResponses = ["HELL yeah!", "Yep!", "I'm pretty sure that's true!", "I\'m not gonna put all the responses here because then it'd be too long..."];
//theres some other bot stuff (console log, now playing) under here but it isn't relevant until...
//the basics...
if (message.content.toLowerCase().includes("rei are")) {
var response = areResponses [Math.floor(Math.random()*areResponses.length)];
message.channel.send(response).then().catch(console.error);
}
What I want to have happen is, preferably, this command to function without setting off the "rei are" command I coded in.
if(message.content.toLowerCase().includes("rei are you happy")) {
message.channel.send("Yes, absolutely.");
}
As of right now, whenever I try to input the above command, it just triggers the "rei are" command AND the "rei are you happy" command with two messages...

else/if chains work beautifully for this actually!!!
if(message.content.toLowerCase().includes("rei do you like girls")) {
message.channel.send("Yes, absolutely. Girls are just... yeah...");
}
else if (message.content.toLowerCase().includes("rei are")) {
var response = areResponses [Math.floor(Math.random()*areResponses.length)];
message.channel.send(response).then().catch(console.error);
}
All you need to do is put the command that would overlap with the larger commands at the very bottom of the else if chain, and then you're good!!

Related

Thunderbird ADDON: port.postmessage() does not work reliably

I am trying, and trying, and trying , … to debug an addon that I wrote for Thunderbird 78.10.2. It sends message to a python backend and then reads the reply back:
async function buttonListener(tab) {
tabId = tab.id;
port = browser.runtime.connectNative("backendMyEd");
cD = await browser.compose.getComposeDetails(tab.id);
var body = extractBody(cD);
await port.postMessage(body);
port.onMessage.addListener(getReply);
}
The listener is just a python script that is doing:
def get_message():
if sys.version_info < (3, 0):
len_field = sys.stdin.read(4)
else:
len_field = sys.stdin.buffer.read(4)
if not len_field:
sys.exit(0)
message_length = struct.unpack('=I', len_field)[0]
message = sys.stdin.read(message_length) <=====
return json.loads(message)
This works half the time but not always. When it doesn’t, my printfs tell me that it’s stuck in stdin read, but port.postMessage() has returned. If I quit TB or remove my add on, the sys.stdin.read() finishes.
Several things come to mind:
Is there a flush needed after port.postMessage()? maybe its not flushing all the time?
could it be related to message size? i tested with different sizes, and for less than 8k it often works. For larger ones, it seems to not work more often.
Could there be some special characters in message causing this?
Something to do with JS being single threaded? But the function is async, and python should be a different process. I also tried doing a fork in python, still hangs off and on in sys.stdin.read()...
Any ideas on whats going on, or how I could debug further?

Trying to execute a random command without having to restart bot

I was trying to follow this link: Random Message Reply Discord.js 2020
But am stuck when trying to get it implemented. Basically, using the discord.js library I have one channel that I want to allow one word and one word only. When someone posts anything in said channel that is NOT that word, I want the bot to delete it and send a randomly picked reply from an array. I have gotten it to randomly select an indexed item, but it does not truly stay random. The only time the reply changes, is if the bot is restarted. I want it to execute after every time the event takes place, but every time I try, I get ReferenceError: execute is not defined.
Today is my first day ever working with Javascript, so any way of simplifying this would be GREATLY appreciated. I have repeatedly tested it, and it works great until I tried working with the execute function. I am using Visual Code Studio for the coding, and node.js for running the file.
I have not imported any other files besides discord.js, dotenv, and nodemon. I don't know what execute is supposed to do. I was just blindly following the link.
Here is my code:
const client = new Client();
const bredfreply = [
'Excuse me person. I see you are participating in the "bredf" channel in the INCREDIBLE Realm of Eden. Unfortunately, this channel is only limited to bredf. I cannot believe you would speak any other word. You should be ashamed!',
'Did you do what I think you just did? EXCUSE ME, but bredf is only for bredf. You MUST say bredf because it is bredf and everyone knows bredf is only bredf and not whatever you thought it was. bredf. Only bredf.',
'You person, are in trouble. For the fact that you are rebelling against the bredf, you have lost one cookie. I do not care if you already did not have cookie because now you have less than no cookies. You actually OWE cookies so pay up.',
'Whatever you just did? That was not bredf.',
'bredf bredf bredf bredf bredf bredf bredf bredf bredf bredf bredf bredf',
'PLEASE only post bredf in the channel. You are scaring the other bredfs.',
'Did you hear about bredf? It is this really cool thing that all the cool kids post in bredf. Oh, you must not be bredf because I do not see you in there. You should be bredf.',
'*sniffles* I want bredf. Can you help me? Put it in the channel then because I do not think you can.',
'I only like people that say bredf. Since you apparently are not, that must mean I do not like you. Hmph'
];
client.on('ready', () => {
console.log(`${client.user.username} is ready to start the day!`);
});
client.on('message', (message) => {
if(message.channel.id == '840422367829164052' && message.content != 'bredf'
&& message.author.id != '854877244157853716')
execute(message,)
const randombredfreply = bredfreply[Math.floor(Math.random(1)) * bredfreply.length];
message.author.send(randombredfreply);
message.delete()
}
)```
Did you import function execute from any other file? Or what does execute function do?
I was able to solve this by playing around with it. I acheived my goal by moving my
By moving my
var randombredfreply = bredfreply[Math.floor(Math.random() * bredfreply.length]);
Into my if(... command) it will re-calculate this whenever the if parameters are met.
Here is the final code:
if(message.channel.id == '840422367829164052' && message.content != 'bredf'
&& message.author.id != '854877244157853716')
{
var randombredfreply = bredfreply[Math.floor(Math.random() * bredfreply.length)];
message.author.send(randombredfreply);
message.delete()
}
})

Words filter stopped working - Discord.JS 12

Hello!
So I'm currently updating my bot to version 12.3.1 of DiscordJS. Unfortunately, I got stuck on a problem that I can't really find a workaround. So, my bot has a module to filter out all of the bad words, like profanity, racial slurs, etc.
It's currently working just fine on 11.4, but cannot get it to work on 12.3.1
For some reason, the bot just does not react at all to given message.
I had two "filters" one for words, one for invites. Both of them stopped working.
bot.on('message', async message => {
// Events Listeners
if (message.author.bot) return;
if (message.channel.type === 'dm') return;
let messageArray = message.content.split(' ');
let command = messageArray[0];
let args = messageArray.slice(1);
if (!command.startsWith(prefix)) return;
let cmd = bot.commands.get(command.slice(prefix.length)) || bot.aliases.get(command.slice(prefix.length));
if (cmd) cmd.run(bot, message, args);
// First filter
var array = ['testing', 'yes', 'no'];
if (array.includes(message.content.toLocaleLowerCase())) {
message.channel.send('test')
}
// Second filter
if (message.content.includes('discord.gg/') {
message.delete()
}
}
That's the current one I found from another StackOverflow post, made 2 months ago.
Discord.js V12 Rude words filter not working
I'd really love to get some help if possible, as I can't find any reason why this feature has stopped working.
Thank you! :)
Your filters are after your command handling logic.
You have the line:
if (!command.startsWith(prefix)) return;
early in your code, and this causes message handling to terminate immediately on any message which is not a command. Due to this, the code will never reach your filters unless the message starts with your bot's prefix, at which point the message content cannot possibly be equal to any of the words and is extremely unlikely to contain discord.gg/.
Simply move your filters to the beginning of your message handler. Or alternatively separate the command handling and filter handling into separate functions, so that the return statement above only exits the command handling and the filter handling will still run.

Phonegap App Error - connection to the server was unsuccessful

I have the following issue and I'm a bit new to Phonegap! On my index page I have three functions that will create a Javascript Prompt asking the user for their name, email and title (position) and store each to the localStorage. Three items like this:
function promptName(){
var salesPName = prompt("Bitte geben Sie Ihren Namen","");
if(salesPName == null || salesPName == ""){
promptName()
}else{
localStorage.setItem("salesP", salesPName);
}
}
Then using $(document).ready I call these three functions:
$(document).ready(function(){
if(!localStorage.getItem("salesP")){
promptName();
promptEmail();
promptPosition();
}
});
This is all working well, however when deploying to my Android device I get the prompts but before I can complete all three I get an error dialog stating:
The connection to the server was unsuccessful (file:///android_asset/www/appname/index.html)
Removing the prompts removes the error but I need this functionality. I have tried different ways of calling the functions, for example on the body tag's onload event or using .load(). I still get this error. I thought about setting a Javascript interval to call this after a few seconds (once the page is loaded) as I'm sure the problem is due to Javascript's blocking nature. Has anyone come across this before?
Please note that I added the following to the com.mypackage.xxx.java file (as advised from phonegap, connection to server unsuccessful)
super.setIntegerProperty("loadUrlTimeoutValue", 10000);
And I still get the problem!
with nothing working I put a setTimeout() around my condition like so...
$(document).ready(function(){
setTimeout(function (){if(!localStorage.getItem("salesP")){
// item doesn't exist... so let's raise some dialogs to capture the name, email address and title
promptName();
promptEmail();
promptPosition();
}
}, 5000)
});
now it works fine... a bit of a fudge but so what, if anyone has any ideas on a better solution or any objections to this please let me know

Code Issues Resulting in Nothing Happening and 'send' Undefined

Here is the error I'm receiving in the console regarding 'send' undefined: https://pastebin.com/CRmZwUA4. This happens when testing our profanity filter. The word on the list is deleted and removed and the bot replies with a message stating that it was removed. This is working as it should except for the error in the console.
For the extra code not working, I'm not sure if it has to do with the error above. A few days ago the code worked fine until I made adjustments to a different section of code which was just to add in a check for the profanity filter to see if a filtered word was said by a member with a specific role. If they had the role, the filter was then ignored so they weren't blocked out. It would seem the 2 would have to do with each others but I can't figure out what the check would even affect. Again the filter worked as intended where the check was fine but the other fun code stopped working.
The code pasted below shows the fun code that stopped working. When a user besides the person being tagged says "bum" the bot replies with a fun message. That's what it's supposed to do. When any user says "bum" now, nothing happens. The concern that I have is that the testing bot used in a different server with the same code works with this perfectly fine. But the real bot on our main server is not working with the code. I even copied the file to the main bot again and restarted the bot and still nothing is happening. Sorry for the long winded post, I just want to make sure i've explained everything clearly.
if (words.includes('bum')) {
if (message.author == '<#458068171241553921>') return;
else {
setTimeout(function() {
message.channel.send('Are we talking about <#458068171241553921>?!');
}, 1500);
}
}
for (let x = 0; x < profanities.length; x++) {
if (message.member.roles.some(role => role.id === '613427562550394891')) return;
else {
if (message.content.toUpperCase().includes(profanities[x].toUpperCase())) {
message.channel.send('Oooooooh you said a bad word!');
client.channels.get('484375912389935126').send(`Message was deleted due to use of a blocked word:\n\n"${message.content}"`);
message.delete();
return;
}
}
}
I figured out the issue. It was the new check that I had added. The check was to see if people had the role then it wouldn't respond. I had the check outside of the For loop which is what messed everything up. I placed the If statement inside the for loop instead so the new code is below
for (let x = 0; x < profanities.length; x++) {
if (message.member.roles.some(role => role.id === '483641589193900043')) return; {
if (message.content.toUpperCase().includes(profanities[x].toUpperCase())) {
message.channel.send('Oooooooh you said a bad word!');
client.channels.get('484375912389935126').send(`Message was deleted due to use of a blocked word:\n\n"${message.content}"`);
message.delete();
return;
}
}
}

Categories

Resources