# Chat bot joins server, receives chat, but can't send anything FIXED

**URL:** <https://discuss.dev.twitch.com/t/chat-bot-joins-server-receives-chat-but-cant-send-anything-fixed/11510>\
**Category:** Chat and IRC\
**Created:** [August 22, 2017, 2:40am UTC](https://discuss.dev.twitch.com/t/chat-bot-joins-server-receives-chat-but-cant-send-anything-fixed/11510 "2017-08-22T02:40:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hunter\_Myers](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/hunter_myers/32/7055_2.png) [@Hunter\_Myers](https://discuss.dev.twitch.com/u/Hunter_Myers)\
**Post date:** [August 22, 2017, 2:40am UTC](https://discuss.dev.twitch.com/t/chat-bot-joins-server-receives-chat-but-cant-send-anything-fixed/11510/1 "2017-08-22T02:40:30Z")

</div>

> ```
> import cfg
> import socket
> import re
> 
> def chat(sock, msg):
> """
> Send a chat message to the server.
> Keyword arguments:
> sock -- the socket over which to send the message
> msg -- the message to be sent
> """
> sock.send("PRIVMSG #{} :{}".format(cfg.CHAN, msg))
> 
> def ban(sock, user):
> """
> Ban a user from the current channel.
> Keyword arguments:
> sock -- the socket over which to send the ban command
> user -- the user to be banned
> """
> chat(sock, ".ban {}".format(user))
> 
> def timeout(sock, user, secs=600):
> """
> Time out a user for a set period of time.
> Keyword arguments:
> sock -- the socket over which to send the timeout command
> user -- the user to be timed out
> secs -- the length of the timeout in seconds (default 600)
> """
> chat(sock, ".timeout {}".format(user, secs))
> 
> HOST = "irc.chat.twitch.tv" # the Twitch IRC server
> PORT = 6667 # always use port 6667!
> NICK = "wolfmanager" # your Twitch username, lowercase
> PASS = "oauth:xxxxxxxxxxxxxxxxxxxxxxxx" # your Twitch OAuth token
> CHAN = "#arcticwolfhowls" # the channel you want to join
> 
> s = socket.socket()
> s.connect((HOST, PORT))
> s.send("PASS {}\r\n".format(PASS).encode("utf-8"))
> s.send("NICK {}\r\n".format(NICK).encode("utf-8"))
> s.send("JOIN {}\r\n".format(CHAN).encode("utf-8"))
> 
> CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
> chat(s,"ITS ME")
> 
> while True:
> response = s.recv(1024).decode("utf-8")
> if response == "PING :tmi.twitch.tv\r\n":
> s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
> else:
> username = re.search(r"\w+", response).group(0) # return the entire match
> message = CHAT_MSG.sub("", response)
> print(username + ": " + message)
> for pattern in cfg.PATT:
> if re.match(pattern, message):
> ban(s, username)
> break
> 
> ```

So I’m confused on why it’s not sending a message at:

> ```
> CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
> chat(s, "ITS ME")
> 
> ```

I’m not very familiar with IRC and I’m still confused after reading up on it, so any help would be awesome.  
EDIT: Fixed, this is the fixed code:

> ```
> #bot.py
> import cfg
> import socket
> import re
> 
> def chat(sock, msg):
> """
> Send a chat message to the server.
> Keyword arguments:
> sock -- the socket over which to send the message
> msg -- the message to be sent
> """
> sock.send("PRIVMSG #{} :{}\r\n".format(cfg.CHAN, msg).encode("utf-8"))
> 
> def ban(sock, user):
> """
> Ban a user from the current channel.
> Keyword arguments:
> sock -- the socket over which to send the ban command
> user -- the user to be banned
> """
> chat(sock, ".ban {}".format(user))
> 
> def timeout(sock, user, secs=600):
> """
> Time out a user for a set period of time.
> Keyword arguments:
> sock -- the socket over which to send the timeout command
> user -- the user to be timed out
> secs -- the length of the timeout in seconds (default 600)
> """
> chat(sock, ".timeout {}".format(user, secs))
> 
> HOST = "irc.chat.twitch.tv" # the Twitch IRC server
> PORT = 6667 # always use port 6667!
> NICK = "wolfmanager" # your Twitch username, lowercase
> PASS = "oauth:xxxxxxxxxxxxxxxxxxxxxxxxxxxx" # your Twitch OAuth token
> CHAN = "arcticwolfhowls" # the channel you want to join
> 
> s = socket.socket()
> s.connect((HOST, PORT))
> s.send("PASS {}\r\n".format(PASS).encode("utf-8"))
> s.send("NICK {}\r\n".format(NICK).encode("utf-8"))
> s.send("JOIN {}\r\n".format(CHAN).encode("utf-8"))
> 
> # Make sure you prefix the quotes with an 'r'!
> CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
> chat(s,"Yo")
> 
> while True:
> response = s.recv(1024).decode("utf-8")
> if response == "PING :tmi.twitch.tv\r\n":
> s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
> else:
> username = re.search(r"\w+", response).group(0) # return the entire match
> message = CHAT_MSG.sub("", response)
> print(username + ": " + message)
> for pattern in cfg.PATT:
> if re.match(pattern, message):
> ban(s, username)
> break
> 
> ```

> ```
> #cfg.py
> HOST = "irc.chat.twitch.tv" # the Twitch IRC server
> PORT = 6667 # always use port 6667!
> NICK = "wolfmanager" # your Twitch username, lowercase
> PASS = "oauth:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # your Twitch OAuth token
> CHAN = "arcticwolfhowls" # the channel you want to join
> 
> RATE = (20/30) # messages per second
> 
> PATT = [ #not used yet
> r"swear",
> # ...
> r"some_pattern"
> ]
> 
> ```

---

<div class="post-metadata">

**Author:** ![Hunter\_Myers](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/hunter_myers/32/7055_2.png) [@Hunter\_Myers](https://discuss.dev.twitch.com/u/Hunter_Myers)\
**Post date:** [August 22, 2017, 6:02am UTC](https://discuss.dev.twitch.com/t/chat-bot-joins-server-receives-chat-but-cant-send-anything-fixed/11510/2 "2017-08-22T06:02:25Z")

</div>

Figured out the issue, my `cfg.py` CHAN variable had an extra ‘#’ in it

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [September 21, 2017, 6:02am UTC](https://discuss.dev.twitch.com/t/chat-bot-joins-server-receives-chat-but-cant-send-anything-fixed/11510/3 "2017-09-21T06:02:37Z")

</div>

Closed on September 21, 2017, 6:02am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
