Skip to content

Commit

Permalink
Fix errors on unicode usernames
Browse files Browse the repository at this point in the history
Normalization and defaulting to the word `unicode` if normalization ends up removing all characters in the name
  • Loading branch information
zajrik committed Jan 17, 2017
1 parent 07c326a commit c876290
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/DMManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LocalStorage } from 'yamdbf';
import { Client, Message, Guild, User, TextChannel, DMChannel, Collection, RichEmbed } from 'discord.js';
import { normalize } from './Util';

export default class DMManager
{
Expand Down Expand Up @@ -76,7 +77,8 @@ export default class DMManager
private async createNewChannel(user: User): Promise<TextChannel>
{
const newChannel: TextChannel = <TextChannel> await this._guild
.createChannel(`${user.username}-${user.discriminator}`, 'text');
.createChannel(`${normalize(user.username) || 'unicode'}-${user.discriminator}`, 'text')
.catch(err => console.error(`DMManager: Failed to create channel: '${normalize(user.username)}-${user.discriminator}'`));
this.channels.set(user.id, newChannel);
this.storeOpenChannels();

Expand Down Expand Up @@ -105,6 +107,7 @@ export default class DMManager
{
if (message.embeds[0] && message.channel.type !== 'dm') return;
if (message.channel.type !== 'dm' && message.guild.id !== this.guild) return;
if (message.guild && message.channel.id === message.guild.id) return;
if (message.author.id !== this.client.user.id && !this.channels.has(message.author.id))
await this.createNewChannel(message.author);

Expand Down
8 changes: 8 additions & 0 deletions src/Util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Remove any non alphanumeric characters from the given text
* and lowercase it
*/
export function normalize(text: string): string
{
return text.toLowerCase().replace(/[^a-z0-9]/g, '');
}
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
],
"typedef": [
"parameter",
"arrow-parameter",
// "arrow-parameter",
"call-signature",
"member-variable-declaration",
"property-declaration",
Expand Down

0 comments on commit c876290

Please sign in to comment.