Bot commands duplicated across multiple streams

Hi all

I’m new to the world of bots and coding and have stumbled across my first problem!

I’m currently making a Twitch bot.

I have my bot set to work across two Twitch channels currently, and have a command - for example !discord

My code looks like this:

if(message.toLowerCase() === ‘!discord’) {
client.say(channel=CHANNEL_NAME, DISCORD_LINK);

I have two pieces of code that look like this, with the discord link differing depending on the channel that the command is run in.

I have added “=CHANNEL_NAME” onto channel, which isolates the command to that stream. However, if I run the !discord command in one channel, it automatically runs the !discord command in the other channel.

Is there a way to isolate commands to just one channel, whilst having the option for multiple channels to use the bot?

I hope this all makes sense :slight_smile:

Thanks so much

Can you send me your code?

Without knowing the rest of the code I may be wrong, but I do not think channel=CHANNEL_NAME is what you want.

Does a truncated version of your file look something like this?

const CHANNEL_NAME = '#someChannel'
const DISCORD_LINK = 'someDiscordLink'

client.on('message', (channel, tags, message, self) => {
  if(message.toLowerCase() === '!discord') {
    client.say(channel=CHANNEL_NAME, DISCORD_LINK)

If that is the case, your bot will always send the link to the #someChannel channel, and not to the channel you write the command in.

If I understand your needs correctly, you want your bot to respond with the link DISCORD_LINK, but only if the '!discord' command is used in the channel CHANNEL_NAME, otherwise it should do nothing?

If that is the case you can use an if statement to prevent it from responding if not the right channel.

Something like this should do the trick, and if you want to add another channel - link combo, you just add another if statement:

if(message.toLowerCase() === '!discord') {
  if (channel === CHANNEL_NAME) {
    client.say(channel, DISCORD_LINK)

One word of caution though. This soluting does not scale well. If you plan to have your bot in a lot of channels, each with their own commands, you should not hardcode it in your message handler. Use a database to store the commands of each channel, and then load them when needed.

1 Like