Stop allowing users to place periods before their nicknames on Discord servers (Programming in discord.js with a Discord Bot)

I have been working on a script to identify when a user alters their name on Discord by adding a period at the beginning, such as changing "bob" to ".bob". The goal is to prevent this change and keep it as "bob".

if (user.nickname.startsWith(".")) {
    user.nickname = "bob";
}

Answer №1

If you want to monitor when a member changes their nickname, you can utilize the guildMemberUpdate event. In this case, you would check if the new nickname starts with a period ('.'). Here's a sample code snippet to demonstrate how you can achieve this:

I personally prefer using displayName over nickname, as nickname may return null if the member does not have one.

client.on('guildMemberUpdate', (oldMember, newMember) => {  
    if (newMember.displayName.startsWith('.')) { 
        newMember.setNickname(oldMember.displayName)
            .catch(console.error);
    }
});

It is important to note that this approach will only work if the member's highestRole 's position is lower than the bot's highestRole position. You may need to verify this condition before proceeding or handle it in your error handling logic.

Answer №2

To remove all instances of "." in the input string, you can use the following code snippet:

user.inputString = user.inputString.replace(/\./g,'');

You can also check out this Codepen for a live example: https://codepen.io/UniqueCoder/pen/qwertyu

Answer №3

When using Discord.js, there is a useful event listener known as guildMemberUpdate. This listener triggers when a user changes their nickname, allowing you to take advantage of this functionality. For instance,

Client.on("guildMemberUpdate", (past, current) => {
    if (current.displayName.startsWith(".")) {
        current.setNickname(past.displayName);
    }
});

Please keep in mind that for this code snippet to function properly, your bot needs to have the MANAGE_NICKNAMES permission. I hope you found this information valuable.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display information based on the radio button chosen

I've set up a radio button with options for "no" and "yes", but neither is selected by default. Here's what I'm trying to achieve: If someone selects "no", nothing should happen. However, if they select "yes", then a message saying "hello w ...

Changing the color of a selected list element using an Angular directive

I'm currently facing an issue with my directive that is supposed to turn a list element red when clicked. It works fine, but I also want it to revert back to black when another list item is selected, so only one item stays in red color. Here is how I ...

Transforming a string into a proc using Ruby and Rails

Here's the scenario I'm dealing with. The current URL appears as follows: /categories/Art Using name = location.pathname.split('/')[2], I extract the Art part of the URL. Then, I send an AJAX request to the controller with the followi ...

Can the AngularJS icon adapt to different lists?

I'm currently developing in AngularJS / Jade and I need to implement functionality where an icon changes when a user clicks on something. The code I have right now looks like this: a(onclick='return false', href='#general', data-t ...

Guide on utilizing Nextjs middleware for directing users to sub-domain according to their IP address

I've been struggling to effectively implement NextJs's middleware for redirecting users from a specific country to another web domain. I've encountered some issues with my setup: Main domain: https://www.example.com sub-domain: src/middle ...

Is there a way to determine if a website is utilizing javascript?

Currently, I am in the process of developing a web scraping tool using beautifulsoup. Some of the websites I am targeting contain JavaScript elements that prevent me from using urllib3 efficiently. As a workaround, I have incorporated selenium into my sc ...

I am having trouble with using document.getElementById().value to retrieve text input. Can anyone help me understand why it's not

It's puzzling to me why document.getelementbyid().value isn't working as expected. Upon inspecting the console, no input seems to be sent or printed out in the console. function callApi() { var keyword = document.getElementById('user_ ...

The Angular ui-calendar introduces an innovative approach to event addition, providing users with

I need help with adding events to the angular ui calendar. Currently, I am using $scope.events.push() method to add events, but they get reset when changing the month. If anyone has experience with angular ui-calendar and event addition, please provide ...

Guide to creating completely static HTML using `nuxt generate`?

Looking for a solution to ensure that Vue pages are fully generated as static HTML files in Nuxt even when they are simple and don't require any dynamic data fetching? Let's dig into the issue. <template> <div>hello world</div&g ...

Margins are added to cards on the right side

Looking for some help to improve the display of three cards in a grid. The card media has a max-width of 345px, but this is causing excessive margin-right space and making the styling look poorly. Any suggestions on how to eliminate this extra margin? If ...

Effectively generating observables that extract a designated element from a collection of observables

Within my application, I am utilizing a RxJS subject to broadcast changes within a collection. Each time the collection is updated, the subject emits the new contents as an array format. let collectionSubject = new Rx.BehaviourSubject(); collectionSubjec ...

JavaScript: Trouble with statement execution

My code is designed to classify a point as 1 if it's above the line y=x, and -1 if it's below the line y=x. I visually represent this line in a canvas by plotting y=x (although due to invertion on the y-axis, it appears like y=-x). For each point ...

Error: Unable to access the property '_locals' of an undefined value

I have been experimenting with nodejs on my Ubuntu 16.04 system, where I successfully installed Node and npm. However, I encountered an error stating "TypeError: Cannot read property '_locals' of undefined" while trying the following code: var e ...

Is it possible to change text dynamically with an input box using JavaScript?

Is there a way to use JavaScript to create two input boxes for text replacement? Instead of constantly editing code to replace different text, I am looking for a simple user interface with Input Box A, Input Box B, and a button. The first input box will ...

Issues with Grunt functionality after installation on Ubuntu

I successfully installed Grunt by running the following commands in the terminal: sudo apt-get install nodejs sudo apt-get install npm npm install -g grunt-cli After executing npm install -g grunt-cli, here is the output from the terminal: (output he ...

How can I change a time duration (e.g. 3 hours 23 minutes) to a specific date and time in

Is there a way to transform a time string such as 2h 25m 15s or 2h 10m into a datetime, or simply convert it to minutes like 1h => 60, using NodeJS? ...

Exploring the depths of Cordova's capabilities: implementing a striking 3D front-to-back screen flip effect

Recently, I came across an interesting app that had a unique feature. By clicking on a button, the entire screen would flip from front to back with a 3D effect on iPhone. It was quite impressive to see in action. The developer mentioned that he achieved t ...

Utilizing data attributes to configure jQuery plugin settings

Having trouble assigning options to an array value in a jQuery plugin using data attributes. When referencing the data attribute with a class selector: $('.tm-input').tagsManager( { prefilled: $('.tm-input').data('loa ...

What could be the reason for the three.js scene failing to render in my Svelte application?

Scene.svelte <!-- Start by binding a container, then add the renderer to this container onMount --> <script> import { onMount } from 'svelte'; import * as THREE from 'three'; let container; onMount(async () = ...

"Learn how to dynamically update the user interface in express.js using handlebars without having to refresh the

As a newcomer to using express.js and Handlebars, I am faced with the challenge of implementing autocomplete functionality. Specifically, I want to enable autocompletion in a text input field without triggering a page refresh when users interact with it. C ...